-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.cp
executable file
·2059 lines (1821 loc) · 63.2 KB
/
process.cp
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
// TABARI Project
//___________________________________________________________________________________
// process.cp
// This file contains the routines for handling the primary reading and coding of
// text records
// ----------------------------------------------------------------------------
// PROJECT FILE PROCEDURES
//
// The Project file contains all of the information the program needs to do
// coding -- phrase files, problem files, indicator for records coded and
// so forth -- and also tracks the history of the coding for a data set. It
// is stored as a single record of type Project_Rec.
//
// FILES:
// textFile : Source of input text
// verbFile : Verb phrase file
// actorFile : Actors file
// optionsFile : Options file (subtle, eh?)
//
//__________________________________________________________________________________
//
// Copyright (c) 2000 - 2012 Philip A. Schrodt. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted under the terms of the GNU General Public License:
// http://www.opensource.org/licenses/gpl-license.html
//
// Report bugs to: [email protected]
// The most recent version of this code is available from the KEDS Web site:
// http://eventdata.psu.edu
//___________________________________________________________________________________
// Headers
#include "TABARI.h"
ProcessorClass Processor;
//___________________________________________________________________________________
// Global Variables
extern TabariFlagsClass TabariFlags;
extern CharStoreClass CharStore;
extern TokenStoreClass TokenStore;
extern LiteralsClass Literals;
extern CodeStoreClass CodeStore;
extern RootsClass Roots;
extern PhrasesClass Phrases;
extern ReadFilesClass ReadFiles;
extern WriteFilesClass WriteFiles;
extern ParserClass Parser;
extern CoderClass Coder;
extern ModifyClass Modify;
extern ReadXMLClass ReadXML;
//___________________________________________________________________________________
void ProcessorClass:: getsLine(void)
// safe version of getline: throws LINE_LONG exception if line is too long
// Note that at the moment LINE_LONG always results in a ShowFatalError,
// so this doesn't finish reading the line
{
char * ps = sLine;
int ka = 0;
while (ka++ < MAX_TAB_INPUT) {
fin.get(*ps);
if (('\n' == *ps) || (fin.eof())) {
*ps = '\0'; // normal exit point
return;
}
++ps;
}
*(ps-1) = '\0'; // terminate string so it can be displayed
throw LINE_LONG;
} // getsLine
void ProcessorClass:: getsLineError(int err)
// processes errors returned from getsLine
{
if (LINE_LONG == err) ShowFatalError("Input line too long in text file; check the file format\nLast line:\n",sLine,shError31);
else ShowFatalError("Indeterminant error occurred reading input file\nLast line:\n",sLine,shError31); // shouldn't hit this...
} // getsLineError
void ProcessorClass:: addDictionary(char *sfilename, wordtype type)
// add dictionary to the dictList
{
if (nextDict) { // continue an existing list
nextDict->next = new(dictListStruct);
nextDict = nextDict->next;
strcpy(nextDict->fileName,sfilename);
nextDict->next = NULL;
}
else {
strcpy(dictFileList.fileName,sfilename); // start the list here
dictFileList.next = NULL;
nextDict = &dictFileList;
}
curDict++;
nextDict->dictType = type;
nextDict->index = curDict;
// Processor.fprob << "PC:cD M1 " << nextDict->fileName << " index " << curDict << endl; // *** debug
nextDict->isFlat = true;
nextDict->hasNounList = false;
nextDict->hasAdjctList = false;
nextDict->tailLoc = -1; // location and number of comments at end of file
nextDict->tailLen = 0;
nextDict->nChanges = 0;
} // addDictionary
void ProcessorClass:: validateDate(char *ps)
// checks that string beginning at ps is more or less valid as a date
// Note that this does not look past the first 6 or 8 chars --putDate is sometimes called with a longer
// string, so extra characters will just be ignored
// Added: vers 7.5
{
char * end; // required for strtol
char year[5], month[3], day[3];
long lval;
// Year
if (TabariFlags.f4DigitYear) {
strncpy(year,ps,4); // year
year[4] = '\0';
ps += 4;
}
else {
strncpy(year,ps,2);
year[2] = '\0';
ps += 2;
}
lval = strtol(year, &end, 10);
if (year == end) throw DATE_INVLD;
if (TabariFlags.f4DigitYear) {
if ((lval < 1850) || (lval>2030)) throw YEAR_OOR; // ### this will eventually need updating...
}
// Month
strncpy(month,ps,2);
month[2] = '\0';
lval = strtol(month, &end, 10);
if (month == end) throw DATE_INVLD;
if ((lval < 1) || (lval>12)) throw MONTH_OOR;
// Day
strncpy(day,ps+2,2);
day[2] = '\0';
lval = strtol(day, &end, 10);
if (day == end) throw DATE_INVLD;
if ((lval < 1) || (lval>31)) throw DAY_OOR; // not strictly accurate -- allows 970231 for example -- but close enough to avoid problems
} // validDate
void ProcessorClass:: getCommands(char *sfilename)
// processes a set of command line arguments
// called from doProcess
{
char *curloc = sfilename;
instring sInfo;
while (curloc) {
// WriteLine(">>",curloc);
if (curloc[1] == 'a') {
switch (curloc[2]) { // handle -a* options
case 'n' : // same as ' '
case ' ':
TabariFlags.fAutoCodemode = 0; break; // default and 'None' ### replace the magic numbers here; also let 0 indicate OFF...
curloc--; // adjust for single-letter option
break;
case 'd' : TabariFlags.fAutoCodemode = 1; break; // 'Date'
case 'f' : TabariFlags.fAutoCodemode = 2; break; // 'Full'
case 'q' : TabariFlags.fAutoCodemode = 4; break; // quiet mode: does not ask to write usage stats
default : ShowWarningError("Unrecognized argument in command line","Command line input will be ignored",shError00);
return;
}
if (curloc[2] == ' ') Copy2Chr(projectFile, curloc+3,'-');
else Copy2Chr(projectFile, curloc+4,'-');
}
if (curloc[1] == 't') { // read single text file name
fCmdText = true;
Copy2Chr(sInfo, curloc+3,'-');
strcpy(textFileList.fileName,sInfo); // start the list here
textFileList.next = NULL;
nextFile = &textFileList;
WriteLine("Text file in command line:", sInfo);
}
if (curloc[1] == 'o') { // read event (output) file name
fCmdEvent = true;
TabariFlags.fwriteEvents = true;
Copy2Chr(&eventFile[1], curloc+3,'-');
eventFile[0] = '-'; // signal this is an <outputfile>
WriteLine("Event output file in command line:", eventFile);
}
if (curloc[1] == 'p') { // read event (output) file name
Copy2Chr(projectFile, curloc+3,'-');
WriteLine("Project file in command line:", projectFile);
}
if (curloc[1] == 'f') { // read eventFile and textfile list from file
fCmdText = true;
TabariFlags.fwriteEvents = true;
fCmdEvent = true;
Copy2Chr(sInfo, curloc+3,'-');
fin.open(sInfo,ios::in);
if (fin.fail()) ShowFatalError("Unable to open the command line file ",sfilename,shError03);
WriteLine("Reading text and event output file names from:", sInfo);
getsLine();
TrimBlanks(sLine); // get event (output) file name
strcpy(&eventFile[1], sLine);
eventFile[0] = '-'; // signal this is an <outputfile>
getsLine();
TrimBlanks(sLine); // get first text file name
strcpy(textFileList.fileName,sLine); // start the list here
textFileList.next = NULL;
nextFile = &textFileList;
while (!fin.eof()) { // get remaining text files
getsLine();
TrimBlanks(sLine);
nextFile->next = new(textListStruct);
nextFile = nextFile->next;
strcpy(nextFile->fileName,sLine);
nextFile->next = NULL;
}
fin.close();
}
curloc = strchr(curloc+1,'-');
}
} // getCommands
void ProcessorClass:: readProject(void)
// reads the project file
{
instring sInfo;
char *pst;
bool skiprun = false;
fin.open(projectFile,ios::in);
if (fin.fail()) ShowFatalError("Unable to open the project file ",projectFile,shError03);
try {
while (!fin.eof()) { // primary input loop
while ((!fin.eof()) && ( '<' != sLine[0])) {
getsLine();
TrimBlanks(sLine);
}
if (('s' == tolower(sLine[1])) && ('e' == tolower(sLine[2]))) { // skip over the <session> tags
getsLine(); // get next line in the file
continue;
}
pst = strchr(sLine,'>'); // find end of tag;
if (pst) {
if (strchr(pst+1,'<')) Copy2Chr(sInfo,pst+1,'<'); // copy to terminating tag
else strcpy(sInfo,pst+1); // copy rest of string
}
else {
ShowWarningError("No closing '>' in tag :\n",sLine,"Continuing to read project file",shError00);
getsLine(); // get next line in the file
continue;
}
TrimBlanks(sInfo);
if (!(*sInfo) && ('r' !=tolower(sLine[1])) ) {
ShowWarningError("No content following tag :\n",sLine,"Continuing to read project file",shError00);
getsLine(); // get next line in the file
continue;
}
switch (tolower(sLine[1])) { // process the various possible tags
case 'a':
if ('c' == tolower(sLine[2])) { // differentiate 'actor' and 'agent'
addDictionary(sInfo, Actor);
if (!firstActorDict) firstActorDict = curDict;
}
else {
addDictionary(sInfo, Agent);
if (!firstAgentDict) firstAgentDict = curDict;
}
break;
case 'c':
strcpy(sCoder, sInfo);
break;
case 'd':
fhasDocs = true;
break;
case 'e':
if ('v' == tolower(sLine[2])) { // differentiate 'event' and 'error'
if (fCmdEvent) {
WriteLine("<outputfile> name set in command line; skipping <eventfile> entry in .project file");
break;
}
strcpy(eventFile, sInfo);
TabariFlags.fwriteEvents = true;
break;
}
else strcpy(errorFile, sInfo);
break;
case 'f':
strcpy(freqFile, sInfo);
TabariFlags.fhasFreqs = true;
break;
case 'i':
strcpy(issueFile, sInfo);
TabariFlags.fhasIssues = true;
break;
case 'o':
if ('u' == tolower(sLine[2])) { // differentiate 'event' and 'error'
if (fCmdEvent) {
WriteLine("Event output file name set in command line; skipping <outtfile> entry in .project file");
break;
}
strcpy(eventFile, "-"); // signals that this is a constant <outputfile> name, not incremented
strcat(eventFile, sInfo);
TabariFlags.fwriteEvents = true;
break;
}
else strcpy(optionsFile, sInfo);
break;
case 'p':
strcpy(probFile, sInfo);
if (strchr(sLine,'!')) fwroteProb = true; // prob file will be used for debugging output; don't delete
break;
case 'r':
skiprun = true;
fhasRun = true;
break;
case 's': { // do system command
if (skiprun) break;
int ka = system(sInfo);
if (-1 == ka) ShowWarningError("Unable to execute <system> command:\n",sInfo,shError34);
else WriteLine("Ran command: ",sInfo); // display last session inf
break;
}
case 't': // store text file names in a linked list
if (fCmdText) {
WriteLine("Text files set in command line; skipping <textfile> entries in .project file");
break;
}
if (nextFile) { // continue an existing list
nextFile->next = new(textListStruct);
nextFile = nextFile->next;
strcpy(nextFile->fileName,sInfo);
nextFile->next = NULL;
}
else {
strcpy(textFileList.fileName,sInfo); // start the list here
textFileList.next = NULL;
nextFile = &textFileList;
}
// check for XML tag
if (strstr(sLine,sXMLField)) {
nextFile->indexXML = ReadXML.setXMLType(sLine);
ReadXML.SetXMLTags(nextFile->indexXML); // reset the tag strings
}
else nextFile->indexXML = 0;
break;
case 'v':
addDictionary(sInfo, Verb);
if (!firstVerbDict) firstVerbDict = curDict;
break;
default:
ShowWarningError("Unrecognized tag :\n",sLine,"Continuing to read project file",shError00);
}
getsLine(); // get next line in the file
} // while !fin.eof
} // try
catch (int i) { // most likely cause here is file in the wrong file format, so just quit
getsLineError(i);
}
fin.close();
// verify that we've got the minimum files to work with
if (!firstActorDict || !firstVerbDict || ('\0' == *textFileList.fileName)) ShowFatalError("At least one .actors, .verbs, and .text file is required to run program ",shError0A);;
// get the skip; date, start
MakeTimeDateStr(sStart,sInfo); // get the start time
if ((sLine) && ('s' == sLine[1])) { // this is actually processing the last *non-blank* line (which is okay)
nSession = atoi(&sLine[9]); // get last session number
pst = strstr(&sLine[1],"<last ");
if (pst) {
Copy2Chr(sInfo,pst+6,'>');
nLast = atoi(sInfo); // get last record coded info
}
else nLast = 0;
WRITESTRING("Last coding session: "); // display last session info
pst = strstr(&sLine[1],"<end ");
if (pst) WRITESTRING(Copy2Chr(sInfo,pst+5,'>'));
WRITESTRING(" ");
pst = strstr(&sLine[1],"<date ");
if (pst) WRITESTRING(Copy2Chr(sInfo,pst+6,'>'));
WRITESTRING("; Coder ");
pst = strstr(&sLine[1],"<coder ");
if (pst) WRITESTRING(Copy2Chr(sInfo,pst+7,'>'));
WriteLine(" "); // ####
}
else {
nSession = 0; // initialize the file
nLast = 0;
}
} // readProject
void ProcessorClass:: nextTextFile(char *s)
// gets the next string containing the next text file name
{
if (nextFile) {
strcpy(s, nextFile->fileName);
fusingXML = (bool)nextFile->indexXML;
nextFile = nextFile->next;
}
else s[0] = '\0';
return;
} // nextTextFile
void ProcessorClass:: writeProject(void)
// writes the project file
{
litstring sdate,stime;
if (TabariFlags.fwriteEvents) {
fout.close(); // close event file
fout.clear(); // clear the eof flag (or if file already closed)
}
fout.open(projectFile,ios::app);
if (fout.fail()) ShowFatalError("Unable to reopen file ",projectFile,shError03);
WRITEEOL();
ResetCurs();
WriteLine("Writing ", projectFile);
MakeTimeDateStr(stime,sdate);
TrimEndBlanks(sStart);
TrimEndBlanks(stime);
fout << "<session " << nSession;
fout << "> <coder " << sCoder;
fout << "> <date " << sdate;
fout << "> <start " << sStart;
fout << "> <end " << stime;
fout << "> <records " << nRecords;
fout << "> <actorchanges " << nActorChanges;
fout << "> <verbchanges " << nVerbChanges;
fout << "> <last " << nLast;
fout << ">" << endl;
fout.close(); // close project file
} // writeProject
void ProcessorClass:: writeDoc(void)
// writes the documentation header to the event file
{
litstring sdate,stime;
instring sInfo;
char *pst;
WriteLine("Writing event file documentation...");
// gets the first date in the first file and create id string
nextFile = &textFileList; // set nextFile to start of text file list
openNextText();
try {getsLine();}
catch (int err) { getsLineError(err);}
if (TabariFlags.f4DigitYear) {
strncpy(sDoc,sLine,8);
sDoc[8] = '\0';
}
else {
strncpy(sDoc,sLine,6);
sDoc[6] = '\0';
}
strcat(sDoc,"\tDOC\tDOC\t999\t");
fin.close();
fin.open(projectFile,ios::in);
if (fin.fail()) ShowFatalError("writeDoc is unable to reopen project file ",projectFile,shError03);
MakeTimeDateStr(stime,sdate);
TrimEndBlanks(stime);
fout << sDoc << "Data generated by TABARI version " << sRelease << endl;
fout << sDoc << sdate << " " << stime << endl;
fout << sDoc << "Session " << nSession << "; Coder " << sCoder << endl;
sLine[0] = '#'; // force read
try {
while (!fin.eof()) { // get the <dochead> lines
while ((!fin.eof()) && ( '<' != sLine[0])) {
getsLine();
TrimBlanks(sLine);
}
if (strstr(sLine,"<sess")) break; // no need to read <session> records
if (strstr(sLine,"<dochead")) {
pst = strchr(sLine,'>'); // find end of tag;
if (pst) strcpy(sInfo,pst+1); // copy rest of string
else continue;
TrimBlanks(sInfo);
fout << sDoc << sInfo << endl;
}
sLine[0] = '#'; // force read
}
fin.close(); // ### is there a better way to rewind a file??
fin.open(projectFile,ios::in);
sLine[0] = '#'; // force read
while (!fin.eof()) { // copy the functional lines
while ((!fin.eof()) && ( '<' != sLine[0])) {
getsLine();
TrimBlanks(sLine);
}
if (strstr(sLine,"<sess")) break; // no need to read <session> records
if (('<' == sLine[0]) && (!strstr(sLine,"<doc"))) {
TrimBlanks(sLine);
fout << sDoc << sLine << endl;
}
sLine[0] = '#'; // force read
}
fin.close();
fin.open(projectFile,ios::in);
sLine[0] = '#'; // force read
while (!fin.eof()) { // get the <doctail> lines
while ((!fin.eof()) && ( '<' != sLine[0])) {
getsLine();
TrimBlanks(sLine);
}
if (strstr(sLine,"<sess")) break; // no need to read <session> records
if (strstr(sLine,"<doctail")) {
pst = strchr(sLine,'>'); // find end of tag;
if (pst) strcpy(sInfo,pst+1); // copy rest of string
else continue;
TrimBlanks(sInfo);
fout << sDoc << sInfo << endl;
}
sLine[0] = '#'; // force read
}
}
catch(int i) {getsLineError(i);}
fin.close(); // close project file
} // writeDoc
void ProcessorClass:: initSession(void)
// initialize coding session
{
char snum[] = " ";
filestring evtfilename;
++nSession; // increment session number
nRecords = 0;
Int2Str(snum,nSession); // get suffix for output files <08.12.30> ### use a C function here?
WriteLine("Coding session ",snum);
#if TAB_DEBUG
strcpy(sCoder,"debug");
#else
if (!sCoder[0]) { // may have already been set by <coder> in .project filestring
GetInput(sCoder,"Enter coder ID");
if (!sCoder[0]) strcpy(sCoder,"***");
}
#endif
if (TabariFlags.fwriteEvents) {
if (eventFile[0] == '-') { // constant <outputfile>
strcpy(evtfilename,&eventFile[1]);
} else { // indexed <eventfile>
strcat(evtfilename,eventFile);
strcat(evtfilename,".");
strcat(evtfilename,snum);
}
fout.open(evtfilename,ios::out);
if (fout.fail()) ShowFatalError("Unable to open event file ",evtfilename,shError03);
WriteLine("Writing events to ",evtfilename);
if (fhasDocs) writeDoc();
}
if (*probFile) {
if (fwroteProb) strcat(probFile,".debug"); // debugging option
else {
strcat(probFile,".");
strcat(probFile,snum);
}
// WriteLine("Open Probfile: ",probFile); // *** debug
fprob.open(probFile,ios::out);
if (fprob.fail()) ShowFatalError("Unable to open problems file ",probFile,shError03);
}
ReadFiles.readOptions(optionsFile);
nextDict = &dictFileList; // read various dictionaries
while (nextDict) {
curDict = nextDict->index;
if (Actor == nextDict->dictType) ReadFiles.readActors();
else if (Agent == nextDict->dictType) ReadFiles.readAgents(nextDict->fileName);
else if (Verb == nextDict->dictType) ReadFiles.readVerbs(nextDict->fileName);
nextDict = nextDict->next;
}
if (*issueFile) ReadFiles.readIssues(issueFile);
if (*freqFile) {
ReadFiles.readFreqs(freqFile);
if (fhasDocs) { // add frequency identifiers to documentation
freqStruct * pHead = &Coder.freqHead; // current issue header
fout << sDoc << "\tFREQUENCIES:"; // write full descriptors
while (pHead) { // traverse list
if (pHead->abbrev) fout << '\t' << pHead->phrase;
else fout << "\t---";
pHead = pHead->pnext;
}
fout << endl;
pHead = &Coder.freqHead; // current issue header
fout << sDoc << "\tFREQS:"; // write abbrevs
while (pHead) { // traverse list
if (pHead->abbrev) fout << '\t' << pHead->abbrev;
else fout << "\t -- ";
pHead = pHead->pnext;
}
fout << endl;
}
}
nextFile = &textFileList; // set nextFile to start of text file list
#if !(TAB_DEBUG)
if ((nLast > 0) && (-1 == TabariFlags.fAutoCodemode) && (TabariFlags.fCheckSkip)) {
instring sprompt;
sprintf(sprompt,"Skip the %ld records coded in previous sessions",nLast);
if (GetAnswer(sprompt, 'Y','N')) skipRecords();
else {
nLast = 0;
openNextText();
}
}
else openNextText();
#else
openNextText();
#endif
} //initSession
bool ProcessorClass:: openNextText(void)
// opens the next text file; returns false when list is finished
{
nextTextFile(textFile);
while (textFile[0]) {
if (fusingXML) {
if (ReadXML.openXMLFile(textFile)) break;
} else {
fin.clear(); // clear the eof flag from previous file operations
fin.open(textFile,ios::in);
if (!fin.fail()) break; // everything is okay, so drop out of loop
ShowWarningError("Unable to open the text file ",textFile,"File will be skipped",shError08);
fin.clear(); // clear the error
}
nextTextFile(textFile); // last file failed, so try next one
}
if (textFile[0]) return true; // we opened a file, so return true
else return false; // none of the files could be opened
} // openNextText
void ProcessorClass:: closeText(void)
// closes the text file
{
if (fusingXML) ReadXML.closeXMLFile();
else fin.close();
} // closeText
void ProcessorClass:: openError(void)
// opens the error file
{
litstring sdate, stime;
ferror.open(errorFile,ios::out);
if (ferror.fail()) ShowFatalError("Unable to open error file ",errorFile,shError03);
fopenError = true;
MakeTimeDateStr(stime,sdate);
TrimEndBlanks(stime);
ferror << "TABARI Error file\nData generated by TABARI version " << sRelease << endl;
ferror << sdate << " " << stime << endl;
ferror << "Session " << nSession << "; Coder " << sCoder << endl;
} // openError
void ProcessorClass:: writeError(const char *s, char *s1, int ierror)
// writes to the error file. An error of a specific type is only written once per record,
// and only a total of MAX_ERROR errors are written for a record.
{
char * pst = sentText;
int ka = 0;
if (!(*errorFile)) return; // .project did not request an <errorfile>
if (!fopenError) openError();
while ((ka < MAX_ERROR) && (ierror != didError[ka]) && (didError[ka]>0)) {
// ferror << "ierror1: " << ierror << " " << ka << " " << didError[ka] << endl;
++ka; // check whether this error has already been reported
}
if (ierror == didError[ka]) {
// ferror << "ierror3: " << ierror << " " << ka << " " << didError[ka] << endl;
return; // we already printed this one
}
if (ka >= MAX_ERROR) return; // printed all of the errors we are going to print for this case...
// ferror << "ierror2: " << ierror << " " << ka << " " << didError[ka] << endl;
didError[ka] = ierror; // record this type of error
ferror << "\nInput record: " << sRecEngDate << " "<< sRecID << endl;
ferror << "Input text: ";
while (*pst) ferror << *pst++;
ferror << endl;
// ferror << "ierror: " << ierror << endl;
ferror << s << s1 << endl;
ferror.flush(); // since this could generate a crash real soon...
} // writeError
void ProcessorClass:: closeError(void)
// closes the error file
{
litstring sdate, stime;
WriteLine("Coder/Parser errors reported in ",errorFile);
MakeTimeDateStr(stime,sdate);
ferror << "\nFile closed : " << sdate << " " << stime << endl;
ferror.close();
} // closeText
void ProcessorClass:: changeDict(int iDict, wordtype wtype)
// sets curDict; increments changes count
{
dictListStruct *dictFile = &Processor.dictFileList;
if (iDict) curDict = iDict; // this is used in initRoot
else {
if (Verb == wtype) curDict = firstVerbDict;
else if (Agent == wtype) curDict = firstAgentDict;
else curDict = firstActorDict;
}
while (dictFile) { // increment nChange
if (curDict == dictFile->index) {
dictFile->nChanges++;
if (Noun == wtype) dictFile->hasNounList = true;
else if (Adjctv == wtype) dictFile->hasAdjctList = true;
break;
}
else dictFile = dictFile->next;
} // while
if (!dictFile) ShowFatalError("Unrecoverable dictionary index error in ProcessorClass:changeDict",shError98);
fchangeDict = true;
if (Verb == dictFile->dictType) ++nVerbChanges; // summary used in the project file
else ++nActorChanges; // this is now both actors and agents
} // changeDict
void ProcessorClass:: writeUsage(void)
// writes .use files
{
WriteLine(""); // \n
if (TabariFlags.fActorUsage) {
if (TRUE_3WAY == TabariFlags.fActorUsage) WriteFiles.writeActorUsage(true);
}
else if (GetAnswer((char *)"Write use report for ", (char *)"actors")) WriteFiles.writeActorUsage(true);
if (TabariFlags.fAgentUsage) {
if (TRUE_3WAY == TabariFlags.fAgentUsage) WriteFiles.writeActorUsage(false);
}
else if (GetAnswer((char *)"Write use report for ", (char *)"agents")) WriteFiles.writeActorUsage(false);
if (TabariFlags.fVerbUsage) {
if (TRUE_3WAY == TabariFlags.fVerbUsage) WriteFiles.writeVerbUsage();
}
else if (GetAnswer((char *)"Write use report for ", (char *)"verbs")) WriteFiles.writeVerbUsage();
} // writeUsage
void ProcessorClass:: backFiles(void)
// writes .back files
{
filestring start, sdate;
dictListStruct *dictFile = &Processor.dictFileList;
if (!fchangeDict){
WriteLine("\a\nNo changes have been made; backup skipped");
return;
}
MakeTimeDateStr(start, sdate);
strcpy(scommNew,sCoder);
strcat(scommNew,&sdate[3]); //skip the day-of-week
WriteLine("");
while (dictFile) {
if (dictFile->nChanges) {
if (GetAnswer((char *)"Save a backup for ", dictFile->fileName)){
strcpy(start,dictFile->fileName); // temporarily rename the existing file
strcat(start,".temp");
if (rename(dictFile->fileName,start)) ShowWarningError ("Adding '.temp' to ", dictFile->fileName, "Existing file will be overwritten",shError09);
// write the current files
if ((dictFile->dictType == Actor) || (dictFile->dictType == Agent)) WriteFiles.writeActors(dictFile);
else WriteFiles.writeVerbs(dictFile);
strcpy(sdate,dictFile->fileName); // rename the file we just wrote with .back
strcat(sdate,".back");
if (rename(dictFile->fileName,sdate)) ShowWarningError ("Adding '.back' to ", dictFile->fileName, "Existing file will be overwritten",shError09);
// restore the name of the original file
if (rename(start, dictFile->fileName)) ShowWarningError ("Restoring the original name of ", dictFile->fileName, "Original file still has .temp suffix",shError09);
} // GetAnswer
} // if nChange
dictFile = dictFile->next;
} // while
} // backFiles
void ProcessorClass:: doFinalSysCalls(void)
// handles the <system> calls following <run>
{
instring sInfo;
char *pst;
bool dorun = false;
fin.open(projectFile,ios::in);
if (fin.fail()) ShowFatalError("Unable to open the project file ",projectFile,shError03);
while (!fin.eof()) { // primary input loop
while ((!fin.eof()) && ( '<' != sLine[0])) {
getsLine();
}
if (('s' == tolower(sLine[1])) && ('e' == tolower(sLine[2]))) { break;} // terminate when we hit <session> tags
if (('s' != tolower(sLine[1])) && ('r' != tolower(sLine[1]))) { // skip over everything except <system> and <run>
getsLine(); // get next line in the file
continue;
}
TrimBlanks(sLine);
pst = strchr(sLine,'>'); // find end of tag;
if (pst) {
if (strchr(pst+1,'<')) Copy2Chr(sInfo,pst+1,'<'); // copy to terminating tag
else strcpy(sInfo,pst+1); // copy rest of string
while ((pst = strstr(sInfo, "$SESSN"))) { // replace $SESSN strings with session number
char sess[5];
instring sNew;
char * pnew = sNew;
char * psr = sInfo;
sprintf(sess,"%d",nSession); // convert session number to string
// WriteLine("sInfo:",sInfo);
// WriteLine("sess:",sess);
while (psr < pst) *pnew++ = *psr++; // copy chars before match
*pnew = '\0';
// WriteLine("sNew1:",sNew);
strcat(sNew,sess); // add session num string
// WriteLine("sNew2",sNew);
strcat(sNew,pst+6); // copy everything else
// WriteLine("sNew3",sNew);
strcpy(sInfo,sNew);
// WriteLine("Ran command: ",sInfo);
}
}
else {
ShowWarningError("No closing '>' in tag :\n",sLine,"Continuing to read project file",shError00);
getsLine(); // get next line in the file
continue;
}
TrimBlanks(sInfo);
if ('s' == tolower(sLine[1])) { // do system command
if (dorun) {
int ka = system(sInfo);
if (-1 == ka) ShowWarningError("Unable to execute <system> command:\n",sInfo,shError34);
else WriteLine("Ran command: ",sInfo); // display last session inf
}
}
else dorun = true;
getsLine(); // get next line in the file
} // while !fin.eof
fin.close();
} // doFinalSysCalls
void ProcessorClass:: endSession(void)
// end the coding session
{
ClearScreen();
// if (!TabariFlags.fActorUsage || !TabariFlags.fVerbUsage) ClearScreen();
WriteLine("Q)uit entered in menu; finishing TABARI session");
fin.close();
fin.clear(); // clear the eof flag (or if file already closed)
if (*probFile) {
// WriteLine("Probfile: ",probFile); // *** debug
fprob.close();
if (fprob.fail()) ShowWarningError("Unable to close problems file ",probFile,shError03);
if (!fwroteProb) remove(probFile); // get rid of problems file if we haven't used it
}
if (fopenError) closeError();
writeProject(); // Note: the event file is closed in this function
#if TAB_DEBUG
remove(eventFile);
exit(0);
#endif
if (fchangeDict){
filestring start;
filestring sdate;
dictListStruct *dictFile = &Processor.dictFileList;
bool freply = false;
// WriteLine("PC:eS Mk1"); // *** debug
MakeTimeDateStr(start, sdate);
strcpy(scommNew,sCoder);
strcat(scommNew,&sdate[3]); //skip the day-of-week
while (dictFile) {
// WriteLine("PC:eS Mk2 ", dictFile->fileName); // *** debug
if (dictFile->nChanges) {
// WriteLine("PC:eS Mk3 ", dictFile->fileName); // *** debug
freply = GetAnswer((char *)"Save changes to ", dictFile->fileName);
if (!freply) {
char s[48];
sprintf(s, "You are about to discard %d changes;", dictFile->nChanges); // ### update this for the individual dictionary counts?
freply = !GetAnswer(s,(char *)" do you *really* want to do this");
}
if (freply) {
strcpy(start,dictFile->fileName);
strcat(start,".old");
remove(start);
if (rename(dictFile->fileName,start)) ShowWarningError ("Adding '.old' to ", dictFile->fileName, "Existing file will be overwritten",shError00);
if (Verb == dictFile->dictType) WriteFiles.writeVerbs(dictFile);
else WriteFiles.writeActors(dictFile); // actors and agents
}
} // if nChange
dictFile = dictFile->next;
} // while
} // if
#if false // deactivated in version 0.8+
if (pTimeModList) {
WriteLine("Sorting time-shifted events");
sortTimeOutput();
}
#endif
if (fhasRun) doFinalSysCalls();
if (TabariFlags.fAutoCodemode < 0) {
ResetCurs();
WriteNewLine("Press any key to end TABARI program");
getUpper();
}
exit(0);
} // endSession
void ProcessorClass:: doOther(void)
// handles the O)ther option
// currently returns to the main menu after selection
{
int imenu;
imenu = OtherMenu((bool) *probFile);
switch (imenu) {
case 1: backFiles();
break;
case 2: writeProblem();
break;
case 3: showMemory();
setupMenu(false);
break;
case 4: writeUsage();
break;