File: UserLogTrial.cpp

package info (click to toggle)
dasher 4.11%2Bgit20130508.adc653-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 40,248 kB
  • ctags: 5,158
  • sloc: xml: 185,479; cpp: 32,301; sh: 11,207; makefile: 828; ansic: 483
file content (1511 lines) | stat: -rw-r--r-- 43,301 bytes parent folder | download
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
#include "../Common/Common.h"

#include <cstring>
#include "UserLogTrial.h"

// Track memory leaks on Windows to the line that new'd the memory
#ifdef _WIN32
#ifdef _DEBUG_MEMLEAKS
#define DEBUG_NEW new( _NORMAL_BLOCK, THIS_FILE, __LINE__ )
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#endif

CUserLogTrial::CUserLogTrial(const string& strCurrentTrialFilename)
{
  //CFunctionLogger f1("CUserLogTrial::CUserLogTrial", g_pLogger);

  InitMemberVars();

  m_strCurrentTrialFilename = strCurrentTrialFilename;
}

CUserLogTrial::~CUserLogTrial()
{
  //CFunctionLogger f1("CUserLogTrial::~CUserLogTrial", g_pLogger);

  for (unsigned int i = 0; i < m_vpParams.size(); i++)
  {
    CUserLogParam* pParam = (CUserLogParam*) m_vpParams[i];

    if (pParam != NULL)
    {
      delete pParam;
      pParam = NULL;
    }
  }

  for (unsigned int i = 0; i < m_vpNavCycles.size(); i++)
  {
    NavCycle* pCycle = (NavCycle*) m_vpNavCycles[i];

    if (pCycle != NULL)
    {
      if (pCycle->pSpan != NULL)
      {
        delete pCycle->pSpan;
        pCycle->pSpan = NULL;
      }

      for (unsigned int i = 0; i < pCycle->vectorButtons.size(); i++)
      {
        CUserButton* pButton = (CUserButton*) pCycle->vectorButtons[i];

        if (pButton != NULL)
        {
          delete pButton;
          pButton = NULL;
        }
      }

      for (unsigned int i = 0; i < pCycle->vectorMouseLocations.size(); i++)
      {
        CUserLocation* pLocation = (CUserLocation*) pCycle->vectorMouseLocations[i];

        if (pLocation != NULL)
        {
          delete pLocation;
          pLocation = NULL;
        }
      }

      for (unsigned int i = 0; i < pCycle->vectorNavLocations.size(); i++)
      {
        NavLocation* pLocation = (NavLocation*) pCycle->vectorNavLocations[i];
        if (pLocation != NULL)
        {
          CTimeSpan* pSpan = pLocation->span;

          if (pSpan != NULL)
          {
            delete pSpan;
            pSpan = NULL;
          }

          Dasher::VECTOR_SYMBOL_PROB* pVectorAdded = pLocation->pVectorAdded;
          if (pVectorAdded != NULL)
          {
            delete pVectorAdded;
            pVectorAdded = NULL;
          }

          delete pLocation;
          pLocation = NULL;
        }
      }

      delete pCycle;
      pCycle = NULL;
    }


  }

  if (m_pSpan!= NULL)
  {
    delete m_pSpan;
    m_pSpan = NULL;
  }

}

// Returns an XML version of all our information
string CUserLogTrial::GetXML(const string& strPrefix)
{
  //CFunctionLogger f1("CUserLogTrial::GetXML", g_pLogger);

  string strResult = "";

  string strPrefixTab = strPrefix;
  strPrefixTab += "\t";

  string strPrefixTabTab = strPrefixTab;
  strPrefixTabTab += "\t";

  strResult += strPrefix;
  strResult += "<Trial>\n";

  // Summarize what happened at the beginning of the block
  strResult += GetSummaryXML(strPrefix);

  // Parameters that we want tracked on a per trial basis
  strResult += GetParamsXML(strPrefix);

  // Size of the window and the canvas in screen coordniates
  strResult += GetWindowCanvasXML(strPrefix);

  strResult += m_strCurrentTrial;

  // All the start and stop navigation events
  strResult += GetNavCyclesXML(strPrefix);

  strResult += strPrefix;
  strResult += "</Trial>\n";

  return strResult;
}

// See if any navigation has occured during this trial yet
bool CUserLogTrial::HasWritingOccured()
{
  //CFunctionLogger f1("CUserLogTrial::HasWritingOccured", g_pLogger);

  if (m_vpNavCycles.size() > 0) 
    return true;
  return false;
}



void CUserLogTrial::StartWriting()
{
  //CFunctionLogger f1("CUserLogTrial::StartWriting", g_pLogger);

  if (m_bWritingStart)
  {
    g_pLogger->Log("CUserLogTrial::StartWriting, nav already marked as started!", logNORMAL);
    return;
  }

  // Make sure our trial time span is running
  if (m_pSpan != NULL) 
    m_pSpan->Continue();

  // Start the task timer if we haven't already done so
  if (m_pSpan == NULL)
    m_pSpan = new CTimeSpan("Time", false);

  if (m_pSpan == NULL) 
  {
    g_pLogger->Log("CUserLogTrial::StartWriting, m_pSpan was NULL!", logNORMAL);
    return;
  }

  m_bWritingStart = true;

  // If we have already done some navigation, then the previous NavStop() would have stopped
  // the timer in the last NavLocation object.  We want to tell it to continue since the 
  // trial is not in fact over.
  if (m_vpNavCycles.size() > 0)
  {
    NavLocation* pLastLocation = GetCurrentNavLocation();
    if ((pLastLocation != NULL) && (pLastLocation->span != NULL))
      pLastLocation->span->Continue();
  }

  //  NavCycle* newCycle = AddNavCycle();
  AddNavCycle();
}

