-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel2c.cpp
46 lines (40 loc) · 1.27 KB
/
model2c.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// this is a program for converting a model file to a C source
// so that you can integrate the model data into your binary.
// usage)
// First, convert the model file into a C source by this program.
// ./a.out < model > modeldata.c
// then, complile and link.
// In your main program, use
// model.load_from_array(me_model_data);
// instead of
// model.load_from_file("model");
#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
int main()
{
cout << "typedef struct ME_Model_Data" << endl;
cout << "{" << endl;
cout << " char * label;" << endl;
cout << " char * feature;" << endl;
cout << " double weight;" << endl;
cout << "} ME_Model_Data;" << endl << endl;
cout << "ME_Model_Data me_model_data[] = {" << endl;
string line;
while (getline(cin, line)) {
char label[256], feature[256];
float weight;
sscanf(line.c_str(), "%s\t%s\t%f", label, feature, &weight);
string s;
for (int i = 0;; i++) {
if (feature[i] == 0) break;
if (feature[i] == '\\') s.push_back('\\');
if (feature[i] == '"') s.push_back('\\');
s.push_back(feature[i]);
}
printf("\t\"%s\",\t\"%s\",\t%f,\n", label, s.c_str(), weight);
}
printf("\t\"///\",\t\"///\",\t0,\n");
cout << "};" << endl;
}