-
Notifications
You must be signed in to change notification settings - Fork 0
/
KWKStdXE.pas
2234 lines (1943 loc) · 66.9 KB
/
KWKStdXE.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
unit KWKStdXE;
{(c) Copyright 1986-2012, Keith W. Kintigh}
interface
uses ansistrings, strutils;
type matrix_elmt=double;
const missing=1.0e32;
max_antana_errors=20;
tokenlen=32;
years='1986-2020';
label_width=12;
{database definition}
maxvlabellen: integer=12;
maxclabellen=12;
{end of database definition constants}
type Int_Vector = array of integer;
Real_Vector = array of real;
String_Vector = array of string;
Int_Array_2D = array of array of integer;
Real_Array_2D = array of array of real;
String_Array_2D = array of array of string;
RealArray3D = array of array of array of real;
datatype=(real_type,string_type);
DataFile= record {this defines a database for the program}
tvar, {total variables}
nvar, {numeric variables}
svar, {string variables}
nobs, {number of observations}
clabellen, {max length of case labels}
vlabellen, {max length of variable labels}
nvarplace: {max number of digits to the left of the decimal point, when read}
integer;
olabel, {obs labels, in sdata[*,0]}
vlabel, {all variable labels}
svlabel, {string variable labels}
nvlabel: {numeric variable labels}
String_Vector;
vtype:array of datatype; {type (numeric or string for each varables}
sdata: String_Array_2D; {array of string variable values}
{default case labels stored in sdata[0,*]}
ndata: Real_Array_2D; {array of numeric variable values}
svindex, {svindex[i]=the original variable number of sdata variable i}
nvindex, {nvindex[i]=the original variable number of ndata variable i}
vindex: {vindex[i]:=the index in sdata(0,-) or ndata(+) of original var i}
Int_Vector; {array of integer;}
end;
DataFile_Int= record {this defines a database for the program}
tvar, {total variables}
nvar, {numeric variables}
svar, {string variables}
nobs, {number of observations}
clabellen, {max length of case labels}
vlabellen, {max length of variable labels}
nvarplace: {max number of digits to the left of the decimal point, when read}
integer;
olabel, {obs labels, in sdata[*,0]}
vlabel, {all variable labels}
svlabel, {string variable labels}
nvlabel: {numeric variable labels}
String_Vector;
vtype: array of datatype; {type (numeric or string for each varables}
sdata: String_Array_2D; {array of string variable values}
{default case labels stored in sdata[0,*]}
ndata: Int_Array_2D; {array of numeric variable values}
svindex, {svindex[i]=the original variable number of sdata variable i}
nvindex, {nvindex[i]=the original variable number of ndata variable i}
vindex: {vindex[i]:=the index in sdata(0,-) or ndata(+) of original var i}
Int_Vector; {array of integer;}
end;
{end of database definition}
tokentype=string[tokenlen];
pathtype=string;
label_string= string[label_width];
label_array = array[1..1] of label_string;
label_pntr = ^label_array;
matrix_type = array[1..1] of matrix_elmt;
matrix_pntr = ^matrix_type;
TimeDateType=string[8];
{***************************************************************************}
{STRING FUNCTIONS}
function uc(c: ansichar): ansichar; {convert a char to upper case}
function ucase(strng: ansistring): ansistring; {convert a string to upper case}
function squash(strng: ansistring): ansistring; {remove all blanks from a string}
function trim(strng: ansistring): ansistring; {remove leading and trailing blanks}
function trimR(strng: ansistring): ansistring; {remove trailing blanks}
function lowstring(xmin: real): tokentype;
function highstring(xmax: real): tokentype;
function padright(strng: ansistring; totallength: integer): ansistring; {pad a string on right}
function truncstring(strng: ansistring; len: integer): ansistring; {truncate to len}
function intstrfn(i: integer): ansistring;
{INPUT-OUTPUT FUNCTIONS}
function validname(var name: ansistring): boolean;
function file_prefix(name: ansistring): ansistring; {return name w/o extension}
function file_extension(name: ansistring): ansistring; {return .ext of file}
function exists(var fvar:text; name: ansistring): boolean; {does file exist}
function readkey: ansichar;
function readbool(message: ansistring; dflt: ansichar): boolean;
{issue message as a prompt and return true or false}
procedure readfile(message: ansistring; var fvar: text; var dflt: ansistring);
{prompt for an input file name, check its existence and open for reading}
procedure writefile(message: ansistring; var fvar: text; var name: ansistring);
{prompt for a file name, if it exists, prompt to erase, and open for write}
function readint(message: ansistring; min,max: longint; dflt: ansistring):
longint; {return an integer between min and max (inclusive)}
function readreal(message: ansistring; min,max: real; dflt: ansistring): real;
{returns a real; works like readint}
function readchoice(message,choices,dflt: ansistring): ansichar;
{print message and read one character which must be in choices list}
function readchar(message: ansistring; dflt: ansichar): ansichar;
{get a nonblank character}
function readline(message,dflt: ansistring): ansistring;
procedure writeplotfile(var plotter: boolean; var plotsize, plotdest: ansichar;
var plotfile: text; var plotout: ansistring);
procedure CloseWindow;
procedure ClrScr;
Procedure HaltProgram(msg: ansistring);
{ANTANA FUNCTIONS}
procedure skip_comment(var fvar: text);
function skip_separator(var fvar: text): ansichar;
function gettoken(var fvar: text): tokentype; {reads an antana token}
function getreal(var fvar: text): real;
{reads antana standard reals with embedded comments}
function getinteger(var fvar: text): integer;
{reads antana standard integers with embedded comments}
{function getlongint(var fvar: text): longint;}
{reads antana standard integers with embedded comments}
function ndx (i,j,cols: integer): integer;
{1,1 origin indexing of sequentially stored matrix}
function t_ndx(i,j,size: integer): integer; {returns triangular matrix indx}
procedure alloc_matrix(var pntr; elmts: longint; size: word);
{generalized memory allocation procedure}
procedure read_antana_matrix(msg: ansistring; var dskin: text;
var filein: ansistring; var nrow,ncol: integer;
var table: matrix_pntr; real_input: boolean);
procedure read_labels(elmt: integer; var maxlen: integer;
var labl: label_pntr; msg: ansistring; var name: ansistring;
ext: ansistring; leftjust: boolean);
{return longest label length}
procedure print_triangle(var mat: matrix_pntr; size: integer ;
var labl: label_pntr; llen: integer; path,t1,t2,t3: ansistring);
{MATHEMATICAL FUNCTIONS}
function log(val: real): real; { compute log10(val) }
function place(val: real): integer; { number of digits in val }
function power(base,expon: real):extended; {raise positive base the expon power}
function normal: extended; {return a normally distributed random number}
procedure setrandom;
function ceil(x: real): longint;
function floor(x: real): longint;
function maximum(a,b: real): real;
function minimum(a,b: real): real;
function i_maximum(a,b: longint): longint;
function i_minimum(a,b: longint): longint;
function roundceil(r: real): real;
function arcsin(val: real): real;
function twotothe(power: integer): integer; {use only for integers}
function r_binomial(n,k: longint; p: real): extended;
function lnfactorial(k: longint): extended;
function IsInteger(rv: real; var error: boolean): integer;
function fisher_prob(a,b,c,d: integer; var other_tail: extended): extended;
function chisq_tail_prob(x2:real; dof:integer): real;
function G_compute(var table: Int_Array_2D; var rsum,csum: Int_Vector;
total: integer): real;
function G_goodfit(var obs: Int_Vector; var expect: Real_Vector;
vals: integer): real;
function williams(var rsum,csum: Int_Vector; total: integer): real;
function williams_goodfit(vals: integer; total: real): real;
{MISCELLANEOUS FUNCTIONS}
procedure sort_real_vector(key: real_vector); { SORT a column of numbers}
Procedure sort_int_vector(key: int_vector);
procedure boxlinec(len: integer; line: ansistring);
procedure boxline(len: integer; line: ansistring);
procedure boxtop(len: integer; top: boolean);
procedure copyright(name,version,years,description: ansistring);
procedure displayheading(var fvar: text);
{print # comments in 1st 5 lines of an ADF file}
{TIME AND DATE FUNCTIONS}
function DateString: ansistring;
function ReverseDate(dte: ansistring): ansistring;
function TimeString: ansistring;
function timesec: real;
function readdate(msg: ansistring; dflt: timedatetype): timedatetype;
{CSV Procedures}
function gettoken_csv(var ln: ansistring; var p1,error: integer; var tokentype: datatype): ansistring;
{reads a token from the line}
{note in string type embedded blanks are allowwed but may be incompatible with other programs}
function get_csv(var f: datafile; prompt: ansistring; var filename: ansistring; quiet: boolean): boolean;
function ReadFile_CSV_Int(var dfi: DataFile_Int; prompt: ansistring; var filename: ansistring): boolean;
procedure readfile_csv(var df: datafile; prompt: ansistring; var filename: ansistring);
procedure readfile_csv_quiet(var df: datafile; prompt: ansistring; var filename: ansistring);
procedure datafiletoscreen_csv(var f: datafile);
procedure datafiletoprinter_csv(var f:datafile; prompt: ansistring);
function csvstring(s: ansistring): ansistring;
function csvreal(v: extended; w,d: integer): ansistring;
procedure datafiletofile_csv(var f: datafile; prompt: ansistring);
{MATRIX UTILITIES}
function vector_median(var vect: Real_Vector; elmt:integer): real;
procedure Clear_2D_Int_Array(var table: Int_Array_2D);
procedure Fill_Work(var itable: Int_Array_2D; var rtable: Real_Array_2D);
function Int_Vector_Is_0(var vector: Int_Vector): boolean;
procedure Multiply_2D_Real_Array(var table: Real_Array_2D; factor: real);
procedure Multiply_Real_Row_Or_Col(var table: Real_Array_2D; rowcol: integer;
rowwise: boolean; factor: real);
procedure Multiply_Real_Vector(var vector: Real_Vector; factor: real);
function Real_Row_Max(var table: Real_Array_2D; rowno: integer): real;
function Real_Column_Max(var table: Real_Array_2D; colno: integer): real;
function Real_Vector_Max(var vector: Real_Vector): real;
function Real_Vector_Min(var vector: Real_Vector): real;
function Int_Vector_Min(var vector: Int_Vector; elmts: integer): longint;
function String_Vector_Maxlen(var s: String_Vector): integer;
procedure Margins_2D_Int_Array(var table: Int_Array_2D; var rowsum,colsum: Int_Vector;
var total: integer);
procedure Margins_2D_Real_Array(var table: Real_Array_2D; var rowsum,colsum: Real_Vector;
var total: real);
procedure pause(var fle: ansistring);
{*************************************************************************}
implementation
uses SysUtils;
{STRING FUNCTIONS}
function uc(c: ansichar): ansichar;
var n: byte;
begin
{n:=ord(c);
if (n>+97) and (n<=122) then c:=chr(n-32);
uc:=c;}
uc:=upcase(c);
end;
function ucase(strng: ansistring): ansistring;
{change all lower case letters in strng to upper case}
var i,j,n: integer;
begin
{j:=length(strng);
for i:=1 to j do begin
n:=ord(strng[i]);
if (n>=97) and (n<=122) then strng[i]:=chr(n-32);
end;
ucase:=strng; }
ucase:=UpperCase(strng);
end;
function squash(strng: ansistring): ansistring;
{eliminate all blanks from strng}
var j: integer;
begin
j:=length(strng);
while j>=1 do begin
if strng[j]=' ' then
strng:=copy(strng,1,j-1)+copy(strng,j+1,length(strng)-j);
j:=j-1;
end;
squash:=strng;
end;
function trim(strng: ansistring): ansistring;
{eliminate leading and trailing blanks from strng}
var i,j: integer;
begin
i:=length(strng);
while (i>0) and (strng[i]=' ') do i:=i-1;
strng:=copy(strng,1,i);
j:=1;
while (J<I) and (strng[j]=' ') do j:=j+1;
trim:=copy(strng,j,i-j+1);
end;
function trimR(strng: ansistring): ansistring;
{eliminate trailing blanks from strng}
var i: integer;
begin
i:=length(strng);
while (i>0) and (strng[i]=' ') do i:=i-1;
trimr:=copy(strng,1,i);
end;
function lowstring(xmin: real): tokentype;
var strng: tokentype;
begin
str(floor(xmin):16,strng);
strng:=trim(strng);
lowstring:=strng;
end;
function highstring(xmax: real): tokentype;
var strng: tokentype;
begin
str(ceil(xmax):16,strng);
strng:=trim(strng);
highstring:=strng;
end;
function padright(strng: ansistring; totallength: integer): ansistring; {pad a string on right}
var i: integer;
begin
for i:=length(strng)+1 to totallength do strng:=strng+' ';
padright:=strng;
end;
function truncstring(strng: ansistring; len: integer): ansistring; {truncate to len}
begin
if length(strng)<=len then truncstring:=strng else truncstring:=copy(strng,1,len);
end;
function intstrfn(i: integer): ansistring;
var rslt: ansistring;
begin
str(i,rslt);
IntStrFn:=rslt;
end;
{INPUT-OUTPUT FUNCTIONS}
function validname(var name: ansistring): boolean;
{check to see if name is a valid pathname, return true or false}
{this procedure is not perfect}
{i.e. it will let some invalid names by, but most will be caught}
const illegal='/*?"<>|';
var ok,logicaldevice: boolean; i,len: integer; c: ansichar;
begin
len:=length(name);
name:=ucase(name);
logicaldevice:=
((length(name)=3) and (pos(name,'PRN|NUL')<>0)) or
((length(name)=4) and (pos(name,'COM1|COM2|LPT1|LPT2|LPT3')<>0));
i:=1;
ok:=not logicaldevice;
while i<=len do begin
c:=name[i];
if (pos(c,illegal)>0) or (ord(c)<32) or (ord(c)>=127) then ok:=false;
inc(i);
end;
validname:=ok and (len>0);
end;
function file_prefix(name: ansistring): ansistring; {return name w/o extension}
var len,pos: integer;
begin
len:=length(name);
pos:=len;
while (pos>1) and (name[pos]<>'.') and (pos>(len-3))do dec(pos);
if (pos>0) and (name[pos]='.') then file_prefix:=copy(name,1,pos-1)
else file_prefix:=name;
end;
function file_extension(name: ansistring): ansistring; {return extension including '.'}
var len,pos: integer;
begin
len:=length(name);
pos:=len;
while (pos>1) and (name[pos]<>'.') and (pos>(len-3))do dec(pos);
if name[pos]='.' then file_extension:=copy(name,pos+1,len-pos+1)
else file_extension:='';
end;
function exists(var fvar:text; name: ansistring): boolean;
var ok: boolean;
begin
AssignFile(fvar,name);
{$I-} reset(fvar); {$I+}
ok:=(ioresult=0);
if ok then close(fvar);
exists:=ok;
end;
function readkey: ansichar;
{reads the rest of the line and extracts the first character}
{if the line is empty returns <CR>}
{replacement for DOS readkey which does not exist in Delphi}
var c: ansichar; s: ansistring;
begin
readln(s);
if length(s)=0 then c:=chr(13)
else c:=s[1];
readkey:=c;
end;
function readbool(message: ansistring; dflt: ansichar): boolean;
{issue message as a prompt and return true or false}
{the procedure reprompts until a valid response is received }
{if dflt='T' then a <CR> will result in true being returned }
{if dflt='F' then a <CR> will result in false being returned}
{otherwise the program requires a definitive response }
{y,ye,yes,n,no,ok (any case) are accepted as valid responses}
var ok: boolean; reply,c: ansichar;
begin
ok:=true; c:=' '; readbool:=true;
repeat
{dflt 'T' true; 'F' false; otherwise no default}
if ok then begin
if dflt='T' then c:='Y' else if dflt='F' then c:='N' else c:=' ';
end;
if c=' ' then write(message,' ? ')
else write(message,' {',c,'} ? ');
reply:=readkey;
if (reply=#13) or (reply=#10) then reply:=c;
reply:=upcase(reply);
ok:=true;
case reply of
'Y': readbool:=true;
'N': readbool:=false;
else begin
ok:=false;
{write(#7);}
end;
end;
until ok;
{writeln(reply);}
end;
procedure readfile(message: ansistring; var fvar: text; var dflt: ansistring);
{prompt for an input file name, check its existence and open for reading}
var ok: boolean; name: ansistring;
begin
{if length(dflt)=0 then dflt:='*';}
ok:=false;
if ((pos('.',name)=4) and (pos(copy(name,1,3),'CON|PRN|NUL')<>0)) or
((pos('.',name)=5) and (pos(copy(name,1,4),'COM1|COM2|LPT1|LPT2|LPT3')<>0))
then name:=copy(name,1,pos('.',name)-1);
while not OK do begin
if dflt='' then write(message,' ? ') else write(message,' {',dflt,'} ? ');
readln(name);
name:=trim(name);
{if name='?' then directory;}
if (length(name)=0) and (length(dflt)>0) and (dflt[1]<>'.') then
name:=dflt;
ok:=validname(name);
if ok and (name<>'CON') and ((length(dflt)>0) and (dflt[1]='.')) and (pos('.',name)=0) then begin
name:=name+dflt; ok:=validname(name); {recheck w/ added ext}
end;
if ok then begin
if name='CON' then assign(fvar,'')
else assign(fvar,name);
{$I-} reset(fvar); {$I+}
ok:=(ioresult=0);
{ok:=ok and (pos(name,'CON|')<>0);}
if not ok then writeln('File Not Found');
end;
end;
dflt:=name;
end;
procedure writefile(message: ansistring; var fvar: text; var name: ansistring);
{prompt for a file name, if it exists, prompt to erase, and open for write}
var
ok: boolean; dflt: ansistring;
begin
{ok:=false;}
if ((pos('.',name)=4) and (pos(copy(name,1,3),'CON|PRN|NUL')<>0)) or
((pos('.',name)=5) and (pos(copy(name,1,4),'COM1|COM2|LPT1|LPT2|LPT3')<>0))
then name:=copy(name,1,pos('.',name)-1);
dflt:=name;
{if length(dflt)=0 then dflt:='*';}
repeat
if dflt='' then write(message,' ? ')
else write(message,' {',dflt,'} ? ');
readln(name);
name:=trim(name);
{ if name='?' then directory; }
if (length(name)=0) and (length(dflt)>0) and (dflt[1]<>'.') then
name:=dflt;
ok:=validname(name);
if ok and (name<>'CON') and ((length(dflt)>0) and (dflt[1]='.')) and (pos('.',name)=0) then begin
name:=name+dflt; ok:=validname(name); {recheck w/ added ext}
end;
if ok then begin
if name='CON' then AssignFile(fvar,'')
else if exists(fvar,name) then begin
ok:=readbool('File Exists; OK to Erase It','F');
if ok then begin
erase(fvar); writeln(name,' Erased');
end;
end
else ok:=true;
if ok then begin
rewrite(fvar);
ok:=(ioresult=0);
if not ok then writeln(' I/O Error - Disk May Be Full');
end;
end;
until ok;
end;
function readint(message: ansistring; min,max: longint; dflt: ansistring):
longint;
{return an integer between min and max (inclusive)}
{if the dflt is specified AS A STRING, it will be returned if <CR> is entered}
var ok: boolean; reply: ansistring; value: longint; error: integer;
begin
ok:=false; readint:=0;
repeat
if dflt<>'' then write(message,' {',dflt,'} ? ')
else write(message,' ? ');
readln(reply);
reply:=trim(reply);
if (reply<>'') or (dflt<>'') then begin
if length(reply)=0 then reply:=dflt;
val(reply,value,error);
ok:=(error=0);
if not ok then writeln('Invalid Character in Integer: "',
reply[error],'"')
else begin
ok:=(value>=min) and (value<=max);
if not ok then
writeln('Value Out of Range (',min,',',max,')')
else readint:=value;
end;
end;
until ok;
end;
function readreal(message: ansistring; min,max: real; dflt: ansistring): real;
{returns a real; works like readint}
var ok: boolean; reply: ansistring; value: real; error: integer;
begin
ok:=false; readreal:=0.0;
repeat
if dflt<>'' then write(message,' {',dflt,'} ? ')
else write(message,' ? ');
readln(reply);
reply:=trim(reply);
if (reply<>'') and (reply[1]='.') then reply:='0'+reply
else if (reply<>'') and (reply[1]='-') and (reply[2]='.') then
reply:='-0.'+copy(reply,3,length(reply)-2);
if (reply<>'') or (dflt<>'') then begin
if length(reply)=0 then reply:=dflt;
val(reply,value,error);
ok:=(error=0);
if not ok then writeln('Invalid Character in Real: "',reply[error],'"')
else begin
ok:=(value>=min) and (value<=max);
if not ok then
writeln('Value Out of Range (',min,',',max,')')
else readreal:=value;
end;
end;
until ok;
end;
function readchoice(message,choices,dflt: ansistring): ansichar;
{print message and read one character which must be in choices list}
{alpha characters in choices must be upper case, default char=dflt}
var ok: boolean; reply: ansichar;
begin
{ok:=true;}
repeat
{if ok then} begin
if dflt='' then write(message,' ? ')
else write(message,' {',dflt,'} ? ');
end;
reply:=readkey;
if (reply=#13) or (reply=#10) then begin
if dflt<>'' then reply:=dflt[1] else reply:=chr(255);
end;
reply:=upcase(reply);
ok:=false;
if pos(reply,choices)>0 then ok:=true else write(#7);
until ok;
{writeln(reply);}
readchoice:=reply;
end;
function readchar(message: ansistring; dflt: ansichar): ansichar;
{get a blank character, if 1st char of message=chr(0), char may be blank}
var ok,allowblank: boolean; reply,c: ansichar; minchar: byte;
begin
{ok:=true;}
allowblank:= ord(message[1])=0;
if allowblank then begin
minchar:=32;
message:=copy(message,2,length(message)-1);
end
else minchar:=33;
repeat
{if dflt=' ' or nul then no default}
{if ok then} begin
if dflt=chr(0) then c:=' ' else c:=dflt;
if c=' ' then write(message,' ? ')
else write(message,' {',c,'} ? ');
end;
reply:=readkey;
if (reply=#13) or (reply=#10) then reply:=c;
ok:=true;
if ord(reply)<minchar then begin
if reply=chr(0) then reply:=readkey; {flush scan code}
ok:=false;
write(#7);
end;
until ok;
{writeln(reply);}
readchar:=reply;
end;
function readline(message,dflt: ansistring): ansistring;
var reply: ansistring;
begin
if length(dflt)>0 then
write(message,'{',dflt,'} ? ')
else write(message,' ? ');
readln(reply);
if reply='' then reply:=dflt;
readline:=reply;
end;
procedure writeplotfile(var plotter: boolean; var plotsize, plotdest: ansichar;
var plotfile: text; var plotout: ansistring);
var dflt: string[1]; {5.1}
begin
plotter:=readbool('Plot to HPGL Plotter or HP LaserJet Printer','F');
if plotter then begin
plotout:='';
writefile('HPGL Output to (PRN, COM1, COM2 or <filename>)',plotfile,
plotout);
if plotout='PRN' then dflt:='L' else dflt:='A';
plotsize:=readchoice(
' HP [L]aserJet 3/4 or Plotter Paper Size: [A] 8.5x11 [B] 11x17',
'ABL',dflt);
plotout:=trim(plotout);
plotout:=ucase(plotout);
if (plotout='COM1') or (plotout='COM2') or (plotout='PRN') or
(plotout='LPT1') or (plotout='LPT2') then plotdest:='D'
else plotdest:='F';
if plotsize='L' then begin
plotsize:='A';
{plotdest:=chr(ord(plotdest)+32);} {sets to d or f instead of D or F}
if plotdest='D' then plotdest:='d' else if plotdest='F' then plotdest:='f';
end;
if (plotdest='D') and (plotsize<>'L') then begin
writeln(' Plotter Must Be Turned on and Connected to ',plotout);
if plotout[1]='C' then
writeln(' Port Must Be Initialized with MODE ',plotout,':baud,,,,P');
if readbool(' Abort Now','F') then
HaltProgram('Plotter Not Initialized');
end;
end else plotsize:='A';
end;
procedure CloseWindow;
begin
repeat
until readbool('OK to Close Program Window','T');
end;
Procedure ClrScr;
var i: byte;
begin
for i:=1 to 25 do writeln;
end;
procedure HaltProgram(msg: ansistring);
begin
writeln;
writeln('<<<<<<<<<<<<>>>>>>>>>>>>');
writeln(msg,' - Program Aborting.');
CloseWindow;
Halt;
end;
{ANTANA FUNCTIONS}
procedure skip_comment(var fvar: text);
var c: ansichar;
begin
{skip over # to end of line or text between #'s, e.g. #site 12 #}
c:=^J;
repeat
if not seekeoln(fvar) then read(fvar,c);
until eoln(fvar) or (c='#') or (c=^J);
end;
function skip_separator(var fvar: text): ansichar;
var c: ansichar;
begin
{skip over blanks, tabs, commas, lf's, comments}
c:=^Z;
repeat
if not seekeof(fvar) then begin
read(fvar,c);
if c='#' then begin skip_comment(fvar); c:=',' end;
end;
until eof(fvar) or (pos(c,',#')=0);
skip_separator:=c;
end;
function gettoken(var fvar: text): tokentype;
{reads an antana token}
var reply: string[32]; len: integer; c: ansichar;
begin
c:=skip_separator(fvar);
len:=0; reply:='';
repeat
len:=succ(len);
reply:=reply+c;
if eoln(fvar) or (ord(reply[1])=26) {or eof(fvar)} then c:=' ' else read(fvar,c);
until pos(c,' ,#'^I^J)>0;
if c='#' then skip_comment(fvar);
{reply[0]:=chr(len);}
gettoken:=leftstr(reply,len);
{gettoken:=reply;}
end;
function getreal(var fvar: text): real;
{reads antana standard reals with embedded comments}
var reply: string[32]; value: real; error: integer;
begin
reply:=gettoken(fvar);
getreal:=missing;
if ord(reply[1])=26 then begin
close(fvar); HaltProgram('Numeric Input Error; not enough numbers in the file');
end
else if pos('?',reply)=0 then begin
val(reply,value,error);
if error<>0 then begin
writeln('Invalid Character in Number: "',reply[error],'"',
' (Decimal ',ord(reply[error]),') - Input: ',reply);
if readbool('Quit Now','T') then begin
close(fvar); HaltProgram('Real Input Error');
end;
end
else getreal:=value;
end;
end;
function getinteger(var fvar: text): integer;
{reads antana standard long integer with embedded comments}
var rvalue: real;
begin
getinteger:=-maxint;
rvalue:=getreal(fvar);
if (rvalue>high(integer)) or (rvalue<low(integer)) or ((rvalue-int(rvalue))<>0.0) then
begin
writeln('Not an Integer or Integer Out of Range: ',rvalue:16:4);
if readbool('Quit Now','T') then begin
close(fvar); HaltProgram('Integer Input Error');
end;
end
else getinteger:=round(rvalue);
end;
{function getlongint(var fvar: text): longint;}
{KWK 12/03 Eliminated because integer is now same as longint}
{reads antana standard long integer with embedded comments}
{var rvalue: real;
begin
getlongint:=-maxlongint;
rvalue:=getreal(fvar);
if (abs(rvalue)>maxlongint) or ((rvalue-int(rvalue))<>0.0) then
begin
writeln('Not an Integer or Integer Out of Range: ',rvalue:16:4);
if readbool('Quit Now','T') then begin
close(fvar); HaltProgram('Integer Input Error');
end;
end
else getlongint:=round(rvalue);
end;}
function ndx(i,j,cols: integer): integer; {1,1 origin indexing}
begin
ndx := (i-1)*cols+j;
end;
function t_ndx(i,j,size: integer): integer;
var rslt,t: integer; {no diagonal}
begin
i:=pred(i); j:=pred(j);
if i>j then begin
t:=i; i:=j; j:=t;
end;
rslt:=j-i+((2*size-i-1)*i) div 2; {assumes i<j}
t_ndx:=rslt;
end;
procedure alloc_matrix(var pntr; elmts: longint; size: word);
var bytes: longint;
begin
bytes:=elmts*size;
try
getmem(pointer(pntr),bytes);
except
on EOutOfMemory do begin
writeln('Insufficient Memory to Allocate Matrix; Program Halts');
CloseWindow;
halt;
end;
end;
end;
{$R-}
procedure read_antana_matrix(msg: ansistring; var dskin: text;
var filein: ansistring; var nrow,ncol: integer;
var table: matrix_pntr; real_input: boolean);
var i,j: integer;
begin
readfile(msg,dskin,filein);
if filein='CON' then begin
nrow:=readint(' Number of Rows (Observations)',1,maxint,'');
ncol:=readint(' Number of Columns (Variables)',1,maxint,'');
writeln(' Enter ',nrow*ncol,' Values Followed by <Enter>');
end
else begin
displayheading(dskin);
nrow:=getinteger(dskin); ncol:=getinteger(dskin);
writeln(' Reading ',nrow,' Rows and ',ncol,' Columns');
end;
alloc_matrix(table,nrow*ncol,sizeof(matrix_elmt));
if filein='CON' then write('? ');
for i:=1 to nrow do
for j:=1 to ncol do begin
if (filein='CON') and (eoln(dskin)) then write('? ');
if real_input then table^[ndx(i,j,ncol)]:=getreal(dskin)
else table^[ndx(i,j,ncol)]:=getinteger(dskin);
end;
close(dskin);
end;
procedure read_labels(elmt: integer; var maxlen: integer;
var labl: label_pntr; msg: ansistring; var name: ansistring;
ext: ansistring; leftjust: boolean);
var uno,ll: integer; strng: label_string; dskin: text;
begin
if readbool('Read '+msg+' Label File','F') then begin
name:=file_prefix(name)+'.'+ext;
readfile(' '+msg+' Label File Name',dskin,name);
for uno:=1 to elmt do begin
readln(dskin,strng);
labl^[uno]:=trim(strng);
if length(labl^[uno])>maxlen then maxlen:=length(labl^[uno]);
end;
close(dskin);
if leftjust then for uno:=1 to elmt do
for ll:=length(labl^[uno])+1 to maxlen do labl^[uno]:=labl^[uno]+' ';
end else
for uno:=1 to elmt do if leftjust then str(uno:maxlen,labl^[uno])
else str(uno,labl^[uno]);
end;
procedure print_triangle(var mat: matrix_pntr; size: integer ;
var labl: label_pntr; llen: integer; path,t1,t2,t3: ansistring);
var i,hlen,linesize,itemwid,dec,maxplace,elmts: integer;
triangle: boolean;
numstr: string[3];
fileout: ansistring;
dskout: text;
scale: real;
procedure print_tri_matrix;
var i,j,ss,across,subset,llim,ulim: integer;
begin
across:=(linesize-llen-1) div itemwid;
subset:=(size+across-1) div across-1;
writeln(dskout);
if length(t1)>0 then writeln(dskout,t1);
if length(t2)>0 then writeln(dskout,t2);
if length(t3)>0 then writeln(dskout,t3);
for ss:=0 to subset do begin
if triangle then llim:=ss*across+2 else llim:=1;
for i:=llim to size do begin
write(dskout,labl^[i]:llen,':');
if triangle then ulim:=i-1 else ulim:=size;
for j:=ss*across+1 to i_minimum(succ(ss)*across,ulim) do begin
if i<j then write(dskout,mat^[t_ndx(i,j,size)]*scale:itemwid:dec)
else if i=j then write(dskout,' ':itemwid)
else write(dskout,mat^[t_ndx(j,i,size)]*scale:itemwid:dec);
end;
writeln(dskout);
end;
write(dskout,' ':llen+1);
if triangle then ulim:=pred(size) else ulim:=size;
for j:=ss*across+1 to i_minimum(succ(ss)*across,ulim) do
write(dskout,' ',copy(labl^[j],1,itemwid-1):itemwid-1);