void CUserLogTrial::StopWriting(double dBits)
{
  //CFunctionLogger f1("CUserLogTrial::StopWriting", g_pLogger);

  if (!m_bWritingStart)
  {
    g_pLogger->Log("CUserLogTrial::StopWriting, nav already marked as stopped!", logNORMAL);
    return;
  }

  if (m_vpNavCycles.size() <= 0)
  {
    g_pLogger->Log("CUserLogTrial::StopWriting, vector was empty!", logNORMAL);
    return;
  }

  NavCycle* pCycle = GetCurrentNavCycle();
  if (pCycle == NULL)
  {
    g_pLogger->Log("CUserLogTrial::StopWriting, current cycle was NULL!", logNORMAL);
    return;
  }

  CTimeSpan* pSpan = (CTimeSpan*) pCycle->pSpan;   
  if (pSpan == NULL)
  {
    g_pLogger->Log("CUserLogTrial::StopWriting, span was NULL!", logNORMAL);
    return;
  }

  pCycle->dBits = dBits;
  pSpan->Stop();

  m_bWritingStart = false;

  // Make sure the last location timer is stopped since this could be the end of the trial and
  // we want the timestamps in the location elements to match the total trial time.
  NavLocation* pLastLocation = GetCurrentNavLocation();
  if ((pLastLocation != NULL) && (pLastLocation->span != NULL))
    pLastLocation->span->Continue();

  // Could be the last event of the trial
  if (m_pSpan != NULL) 
    m_pSpan->Stop();

  // We want to use the UserTrial info from the navigation period in Dasher.  We'll update
  // this everytime the user stops, this should make sure we get the right bit.
  GetUserTrialInfo();
}

// The user has entered one or more new symbols.  UserLog object will
// pass us the pointer to the current alphabet that is being used.
void CUserLogTrial::AddSymbols(Dasher::VECTOR_SYMBOL_PROB* vpNewSymbolProbs, 
                               eUserLogEventType iEvent)
{
  // Add the symbols to our running total of symbols.
  
  // ACL: Old comment said "We track the symbols and not the display text
  // since we may need to delete symbols later and 
  // a given symbol might take up multiple chars."
  //   - yet stored the display text????
  
  // We also keep the probability around so we can
  // calculate the average bits of the history.
  m_vHistory.insert(m_vHistory.end(), vpNewSymbolProbs->begin(), vpNewSymbolProbs->end());

  StopPreviousTimer();

  // Create the new NavLocation struct that record the data about this addition
  NavLocation* pLocation  = NULL;
  pLocation               = new NavLocation;

  if (pLocation == NULL)
  {
    g_pLogger->Log("CUserLogTrial::AddSymbols, failed to create location!", logNORMAL);
    return;
  }

  pLocation->strHistory    = GetHistoryDisplay();
  pLocation->span          = new CTimeSpan("Time", false);
  pLocation->avgBits       = GetHistoryAvgBits();
  pLocation->event         = iEvent;
  pLocation->numDeleted    = 0;
  pLocation->pVectorAdded  = new std::vector<Dasher::SymbolProb>(*vpNewSymbolProbs);

  NavCycle* pCycle = GetCurrentNavCycle();
  if (pCycle != NULL)
    pCycle->vectorNavLocations.push_back(pLocation);
  else
    g_pLogger->Log("CUserLogTrial::AddSymbols, cycle was NULL!", logNORMAL);

}

void CUserLogTrial::DeleteSymbols(int iNumToDelete, eUserLogEventType iEvent)
{
  //CFunctionLogger f1("CUserLogTrial::DeleteSymbols", g_pLogger);

  if (iNumToDelete <= 0)
    return;

  // Be careful not to pop more things than we have (this will hork the
  // memory up on linux but not windows).
  int iActualNumToDelete = min((int) m_vHistory.size(), iNumToDelete);

  for (int i = 0; i < iActualNumToDelete; i++)
  {
    // Remove the request number of symbols from our
    // ongoing list.
    m_vHistory.pop_back();
  }

  StopPreviousTimer();

  // Create the new NavLocation struct that record the data about this addition
  NavLocation* pLocation   = NULL;
  pLocation                = new NavLocation;

  if (pLocation == NULL)
  {
    g_pLogger->Log("CUserLogTrial::DeleteSymbols, failed to create location!", logNORMAL);
    return;
  }

  pLocation->strHistory    = GetHistoryDisplay();
  pLocation->span          = new CTimeSpan("Time", false);
  pLocation->avgBits       = GetHistoryAvgBits();
  pLocation->event         = iEvent;
  pLocation->numDeleted    = iNumToDelete;
  pLocation->pVectorAdded  = NULL;

  NavCycle* pCycle = GetCurrentNavCycle();
  if (pCycle != NULL)
    pCycle->vectorNavLocations.push_back(pLocation);
  else
    g_pLogger->Log("CUserLogTrial::DeleteSymbols, cycle was NULL!", logNORMAL);

}


// Called by UserLog object whenever we move on to the next trial.  This lets
// our trial object finalize any timers.
void CUserLogTrial::Done()
{
  //CFunctionLogger f1("CUserLogTrial::Done", g_pLogger);

  StopPreviousTimer();

  // Stop the time span that tracks the total trial time (if not already stopped)
  if ((m_pSpan != NULL) && (!m_pSpan->IsStopped()))
    m_pSpan->Stop();

}


void CUserLogTrial::AddMouseLocation(int iX, int iY, float dNats)
{
  //CFunctionLogger f1("CUserLogTrial::AddMouseLocation", g_pLogger);

  CUserLocation* pLocation = NULL;

  pLocation = new CUserLocation(iX, iY, dNats);

  if (pLocation != NULL)
  {
    // m_vectorMouseLocations.push_back(location);

    NavCycle* pCycle = GetCurrentNavCycle();

    if (pCycle != NULL)
      pCycle->vectorMouseLocations.push_back(pLocation);
    else
      g_pLogger->Log("CUserLogTrial::AddLocation, cycle was NULL!", logNORMAL);
  }
  else
    g_pLogger->Log("CUserLogTrial::AddLocation, location was NULL!", logNORMAL);

}

// Adds a normalized version of our mouse coordinates based on the size
// of the window.  Can optionally be told to store both representations.
void CUserLogTrial::AddMouseLocationNormalized(int iX, int iY, bool bStoreIntegerRep, float dNats)
{
  //CFunctionLogger f1("CUserLogTrial::AddMouseLocationNormalized", g_pLogger);

  CUserLocation* pLocation = NULL;

  if ((m_sCanvasCoordinates.bottom == 0) &&
    (m_sCanvasCoordinates.left == 0) &&
    (m_sCanvasCoordinates.right == 0) &&
    (m_sCanvasCoordinates.top == 0))
    g_pLogger->Log("CUserLogTrial::AddMouseLocationNormalized, called before AddCanvasSize()?", logNORMAL);

  pLocation = new CUserLocation(iX, 
                                iY, 
                                m_sCanvasCoordinates.top, 
                                m_sCanvasCoordinates.left, 
                                m_sCanvasCoordinates.bottom, 
                                m_sCanvasCoordinates.right, 
                                bStoreIntegerRep, 
                                dNats);

  if (pLocation != NULL)
  {
    // m_vectorMouseLocations.push_back(location);
    NavCycle* pCycle = GetCurrentNavCycle();

    if (pCycle != NULL)
      pCycle->vectorMouseLocations.push_back(pLocation);
    else
      g_pLogger->Log("CUserLogTrial::AddMouseLocationNormalized, cycle was NULL!", logNORMAL);
  }
  else
    g_pLogger->Log("CUserLogTrial::AddLocation, location was NULL!", logNORMAL);    
}

