tagging release
[dasher.git] / trunk / Src / Tools / UserLog / main.cpp
blob054adad4665ef304ec6cac88254b6d9f1183c8df
1 // Command line application that can read a UserLog XML file
2 // and rebuilds the C++ objects that represented it.
3 //
4 // Copyright 2005 by Keith Vertanen
5 //
7 #include "../../Common/Common.h"
9 #include <string>
10 #include <vector>
12 #include "../../DasherCore/DasherTypes.h"
14 // Declare our global file logging object
15 #include "FileLogger.h"
16 #ifdef _DEBUG
17 const eLogLevel gLogLevel = logDEBUG;
18 const int gLogOptions = logTimeStamp | logDateStamp | logDeleteOldFile;
19 #else
20 const eLogLevel gLogLevel = logNORMAL;
21 const int gLogOptions = logTimeStamp | logDateStamp;
22 #endif
23 CFileLogger* gLogger = NULL;
25 #include <iostream>
27 #include "../../DasherCore/UserLog.h"
29 // Can't add Utils.h back to CVS so I'm relative linking to another fricken directory
30 #include "../UserLogLoadTest/Utils.h"
32 #ifdef _WIN32
33 // In order to track leaks to line number, we need this at the top of every file
34 #include "MemoryLeak.h"
35 #ifdef _DEBUG
36 #define new DEBUG_NEW
37 #undef THIS_FILE
38 static char THIS_FILE[] = __FILE__;
39 #endif
40 #endif
42 using namespace std;
44 #ifndef VECTOR_VECTOR_VECTOR_STRING
45 typedef vector<VECTOR_VECTOR_STRING> VECTOR_VECTOR_VECTOR_STRING;
46 #endif
47 #ifndef VECTOR_VECTOR_VECTOR_STRING_ITER
48 typedef vector<VECTOR_VECTOR_STRING>::iterator VECTOR_VECTOR_VECTOR_STRING_ITER;
49 #endif
51 string GetOutputName(const string& strBase, int numFile, int numTrial, int numNav);
52 void OutputToFile(const string& strData, const string& strBase, int numFile, int numTrial, int numNav);
53 void OutputToFile(DENSITY_GRID data, int gridSize, const string& strBase, int numFile, int numTrial, int numNav);
54 void OutputData(const string& strBase, VECTOR_VECTOR_VECTOR_STRING* vectorData, int numFile, int numTrial, int numNav);
56 // Create the output filename based on the current counts
57 string GetOutputName(const string& strBase, int numFile, int numTrial, int numNav)
59 string strResult = strBase;
60 char strNum[256];
62 if (numFile >= 0)
64 sprintf(strNum, "_file%d", numFile);
65 strResult += strNum;
67 if (numTrial >= 0)
69 sprintf(strNum, "_trial%d", numTrial);
70 strResult += strNum;
72 if (numNav >= 0)
74 sprintf(strNum, "_nav%d", numNav);
75 strResult += strNum;
78 return strResult;
81 void OutputToFile(const string& strData, const string& strBase, int numFile, int numTrial, int numNav)
83 string strFilename = GetOutputName(strBase, numFile, numTrial, numNav);
85 FILE* fp = NULL;
86 fp = fopen(strFilename.c_str(), "w");
88 if (fp != NULL)
90 fputs(strData.c_str(), fp);
91 fclose(fp);
92 fp = NULL;
96 // Output a two dimensional grid of floating point values.
97 // Free up the memory as well.
98 void OutputToFile(DENSITY_GRID data, int gridSize, const string& strBase, int numFile, int numTrial, int numNav)
100 if (data == NULL)
101 return;
103 string strFilename = GetOutputName(strBase, numFile, numTrial, numNav);
105 FILE* fp = NULL;
106 fp = fopen(strFilename.c_str(), "w");
108 char strNum[256];
110 if (fp != NULL)
112 for (int i = 0; i < gridSize; i++)
114 string strLine = "";
116 for (int j = 0; j < gridSize; j++)
118 sprintf(strNum, "%0.4f", data[i][j]);
119 strLine += strNum;
120 if (j < (gridSize - 1))
121 strLine += "\t";
124 strLine += "\n";
125 fputs(strLine.c_str(), fp);
128 fclose(fp);
129 fp = NULL;
132 if (data != NULL)
134 for (int i = 0; i < gridSize; i++)
136 if (data[i] != NULL)
138 delete data[i];
139 data[i] = NULL;
142 delete data;
143 data = NULL;
148 // Output the passed in string data that is in a vector vector vector.
149 // Outer vector is for the data beloning to each file.
150 // Middle vector has data for each trial in a file.
151 // Inner vector is data for each navigation cycle in a trial.
152 void OutputData(const string& strBase, VECTOR_VECTOR_VECTOR_STRING* vectorData, int numFile, int numTrial, int numNav)
154 if (vectorData == NULL)
155 return;
157 string strOutput = "";
159 // Loop over each file
160 for (VECTOR_VECTOR_VECTOR_STRING_ITER iter = vectorData->begin();
161 iter < vectorData->end();
162 iter++)
164 // Loop over each trial
165 for (VECTOR_VECTOR_STRING_ITER iter2 = iter->begin();
166 iter2 < iter->end();
167 iter2++)
169 // Loop over each navigation cycle
170 for (VECTOR_STRING_ITER iter3 = iter2->begin();
171 iter3 < iter2->end();
172 iter3++)
174 strOutput += (*iter3);
176 // Output if we are suppose to separate each navigation cycle
177 if ((numNav >= 0) && (strOutput.length() > 0))
179 OutputToFile(strOutput, strBase, numFile, numTrial, numNav);
180 strOutput = "";
181 numNav++;
184 } // end for each nav cycle
186 // Output to the trial file
187 if (numTrial >= 0)
189 if (strOutput.length() > 0)
191 OutputToFile(strOutput, strBase, numFile, numTrial, numNav);
192 strOutput = "";
194 if (numNav > 0)
195 numNav = 0;
196 numTrial++;
199 } // end for each trial
201 // Output to for a file
202 if (numFile >= 0)
204 if (strOutput.length() > 0)
206 OutputToFile(strOutput, strBase, numFile, numTrial, numNav);
207 strOutput = "";
210 if (numTrial > 0)
211 numTrial = 0;
212 if (numNav > 0)
213 numNav = 0;
214 numFile++;
217 } // end for each file
219 // See if we need to output everything to one big file
220 if (strOutput.length() > 0)
222 OutputToFile(strOutput, strBase, numFile, numTrial, numNav);
227 // Output the passed in density grid data that is in a vector vector vector.
228 // Outer vector is for the data beloning to each file.
229 // Middle vector has data for each trial in a file.
230 // Inner vector is data for each navigation cycle in a trial.
231 void OutputData(const string& strBase, VECTOR_VECTOR_VECTOR_DENSITY_GRIDS* vectorData, int gridSize, int numFile, int numTrial, int numNav)
233 if (vectorData == NULL)
234 return;
236 DENSITY_GRID output = NULL;
238 // Loop over each file
239 for (VECTOR_VECTOR_VECTOR_DENSITY_GRIDS_ITER iter = vectorData->begin();
240 iter < vectorData->end();
241 iter++)
243 // Loop over each trial
244 for (VECTOR_VECTOR_DENSITY_GRIDS_ITER iter2 = iter->begin();
245 iter2 < iter->end();
246 iter2++)
248 // Loop over each navigation cycle
249 for (VECTOR_DENSITY_GRIDS_ITER iter3 = iter2->begin();
250 iter3 < iter2->end();
251 iter3++)
253 DENSITY_GRID grid = *iter3;
254 output = CUserLogTrial::MergeGrids(gridSize, output, grid);
256 // Output if we are suppose to separate each navigation cycle
257 if ((numNav >= 0) && (output != NULL))
259 OutputToFile(output, gridSize, strBase, numFile, numTrial, numNav);
260 output = NULL;
261 numNav++;
264 } // end for each nav cycle
266 // Output to the trial file
267 if (numTrial >= 0)
269 if (output != NULL)
270 OutputToFile(output, gridSize, strBase, numFile, numTrial, numNav);
271 output = NULL;
272 if (numNav > 0)
273 numNav = 0;
274 numTrial++;
277 } // end for each trial
279 // Output to for a file
280 if (numFile >= 0)
282 if (output != NULL)
283 OutputToFile(output, gridSize, strBase, numFile, numTrial, numNav);
284 output = NULL;
285 if (numTrial > 0)
286 numTrial = 0;
287 if (numNav > 0)
288 numNav = 0;
289 numFile++;
292 } // end for each file
294 // See if we need to output everything to one big file
295 if (output != NULL)
296 OutputToFile(output, gridSize, strBase, numFile, numTrial, numNav);
297 output = NULL;
300 int main(int argc, char* argv[])
302 #ifdef _WIN32
303 #ifdef _DEBUG
304 // Windows debug build memory leak detection
305 EnableLeakDetection();
306 #endif
307 #endif
309 // Global logging object we can use from anywhere
310 gLogger = new CFileLogger("UserLog.log",
311 gLogLevel,
312 gLogOptions);
314 { // memory leak scoping
316 string strInListFilename = "";
317 string strOutputBase = "out";
318 bool bNormMouse = false;
319 bool bDensityMouse = false;
320 int gridSize = 20;
322 // These track the number index we use on output files.
323 // -1 if we aren't separating on this item
324 int numOutputFile = -1;
325 int numOutputTrial = -1;
326 int numOutputNav = -1;
328 if (argc <= 2)
330 cout << "UserLog" << endl;
331 cout << " -in <XML log filename>" << endl;
332 cout << " -inList <file with list of log filenames>" << endl;
333 cout << " -out <output base name>" << endl;
334 cout << " -separateFiles" << endl;
335 cout << " -separateTrials" << endl;
336 cout << " -separateNavs" << endl;
337 cout << " -normMouse" << endl;
338 cout << " -densityMouse" << endl;
339 cout << " -densityGrid <grid size>" << endl;
341 cout << endl;
342 return 0;
345 VECTOR_STRING vectorInputFiles;
347 // Parse the command line options
348 int i = 1;
349 while (i < argc)
351 string strParam = makeLower(argv[i]);
352 string strNextParam = "";
354 if (i < argc - 1)
355 strNextParam = argv[i+1];
357 if (strParam.compare("-in") == 0)
359 vectorInputFiles.push_back(strNextParam);
360 i++;
362 else if (strParam.compare("-inlist") == 0)
364 strInListFilename = strNextParam;
365 i++;
367 else if (strParam.compare("-out") == 0)
369 strOutputBase = strNextParam;
370 i++;
372 else if (strParam.compare("-separatefiles") == 0)
374 numOutputFile = 0;
376 else if (strParam.compare("-separatetrials") == 0)
378 numOutputTrial = 0;
380 else if (strParam.compare("-separatenavs") == 0)
382 numOutputNav = 0;
384 else if (strParam.compare("-normmouse") == 0)
386 bNormMouse = true;
388 else if (strParam.compare("-densitymouse") == 0)
390 bDensityMouse = true;
392 else if (strParam.compare("-densitygrid") == 0)
394 gridSize = atoi(strNextParam.c_str());
395 i++;
397 else
399 // Failure to parse this parameter
400 cout << "Unknown switch: " << strParam << endl;
403 i++;
406 // Read in the list of files we are suppose to process
407 if (strInListFilename.length() > 0)
409 FILE* fp = NULL;
410 fp = fopen(strInListFilename.c_str(), "r");
411 if (fp != NULL)
413 char buffer[1024];
415 while (!feof(fp))
417 fscanf(fp, "%s\n", buffer);
419 if (strlen(buffer) > 0)
420 vectorInputFiles.push_back(buffer);
423 fclose(fp);
424 fp = NULL;
426 else
427 cout << "Failed to open test list file: " << strInListFilename.c_str() << endl;
430 // Process each of our input files
431 VECTOR_VECTOR_VECTOR_STRING vectorStrResult;
432 VECTOR_VECTOR_VECTOR_DENSITY_GRIDS vectorGridResult;
434 for (VECTOR_STRING_ITER iter = vectorInputFiles.begin(); iter < vectorInputFiles.end(); iter++)
436 string strFile = (string) *iter;
438 CUserLog objUserLog(strFile);
440 if (bNormMouse)
442 VECTOR_VECTOR_STRING vectorMouse = objUserLog.GetTabMouseXY(true);
443 vectorStrResult.push_back(vectorMouse);
444 } else if (bDensityMouse)
446 VECTOR_VECTOR_DENSITY_GRIDS vectorGrid = objUserLog.GetMouseDensity(gridSize);
447 vectorGridResult.push_back(vectorGrid);
451 if (vectorStrResult.size() > 0)
452 OutputData(strOutputBase, &vectorStrResult, numOutputFile, numOutputTrial, numOutputNav);
453 else if (vectorGridResult.size() > 0)
454 OutputData(strOutputBase, &vectorGridResult, gridSize, numOutputFile, numOutputTrial, numOutputNav);
457 if (gLogger != NULL)
459 delete gLogger;
460 gLogger = NULL;
463 return 0;