1 | #include "dataset.h" |
---|
2 | #include "wiic_internal.h" |
---|
3 | |
---|
4 | /** |
---|
5 | * Dataset destructor, which calls clear. |
---|
6 | */ |
---|
7 | Dataset::~Dataset() |
---|
8 | { |
---|
9 | clear(); |
---|
10 | } |
---|
11 | |
---|
12 | /** |
---|
13 | * Add a new training to the dataset. |
---|
14 | * |
---|
15 | * @param training Add a training to the dataset. |
---|
16 | */ |
---|
17 | void Dataset::addTraining(Training* t) |
---|
18 | { |
---|
19 | if(t) |
---|
20 | trainings.push_back(t); |
---|
21 | |
---|
22 | loaded = true; |
---|
23 | } |
---|
24 | |
---|
25 | /** |
---|
26 | * Load a dataset stored in a file. |
---|
27 | * |
---|
28 | * @param filename Dataset filename |
---|
29 | */ |
---|
30 | bool Dataset::loadDataset(const string& nomefile) |
---|
31 | { |
---|
32 | int version; |
---|
33 | string line, dummy; |
---|
34 | ifstream infile(nomefile.c_str()); |
---|
35 | |
---|
36 | if(!infile.is_open()) { |
---|
37 | cout << "[Error] Unable to open the dataset file" << endl; |
---|
38 | return false; |
---|
39 | } |
---|
40 | else { |
---|
41 | trainings.clear(); |
---|
42 | |
---|
43 | // Version |
---|
44 | getline(infile,line); // First line is WiiC log version |
---|
45 | if(line.find("WiiC") == string::npos) { |
---|
46 | cout << "[Error] Bad log format." << endl; |
---|
47 | return false; |
---|
48 | } |
---|
49 | istringstream iline(line); |
---|
50 | iline >> dummy >> version; |
---|
51 | if(version > WIIC_LOG_VERSION) { |
---|
52 | cout << "[Error] Unsupported WiiC log version." << endl; |
---|
53 | return false; |
---|
54 | } |
---|
55 | getline(infile,line); // Date (we should load this as well...) |
---|
56 | |
---|
57 | // For each training |
---|
58 | int counter = 0; |
---|
59 | while(!infile.eof()) { |
---|
60 | getline(infile,line); |
---|
61 | istringstream iline(line); |
---|
62 | string cmd; |
---|
63 | unsigned long ts; |
---|
64 | iline >> cmd >> ts; |
---|
65 | if(cmd == "START") { |
---|
66 | // Each training is inserted in the training vector |
---|
67 | trainings.push_back(new Training()); |
---|
68 | trainings[counter]->setTimestampFromMidnight(ts); |
---|
69 | if(!(trainings[counter++]->loadTraining(infile))) { |
---|
70 | cout << "[Error] Unable to load a training in the dataset" << endl ; |
---|
71 | return false; |
---|
72 | } |
---|
73 | } |
---|
74 | } |
---|
75 | } |
---|
76 | infile.close(); |
---|
77 | loaded = true; |
---|
78 | |
---|
79 | return loaded; |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Delete all trainings and clear the buffer. This method will take |
---|
84 | * care of freeing the memory of each training in the dataset, |
---|
85 | * hence you don't need to free them in your code. |
---|
86 | */ |
---|
87 | void Dataset::clear() |
---|
88 | { |
---|
89 | for(unsigned int i = 0 ; i < trainings.size() ; i++) { |
---|
90 | if(trainings[i]) { |
---|
91 | delete trainings[i]; |
---|
92 | trainings[i] = 0; |
---|
93 | } |
---|
94 | } |
---|
95 | trainings.clear(); |
---|
96 | loaded = false; |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Save the dataset into a file for training and recognition. |
---|
101 | * |
---|
102 | * @param filename Pathname of the destination file |
---|
103 | * @param addr Wii device mac address |
---|
104 | */ |
---|
105 | bool Dataset::save(const char* file, const char* addr) const |
---|
106 | { |
---|
107 | // Open file |
---|
108 | ofstream out(file, ios::trunc); |
---|
109 | if(!out.is_open()) { // File does not exist, hence I create it |
---|
110 | cout << "[Error] Unable to open " << file << endl; |
---|
111 | return false; |
---|
112 | } |
---|
113 | |
---|
114 | // Save header |
---|
115 | saveHeader(out, addr); |
---|
116 | |
---|
117 | // For each training |
---|
118 | for(int i = 0 ; i < size() ; i++) { |
---|
119 | const Training* training = trainingAt(i); |
---|
120 | if(training) |
---|
121 | training->save(out); |
---|
122 | } |
---|
123 | |
---|
124 | out.close(); |
---|
125 | |
---|
126 | return true; |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * Save the dataset header into a file. |
---|
131 | * |
---|
132 | * @param out Output file stream |
---|
133 | * @param addr Wii device MAC address |
---|
134 | */ |
---|
135 | void Dataset::saveHeader(ofstream& out, const char* addr) const |
---|
136 | { |
---|
137 | // Log Version |
---|
138 | out << "WiiC " << WIIC_LOG_VERSION << endl; |
---|
139 | |
---|
140 | // Date |
---|
141 | time_t tim=time(NULL); |
---|
142 | char *s=ctime(&tim); |
---|
143 | s[strlen(s)-1]=0; // remove \n |
---|
144 | out << s << endl; |
---|
145 | |
---|
146 | // Mac Address |
---|
147 | out << addr << endl; |
---|
148 | } |
---|
149 | |
---|
150 | |
---|