void CUserLogTrial::AddKeyDown(int iId, int iType, int iEffect) {
  CUserButton* pButton = new CUserButton(iId, iType, iEffect);

  if(pButton) {
    NavCycle* pCycle = GetCurrentNavCycle();

    if(pCycle)
      pCycle->vectorButtons.push_back(pButton);
    else
      g_pLogger->Log("CUserLogTrial::AddLocation, cycle was NULL!", logNORMAL);
  }
  else
    g_pLogger->Log("CUserLogTrial::AddLocation, location was NULL!", logNORMAL);
}

// Sets the current window size, this includes area for the menu bar, 
// sliders, canvas, etc.
void CUserLogTrial::AddWindowSize(int iTop, int iLeft, int iBottom, int iRight)
{
  //CFunctionLogger f1("CUserLogTrial::AddWindowSize", g_pLogger);

  m_sWindowCoordinates.top     = iTop;
  m_sWindowCoordinates.left    = iLeft;
  m_sWindowCoordinates.bottom  = iBottom;
  m_sWindowCoordinates.right   = iRight;
}

// Sets the current canvas size
void CUserLogTrial::AddCanvasSize(int iTop, int iLeft, int iBottom, int iRight)
{
  //CFunctionLogger f1("CUserLogTrial::AddCanvasSize", g_pLogger);

  m_sCanvasCoordinates.top     = iTop;
  m_sCanvasCoordinates.left    = iLeft;
  m_sCanvasCoordinates.bottom  = iBottom;
  m_sCanvasCoordinates.right   = iRight;
}

// Are we currently navigating?
bool CUserLogTrial::IsWriting()
{
  return m_bWritingStart;
}

////////////////////////////////////////// private methods ////////////////////////////////////////////////

void CUserLogTrial::InitMemberVars()
{
  //CFunctionLogger f1("CUserLogTrial::InitMemberVars", g_pLogger);

  m_bWritingStart                 = false;
  m_pSpan                         = NULL;
  m_strCurrentTrial               = "";

  m_sWindowCoordinates.bottom      = 0;
  m_sWindowCoordinates.top         = 0;
  m_sWindowCoordinates.left        = 0;
  m_sWindowCoordinates.right       = 0;

  m_sCanvasCoordinates.bottom      = 0;
  m_sCanvasCoordinates.top         = 0;
  m_sCanvasCoordinates.left        = 0;
  m_sCanvasCoordinates.right       = 0;
}

// Obtain information that is being passed in from the UserTrial standalone application.
// This information tell us what the user is actually trying to enter.
void CUserLogTrial::GetUserTrialInfo()
{
  //CFunctionLogger f1("CUserLogTrial::GetUserTrialInfo", g_pLogger);

  m_strCurrentTrial = "";

  try
  {
    if (m_strCurrentTrialFilename.length() > 0)
    {
      fstream fin(m_strCurrentTrialFilename.c_str(), ios::in);       // We want ios::nocreate, but not available in .NET 2003, arrgh

      // Make sure we successfully opened before we start reading it
      if (fin.is_open())
      {
        while(!fin.eof()) 
        {
          fin.getline(m_szTempBuffer, TEMP_BUFFER_SIZE);
          if (strlen(m_szTempBuffer) > 0)
          {
            m_strCurrentTrial += "\t\t\t";
            m_strCurrentTrial += m_szTempBuffer;
            m_strCurrentTrial += "\n";
          }        
        }
        fin.close();
      }
    }
  } catch (...)
  {
    // The application might not be running in which case the read will fail.
  }
}

// Returns the concatenation of all our symbol history using
// the display text that the alphabet at the time of the 
// symbol being added gave us.  
string CUserLogTrial::GetHistoryDisplay()
{
  //CFunctionLogger f1("CUserLogTrial::GetHistoryDisplay", g_pLogger);

  string strResult = "";

  for (unsigned int i = 0; i < m_vHistory.size(); i++)
  {
    Dasher::SymbolProb sItem = (Dasher::SymbolProb) m_vHistory[i];
    strResult += sItem.strDisplay;
  }

  return strResult;
}

double CUserLogTrial::GetHistoryAvgBits()
{
  //CFunctionLogger f1("CUserLogTrial::GetHistoryAvgBits", g_pLogger);

  double dResult = 0.0;

  if (m_vHistory.size() > 0)
  {
    for (unsigned int i = 0; i < m_vHistory.size(); i++)
    {
      Dasher::SymbolProb sItem = (Dasher::SymbolProb) m_vHistory[i];

      dResult += log(sItem.prob);
    }
    dResult = dResult * -1.0;
    dResult = dResult / log(2.0);
    dResult = dResult / m_vHistory.size();
  }

  return dResult;
}

void CUserLogTrial::StopPreviousTimer()
{
  //CFunctionLogger f1("CUserLogTrial::StopPreviousTimer", g_pLogger);

  // Make sure the previous time span (if any) has had its timer stopped
  if (m_vpNavCycles.size() > 0)
  {
    NavLocation* pLastLocation = GetCurrentNavLocation();
    if ((pLastLocation != NULL) && (pLastLocation->span != NULL))
      pLastLocation->span->Stop();
  }

}

