-
Notifications
You must be signed in to change notification settings - Fork 0
/
codes.cp
executable file
·1167 lines (1031 loc) · 37.3 KB
/
codes.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
//__________________________________________________________________________________
// codes.cp
// This file contains an assortment of utilities for managing codes
// Code storage strategy:
// Codes are stored as a sequence of 32-bit words in the TokenBuf array. 1-bit
// flags in the high-order bits -- and the local sequencing of the words --
// determines the content of each words. These sequences are interpreted by
// various storage and access routines in the CodeStoreClass. The storage scheme
// is designed to:
// (a) Minimize the storage required for the most common type of codes;
// (b) Provide a straightforward for the new codes types;
// (c) Substantially extend the 1024 code limitation of KEDS, while still
// making efficient use of storage
// [All of this is a way of apologizing for the fact that yes, this is complicated,
// but it is also fast and efficient].
// All objects with codes have a pointer to a 1-word "header" that determines the
// type of information that follows. In addition, the structure of event, actor and
// issue codes is quite different; so type of code being accessed also determines
// how the TokenBuf words are interpreted.
// Note: <09.09.17> There are some older work-arounds in the code that were used
// to get around a 12-bit indexing scheme that was used prior to version 0.7.4
// when TokBuf consisted of 16-bit words and was limited to 65536 entries. The
// current version uses 32-bit words and 28-bit indexing, and should accommodate
// ay dictionaries that are foreseeable at present.
// In this description, bits are numbered from right to left starting at zero (i.e.
// 0x80000000 is bit 31; 0x00000040 is bit 6, etc).
// =========== EVENT CODES ===========
//
// Header and paired code
// bit
// 31-30 00 : No precedence
// 01 : subordinate code
// 10 : dominant code
// 11 : [currently unused: getEventString will need to be changed to use it]
// 29-28 00 : simple code
// 01 : paired code follows
// 10 : issue follows
// 11 : STA follows
// 27-00 : code index
//
// Issues [this is still tentative...a hex serial for the issue might be added...###]
// 31-30 : [currently unused]
// 29-28 00 : termination
// 01 : [currently unused]
// 10 : issue follows
// 11 : STA follows
// 27-00 : index of issue code sequence
//
// Source, target and attribution (STA) links
// 31-30 00 : source
// 01 : target
// 10 : attribution
// 11 : special code -- all other code formats
// 29 : continuation
// 27-00 : index of actor/agent code sequence
// Special formats
// ### need to figure out storage of issue codes
// 31-30 00 : source
// 01 : target
// 10 : attribution
// 11 : special code -- all other code formats [currently unused]
// 29 : continuation
// 27-00 : index of actor/agent code sequence
// ====== ACTOR AND AGENT CODES ======
// These form a hierarchy:
// [Date restrictions] : interpreted through date headers
// [Compound codes] : contiguous; end of list has bit-13 = 0
// [Actor:Agent pairs] : interpreted through bit 14
//
// Header tags:
// bit
// 31-30 00 : simple actor or agent code
// 01 : actor:agent pair
// 10 : date header
// 11 : special code -- all other code formats [currently unused]
//
// 29 1 : if the code is continued in a compound
// 0 : if code is isolated or the final code of a compound
// 28-00 :code index
// Notes on actor and agent codes
//
// 1. The header for a simple code is simply its code index, which can be used
// without further processing.
//
// 2. Compound actor and actor:agent codes are simply stored in sequence. If the
// continuation bit is set then another code follows. This is list is followed
// until the continuation bit is off
//
// 3. Isolated actor and agent codes are stored in the same manner; the routine
// that accesses the code is assumed to know the type.
// FORMAT OF DATE RESTRICTION HEADER
//
// bit [least significant = 0]
// 31 1
// 30 0
// 29 continuation?
// 28-27 00: default code; next word is start of code sequence
// 01: next word is a <[date] constraint
// 10: next word is a >[date] constraint
// 11: next two words are [date]-[date] constraints
// 26-25 Currently unused
// 24-17 Number of words to next date header; if zero, this is in next word
// ### <09.09.17> how is this used? -- when date restrictions are added?
//
// Date restrictions are handled by simply skipping through this list until a
// code sequence matching the date is found. The output system puts the default
// code at the end of the list irrespective of how it was originally entered.
//
// Date constraints are Julian dates (unsigned word) with a base at 1 January 1904
// (same as Macintosh MS-Excel). This will handle dates until around 2085 (the base
// can be adjusted by changing a constant, but if the current system *does not*
// handle century leap years correctly [but it will, I'm sure...###]
//
// =========== TIME SHIFT CODES ===========
//
// These are used in the <TIME> segment
// bit
// 31-33 00 : Increment
// 10 : Decrement
// 01 : Value is a code index
// 11 : not used
// 29-00 : value
//__________________________________________________________________________________
//
// Copyright (c) 2002-2009 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: schrodt@ku.edu
// The most recent version of this code is available from the KEDS Web site:
// http://www.ku.edu/~keds
// For plausible indenting of this source code, set the tab size in your editor to "2"
//___________________________________________________________________________________
// Headers
#include "TABARI.h"
//___________________________________________________________________________________
// Declare globals
extern CharStoreClass CharStore;
extern TokenStoreClass TokenStore;
extern ProcessorClass Processor;
//___________________________________________________________________________________
// Utility functions
void CodeStoreClass::codeTest(void)
// this is a dummy function for testing components of this class
// ### need to go through and figure out the response to various errors
{
//instring s, sa;
/*
instring sdate;
toktype itok;
toktype ka;
bool isCode;
toktype actcode, agtcode;
tokptr pt;
#if FALSE
while (true) { // test Julian system
cin >> s;
if (!isdigit(*s)) break;
cout << "Input :" << s << endl;
itok = MakeJulian(s);
cout << "Julian:" << itok << endl;
JulianString(sa,itok);
cout << "Output:" << sa << endl;
}
#endif
#if TRUE // test parseActorString
cout << "parseActorString" << endl;
strcpy(s,"ABC:CDE");
// strcpy(s,"[ABC:CDE/GHI:JKL/ZXC/YHG]");
// strcpy(s,"[ABC:CDE/GHI:JKL/ZXC/YHG <980101] [ABC/DFG >990101] [ABC:XYZ123/DFG]");
strcpy(s,"[ABC:CDE/GHI:JKL/ZXC/YHG <980101][ABC:CDE/GHI:JKL/---/YHG 980101-990101] [ABC/DFG >990101] [QWE]");
cout << "Input :" << s << endl;
parseActorString(sa,s);
cout << "Output:" << sa << endl;
#endif
#if TRUE // test parseEventString
cout << "\nparseEventString" << endl;
strcpy(s,"[ABC:CDE]");
// strcpy(s,"[ABC:CDE/GHI//ZXC/YHG]");
strcpy(s,"[ABC:CDE/GHI//ZXC/YHG +([ABC:DFG/GHJ<980101]) $([---]) ]");
cout << "Input :" << s << endl;
parseEventString(sa,s);
cout << "Output:" << sa << endl;
#endif
#if TRUE // test store/getActorString
cout << "\ngetActorString" << endl;
strcpy(s,"[ABC:CDE]");
strcpy(s,"[ABC]");
// strcpy(s,"[ABC:CDE/GHI:JKL/ZXC/YHG]");
// strcpy(s,"[ABC:CDE/GHI:JKL/ZXC/YHG <980101] [ABC/DFG >990101] [ABC:XYZ123/DFG]");
// strcpy(s,"[ABC:CDE/GHI:JKL/ZXC/YHG <980101][ABC:CDE/GHI:JKL/---/YHG 980101-990101] [ABC/DFG >990101] [QWE]");
strcpy(s,"[LEBAM1:ML1/LEBHZ1:GR1 <930101] [LEBAM2:ML2/LEBHZ2:GR2 930102-940101] [LEBAM3:ML3/LEBHZ3:GR3 >950101]");
cout << "Input :" << s << endl;
itok = storeActorString(s);
getActorString(sa,itok);
cout << "Output:" << sa << endl;
#endif
#if TRUE // test nextActorCode
strcpy(sdate,"19981220");
// strcpy(s,"[ABC]");
// strcpy(s,"[ABC:CDE]");
// strcpy(s,"[ABC:CDE/GHI:JKL/ZXC/YHG]");
// strcpy(s,"[ABC:CDE/GHI:JKL/ZXC/YHG <980101] [ABC/DFG >990101] [ABC:XYZ123/DFG]");
strcpy(s,"[ABC:CDE/GHI:JKL/ZXC/YHG <980101][ABC:CDE/GHI:JKL/---/YHG 980101-990101] [ABC/DFG >990101] [QWE]");
Processor.julianDate = MakeJulian(sdate);
cout << "Input :" << s << endl;
cout << "Date :" << sdate << endl;
itok = storeActorString(s);
pt = NULL;
do {
nextActorCode(actcode,agtcode,&pt,itok);
if (actcode) cout << "Actor: " << getCodePtr(actcode) << endl;
if (agtcode) cout << "Agent: " << getCodePtr(agtcode) << endl;
} while (pt);
#endif
#if TRUE // test store/getEventString
// strcpy(s,"ABC!:CDE?");
// strcpy(s,"[ABC:CDE/GHI/ZXC/YHG]");
strcpy(s,"[ABC:CDE/GHI//ZXC/YHG +([ABC:DFG/GHJ <980101]) $([---]) ]");
cout << "Input :" << s << endl;
itok = storeEventString(s);
getEventString(sa,itok);
cout << "Output:" << sa << endl;
#endif
#if FALSE // test store/getTimeCode
strcpy(s,"[ 123 ]");
// strcpy(s,"[---]");
// strcpy(s,"[-1023]");
// strcpy(s,"[10023]");
WriteLine("Input :",s);
itok = storeTimeCode(s);
ka = getTimeCode(itok,isCode);
getTimeString(sa,itok);
WriteLine("Output:",sa);
#endif
*/
} // codeTest
toktype CodeStoreClass::findCode (char *scode)
// Finds code index in Code_Buf; set to 0 if doesn't exist
{ toktype ka = 1;
// cout << "fC :" << scode << endl; // *** debugging
while ((ka < iCode) && (strcmp(scode, codeArray[ka].pcode))) { ka++;
// cout << " " << ka << " " << codeArray[ka].pcode << endl; // *** debugging
}
if (ka < iCode) return ka;
else return 0;
} // findCode
toktype CodeStoreClass::addCode (char *scode)
// Finds code index in Code_Buf if it exists or else adds it; stores null text
// [single-parameter version of the original addCode: see notes]
{
toktype ka = findCode(scode);
if (!ka) {
if (iCode >= MAX_CODES - WARN_LEVEL) {
if (iCode < MAX_CODES)
ShowWarningError("Running out of memory in CodeStoreClass::codeArray", "Program will continue",shError10);
else ShowFatalError("Allocated memory exceeded in CodeStoreClass::codeArray",shError11);
}
codeArray[iCode].pcode = CharStore.putCharStr(scode);
codeArray[iCode].ptext = CharStore.charNullString;
++iCode;
return (iCode-1);
}
else return ka;
} // addCode
toktype CodeStoreClass::addCode (const char *scode, char *stext)
// Finds code index in Code_Buf if it exists or else adds it; also stores text
// This version is used only in the initialization
// ### and therefore could probably eliminate some of the contigencies
{
toktype ka = findCode(const_cast<char *>(scode));
if (!ka) {
if (iCode >= MAX_CODES - WARN_LEVEL) {
if (iCode < MAX_CODES)
ShowWarningError("Running out of memory in CodeStoreClass::codeArray", "Program will continue",shError10);
else ShowFatalError("Allocated memory exceeded in CodeStoreClass::codeArray",shError11);
}
codeArray[iCode].pcode = CharStore.putCharStr(const_cast<char *>(scode));
if (stext[0]) codeArray[iCode].ptext = CharStore.putCharStr(stext);
else codeArray[iCode].ptext = CharStore.charNullString;
++iCode;
return (iCode-1);
}
else return ka;
} // addCode
toktype CodeStoreClass::addCode (char *scode, char *stext)
// Finds code index in Code_Buf if it exists or else adds it; also stores text
// ### should we switch this so that iCode is the *last* code added, rather than the
// *next* code? -- that is how most everything else works
{
toktype ka = findCode(scode);
if (!ka) {
if (iCode >= MAX_CODES - WARN_LEVEL) {
if (iCode < MAX_CODES)
ShowWarningError("Running out of memory in CodeStoreClass::codeArray", "Program will continue",shError10);
else ShowFatalError("Allocated memory exceeded in CodeStoreClass::codeArray",shError11);
}
codeArray[iCode].pcode = CharStore.putCharStr(scode);
if (stext[0]) codeArray[iCode].ptext = CharStore.putCharStr(stext);
else codeArray[iCode].ptext = CharStore.charNullString;
++iCode;
return (iCode-1);
}
else return ka;
} // addCode
char* CodeStoreClass::getCodePtr (toktype itok)
// Returns a pointer to the text of code for Code_Buf[itok]; warn and set to 0 if doesn't exist
{
if (itok < iCode) return codeArray[itok].pcode;
else {
ShowWarningError ("Out of range code index in CodeStoreClass::getCodePtr","Program will continue but may crash soon",shError00 );
return 0;
}
} // Find_Code
char* CodeStoreClass::getCodeLabel (toktype itok)
// Returns a pointer to label for Code_Buf[itok]; warn and set to 0 if doesn't exist
{
if (itok < iCode) return codeArray[itok].ptext;
else {
ShowWarningError ("Out of range code index in CodeStoreClass::getCodeLabel","Program will continue but may crash soon",shError00 );
return 0;
}
} // getCodeLabel
toktype CodeStoreClass::storeCode (char *scode, toktype mask)
// cleans blanks from scode, get the codeindex, |'s the index with mask,
// stores in TokBuf, returns index of that TokBuf location
// ### needs to handle extra storage of high codes
{
toktype index;
TrimBlanks(scode);
index = (toktype) addCode(scode);
index |= mask;
return TokenStore.putToken(index);
} // storeCode
toktype CodeStoreClass::makeCode (char *scode, toktype mask)
// same cleaning as storeCode, except it doesn't store
// ### needs to handle extra storage of high codes
{
toktype index;
TrimBlanks(scode);
index = (toktype) addCode(scode);
index |=mask;
return index;
} // makeCode
//___________________________________________________________________________________
// Actor functions
void CodeStoreClass::addNullString (char *s)
// checks for a solitary dated code; adds null code as default
// ### <09.11.18> This looks problematic on multiple dimensions...
// a. first statement would get confused with a null code
// b. why just look for two blocks? -- what about other incomplete combinations?
// c. can't the null default be handled by the coder?
{
char * pst;
pst = strpbrk(s,"<>-"); // check for the date operators
if ((!pst) || (!isdigit(*(pst + 1)))) return; // none there
pst = strchr(s,']'); // find terminator of code block
if (strchr(pst,'[')) return; // we've got more than one, so okay
strcat(s," [---]"); // add a null code
} // addNullString
void CodeStoreClass::parseActorString (char * sparse, char *s)
// parse the actor string in s; return in sparse
// parser does the following:
// 1. converts ][ to | and removes initial [ and final ]
// 2. converts <date>-<date> to ~date~date (so it won't be confused with --- null code)
// this is a bit messy but the notation is sufficiently intuitive that there is no point in changing it
// 3. blank-terminates string
//
// Throws: EMPTY_CODE, MISS_RBRAC
{
instring sa;
char * pst;
char * pivot;
if (!strchr(s,'[')) { // handle a simple code entered from the editor
TrimBlanks(s);
strcpy(sparse,s);
strcat(sparse," ");
return;
}
strcpy(sparse,"");
addNullString(s); // check whether we need to add a null
// dated string
pst = strchr(s,'[');
while (pst) { // go through the [code <time>] units
pst++; // move past the [
if (strchr(pst,']')) {
Copy2Chr(sa, pst,']'); // copy the string inside [...]
TrimBlanks(sa);
if('\0' == sa[0]) throw EMPTY_CODE;
}
else throw MISS_RBRAC;
pivot = strchr(sa,'-'); // search for a <date>-<date> structure
if (pivot && (*(pivot+1) == '-') && (*(pivot+2) == '-')) { // context is null code, so skip it
pivot += 3;
pivot = strchr(pivot,'-');
}
if (pivot) {
*pivot = '~'; // replace '-' with '~'
--pivot;
while (*pivot == ' ') --pivot; // allow blanks
while (isdigit(*pivot)) --pivot; // back down past the date
if ((pivot >= sa) && (*pivot == ' ')) *pivot = '~';
else {
throw EMPTY_CODE; // not always, but must likely cause of incorrect formating here
}
}
strcat(sparse,sa);
pst = strchr(pst,'[');
if (pst) strcat(sparse, "|"); // separate units with "|"
else strcat(sparse, " "); // blank-terminate string
}
} // parseActorString
void CodeStoreClass::getActorCode (char *s, tokptr ptok)
// Gets the string version of the undated actor code sequence pointed to by ptok
{
toktype token;
token = *ptok;
if (!(token & maskActrFlag)) { // simple code
strcpy(s,getCodePtr(token));
return;
}
strcpy(s,"");
do { // loop through the compound
strcat(s,getCodePtr(token & maskActrCode));
if (token & maskActrPair) { // get the agent
strcat(s,":");
token = *(++ptok); // get next token
strcat(s,getCodePtr(token & maskActrCode));
}
if (token & maskContinue) { // addition compound element
strcat(s,"/");
token = *(++ptok); // get next token
}
else token = 0; // we're done
} while (token);
} // getActorCode
char * CodeStoreClass::getAgentCodePtr (toktype tokindex)
// Gets the pointer to string of agent code at index tokloc
{
return getCodePtr(TokenStore.getTokenValue(tokindex));
} // getAgentCodePtr
void CodeStoreClass::getActorString (char *s, toktype itok)
// Gets the string version of the optionally dated actor code sequence pointed to by itok
{
instring sa;
toktype token;
tokptr ptok = TokenStore.getTokPtr(itok);
strcpy(s,"[");
token = *ptok;
if (!(token & maskActrFlag)) { // simple code
strcat(s,getCodePtr(token));
strcat(s,"]");
return;
}
if (!(token & maskDateHead)) { // undated compound code
getActorCode(sa, ptok);
strcat(s,sa);
strcat(s,"]");
return;
}
// we've got a dated sequence
do {
if (!((token & maskConstrnt) ^ maskINCntsrt)) { // - date constraint
getActorCode(sa, (ptok+3));
strcat(s,sa);
strcat(s," ");
JulianString(sa,*(ptok+1));
strcat(s,sa);
strcat(s,"-");
JulianString(sa,*(ptok+2));
}
else if (token & maskLTCntsrt) { // < date constraint
getActorCode(sa, (ptok+2));
strcat(s,sa);
strcat(s," <");
JulianString(sa,*(ptok+1));
}
else if (token & maskGTCntsrt) { // > date constraint
getActorCode(sa, (ptok+2));
strcat(s,sa);
strcat(s," >");
JulianString(sa, *(ptok+1));
}
else getActorCode(sa, (ptok+1));// default constraint
strcat(s,sa);
strcat(s,"]");
if (token & maskContinue) { // additional date element
strcat(s," [");
ptok += (token & maskDateSkip);
token = *ptok; // skip to the next date header
}
else token = 0; // we're done
} while (token);
} // getActorString
void CodeStoreClass::putDate(char *s)
// transfers a date beginning at s into TokenBuf
{
//litstring sdate; // date segment of s; allows quite a bit of slack for a string that is too long
//char *pdate;
toktype tjulian; // value of date
// Processor.fprob << s << endl;
Processor.validateDate(s); // this can throw errors that will be caught in ReadActors
// pdate = sdate; // ### <09.11.18> These 3 lines were some earlier error checking, after a fashion...not sure we need it now...
// while (isdigit(*s)) *pdate++ = *s++;
// *pdate = '\0'; // terminate string
// tjulian = MakeJulian(sdate);
tjulian = MakeJulian(s);
TokenStore.putToken(tjulian);
} // putDate
void CodeStoreClass::setDateSkips(toktype datehead)
// goes through a sequence of date headers and sets the skip fields
{
tokptr dateptr = TokenStore.getTokPtr(datehead); // next date header
tokptr nextptr = dateptr; // next date header
while (*dateptr & maskContinue) { // loop until continuation bit off
if (!((*nextptr & maskConstrnt) ^ maskINCntsrt)) nextptr += 2; // skip other - date
else if ((*nextptr & maskLTCntsrt) || (*nextptr & maskGTCntsrt)) ++nextptr; // skip over <, > date
++nextptr;
while (!(*nextptr & maskDateHead)) ++nextptr; // skip over the codes
*dateptr |= (nextptr - dateptr); // set the skip field
dateptr = nextptr;
}
} // setDateSkips
toktype CodeStoreClass::storeActorCode (char * s)
// parses and stores actor codes
{
char *pst = s;
char *pair;
instring sa;
toktype itok = TokenStore.iToken;
while (pst) {
pst = strchr(s,'/'); // get the next segment of a compound list
if (pst) {
Copy2Chr(sa,s,'/'); // ### could substitute code here; we already know pst
s = ++pst;
}
else strcpy(sa,s); // no more compound segments
pair = strchr(sa,':');
if (pair) { // we've got a code pair
*pair = '\0'; // split the code into two strings
storeCode(sa,maskActrPair);
if (pst) storeCode(++pair, maskContinue);
else storeCode(++pair, maskActrStop);
}
else {
if (pst) storeCode(sa, maskContinue);
else storeCode(sa, maskActrStop);
}
}
return itok;
} // storeActorCode
toktype CodeStoreClass::storeAgentCode (char * s)
// stores agent codes
{
char *pst = s;
instring sa;
toktype itok = TokenStore.iToken;
pst = strchr(s,'[')+1;
if (strchr(s,']')) {
Copy2Chr(sa, pst,']'); // copy the string inside [...]
TrimBlanks(sa); // ### not needed, right? storeCode does this...
if('\0' == sa[0]) throw EMPTY_CODE;
}
itok = storeCode(sa, maskActrStop);
return itok;
} // storeAgentCode
toktype CodeStoreClass::storeDatedString (char * s)
// creates a date header, then stores codes. Returns index of header
{
toktype idate; // index of date header
toktype tokhead = maskDateInit; // header record
instring scode; // code segment of s
char *pst;
// extract dates from string
if ((strchr(s,'<')) || (strchr(s,'>'))) { // deal with <... and >... constraints
if (strchr(s,'<')) {
tokhead |= maskLTCntsrt; // set constraint bit
pst = strchr(s,'<');
Copy2Chr(scode,s,'<');
}
else {
tokhead |= maskGTCntsrt;
pst = strchr(s,'>');
Copy2Chr(scode,s,'>');
}
if (!*scode) throw EMPTY_CODE; //no code is present
idate = TokenStore.putToken(tokhead);
while(*pst && !isdigit(*pst)) ++pst; // move past < or > to first digit of date
putDate(pst);
storeActorCode(scode);
}
else if (strchr(s,'~')) { // deal with - constraint, which is now ~...~...
tokhead |= maskINCntsrt; // set constraint bit
idate = TokenStore.putToken(tokhead);
Copy2Chr(scode,s,'~');
if (!*scode) throw EMPTY_CODE; //no code is present
pst = strchr(s,'~');
while(*pst && !isdigit(*pst)) ++pst; // move past first ~
putDate(pst);
pst = strchr(pst,'~'); // go to the second ~
if (!pst) throw EMPTY_CODE; // [apprently?] can occur if no code is present in [dddddd-dddddd]
while(*pst && !isdigit(*pst)) ++pst; // move past second ~
putDate(pst);
storeActorCode(scode);
}
else {
idate = TokenStore.putToken(tokhead);
storeActorCode(s); // default code
}
return idate;
} // storeDatedString
toktype CodeStoreClass::storeActorString (char *st)
// Processes the s code string; returns index of the header
{
toktype itok = TokenStore.iToken;
toktype ia;
tokptr pa;
char *pst;
instring s,sitem; // stores intermediate strings
parseActorString(s,st);
// ### if (!pst) then we've got an error
if (strchr(s,'|')) { // parse date restricted codes
pst = s;
while (pst) {
Copy2Chr(sitem,pst,'|');
ia = storeDatedString(sitem);
pst = strchr(pst,'|'); // go to start of next code
if (pst) pst++; // skip past |
}
pa = TokenStore.getTokPtr(ia);
*pa &= maskContnOff; // set the continuation bit of last header to zero
setDateSkips(itok); // go back and set the skip fields
}
else storeActorCode(s);
return itok;
} // storeActorString
void CodeStoreClass:: nextActorCode (toktype &idxactor, toktype &idxagent, tokptr *pnext, toktype istart)
// Gets the next actor and agent codes from the optionally dated actor code sequence pointed
// to by *pnext. If *pnext == NULL, then the date restrictions are resolved, and the
// first code-pair is returned. Subsequent calls work through any coded-compound.
// Returns false when there are no additional codes.
{
toktype token;
tokptr ptok;
bool found = false;
if (!(*pnext)) { //initialize sequence
ptok = TokenStore.getTokPtr(istart);
token = *ptok;
if (!(token & maskActrFlag)) { // simple code
idxactor = token;
idxagent = 0;
*pnext = NULL;
return;
}
if (token & maskDateHead) { // we've got a dated sequence
while (!found) {
if (!((token & maskConstrnt) ^ maskINCntsrt)) { // - date constraint
if ((Processor.julianDate >= *(ptok+1)) && (Processor.julianDate <= *(ptok+2))) {
*pnext = ptok + 3;
found = true;
}
}
else if (token & maskLTCntsrt) { // < date constraint
if (Processor.julianDate < *(ptok+1)) {
*pnext = ptok + 2;
found = true;
}
}
else if (token & maskGTCntsrt) { // > date constraint
if (Processor.julianDate > *(ptok+1)) {
*pnext = ptok + 2;
found = true;
}
}
else { // default constraint
*pnext = ptok + 1;
found = true;
}
if (!found) {
if (token & maskContinue) { // there are additional date elements
ptok += (token & maskDateSkip);
token = *ptok; // skip to the next date header
}
else { // no valid codes for this date
idxactor = 0;
idxagent = 0;
*pnext = NULL;
return;
}
}
} // while
} // if maskDateHead
else *pnext = ptok; // undated compound code
} // if !*pnext
// ptok now points to the first codes
token = **pnext;
idxactor = (token & maskActrCode); // get actor code
if (token & maskActrPair) { // get the agent
token = *(++(*pnext)); // get next token
idxagent = (token & maskActrCode);
}
else idxagent = 0;
if (token & maskContinue) { // addition compound element
++(*pnext); // return pointer to next token
return;
}
else {
*pnext = NULL;
return;
}
} // nextActorCode
//___________________________________________________________________________________
// Time functions
toktype CodeStoreClass::storeTimeCode (char * s)
// Stores time codes. Returns the location where the code is stored
{
int ival = 0;
toktype ka = 0;
toktype itok = TokenStore.iToken; // save this so it can be returned
if (strstr(s,sNullCode)) ka = indexNull; // check for the standard codes
else if (strstr(s,sDiscardCode)) ka = indexDiscard;
else if (strstr(s,sComplexCode)) ka = indexComplex;
if (ka) ka |= maskTimeCode; // store a standard code index
else {
ival = atoi(&s[1]); // copy integer following [
if (ival>0) ka = ival;
else { // store decrement
ival = -ival;
ka = ival;
ka |= maskTimeDecr; // set decrement bit
}
}
if (ival > MAX_TIME_VALUE) { // do bounds checking
ShowWarningError("Time increment/decrement is too large", s,"Value has been set to zero",shError52);
ka = 0;
}
TokenStore.putToken(ka);
return itok;
} // storeTimeCode
int CodeStoreClass::getTimeCode (toktype itok, bool &isIndex)
// Get time code. If isIndex is true, value is a code index
{
toktype ka = TokenStore.getTokenValue (itok);
int ival = ka & maskTimeClr;
isIndex = (bool)(ka & maskTimeCode);
if (ka & maskTimeDecr) ival = -ival;
return ival;
} // getTimeCode
void CodeStoreClass::getTimeString (char *s, toktype itok)
// Get the string version of the time code pointed to by itok
{
char scr[8]; // holds value
toktype ka = TokenStore.getTokenValue (itok);
toktype ival = ka & maskTimeClr;
s[0] = '[';
if (ka & maskTimeCode) { // code
strcpy(&s[1],getCodePtr(ival));
strcat(s,"]");
return;
}
if (ka & maskTimeDecr) s[1] = '-';
else s[1] = '+';
sprintf(scr,"%d",ival);
strcpy(&s[2],scr);
strcat(s,"]");
} // getTimeString
//___________________________________________________________________________________
// Event functions
void CodeStoreClass:: appendEventActor(char * sparse, char *s, const char *sc)
// handles an actor $(...) substring, appends to sparse. c is the initial marker
{
instring sa, sb;
strcat(sparse,sc);
Copy2Chr(sa,(s+2),')'); // ### error check here?
parseActorString(sb,sa);
strcat(sparse,sb);
strcat(sparse,") ");
} // appendEventActor
void CodeStoreClass::parseEventString (char * sparse, char *s)
// parse the event string in s; return in sparse
// parser does the following:
// 1. converts $(...) to $...)
// 2. converts +(...) to &...)
// 3. converts @(...) to @...)
// 4. converts actor strings using parseActorString
// 5. converts issue strings using parseIssueString
// 6. gets rid of outermost []
//
// Throws MISS_RBRAC
//
// Note 1. ### once this is debugged, switch the parse delimiters to \01,\02, etc.
// Note 2. The strpbrk(s,"/@&$") is a partially-implemented facility that would allow
// an event string to also designate the source, target, attribute (STA) and
// issues. However, these have not been integrated into the coder as of
// 2002.01 since we aren't using them.
{
instring sa, sb;
char * pst;
pst = strstr(s,"+("); // change + to & to avoid problems with +++
if (pst) *pst = '&';
pst = strstr(s,"["); // move past [ if it exists;
if (pst) s = (pst+1);
pst = s;
while (*pst) pst++; // go to the end of the string
while ((pst > s) && (*pst != ']')) {
--pst; // search backwards for last ']'
}
if (']' == *pst) *pst = '\0'; // so terminate it here
else throw MISS_RBRAC;
// Processor.fprob << "CSC:pES Mk1 \"" << s << "\"" << endl; // *** debug
pst = strpbrk(s,"/@&$");
if (!pst) {
strcpy(sparse,s); // nothing more to process
return;
}
else Copy2Chr(sparse,s,*pst);
while ((pst) && ('/' == *pst)) { // process the issue strings
s = pst + 1;
if (!(*s)) break;
pst = strpbrk(s,"/ ");
if (pst) Copy2Chr(sa,s,*pst);
else strcpy(sa,s);
parseIssueString(sb,sa);
strcat(sparse,"/");
strcat(sparse,sb);
}
// process the STA string
pst = strstr(s,"$(");
if (pst) appendEventActor (sparse, pst, "$");
pst = strstr(s,"&(");
if (pst) appendEventActor (sparse, pst, "&");
pst = strstr(s,"@(");
if (pst) appendEventActor (sparse, pst, "@");
} // parseEventString
void CodeStoreClass::getEventString (char *s, toktype itok)
// Gets the string version of the event code sequence pointed to by itok }
{
instring scode;
toktype token;
tokptr ptok = TokenStore.getTokPtr(itok);
strcpy(s,"[");
token = *ptok;
if (!(token & maskEvntFlag)) { // simple code
strcat(s,getCodePtr(token));
strcat(s,"]");
return;
}
// get initial code
strcat(s,getCodePtr(token & maskEvntCode));
if (token & maskEvtSubor) strcat(s,"?");
else if (token & maskEvtDomin) strcat(s,"!");
if (((token & maskSTANext) ^ maskSTANext) && (token & maskPairNext)) { // process paired code
strcat(s,":");
token = *(++ptok); // get next token
strcat(s,getCodePtr(token & maskEvntCode));
if (token & maskEvtSubor) strcat(s,"?");
else if (token & maskEvtDomin) strcat(s,"!");
}
while (((token & maskSTANext) ^ maskSTANext) && (token & maskIssuNext)) { // process issues code
strcat(s,"/");
token = *(++ptok); // get next token
getIssueString(scode,token & maskEvntCode);
strcat(s,scode);
}
while (token & maskContinue) { // process STA cases
token = *(++ptok); // get next token
getActorString(scode,token & maskEvntCode);
if (token & maskEvntTar) strcat(s," +("); // target code
else if (token & maskEvntAtt) strcat(s," @("); // attribution code
else strcat(s," $("); // source
strcat(s,scode);
strcat(s,")");
}
strcat(s,"]");
} // getEventString
void CodeStoreClass:: decodeEventCode (toktype &evtcode, bool &fissub, bool &fisdom, toktype icode)
// Gets the code and status out of icode
{
evtcode = (icode & maskEvntCode);
fissub = (bool) (icode & maskEvtSubor);
fisdom = (bool) (icode & maskEvtDomin);
}