-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdelta.pas
1534 lines (1406 loc) · 45.7 KB
/
delta.pas
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
{========================================================================}
{ D E L T A Library }
{ }
{ A General-Purpose Library of Routines for Reading and Writing }
{ Text Files in DELTA (DEscription Language for TAxonomy) Format }
{ }
{ Version 1.0, November 1994 }
{ Version 2.1, August 1996, Updated May 1998 }
{ Version 3.3, June 2016, Updated July 2020, June 2021, December 2023 }
{ }
{ Author: Mauro J. Cavalcanti, Rio de Janeiro, BRASIL }
{ E-mail: <maurobio@gmail.com> }
{ }
{ This program is free software; you can redistribute it and/or modify }
{ it under the terms of the GNU Lesser General Public License as }
{ published by the Free Software Foundation; either version 3 of the }
{ License, or (at your option) any later version. }
{ }
{ This program is distributed in the hope that it will be useful, }
{ but WITHOUT ANY WARRANTY; without even the implied warranty of }
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the }
{ GNU General Public License for more details. }
{========================================================================}
unit Delta;
{$MODE DELPHI}
interface
uses
Classes, SysUtils, StrUtils, RegExpr;
const
{ Return value constants }
dt_OK = 0;
dt_CharsFileNotFound = -1;
dt_InvalidCharsFile = -2;
dt_CharListEmpty = -3;
dt_ItemsFileNotFound = -4;
dt_InvalidItemsFile = -5;
dt_ItemListEmpty = -6;
dt_SpecsFileNotFound = -7;
dt_DirectiveNotFound = -8;
dt_NotesFileNotFound = -9;
dt_InvalidNotesFile = -10;
{ CONFOR directives }
showDirective = '*SHOW';
itemDirective = '*ITEM DESCRIPTIONS';
charDirective = '*CHARACTER LIST';
typeDirective = '*CHARACTER TYPES';
noteDirective = '*CHARACTER NOTES';
dependentDirective = '*DEPENDENT CHARACTERS';
inapplicableDirective = '*INAPPLICABLE CHARACTERS';
weightDirective = '*CHARACTER RELIABILITIES';
mandatoryDirective = '*MANDATORY CHARACTERS';
implicitDirective = '*IMPLICIT VALUES';
keyDirective = '*KEY STATES';
cimgDirective = '*CHARACTER IMAGES';
timgDirective = '*TAXON IMAGES';
numCharsDirective = '*NUMBER OF CHARACTERS';
maxNumStatesDirective = '*MAXIMUM NUMBER OF STATES';
numStatesDirective = '*NUMBERS OF STATES';
maxNumItemsDirective = '*MAXIMUM NUMBER OF ITEMS';
dataBufferSizeDirective = '*DATA BUFFER SIZE';
subHeadingsDirective = '*ITEM SUBHEADINGS';
{ PANKEY directives }
headingDirective = '*HEADING';
altCharDirective = '*CHARACTER DESCRIPTIONS';
altWeightDirective = '*CHARACTER WEIGHTS';
endDirective = '*END';
type
{ Type definitions }
{ Stores an item }
TItem = record
itemName: string;
itemComment: string;
itemAttributes: TStringList;
end;
{ A list of items }
TItemList = array of TItem;
{ Stores a character }
TCharacter = record
charName: string;
charType: string;
charUnit: string;
charWeight: byte;
charMandatory: boolean;
charImplicit: integer;
charDependent: TStringList;
charNote: string;
charStates: TStringList;
end;
{ A list of characters }
TCharacterList = array of TCharacter;
TDelta = class(TObject)
public
Heading: string;
DataBufferSize: integer;
ItemList: TItemList;
CharacterList: TCharacterList;
constructor Create;
destructor Destroy; override;
procedure ReadTitle(anyFile: string);
function ReadItems(itemsFile: string): integer;
function ReadChars(charsFile: string): integer;
function ReadSpecs(specsFile: string; const targetDirective: string): integer;
function ReadNotes(notesFile: string): integer;
function WriteChars(charsFile: string): integer;
function WriteItems(itemsFile: string): integer;
function WriteSpecs(specsFile: string): integer;
function WriteNotes(notesFile: string): integer;
end;
{ High-level calls }
function ReadDelta(const charsFile, itemsFile, specsFile: string;
cnotesFile: string = ''): TDelta;
procedure WriteDelta(Dataset: TDelta; const charsFile, itemsFile, specsFile: string;
cnotesFile: string = '');
{ General-purpose routines }
function GetSubHeadings(const specsFile: string): string;
function GetDataBufferSize(const specsFile: string): integer;
function ReadDirective(const specsFile, targetDirective: string;
getValue: boolean = False): string;
function RemoveComments(const S: string): string;
function OmitTypesettingMarks(const S: string): string;
function OmitInnerComments(const S: string): string;
function CheckBrackets(const str: string): boolean;
function ExpandRange(const Range: string): TStringList;
function CompressRange(const Numbers: string): string;
{ Helper routines }
function Frequency(const C: char; const S: string): integer;
function LastPos(Ch: char; St: string): integer;
function StripChars(const Text: string; InValidChars: TSysCharSet): string;
function Iif(BoolVar: boolean; IfTrue, IfFalse: string): string;
function FirstChar(inString: string): char;
function ExtractText(const Str: string; const Delim1, Delim2: char): string;
implementation
{ Helper routines }
function Frequency(const C: char; const S: string): integer;
var
i: integer;
begin
Result := 0;
for i := 1 to Length(S) do
if S[i] = C then
Inc(Result);
end;
function LastPos(Ch: char; St: string): integer;
var
i: integer;
begin
i := Length(St);
while (i > 0) and (St[i] <> Ch) do
Dec(i);
Result := i;
end;
function StripChars(const Text: string; InValidChars: TSysCharSet): string;
var
S: string;
i: integer;
begin
Result := '';
if (Length(Text) > 0) then
begin
S := '';
for i := 1 to Length(Text) do
if not CharInSet(Text[i], InValidChars) then
S := S + Text[i];
Result := S;
end;
end;
function Iif(BoolVar: boolean; IfTrue, IfFalse: string): string;
begin
if BoolVar then
Result := IfTrue
else
Result := IfFalse;
end;
function FirstChar(inString: string): char;
begin
Result := inString[1];
end;
function ExtractText(const Str: string; const Delim1, Delim2: char): string;
var
pos1, pos2: integer;
begin
Result := '';
pos1 := Pos(Delim1, Str);
pos2 := Pos(Delim2, Str);
if (pos1 > 0) and (pos2 > pos1) then
Result := Copy(Str, pos1 + 1, pos2 - pos1 - 1);
end;
{ TDelta }
constructor TDelta.Create;
begin
Heading := '';
DataBufferSize := 0;
SetLength(ItemList, 0);
SetLength(CharacterList, 0);
end;
destructor TDelta.Destroy;
var
RecNo: integer;
begin
for RecNo := 0 to Length(ItemList) - 1 do
begin
if ItemList[RecNo].itemAttributes <> nil then
ItemList[RecNo].itemAttributes.Free;
end;
for RecNo := 0 to Length(CharacterList) - 1 do
begin
if CharacterList[RecNo].charStates <> nil then
CharacterList[RecNo].charStates.Free;
if CharacterList[RecNo].charDependent <> nil then
CharacterList[RecNo].charDependent.Free;
end;
inherited Destroy;
end;
{==========================================================================}
{ Reads any DELTA file and extracts the database name }
{==========================================================================}
procedure TDelta.ReadTitle(anyFile: string);
var
Infile: TextFile;
Line: string;
title: string[80];
begin
title := '';
{ Open the items file }
if FileExists(anyFile) then
begin
AssignFile(Infile, anyFile);
Reset(Infile);
end
else
exit;
while not EOF(Infile) do
begin
ReadLn(Infile, Line);
{ Makes sure that the '*SHOW' directive is present in the file }
{ If not found, look for the '*HEADING' or '(HEADING)' directives }
if (Pos(showDirective, Line) > 0) then
title := Copy(Line, Pos(showDirective, Line) + Length(showDirective), 80)
else if (Pos(headingDirective, Line) > 0) then
title := Copy(Line, Pos(headingDirective, Line) + Length(headingDirective), 80);
if (Pos('.', title) > 0) then
title := Copy(title, 1, Pos('.', title) - 1);
if (Pos('/', title) > 0) then
title := Copy(title, 1, Pos('/', title) - 1);
if (Pos('-', title) > 0) then
title := Copy(title, 1, Pos('-', title) - 1);
if (title[1] = ':') then
title := Copy(title, 2, 80);
if (Pos(':', title) > 0) then
title := Copy(title, 1, Pos(':', title) - 1);
title := Trim(title);
break;
end;
CloseFile(Infile);
{ Return the title name }
Heading := title;
end; { ReadTitle }
{==========================================================================}
{ Reads the ITEMS file and extracts the item names }
{==========================================================================}
function TDelta.ReadItems(itemsFile: string): integer;
var
Infile: TextFile;
Line, Name, junk, Value, token: string;
itemName, itemComment, itemAttribute: string;
i, number, itemCount, NChars: integer;
itemsFound, isComment: boolean;
ch, aux: char;
procedure ExpandDataWhenNeeded(RecNo: integer);
begin
if (Length(ItemList) >= RecNo) then
Exit;
SetLength(ItemList, RecNo);
end;
begin
itemCount := 0;
itemsFound := False;
isComment := False;
token := '';
junk := '';
Value := '';
NChars := Length(CharacterList);
if (NChars = 0) then
begin
ReadItems := dt_CharListEmpty; { List empty }
Exit;
end;
{ Open the items file }
if FileExists(itemsFile) then
begin
AssignFile(Infile, itemsFile);
Reset(Infile);
end
else
begin
ReadItems := dt_ItemsFileNotFound; { File not found }
Exit;
end;
while (not itemsFound) and not EOF(Infile) do
begin
ReadLn(Infile, Line);
{ Makes sure that the '*ITEM DESCRIPTIONS' directive
is present in the file }
if (Pos(itemDirective, Line) > 0) then
itemsFound := True;
{ Will run along the ITEMS file until it sees a '#'
then parse and read in the item descriptions }
if (itemsFound) then
begin
while not EOF(Infile) do
begin
Read(Infile, ch);
{ Read the item name }
if (ch = '#') and (not isComment) then
begin
Inc(itemCount);
Read(Infile, ch);
ReadLn(Infile, Name);
RemoveTrailingChars(Name, ['/']);
if (Pos('<', Name) > 0) then
begin
itemName := Trim(Copy(Name, Pos('#', Name) + 1, Pos('<', Name) - 2));
itemComment := Trim(Copy(Name, Pos('<', Name), Pos('>', Name)));
end
else
begin
itemName := Trim(Copy(Name, Pos('#', Name) + 1, Length(Name)));
itemComment := '';
end;
{ Store item }
ExpandDataWhenNeeded(itemCount);
ItemList[itemCount - 1].itemName := itemName;
ItemList[itemCount - 1].itemComment := itemComment;
ItemList[itemCount - 1].itemAttributes := TStringList.Create;
for i := 0 to NChars - 1 do
ItemList[itemCount - 1].itemAttributes.Add('U');
end;
{ Read item attributes }
case ch of
'<': isComment := True;
'>': isComment := False;
end;
if ((ch = ' ') or (ch = #10) or (ch = #13)) and (not isComment) then
begin
token := StringReplace(token, #10, #32, [rfReplaceAll]);
token := StringReplace(token, #13, #32, [rfReplaceAll]);
token := Trim(token);
token := DelSpace1(token);
if (Frequency(',', token) = 1) then
begin
aux := token[Pos(',', token) + 1];
if (aux in ['0'..'9']) then
begin
junk := ExtractDelimited(1, token, [',']);
Value := Copy(token, Pos(',', token) + 1, Length(token));
end
else
begin
junk := ExtractDelimited(1, token, ['<']);
Value := Copy(token, Pos('<', token), Length(token));
end;
end
else
begin
junk := ExtractDelimited(1, token, ['<']);
Value := Copy(token, Pos('<', token), Length(token));
end;
if (Frequency(',', junk) > 0) then
begin
junk := ExtractDelimited(1, token, [',']);
Value := Copy(token, Pos(',', token) + 1, Length(token));
end;
if (Frequency('<', junk) > 0) then
begin
Value := Concat(Copy(junk, Pos('<', junk), Length(token)), Value);
junk := ExtractDelimited(1, junk, ['<']);
end;
{ Store attribute }
number := StrToIntDef(junk, 0);
if (number > 0) then
begin
if (Pos('<<', Value) > 0) then
//Value := ExtractDelimited(1, Value, ['<']);
Value := OmitInnerComments(Value);
itemAttribute := Value;
ItemList[itemCount - 1].itemAttributes.Insert(number -
1, itemAttribute);
end;
token := '';
end;
token := Concat(token, ch);
end;
end;
end;
CloseFile(Infile);
for i := 0 to Length(ItemList) - 1 do
ItemList[i].itemAttributes.Capacity := NChars;
{ Return the number of items in the file, otherwise an error code }
if itemsFound then
ReadItems := itemCount
else
ReadItems := dt_InvalidItemsFile; { Invalid file }
end; { ReadItems }
{==========================================================================}
{ Reads the CHARS file and extracts the character names }
{==========================================================================}
function TDelta.ReadChars(charsFile: string): integer;
var
infile: TextFile;
Line, charName, stateName: string;
charCount, RecNo: integer;
charsFound, isComment, stop: boolean;
ch: char;
procedure ExpandDataWhenNeeded(RecNo: integer);
begin
if (Length(CharacterList) >= RecNo) then
Exit;
SetLength(CharacterList, RecNo);
end;
procedure GetCharAndStates;
begin
stateName := '';
isComment := False;
stop := False;
while not EoLn(infile) do
begin
repeat
{ Read the character names }
if (ch = '#') then
begin
Inc(charCount);
charName := '';
repeat
if EoLn(infile) then
ReadLn(infile);
Read(infile, ch);
case ch of
'<': isComment := True;
'>': isComment := False;
end;
if (ch <> #13) and (ch <> #10) then
charName := Concat(charName, ch);
until (((ch = '/') and not isComment) or EOF(infile));
RemoveTrailingChars(charName, ['/']);
charName := Copy(charName, Pos('.', charName) + 1, Length(charName));
charName := DelSpace1(charName);
charName := Trim(charName);
{ Store character }
ExpandDataWhenNeeded(charCount);
CharacterList[charCount - 1].charName := charName;
CharacterList[charCount - 1].charType := 'UM';
CharacterList[charCount - 1].charUnit := '';
CharacterList[charCount - 1].charNote := '';
CharacterList[charCount - 1].charWeight := 1;
CharacterList[charCount - 1].charMandatory := False;
CharacterList[charCount - 1].charImplicit := 0;
CharacterList[charCount - 1].charDependent := TStringList.Create;
CharacterList[charCount - 1].charStates := TStringList.Create;
end;
{ Read the state names }
Read(infile, ch);
case ch of
'<': isComment := True;
'>': isComment := False;
end;
if (ch = '/') and EoLn(infile) then
stop := True;
if (ch <> '#') and (ch <> #13) and (ch <> #10) then
stateName := Concat(stateName, ch);
until ((stop and not isComment) or EOF(infile));
//until (((ch = '/') and not isComment) or EOF(infile));
//until (EoLn(infile) or EOF(infile));
if Length(stateName) > 0 then
begin
RemoveTrailingChars(stateName, ['/']);
stateName := Copy(stateName, Pos('.', stateName) + 1, Length(stateName));
stateName := DelSpace1(stateName);
stateName := Trim(stateName);
{ Stores states }
CharacterList[charCount - 1].charStates.Add(stateName);
end;
end;
end;
begin
charName := '';
charCount := 0;
charsFound := False;
{ Clear previous data }
for RecNo := 0 to Length(CharacterList) - 1 do
CharacterList[RecNo].charStates.Free;
SetLength(CharacterList, 0);
{ Open the characters file }
if FileExists(charsFile) then
begin
AssignFile(infile, charsFile);
Reset(infile);
end
else
begin
ReadChars := dt_CharsFileNotFound; { File not found }
exit;
end;
while (not charsFound) and not EOF(infile) do
begin
ReadLn(infile, Line);
{ Makes sure that the '*CHARACTER LIST' directive is present
in the file }
if (Pos(charDirective, Line) > 0) or (Pos(altCharDirective, Line) > 0) then
charsFound := True;
{ Will run along the characters file until it sees a '#'
then read in the state names }
if (charsFound) then
begin
while not EOF(infile) do
begin
while not EoLn(infile) do
begin
Read(infile, ch);
GetCharAndStates;
ReadLn(infile);
end;
ReadLn(infile);
end;
end;
end;
CloseFile(infile);
{ Return the number of characters in the file, otherwise an error code }
if charsFound then
ReadChars := charCount
else
ReadChars := dt_InvalidCharsFile; { Invalid file }
end; { ReadChars }
{==========================================================================}
{ Reads the SPECS file and extracts the character types, or weights }
{==========================================================================}
function TDelta.ReadSpecs(specsFile: string; const targetDirective: string): integer;
var
Infile: TextFile;
directiveFound: boolean;
firstChars, nextChars, Buffer: string;
Character: TCharacter;
procedure ParseChars(Buffer: string; const targetDirective: string);
{ Parse and read in the character types, weights, or implicit values }
var
i, j, nchar, First, last: integer;
aux, ctype, cdepend, charNum: string;
cweight, cimpval: byte;
begin
if (Length(Buffer) > 0) then
begin
Buffer := Trim(Buffer);
for i := 1 to WordCount(Buffer, StdWordDelims) do
begin
aux := ExtractWord(i, Buffer, [' ']);
if (targetDirective = mandatoryDirective) then
charNum := aux
else
charNum := Copy(aux, 1, Pos(',', aux) - 1);
if (Pos('-', charNum) > 0) then
begin
First := StrToInt(Copy(charNum, 1, Pos('-', charNum) - 1));
last := StrToInt(Copy(charNum, Pos('-', charNum) + 1, Length(charNum)));
for j := First to last do
begin
nchar := j;
Character := CharacterList[nchar - 1];
if (targetDirective = typeDirective) then
begin
ctype := Copy(aux, Pos(',', aux) + 1, 2);
Character.charType := ctype;
if (ctype = 'IN') or (ctype = 'RN') then
Character.charUnit :=
StringReplace(Character.charStates.Text, #13#10, ' ', [rfReplaceAll]);
end
else if (targetDirective = weightDirective) or
(targetDirective = altWeightDirective) then
begin
cweight := StrToInt(Copy(aux, Pos(',', aux) + 1, 2));
Character.charWeight := cweight;
end
else if (targetDirective = dependentDirective) or
(targetDirective = inapplicableDirective) then
begin
cdepend := ExtractDelimited(2, aux, [',']);
Character.charDependent.Add(cdepend);
end
else if (targetDirective = implicitDirective) then
begin
cimpval := StrToInt(Copy(aux, Pos(',', aux) + 1, 2));
Character.charImplicit := cimpval;
end
else if (targetDirective = mandatoryDirective) then
begin
Character.charMandatory := True;
end;
CharacterList[nchar - 1] := Character;
end;
end
else
begin
if (targetDirective = mandatoryDirective) then
nchar := StrToIntDef(aux, 0)
else
nchar := StrToIntDef(Copy(aux, 1, Pos(',', aux) - 1), 0);
if (nchar > 0) then
begin
Character := CharacterList[nchar - 1];
if (targetDirective = typeDirective) then
begin
ctype := Copy(aux, Pos(',', aux) + 1, 2);
Character.charType := ctype;
if (ctype = 'IN') or (ctype = 'RN') then
Character.charUnit :=
StringReplace(Character.charStates.Text, #13#10, ' ', [rfReplaceAll]);
end
else if (targetDirective = weightDirective) or
(targetDirective = altWeightDirective) then
begin
cweight := StrToInt(Copy(aux, Pos(',', aux) + 1, 2));
Character.charWeight := cweight;
end
else if (targetDirective = dependentDirective) or
(targetDirective = inapplicableDirective) then
begin
cdepend := ExtractDelimited(2, aux, [',']);
Character.charDependent.Add(cdepend);
end
else if (targetDirective = implicitDirective) then
begin
cimpval := StrToInt(Copy(aux, Pos(',', aux) + 1, 2));
Character.charImplicit := cimpval;
end
else if (targetDirective = mandatoryDirective) then
begin
Character.charMandatory := True;
end;
CharacterList[nchar - 1] := Character;
end;
end;
end;
end;
end;
begin
if (Length(CharacterList) = 0) then
begin
ReadSpecs := dt_CharListEmpty; { List empty }
Exit;
end;
{ Open the specifications file }
if FileExists(specsFile) then
begin
AssignFile(Infile, specsFile);
Reset(Infile);
end
else
begin
ReadSpecs := dt_SpecsFileNotFound; { File not found }
Exit;
end;
{ Make sure that the target directive is present in the file }
directiveFound := False;
repeat
ReadLn(Infile, Buffer);
if (Pos(targetDirective, Buffer) > 0) then
directiveFound := True
until (directiveFound) or EOF(Infile);
{ Read in the character numbers }
if EOF(Infile) and (not directiveFound) then
begin
ReadSpecs := dt_DirectiveNotFound; { Invalid file }
CloseFile(Infile);
Exit;
end
else
begin
firstChars := Copy(Trim(Buffer), Length(targetDirective) + 1,
Length(Trim(Buffer)) - Length(targetDirective));
ParseChars(firstChars, targetDirective);
repeat
ReadLn(Infile, nextChars);
if (Pos('*', nextChars) = 0) then
ParseChars(nextChars, targetDirective);
until (Pos('*', nextChars) > 0) or (Length(nextChars) = 0) or EOF(Infile);
end;
CloseFile(Infile);
ReadSpecs := dt_OK; { File OK }
end; { ReadSpecs }
{==========================================================================}
{ Reads the CNOTES file and extracts a character note }
{==========================================================================}
function TDelta.ReadNotes(notesFile: string): integer;
var
Infile: TextFile;
TxtLine: string;
charNumber, noteCount: integer;
notesFound: boolean;
Buffer: array [0..2048] of char;
Character: TCharacter;
procedure GetNumber(var f: TextFile; var charNumber: integer);
var
S: string;
Ch: char;
begin
S := '';
repeat
Read(f, Ch);
if EoLn(f) then
ReadLn(f);
if not (Ch in ['#', '.', ' ']) then
S := S + Ch;
until (Ch = '.') or EOF(f);
if (Pos('-', S) > 0) then
S := Copy(S, 1, Pos('-', S) - 1);
charNumber := StrToIntDef(S, 0);
end;
procedure GetField(var f: TextFile; Buffer: PChar; Size: word);
var
I: integer;
Ch: char;
begin
Ch := #0;
I := 0;
while ((Ch <> '#') and not EOF(f) and (I < Size)) do
begin
Read(f, Ch);
Buffer[I] := Ch;
Inc(I);
end;
Buffer[I - 1] := Chr(0);
end;
begin
noteCount := 0;
notesFound := False;
Buffer[0] := Chr(0);
charNUmber := 0;
{ Open the characters file }
if FileExists(notesFile) then
begin
AssignFile(Infile, notesFile);
Reset(Infile);
end
else
begin
ReadNotes := dt_NotesFileNotFound; { File not found }
Exit;
end;
while (not notesFound) and not EOF(Infile) do
begin
ReadLn(Infile, TxtLine);
{ Makes sure that the '*CHARACTER NOTES' directive is present
in the file }
if (Pos(noteDirective, TxtLine) > 0) then
notesFound := True;
{ Will run along the notes file until it sees a '#'
then read in the character notes }
if (notesFound) then
begin
if EoLn(Infile) then
ReadLn(Infile);
while not EOF(Infile) do
begin
Inc(noteCount);
GetNumber(Infile, charNumber);
GetField(Infile, Buffer, SizeOf(Buffer));
if (charNumber > 0) then
begin
Character := CharacterList[charNumber - 1];
Character.charNote := Buffer;
CharacterList[charNumber - 1] := Character;
end;
end;
end;
end;
CloseFile(Infile);
{ Return the number of character notes in the file, otherwise an error code }
if notesFound then
ReadNotes := noteCount
else
ReadNotes := dt_InvalidNotesFile; { Invalid file }
end; { ReadNotes }
{==========================================================================}
{ Writes the CHARS File }
{==========================================================================}
function TDelta.WriteChars(charsFile: string): integer;
var
Outfile: TextFile;
charStr, stateStr, unitStr: string;
I, J, numChars: integer;
Character: TCharacter;
begin
if (Length(CharacterList) = 0) then
begin
WriteChars := dt_CharListEmpty; { List empty }
Exit;
end;
{ Open the output file }
AssignFile(Outfile, charsFile);
Rewrite(Outfile);
{ Write header information }
WriteLn(Outfile, showDirective, ' ', Heading, '. Revised ', DateTimeToStr(Now), '.');
WriteLn(Outfile);
WriteLn(Outfile, charDirective);
WriteLn(Outfile);
numChars := 0;
charStr := '';
stateStr := '';
unitStr := '';
{ Write characters and states }
for I := 0 to Length(CharacterList) - 1 do
begin
Character := CharacterList[I];
//if (Character.charType = 'TE') then
//begin
// Character.charName := StripChars(Character.charName, ['<', '>']);
// Character.charName := Concat('<', Character.charName, '>');
//end;
charStr := Concat('#', IntToStr(I + 1), '. ', Trim(Character.charName), '/');
WriteLn(Outfile, WrapText(charStr, LineEnding + StringOfChar(' ', 8),
[' ', #9, '-'], 79));
if (Character.charType = 'RN') or (Character.charType = 'IN') then
begin
if not IsEmptyStr(Character.charUnit, [' ']) then
begin
unitStr := Concat(StringOfChar(' ', 7), Trim(Character.charUnit), '/');
WriteLn(Outfile, unitStr);
end;
end;
if (Character.charType = 'OM') or (Character.charType = 'UM') then
begin
for J := 0 to Character.charStates.Count - 1 do
begin
stateStr := Concat(StringOfChar(' ', 7), IntToStr(J + 1),
'. ', Trim(Character.charStates[J]), '/');
//WriteLn(Outfile, WrapText(stateStr, LineEnding + StringOfChar(' ', 8),
// [' ', #9, '-'], 79));
WriteLn(Outfile, WrapText(stateStr, LineEnding + StringOfChar(' ', 8),
[' ', #9], 79));
end;
end;
Inc(numChars);
WriteLn(Outfile);
end;
CloseFile(Outfile);
{ Return the number of characters written to the file }
WriteChars := numChars;
end; { WriteChars }
{==========================================================================}
{ Writes the ITEMS File }
{==========================================================================}
function TDelta.WriteItems(itemsFile: string): integer;
var
Outfile: TextFile;
descStr, comment: string;
I, J, numItems: integer;
Item: TItem;
Character: TCharacter;
begin
if (Length(ItemList) = 0) then
begin
WriteItems := dt_ItemListEmpty; { List empty }
Exit;
end;
{ Open the output file }
AssignFile(Outfile, itemsFile);
Rewrite(Outfile);
{ Write header information }
WriteLn(Outfile, showDirective, ' ', Heading, '. Revised ', DateTimeToStr(Now), '.');
WriteLn(Outfile);
WriteLn(Outfile, itemDirective);
WriteLn(Outfile);
numItems := 0;
descStr := '';
{ Write items and attributes }
for I := 0 to Length(ItemList) - 1 do
begin
Item := ItemList[I];
WriteLn(Outfile, '# ', Item.itemName + Iif(Length(Item.itemComment) >
0, ' ' + Item.itemComment, '') + '/');
for J := 0 to Item.itemAttributes.Count - 1 do
begin
Character := CharacterList[J];
if (Item.itemAttributes[J] <> 'U') then
begin
if (Character.charType = 'TE') then
descStr := Concat(descStr, IntToStr(J + 1), '', Item.itemAttributes[J], ' ')
else
begin
if (FirstChar(Item.itemAttributes[J]) = '<') then
begin
if Frequency(',', Item.itemAttributes[J]) > 0 then
descStr := Concat(descStr, IntToStr(J + 1), '',
Item.itemAttributes[J], ' ')
else
begin
comment := ExtractText(Item.itemAttributes[J], '<', '>');
descStr := Concat(descStr, IntToStr(J + 1), '<' + comment +
'>', ',', RemoveComments(Item.itemAttributes[J]), ' ');
end;
end
else
descStr := Concat(descStr, IntToStr(J + 1), ',',
Item.itemAttributes[J], ' ');
end;
end;
end;
WriteLn(Outfile, WrapText(descStr, #13#10, [' '], 79));
descStr := '';
WriteLn(Outfile);
Inc(numItems);