// Gets XML string for a given NavLocation struct
string CUserLogTrial::GetLocationXML(NavLocation* pLocation, const string& strPrefix)
{
  //CFunctionLogger f1("CUserLogTrial::GetLocationXML", g_pLogger);

  string strResult = "";
  if (pLocation == NULL)
  {
    g_pLogger->Log("CUserLogTrial::GetLocationXML, location was NULL!", logNORMAL);
    return strResult;
  }

  strResult += strPrefix;
  strResult += "<Location>\n";

  strResult += strPrefix;
  strResult += "\t<History>";
  strResult += pLocation->strHistory;
  strResult += "</History>\n";

  strResult += strPrefix;
  strResult += "\t<AvgBits>";
  sprintf(m_szTempBuffer, "%0.6f", pLocation->avgBits);
  strResult += m_szTempBuffer;
  strResult += "</AvgBits>\n";

  // Only output the event if it is interesting type, not normal mouse navigation
  if (pLocation->event != userLogEventMouse)
  {
    strResult += strPrefix;
    strResult += "\t\t<Event>";
    sprintf(m_szTempBuffer, "%d", (int) pLocation->event);
    strResult += m_szTempBuffer;
    strResult += "</Event>\n";                
  }

  if ((pLocation->pVectorAdded != NULL) && (pLocation->pVectorAdded->size() > 0))
  {
    strResult += strPrefix;
    strResult += "\t<NumAdded>";
    sprintf(m_szTempBuffer, "%d", pLocation->pVectorAdded->size());
    strResult += m_szTempBuffer;
    strResult += "</NumAdded>\n";

    Dasher::VECTOR_SYMBOL_PROB* pVectorAdded = pLocation->pVectorAdded;

    if (pVectorAdded != NULL)
    {
      // Output the details of each add
      for (unsigned int j = 0; j < pVectorAdded->size(); j++)
      {            
        Dasher::SymbolProb sItem = (Dasher::SymbolProb) (*pVectorAdded)[j];

        strResult += strPrefix;
        strResult += "\t<Add>\n";

        strResult += strPrefix;
        strResult += "\t\t<Text>";
        strResult += sItem.strDisplay;
        strResult += "</Text>\n";

        strResult += strPrefix;
        strResult += "\t\t<Prob>";
        sprintf(m_szTempBuffer, "%0.6f", sItem.prob);
        strResult += m_szTempBuffer;
        strResult += "</Prob>\n";

        strResult += strPrefix;
        strResult += "\t</Add>\n";
      }
    }
  }

  if (pLocation->numDeleted > 0)
  {
    strResult += strPrefix;
    strResult += "\t<NumDeleted>";
    sprintf(m_szTempBuffer, "%d", pLocation->numDeleted);
    strResult += m_szTempBuffer;
    strResult += "</NumDeleted>\n";
  }

  if (pLocation->span != NULL)
  {
    string strPrefixTabTabTab = strPrefix;
    strPrefixTabTabTab += "\t";

    strResult += pLocation->span->GetXML(strPrefixTabTabTab);
  }

  strResult += strPrefix;
  strResult += "</Location>\n";

  return strResult;
}

// Output the XML for the summary section of XML
string CUserLogTrial::GetSummaryXML(const string& strPrefix)
{
  //CFunctionLogger f1("CUserLogTrial::GetSummaryXML", g_pLogger);

  string strResult = "";

  strResult += strPrefix;
  strResult += "\t<Summary>\n";

  // Figure out what the user ended up writing and how fast they did it
  string strText = "";
  double dAvgBits = 0.0;

  NavLocation* pLocation = GetCurrentNavLocation();
  if (pLocation != NULL)
  {
    strText = GetHistoryDisplay();
    dAvgBits = pLocation->avgBits;
  }

  int iButtonCount = GetButtonCount();
  double dTotalBits = GetTotalBits();

  strResult += GetStatsXML(strPrefix, strText, m_pSpan, dAvgBits, iButtonCount, dTotalBits);

  strResult += strPrefix;
  strResult += "\t</Summary>\n";

  return strResult;
}

// Calculates the various summary stats we output
string CUserLogTrial::GetStatsXML(const string& strPrefix, const string& strText, CTimeSpan* pSpan, double dAvgBits, int iButtonCount, double dTotalBits)
{
  //CFunctionLogger f1("CUserLogTrial::GetStatsXML", g_pLogger);

  string strResult = "";

  if (pSpan == NULL)
  {
    g_pLogger->Log("CUserLogTrial::GetStatsXML, pSpan = NULL!", logNORMAL);
    return strResult;
  }

  strResult += strPrefix;
  strResult += "\t\t<Text>";
  strResult += strText;
  strResult += "</Text>\n";

  // Average number of bits along the path to the final string
  strResult += strPrefix;
  strResult += "\t\t<AvgBits>";
  sprintf(m_szTempBuffer, "%0.6f", dAvgBits);
  strResult += m_szTempBuffer;
  strResult += "</AvgBits>\n";

  strResult += strPrefix;
  strResult += "\t\t<TotalBits>";
  sprintf(m_szTempBuffer, "%0.6f", dTotalBits);
  strResult += m_szTempBuffer;
  strResult += "</TotalBits>\n";

  
  strResult += strPrefix;
  strResult += "\t\t<ButtonCount>";
  sprintf(m_szTempBuffer, "%d", iButtonCount);
  strResult += m_szTempBuffer;
  strResult += "</ButtonCount>\n";

  // Calculate the number of words and characters
  strResult += strPrefix;
  strResult += "\t\t<Chars>";

  // We want the number of symbols which might differ
  // from the actual length of the text history.
  int iNumChars = m_vHistory.size();
  sprintf(m_szTempBuffer, "%d", iNumChars);
  strResult += m_szTempBuffer;
  strResult += "</Chars>\n";

  strResult += strPrefix;
  strResult += "\t\t<Words>";
  double dNumWords = (double) iNumChars / (double) 5;
  sprintf(m_szTempBuffer, "%0.2f", dNumWords);
  strResult += m_szTempBuffer;
  strResult += "</Words>\n";

  double dWPM = 0.0;
  double dCPM = 0.0;

  if (m_pSpan != NULL)
  {
    dWPM  = (double) dNumWords / (m_pSpan->GetElapsed() / 60.0);
    dCPM  = (double) iNumChars / (m_pSpan->GetElapsed() / 60.0);    
  }

  strResult += strPrefix;
  strResult += "\t\t<WPM>";
  sprintf(m_szTempBuffer, "%0.3f", dWPM);
  strResult += m_szTempBuffer;
  strResult += "</WPM>\n";

  strResult += strPrefix;
  strResult += "\t\t<CPM>";
  sprintf(m_szTempBuffer, "%0.3f", dCPM);
  strResult += m_szTempBuffer;
  strResult += "</CPM>\n";

  string strPrefixTabTab = strPrefix;
  strPrefixTabTab += "\t\t";

  if (m_pSpan != NULL)
    strResult += m_pSpan->GetXML(strPrefixTabTab);

  return strResult;
}

