-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path661_REXX.pas
646 lines (531 loc) · 17.3 KB
/
661_REXX.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
UNIT REXX;
{Copyright (C) 1989-1992, 1995-1997. Earl F. Glynn, Overland Park, KS.
All Rights Reserved. This UNIT may be freely distributed only
for non-commercial use.
#sign:Max: MAXBOX10: 18/05/2016 10:28:20
REXX-like functions. For information about REXX see the book
"The REXX Language," by M.F. Cowlishaw, Prentice-Hall, 1985,
or various IBM CMS or OS/2 manuals.
Pascal/Delphi functions with similar REXX functions:
ABS,
LENGTH,
MAX,
MIN,
POS (similar to FIND or INDEX)
COPY (similar to SUBSTR)
TRUNC
REXX functions with Pascal/Delphi equivalents:
Format, BitAnd, BitOr, Compare, Random, Insert
REXX DATE and TIME can be simulated with the FormatDateTime function.
REXX DelStr is equivalent to Delete in Delphi
Delphi functions that work similar to existing REXX functions:
Trim <-> Strip
REXX functions to be considered for future development:
Justify, LastPos, Verify
The Date and Time functions provided by REXX can be replaced with
a variety of Delphi functions, in particular, EncodeDate, DecodeDate,
DateToStr, FormatDateTime, etc.
Instead of REXX functions "Word", "WordIndex" and "WordLength",
see Tokens Unit.
}
INTERFACE
{USES
SysUtils; {Exception, AppendStr}
(* TYPE
TState = (TrimLeader, StartToken, EndToken); {States of finite state machine}
{$IFDEF WIN32}
TStrIndex = LongInt;
TTokIndex = WORD;
{$ELSE}
TStrIndex = BYTE;
TTokIndex = BYTE;
{$ENDIF}
EConversionError = CLASS(Exception);
*)
{STRING-related FUNCTIONs}
FUNCTION Abbrev(CONST information,info: STRING; CONST nMatch: TStrIndex): BOOLEAN;
FUNCTION AllSame(CONST s: STRING; CONST c: CHAR): BOOLEAN;
FUNCTION Capitalize (CONST s: STRING): STRING;
FUNCTION Center(CONST s: STRING; CONST sLength: TStrIndex): STRING;
FUNCTION Left(CONST s: STRING; CONST sLength: TStrIndex): STRING;
FUNCTION Right(CONST s: STRING; CONST sLength: TStrIndex): STRING;
FUNCTION Copies(CONST s: STRING; CONST n: TStrIndex): STRING;
FUNCTION CountChar(CONST s: STRING; CONST c: CHAR): TStrIndex;
FUNCTION DeleteString(CONST substring: STRING; CONST s: STRING): STRING;
FUNCTION Overlay(CONST ovly,target: STRING; CONST n: TStrIndex): STRING;
FUNCTION Plural(CONST n: LongInt; CONST singularform,pluralform: STRING): STRING;
FUNCTION Reverse(CONST s: STRING): STRING;
FUNCTION Spacerexx(CONST s: STRING; CONST n: TStrIndex): STRING;
FUNCTION Strip(CONST s: STRING; CONST option: STRING): STRING;
FUNCTION TestString(CONST sLength: TStrIndex): STRING;
FUNCTION Translate(CONST s,OutTable,InTable: STRING): STRING;
FUNCTION XRange (CONST start,stop: BYTE): STRING;
{Conversion FUNCTIONs -- many have new Delphi equivalents}
FUNCTION B2X(CONST b: BYTE): STRING; {byte-to-hexadecimal}
FUNCTION C2D(CONST s: STRING): DOUBLE; {character-to-double}
FUNCTION C2I(CONST s: STRING): INTEGER; {character-to-integer}
FUNCTION C2L(CONST s: STRING): LONGINT; {character-to-longint}
FUNCTION C2W(CONST s: STRING): WORD; {character-to-word}
FUNCTION C2X(CONST s: STRING): STRING; {character-to-hexadecimal}
FUNCTION I2C(CONST i: INTEGER): STRING; {integer-to-character}
FUNCTION I2X(CONST i: INTEGER): STRING; {integer-to-hexadecimal}
FUNCTION L2C(CONST i: LONGINT): STRING; {longint-to-character}
FUNCTION L2X(CONST i: LONGINT): STRING; {longint-to-hexadecimal}
FUNCTION D2C(CONST x: DOUBLE; CONST d: BYTE): STRING; {double-to-character}
FUNCTION W2C(CONST w: WORD): STRING; {word-to-character}
FUNCTION W2X(CONST w: WORD): STRING; {word-to-hexadecimal}
FUNCTION X2W(CONST s: STRING): WORD; {hexadecimal-to-word}
{Date/Time FUNCTIONs}
FUNCTION JulianDate(CONST DateTime: TDateTime): LongInt;
FUNCTION TimeDifference(CONST StartTime, StopTime: TDateTime): DOUBLE;
{Math FUNCTION}
FUNCTION Pwr(CONST x,y: DOUBLE): DOUBLE;
IMPLEMENTATION
{** STRING-related FUNCTIONs **************************************}
FUNCTION Abbrev(CONST information,info: STRING; CONST nMatch: TStrIndex): BOOLEAN;
VAR
i : TStrIndex;
match : BOOLEAN;
matchlength: TStrIndex;
BEGIN
match := TRUE;
IF LENGTH(info) < nMatch
THEN match := FALSE
ELSE BEGIN
i := 1;
matchlength := nMatch;
IF LENGTH(info) > nMatch
THEN matchlength := LENGTH(info);
IF matchlength > LENGTH(information)
THEN matchlength := LENGTH(information);
WHILE (i <= matchlength) AND match
DO BEGIN
match := (information[i] = info[i]);
INC (i)
END
END;
RESULT := match
END {Abbrev};
FUNCTION AllSame(CONST s: STRING; CONST c: CHAR): BOOLEAN;
VAR
i: INTEGER;
BEGIN
RESULT := TRUE;
i := 1;
WHILE RESULT AND (i <= LENGTH(s))
DO BEGIN
RESULT := RESULT AND (s[i] = c);
INC (i)
END
END {AllSame};
FUNCTION Capitalize (CONST s: STRING): STRING;
VAR
flag: BOOLEAN;
i : TStrIndex;
t : STRING;
BEGIN
flag := TRUE;
t := '';
FOR i := 1 TO LENGTH(s) DO
BEGIN
IF flag
THEN AppendStr(t, UpCase(s[i]))
ELSE AppendStr(t, s[i]);
flag := (s[i] = ' ')
END;
RESULT := t
END {Capitalize};
FUNCTION Center(CONST s: STRING; CONST sLength: TStrIndex): STRING;
VAR
LeftBlanks : TStrIndex;
RightBlanks: TStrIndex;
BEGIN
IF sLength <= 0
THEN RESULT := ''
ELSE
IF LENGTH(s) >= sLength
THEN RESULT := Copy(s, LENGTH(s) DIV 2 - (sLength-1) DIV 2, sLength)
ELSE BEGIN
LeftBlanks := (sLength - LENGTH(s)) DIV 2;
RightBlanks := sLength - LeftBlanks - LENGTH(s);
RESULT := Copies(' ',LeftBlanks) + s + Copies(' ',RightBlanks)
END
END {Center};
FUNCTION Left(CONST s: STRING; CONST sLength: TStrIndex): STRING;
BEGIN
IF sLength <= 0
THEN RESULT := ''
ELSE
IF LENGTH(s) >= sLength
THEN RESULT := Copy(s,1,sLength)
ELSE RESULT := s + Copies(' ',sLength-LENGTH(s))
END {Left};
FUNCTION Right(CONST s: STRING; CONST sLength: TStrIndex): STRING;
BEGIN
IF sLength <= 0
THEN RESULT := ''
ELSE
IF LENGTH(s) >= sLength
THEN RESULT := Copy(s, LENGTH(s)-sLength+1, sLength)
ELSE RESULT := Copies(' ', sLength-LENGTH(s)) + s
END {Right};
FUNCTION Copies(CONST s: STRING; CONST n: TStrIndex): STRING;
VAR
i: TStrIndex;
t: STRING;
BEGIN
t := '';
FOR i := 1 TO n DO
AppendStr(t, s);
RESULT := t
END {Copies};
FUNCTION CountChar(CONST s: STRING; CONST c: CHAR): TStrIndex;
VAR
i: TStrIndex;
BEGIN
RESULT := 0;
FOR i := 1 TO LENGTH(s) DO
IF s[i] = c
THEN INC(RESULT)
END {CountChar};
{ DeleteString is NOT like the original REXX DelStr function. DeleteString
deletes a given string, if present. To delete a word from a sentence, add
a blank to the beginning and end of the strings: e.g.,
DeleteString(' Bill ', ' Bill Preston Earl ')
}
FUNCTION DeleteString(CONST substring: STRING; CONST s: STRING): STRING;
VAR
i: TStrIndex;
t: STRING;
BEGIN
t := s;
i := POS(substring, t);
IF i > 0
THEN Delete(t, i, LENGTH(substring));
RESULT := t
END {DeleteString};
FUNCTION Overlay(CONST ovly,target: STRING; CONST n: TStrIndex): STRING;
VAR
i : TStrIndex;
sLength: TStrIndex;
t : STRING;
BEGIN
sLength := LENGTH(Target);
IF n+LENGTH(ovly)-1 > sLength
THEN sLength := n+LENGTH(ovly)-1;
t := Left(target,sLength);
FOR i := 1 TO LENGTH(ovly) DO
t[n+i-1] := ovly[i];
RESULT := t
END {Overlay};
FUNCTION Plural(CONST n: LongInt; CONST singularform,pluralform: STRING): STRING;
BEGIN {function similar to one on p. 314, Byte, December 1988}
IF n = 1
THEN RESULT := singularform
ELSE
IF pluralform = ''
THEN RESULT := singularform + 's'
ELSE RESULT := pluralform
END {Plural};
FUNCTION Reverse(CONST s: STRING): STRING;
VAR
i: TStrIndex;
j: TStrIndex;
t: STRING;
BEGIN
t := '';
j := LENGTH(s)+1;
FOR i := 1 TO LENGTH(s) DO
AppendStr(t, s[j-i]);
RESULT := t
END {Reverse};
FUNCTION Spacerexx(CONST s: STRING; CONST n: TStrIndex): STRING;
VAR
i : TStrIndex;
state: byte; //0..2; {state of finite state machine}
t : STRING;
BEGIN
t := '';
state := 0;
FOR i := 1 TO LENGTH(s) DO
CASE state OF
0: IF s[i] <> ' '
THEN BEGIN
state := 1;
AppendStr(t, s[i])
END;
1: IF s[i] = ' '
THEN state := 2
ELSE AppendStr(t, s[i]);
2: IF s[i] <> ' '
THEN BEGIN
state := 1;
AppendStr(t, Copies(' ',n) + s[i])
END
END;
RESULT := t
END {Space};
FUNCTION Strip(CONST s: STRING; CONST option: STRING): STRING;
VAR
c: CHAR;
i: TStrIndex;
t: STRING;
BEGIN
t := s;
IF LENGTH(option) > 0
THEN c := UpCase(option[1])
ELSE c := 'B';
IF (c <> 'L') AND (c <> 'T')
THEN c := 'B';
IF ((c = 'L') OR (c = 'B')) AND (LENGTH(t) > 0) {Leading or Both}
THEN BEGIN
i := 1;
WHILE (t[i] = ' ') AND (i <= LENGTH(t)) DO
i := i + 1;
t := COPY(t,i,LENGTH(t)+1-i)
END;
IF ((c = 'T') OR (c = 'B')) AND (LENGTH(t) > 0) {Trailing or Both}
THEN BEGIN
i := LENGTH(t);
WHILE (t[i] = ' ') AND (i >= 0) DO
i := i - 1;
t := COPY(t,1,i)
END;
RESULT := t
END {Strip};
FUNCTION TestString(CONST sLength: TStrIndex): STRING;
VAR
i: TStrIndex;
BEGIN
RESULT := '';
FOR i := 1 TO sLength DO
AppendStr(RESULT, CHR(48+Random(79)))
END {TestString};
FUNCTION Translate(CONST s,OutTable,InTable: STRING): STRING;
VAR
Flag : ARRAY[0..255] OF BOOLEAN;
i,j,n: TStrIndex;
t : STRING;
Table: ARRAY[0..255] OF CHAR;
BEGIN
FOR i := 0 TO 255 DO BEGIN
Table[i] := CHR(i);
Flag[i] := TRUE
END;
IF LENGTH(OutTable) < LENGTH(InTable)
THEN BEGIN
FOR i := LENGTH(OutTable) + 1 TO LENGTH(InTable) DO
Flag[ORD(InTable[i])] := FALSE;
n := LENGTH(OutTable)
END
ELSE n := LENGTH(InTable);
FOR i := 1 TO n DO
Table[ORD(InTable[i])] := OutTable[i];
t := '';
FOR i := 1 TO LENGTH(s) DO BEGIN
j := ORD(s[i]);
IF Flag[j]
THEN AppendStr(t, Table[j]);
END;
RESULT := t
END {Translate};
FUNCTION XRange (CONST start,stop: BYTE): STRING;
VAR
i: BYTE;
t: STRING;
BEGIN
t := '';
IF stop >= start
THEN BEGIN
FOR i := start TO stop DO
AppendStr(t, CHR(i))
END
ELSE BEGIN
FOR i := start TO $FF DO
t := t + CHR(i);
FOR i := $00 TO stop DO
AppendStr(t, CHR(i))
END;
RESULT := t
END {XRange};
{** Conversion FUNCTIONs *******************************************}
//CONST HexDigit: ARRAY[0..15] OF CHAR = '0123456789ABCDEF';
FUNCTION B2X(CONST b: BYTE): STRING; {byte-to-hexadecimal}
BEGIN
RESULT := HexDigits[b SHR 4] + HexDigits[b AND $0F]
END {B2X};
FUNCTION C2D(CONST s: STRING): DOUBLE; {character-to-double}
VAR
ConvertError: INTEGER;
x : DOUBLE;
BEGIN
//VAL (Strip(s,'T'), x, ConvertError);
IF ConvertError = 0
THEN RESULT := x
ELSE BEGIN
RAISE; //EConversionError.Create('REXX C2D Conversion Error');
RESULT := 0.0
END
END {C2D};
FUNCTION C2I(CONST s: STRING): INTEGER; {character-to-integer}
VAR
ConvertError: INTEGER;
i : LONGINT;
BEGIN
VAL (Strip(s,'T'), i, ConvertError);
IF (ConvertError = 0) AND (i >= -32768) AND (i <= 32767)
THEN RESULT := INTEGER(i)
ELSE BEGIN
IF ConvertError = 0
THEN RAISE //EConversionError.Create('REXX C2I Range Error')
ELSE RAISE; //EConversionError.Create('REXX C2I Conversion Error');
RESULT := 0
END
END {C2I};
FUNCTION C2L(CONST s: STRING): LONGINT; {character-to-LONGINT}
VAR
ConvertError: INTEGER;
i : LONGINT;
BEGIN
VAL (Strip(s,'T'), i, ConvertError);
IF ConvertError = 0
THEN RESULT := i
ELSE BEGIN
RAISE; //EConversionError.Create('REXX C2L Conversion Error');
RESULT := 0
END
END {C2I};
FUNCTION C2W(CONST s: STRING): WORD; {character-to-word}
VAR
ConvertError: INTEGER;
i : LONGINT;
BEGIN
VAL (Strip(s,'T'), i, ConvertError);
IF (ConvertError = 0) AND (i >= 0) AND (i <= 65535)
THEN RESULT := WORD(i)
ELSE BEGIN
IF ConvertError = 0
THEN RAISE //EConversionError.Create('REXX C2W Range Error')
ELSE RAISE; //EConversionError.Create('REXX C2W Conversion Error');
RESULT := 0
END
END {C2W};
FUNCTION C2X(CONST s: STRING): STRING; {character-to-hexadecimal}
VAR
i: BYTE;
t: STRING;
BEGIN
t := '';
FOR i := 1 TO LENGTH(s) DO
BEGIN
AppendStr(t, B2X( ord(s[i]) ));
END;
RESULT := t
END {C2X};
FUNCTION I2C(CONST i: INTEGER): STRING; {integer-to-character}
{I2C and W2C are replacements for the standard D2C (decimal-to-
character) function. However, the standard D2C works like the
Pascal CHR function. Here, I2C is written to be the inverse
of the C2I function.}
VAR
t: STRING;
BEGIN
STR (i,t); {STR is Turbo Pascal Procedure}
RESULT := t
END {I2C};
FUNCTION I2X(CONST i: INTEGER): STRING; {integer-to-hexadecimal}
BEGIN
RESULT := B2X(Hi(i)) + B2X(Lo(i))
END {I2X};
FUNCTION L2C(CONST i: LONGINT): STRING; {LONGINT-to-character}
VAR
t: STRING;
BEGIN
STR (i,t); {STR is Turbo Pascal Procedure}
RESULT := t
END {L2C};
FUNCTION L2X(CONST i: LONGINT): STRING; {LONGINT-to-hexadecimal}
{VAR
ovly:
RECORD
CASE INTEGER OF
0: (j: LONGINT);
1: (w: ARRAY[1..2] OF WORD)
END;}
{ RECORD
CASE INTEGER OF
0: (j: LONGINT);
1: (w: ARRAY[1..2] OF WORD)
END;}
BEGIN
//ovly.j := i;
//RESULT := W2X(ovly.w[1]) + W2X(ovly.w[2])
END {L2X};
FUNCTION D2C(CONST x: DOUBLE; CONST d: BYTE): STRING;
VAR
t: STRING;
BEGIN
//STR(x:30:d,t); {'d' digits after decimal point}
RESULT := Strip(t,'L')
END {R2C};
FUNCTION W2C(CONST w: WORD): STRING; {word-to-character}
{W2C and I2C are replacements for the standard D2C (decimal-to-
character) function. However, the standard D2C works like the
Pascal CHR function. Here, W2C is written to be the inverse
of the C2W function.}
VAR
t: STRING;
BEGIN
STR (w,t); {STR is Turbo Pascal Procedure}
RESULT := t
END {W2C};
FUNCTION W2X(CONST w: WORD): STRING; {word-to-hexadecimal}
BEGIN
RESULT := B2X(Hi(w)) + B2X(Lo(w))
END {W2X};
FUNCTION X2W(CONST s: STRING): WORD; {hex string to Pascal WORD}
//CONST HexDigits: STRING = '0123456789ABCDEF';
VAR
i : WORD;
digit: WORD;
sum : WORD;
power: WORD;
BEGIN
power := 1;
sum := 0;
FOR i := 1 TO LENGTH(s) DO BEGIN
digit := POS(s[LENGTH(s)+1-i],HexDigits) - 1;
sum := sum + digit*power;
power := 16*power;
END;
RESULT := sum
END {X2W};
{** Date/Time FUNCTIONS ********************************************}
FUNCTION JulianDate(CONST DateTime: TDateTime): LongInt;
VAR
day : WORD;
month : WORD;
NewYears: TDateTime;
Start : LongInt;
Stop : LongInt;
year : WORD;
BEGIN
DecodeDate (DateTime, year, month, day);
NewYears := EncodeDate (year, 1, 1);
Stop := Trunc(DateTime);
Start := Trunc(NewYears);
RESULT := Stop - Start + 1;
END {JulianDate};
FUNCTION TimeDifference(CONST StartTime, StopTime: TDateTime): DOUBLE;
BEGIN
RESULT := 86400.0*(StopTime - StartTime); {Seconds}
END {TimeDifference};
{** Math FUNCTION *************************************************}
FUNCTION Pwr(CONST x,y: DOUBLE): DOUBLE; {x^y or x**y -- x raised to power y}
BEGIN
IF (x = 0.0) AND (y = 0.0)
THEN RESULT := 1
ELSE RESULT := EXP( y * LN(x) ) {LN(x) is undefined for x <= 0.0}
END {Pwr}; {a run-time error will occur}
INITIALIZATION
END.