string CUserLogTrial::GetWindowCanvasXML(const string& strPrefix)
{
  //CFunctionLogger f1("CUserLogTrial::GetWindowCanvasXML", g_pLogger);

  string strResult = "";

  // Log the window location and size that was last used during this trial
  strResult += strPrefix;
  strResult += "\t<WindowCoordinates>\n";

  strResult += strPrefix;
  sprintf(m_szTempBuffer, "\t\t<Top>%d</Top>\n", m_sWindowCoordinates.top);
  strResult += m_szTempBuffer;

  strResult += strPrefix;
  sprintf(m_szTempBuffer, "\t\t<Bottom>%d</Bottom>\n", m_sWindowCoordinates.bottom);
  strResult += m_szTempBuffer;

  strResult += strPrefix;
  sprintf(m_szTempBuffer, "\t\t<Left>%d</Left>\n", m_sWindowCoordinates.left);
  strResult += m_szTempBuffer;

  strResult += strPrefix;
  sprintf(m_szTempBuffer, "\t\t<Right>%d</Right>\n", m_sWindowCoordinates.right);
  strResult += m_szTempBuffer;

  strResult += strPrefix;
  strResult += "\t</WindowCoordinates>\n";

  // Log the canvas location and size that was last used during this trial
  strResult += strPrefix;
  strResult += "\t<CanvasCoordinates>\n";

  strResult += strPrefix;
  sprintf(m_szTempBuffer, "\t\t<Top>%d</Top>\n", m_sCanvasCoordinates.top);
  strResult += m_szTempBuffer;

  strResult += strPrefix;
  sprintf(m_szTempBuffer, "\t\t<Bottom>%d</Bottom>\n", m_sCanvasCoordinates.bottom);
  strResult += m_szTempBuffer;

  strResult += strPrefix;
  sprintf(m_szTempBuffer, "\t\t<Left>%d</Left>\n", m_sCanvasCoordinates.left);
  strResult += m_szTempBuffer;

  strResult += strPrefix;
  sprintf(m_szTempBuffer, "\t\t<Right>%d</Right>\n", m_sCanvasCoordinates.right);
  strResult += m_szTempBuffer;

  strResult += strPrefix;
  strResult += "\t</CanvasCoordinates>\n";

  return strResult;
}

string CUserLogTrial::GetParamsXML(const string& strPrefix)
{
  //CFunctionLogger f1("CUserLogTrial::GetParamsXML", g_pLogger);

  string strResult = "";

  if (m_vpParams.size() > 0)
  {
    // Make parameters with the same name appear near each other in the results
    sort(m_vpParams.begin(), m_vpParams.end(), CUserLogParam::ComparePtr);    

    strResult += strPrefix;
    strResult += "\t<Params>\n";

    string strPrefixPlusTabTab = strPrefix;
    strPrefixPlusTabTab += "\t\t";

    for (unsigned int i = 0; i < m_vpParams.size(); i++)
    {
      CUserLogParam* pParam = (CUserLogParam*) m_vpParams[i];

      strResult += GetParamXML(pParam, strPrefixPlusTabTab);
    }

    strResult += strPrefix;
    strResult += "\t</Params>\n";
  }

  return strResult;

}

int CUserLogTrial::GetButtonCount() {
  int iCount(0);

  for(VECTOR_NAV_CYCLE_PTR::iterator it(m_vpNavCycles.begin()); it != m_vpNavCycles.end(); ++it)
    for(VECTOR_USER_BUTTON_PTR::iterator it2((*it)->vectorButtons.begin()); it2 != (*it)->vectorButtons.end(); ++it2)
      iCount += (*it2)->GetCount();

  return iCount;
}

double CUserLogTrial::GetTotalBits() {
  double dBits(0.0);

  for(VECTOR_NAV_CYCLE_PTR::iterator it(m_vpNavCycles.begin()); it != m_vpNavCycles.end(); ++it)
    dBits += (*it)->dBits;

  return dBits;
}

// Parameters can optionally be specified to be added to the Trial objects.
// This allows us to easily see what a certain parameter value was used
// in a given trial.  
void CUserLogTrial::AddParam(const string& strName, const string& strValue, int iOptionMask)
{
  //CFunctionLogger f1("CUserLogTrial::AddParam", g_pLogger);

  bool bTrackMultiple     = false;

  if (iOptionMask & userLogParamTrackMultiple)
    bTrackMultiple = true;

  // See if this matches an existing parameter value that we may want to 
  // overwrite.  But only if we aren't suppose to keep track of multiple changes.
  if (!bTrackMultiple)
  {
    for (unsigned int i = 0; i < m_vpParams.size(); i++)
    {
      CUserLogParam* pParam = (CUserLogParam*) m_vpParams[i];

      if (pParam != NULL)
      {
        if (pParam->strName.compare(strName) == 0)
        {
          pParam->strValue = strValue;
          return;
        }
      }
    }
  }
  // We need to add a new param
  CUserLogParam* pNewParam = new CUserLogParam;
  if (pNewParam == NULL)
  {
    g_pLogger->Log("CUserLogTrial::AddParam, newParam was NULL!", logNORMAL);
    return;
  }

  pNewParam->strName           = strName;
  pNewParam->strValue          = strValue;
  pNewParam->strTimeStamp      = "";

  // Parameters that can have multiple values logged will also log when they were changed
  if (bTrackMultiple)
    pNewParam->strTimeStamp  = CTimeSpan::GetTimeStamp();

  m_vpParams.push_back(pNewParam);
}

// Static method that generates the XML representation of a 
// single param name value set.  Used both to output params
// for a trial and for the parent UserLog object.
string CUserLogTrial::GetParamXML(CUserLogParam* pParam, const string& strPrefix)
{
  //CFunctionLogger f1("CUserLogTrial::GetParamXML", g_pLogger);

  string strResult = "";

  if (pParam != NULL)
  {
    strResult += strPrefix;
    strResult += "<";
    strResult += pParam->strName;
    strResult += ">";

    if (pParam->strTimeStamp.length() > 0)
    {
      strResult += "\n";
      strResult += strPrefix;
      strResult += "\t<Value>";
      strResult += pParam->strValue;
      strResult += "</Value>\n";

      strResult += strPrefix;
      strResult += "\t<Time>";
      strResult += pParam->strTimeStamp;
      strResult += "</Time>\n";

      strResult += strPrefix;
    }
    else 
    {
      strResult += pParam->strValue;
    }

    strResult += "</";
    strResult += pParam->strName;
    strResult += ">\n";
  }

  return strResult;
}

// Returns a pointer to the currently active navigation cycle
NavCycle* CUserLogTrial::GetCurrentNavCycle()
{
  //CFunctionLogger f1("CUserLogTrial::GetCurrentNavCycle", g_pLogger);

  if (m_vpNavCycles.size() <= 0)
    return NULL;
  return m_vpNavCycles[m_vpNavCycles.size() - 1];
}

// Gets a pointer to the last NavLocation object
// in the current navication cycle.
NavLocation* CUserLogTrial::GetCurrentNavLocation()
{
  //CFunctionLogger f1("CUserLogTrial::GetCurrentNavLocation", g_pLogger);

//   NavCycle* pCycle = GetCurrentNavCycle();

//   if (pCycle == NULL)
//     return NULL;

//   if (pCycle->vectorNavLocations.size() <= 0)
//     return NULL;

//   return (NavLocation*) pCycle->vectorNavLocations[pCycle->vectorNavLocations.size() - 1];

  // New version - reverse iterate through the list and find the last nav cycle which has any locations

  for(VECTOR_NAV_CYCLE_PTR::reverse_iterator it(m_vpNavCycles.rbegin()); it != m_vpNavCycles.rend(); ++it) {
    if((*it)->vectorNavLocations.size() > 0)
      return (NavLocation*) (*it)->vectorNavLocations[(*it)->vectorNavLocations.size() - 1];
  }

  return NULL;
}

// Adds a new navgiation cycle to our collection
NavCycle* CUserLogTrial::AddNavCycle()
{
  //CFunctionLogger f1("CUserLogTrial::AddNavCycle", g_pLogger);

  NavCycle* pNewCycle = new NavCycle;
  if (pNewCycle == NULL)
  {
    g_pLogger->Log("CUserLogTrial::AddNavCycle, failed to create NavCycle!", logNORMAL);
    return NULL;
  }

  pNewCycle->pSpan = new CTimeSpan("Time", false);

  m_vpNavCycles.push_back(pNewCycle);
  return pNewCycle;
}

string CUserLogTrial::GetNavCyclesXML(const string& strPrefix)
{
  //CFunctionLogger f1("CUserLogTrial::GetNavCyclesXML", g_pLogger);

  string strResult = "";

  string strPrefixTab = strPrefix;
  strPrefixTab += "\t";

  string strPrefixTabTab = strPrefixTab;
  strPrefixTabTab += "\t";

  string strPrefixTabTabTab = strPrefixTabTab;
  strPrefixTabTabTab += "\t";

  string strPrefixTabTabTabTab = strPrefixTabTabTab;
  strPrefixTabTabTabTab += "\t";

  strResult += strPrefixTab;
  strResult += "<Navs>\n";

  for (unsigned int i = 0; i < m_vpNavCycles.size(); i++)
  {
    NavCycle* pCycle = (NavCycle*) m_vpNavCycles[i];

    if (pCycle != NULL)
    {
      strResult += strPrefixTabTab;
      strResult += "<Nav>\n";

      if (pCycle->pSpan != NULL)
        strResult += pCycle->pSpan->GetXML(strPrefixTabTabTab);

      if (pCycle->vectorNavLocations.size() > 0)
      {
        strResult += strPrefixTabTabTab;
        strResult += "<Locations>\n";

        for (unsigned int i = 0; i < pCycle->vectorNavLocations.size(); i++)
        {
          NavLocation* pLocation = (NavLocation*) pCycle->vectorNavLocations[i];

          if (pLocation != NULL)
            strResult += GetLocationXML(pLocation, strPrefixTabTabTabTab);
        }
        strResult += strPrefixTabTabTab;
        strResult += "</Locations>\n";
      }

      if (pCycle->vectorMouseLocations.size() > 0)
      {
        strResult += strPrefixTabTabTab;
        strResult += "<MousePositions>\n";

        for (unsigned int i = 0; i < pCycle->vectorMouseLocations.size(); i++)
        {
          CUserLocation* pLocation = (CUserLocation*) pCycle->vectorMouseLocations[i];

          if (pLocation != NULL)
          {
            strResult += pLocation->GetXML(strPrefixTabTabTabTab);
          }
        }

        strResult += strPrefixTabTabTab;
        strResult += "</MousePositions>\n";
      }

      if (pCycle->vectorButtons.size() > 0)
      {
        strResult += strPrefixTabTabTab;
        strResult += "<Buttons>\n";

        for (unsigned int i = 0; i < pCycle->vectorButtons.size(); i++)
        {
          CUserButton* pButton = (CUserButton*) pCycle->vectorButtons[i];

          if(pButton)
          {
            strResult += pButton->GetXML(strPrefixTabTabTabTab);
          }
        }

        strResult += strPrefixTabTabTab;
        strResult += "</Buttons>\n";
      }

      strResult += strPrefixTabTab;
      strResult += "</Nav>\n";

    }
  }

  strResult += strPrefixTab;
  strResult += "</Navs>\n";

  return strResult;
}

// Construct based on some XML, second parameter is just to make signature
// different from the normal constructor.
CUserLogTrial::CUserLogTrial(const string& strXML, int iIgnored)
{
  //CFunctionLogger f1("CUserLogTrial::CUserLogTrial(XML)", g_pLogger);

  InitMemberVars();
  VECTOR_STRING vNavs;

  string strParams        = XMLUtil::GetElementString("Params", strXML, true);           
  string strWindow        = XMLUtil::GetElementString("WindowCoordinates", strXML, true);           
  string strCanvas        = XMLUtil::GetElementString("CanvasCoordinates", strXML, true);           
  string strNavs          = XMLUtil::GetElementString("Navs", strXML, true);           
  string strSummary       = XMLUtil::GetElementString("Summary", strXML, true);
  string strSummaryTime   = XMLUtil::GetElementString("Time", strSummary, true);
  vNavs                   = XMLUtil::GetElementStrings("Nav", strNavs, true);

  string strCurrentTrial  = XMLUtil::GetElementString("CurrentTrial", strXML, false);
  if (strCurrentTrial.length() > 0)
  {
    // We copied the XML string directly into the member variable
    // including the start/end tag, so we need to reproduce the
    // tags ourselves.
    m_strCurrentTrial       =  "\t\t\t<CurrentTrial>\n";
    m_strCurrentTrial       += strCurrentTrial;
    m_strCurrentTrial       += "</CurrentTrial>\n";
  }

  m_vpParams                = ParseParamsXML(strParams);
  m_sWindowCoordinates      = ParseWindowXML(strWindow);
  m_sCanvasCoordinates      = ParseWindowXML(strCanvas);
  m_pSpan                   = new CTimeSpan("Time", strSummaryTime);

  // Process each <Nav> tag
  string strTime              = "";
  string strLocations         = "";
  string strMousePositions    = "";

  VECTOR_STRING vLocations;
  VECTOR_STRING vMousePositions;
  VECTOR_STRING vAdded;

  for (VECTOR_STRING_ITER iter = vNavs.begin(); iter < vNavs.end(); iter++)
  {
    strTime              = "";
    strLocations         = "";
    strMousePositions    = "";
    vLocations.erase(vLocations.begin(), vLocations.end());
    vMousePositions.erase(vMousePositions.begin(), vMousePositions.end());

    strTime             = XMLUtil::GetElementString("Time", *iter, true);
    strLocations        = XMLUtil::GetElementString("Locations", *iter, true);
    strMousePositions   = XMLUtil::GetElementString("MousePositions", *iter, true);

    NavCycle* pCycle = new NavCycle();
    if (pCycle == NULL)
    {
      g_pLogger->Log("CUserLogTrial::CUserLogTrial, failed to create NavCycle!", logNORMAL);
      return;
    }

    pCycle->pSpan = NULL;

    if (strTime.length() > 0)
    {
      pCycle->pSpan = new CTimeSpan("Time", strTime);
    }
    if (strLocations.length() > 0)
    {
      vLocations = XMLUtil::GetElementStrings("Location", strLocations, true);

      for (VECTOR_STRING_ITER iter2 = vLocations.begin(); iter2 < vLocations.end(); iter2++)
      {
        vAdded.erase(vAdded.begin(), vAdded.end());

        NavLocation* pLocation = new NavLocation();
        if (pLocation == NULL)
        {
          g_pLogger->Log("CUserLogTrial::CUserLogTrial, failed to create NavLocation!", logNORMAL);
          return;
        }                

        pLocation->strHistory     = XMLUtil::GetElementString("History", *iter2);
        pLocation->avgBits        = (double) XMLUtil::GetElementFloat("AvgBits", *iter2);
        pLocation->event          = (eUserLogEventType) XMLUtil::GetElementInt("Event", *iter2);
        pLocation->numDeleted     = XMLUtil::GetElementInt("NumDeleted", *iter2);

        pLocation->span           = NULL;
        string strTime            = XMLUtil::GetElementString("Time", *iter2);
        pLocation->span           = new CTimeSpan("Time", strTime);

        // Handle the multiple <Add> tags that might exist
        vAdded                    = XMLUtil::GetElementStrings("Add", *iter2);                
        pLocation->pVectorAdded   = new Dasher::VECTOR_SYMBOL_PROB;

        for (VECTOR_STRING_ITER iter3 = vAdded.begin(); iter3 < vAdded.end(); iter3++)
        {
          Dasher::SymbolProb sAdd(0, // We don't have the original integer symbol index
                                  XMLUtil::GetElementString("Text", *iter3),
                                  XMLUtil::GetElementFloat("Prob", *iter3));

          if (pLocation->pVectorAdded != NULL)
            pLocation->pVectorAdded->push_back(sAdd);

          // Also track it in one complete vector of all the adds
          m_vHistory.push_back(sAdd);        
        }

        // If this was a deleted event, then we need to erase some stuff from the running history
        // Be careful not to pop more things than we have (this will hork the
        // memory up on linux but not windows).
        int iActualNumToDelete = min((int) m_vHistory.size(), pLocation->numDeleted);
        for (int i = 0; i < iActualNumToDelete; i++)
          m_vHistory.pop_back();

        pCycle->vectorNavLocations.push_back(pLocation);
      }
    }

    if (strMousePositions.length() > 0)
    {
      vMousePositions = XMLUtil::GetElementStrings("Pos", strMousePositions, true);
      for (VECTOR_STRING_ITER iter2 = vMousePositions.begin(); iter2 < vMousePositions.end(); iter2++)
      {
        CUserLocation* pLocation = new CUserLocation(*iter2);
        pCycle->vectorMouseLocations.push_back(pLocation);
      }

    }

    m_vpNavCycles.push_back(pCycle);
  }

}

// Helper that parses parameters out of the XML block, used by UserLog 
// and by UserLogTrial to do the same thing.
VECTOR_USER_LOG_PARAM_PTR CUserLogTrial::ParseParamsXML(const string& strXML)
{
  //CFunctionLogger f1("CUserLogTrial::ParseParamsXML", g_pLogger);

  VECTOR_USER_LOG_PARAM_PTR   vResult;
  VECTOR_NAME_VALUE_PAIR      vParams;

  vParams = XMLUtil::GetNameValuePairs(strXML, true);

  // Handle adding all the name/value parameter pairs.  XML looks like:
  //      <Eyetracker>0</Eyetracker>
  //      <MaxBitRate>
  //	    	<Value>7.0100</Value>
  //			<Time>15:48:53.140</Time>
  //  	</MaxBitRate>
  for (VECTOR_NAME_VALUE_PAIR_ITER iter = vParams.begin(); iter < vParams.end(); iter++)
  {
    CUserLogParam* pParam = new CUserLogParam();

    if (pParam != NULL) 
    {
      pParam->strName      = iter->strName;

      // See if we have a type that has a timestamp
      string strValue     = XMLUtil::GetElementString("Value", iter->strValue, true);
      string strTime      = XMLUtil::GetElementString("Time", iter->strValue, true);

      if ((strValue.length() > 0)  || (strTime.length() > 0))
      {
        pParam->strValue     = strValue;
        pParam->strTimeStamp = strTime;
      }
      else
        pParam->strValue     = iter->strValue;

      pParam->options = 0;

      vResult.push_back(pParam);
    }
  }

  return vResult;
}

// Parse our window or canvas coorindates from XML
WindowSize CUserLogTrial::ParseWindowXML(const string& strXML)
{
  //CFunctionLogger f1("CUserLogTrial::ParseWindowXML", g_pLogger);

  WindowSize sResult;

  sResult.top      = XMLUtil::GetElementInt("Top", strXML);
  sResult.bottom   = XMLUtil::GetElementInt("Bottom", strXML);
  sResult.left     = XMLUtil::GetElementInt("Left", strXML);
  sResult.right    = XMLUtil::GetElementInt("Right", strXML);

  return sResult;
}

// Returns a vector that contains the tab delimited mouse
// coordinates for each of our navigation cycles.
VECTOR_STRING CUserLogTrial::GetTabMouseXY(bool bReturnNormalized)
{
  //CFunctionLogger f1("CUserLogTrial::GetTabMouseXY", g_pLogger);

  VECTOR_STRING vResult;
  for (VECTOR_NAV_CYCLE_PTR_ITER iter = m_vpNavCycles.begin(); iter < m_vpNavCycles.end(); iter++)
  {
    string strResult = "";

    if (*iter != NULL)
    {
      for (VECTOR_USER_LOCATION_PTR_ITER iter2 = (*iter)->vectorMouseLocations.begin(); 
        iter2 < (*iter)->vectorMouseLocations.end();
        iter2++)
      {
        if (*iter2 != NULL)
          strResult += (*iter2)->GetTabMouseXY(bReturnNormalized);
      }
    }

    vResult.push_back(strResult);
  }

  return vResult;
}

// Calculates the mouse density with a given grid size number of buckets.
// Each element of the vector is a 2D array of double values from 0.0 - 1.0.
//
// NOTE: We allocate the memory here for the double**, our caller must 
// handle freeing it!
VECTOR_DENSITY_GRIDS CUserLogTrial::GetMouseDensity(int iGridSize)
{
  //CFunctionLogger f1("CUserLogTrial::GetMouseDensity", g_pLogger);

  VECTOR_DENSITY_GRIDS vResult;

  for (VECTOR_NAV_CYCLE_PTR_ITER iter = m_vpNavCycles.begin(); iter < m_vpNavCycles.end(); iter++)
  {
    if (*iter != NULL)
    {
      DENSITY_GRID ppGrid;

      // Init the grid with all 0.0 values
      ppGrid = new double*[iGridSize];
      for (int i = 0; i < iGridSize; i++)
        ppGrid[i] = new double[iGridSize];

      for (int i = 0; i < iGridSize; i++)
        for (int j = 0; j < iGridSize; j++)
          ppGrid[i][j] = 0.0;

      unsigned int iCount = 0;

      // Assign each mouse to location to one of the buckets and increment 
      // the count on that bucket.
      for (VECTOR_USER_LOCATION_PTR_ITER iter2 = (*iter)->vectorMouseLocations.begin(); 
        iter2 < (*iter)->vectorMouseLocations.end();
        iter2++)
      {
        if (*iter2 != NULL)
        {
          int i = 0;
          int j = 0;
          (*iter2)->GetMouseGridLocation(iGridSize, &i, &j);
          // Increment the count on this location, we'll throw away points
          // that were outside the canvas grid.
          if ((i < iGridSize) && (j < iGridSize) && (i >= 0) && (j >= 0))
          {
            // We reverse j and i to get x to increase left top right
            // and y from top to bottom
            ppGrid[j][i] = ppGrid[j][i] + 1.0;
            iCount++;
          }
        }
      }
      // Now normalize everything so each grid location is a 
      // percentage of the time we spent in that location.
      for (int i = 0; i < iGridSize; i++)
      {
        {
          for (int j = 0; j < iGridSize; j++)
          {
            ppGrid[i][j] = ppGrid[i][j] / (double) iCount;
          }
        }
      }

      vResult.push_back(ppGrid);
    }
  }

  return vResult;
}

// Merge the density of two grids together.  This is done but adding their values
// and dividing by two.  If either pointer is NULL, then we return the other grid
// values intact.  We free the memory in our parameter grids.
DENSITY_GRID CUserLogTrial::MergeGrids(int iGridSize, DENSITY_GRID ppGridA, DENSITY_GRID ppGridB)
{
  //CFunctionLogger f1("CUserLogTrial::MergeGrids", g_pLogger);

  DENSITY_GRID ppResult;
  ppResult = new double*[iGridSize];
  for (int i = 0; i < iGridSize; i++)
    ppResult[i] = new double[iGridSize];

  for (int i = 0; i < iGridSize; i++)
    for (int j = 0; j < iGridSize; j++)
      ppResult[i][j] = 0.0;

  // Both NULL, then return grid of all 0.0's
  if ((ppGridA == NULL) && (ppGridB == NULL))
    return ppResult;

  if (ppGridA == NULL)
  {
    // grid A is NULL, return copy of grid B
    for (int i = 0; i < iGridSize; i++)
    {
      for (int j = 0; j < iGridSize; j++)
        ppResult[i][j] = ppGridB[i][j];
    }    
  }    
  else if (ppGridB == NULL)
  {
    // grid B is NULL, return copy of grid A
    for (int i = 0; i < iGridSize; i++)
    {
      for (int j = 0; j < iGridSize; j++)
        ppResult[i][j] = ppGridA[i][j];
    }

  }
  else
  {
    // Normal case, merge the two density grids
    for (int i = 0; i < iGridSize; i++)
    {
      for (int j = 0; j < iGridSize; j++)
        ppResult[i][j] = (ppGridA[i][j] + ppGridB[i][j]) / 2.0;
    }
  }

  if (ppGridA != NULL)
  {
    for (int i = 0; i < iGridSize; i++)
    {
      if (ppGridA[i] != NULL)
      {
        delete ppGridA[i];
        ppGridA[i] = NULL;
      }
    }
    delete ppGridA;
    ppGridA = NULL;

  }
  if (ppGridB != NULL)
  {
    for (int i = 0; i < iGridSize; i++)
    {
      if (ppGridB[i] != NULL)
      {
        delete ppGridB[i];
        ppGridB[i] = NULL;
      }
    }
    delete ppGridB;
    ppGridB = NULL;
  }

  return ppResult;
}