-
Notifications
You must be signed in to change notification settings - Fork 0
/
monstring.pas
318 lines (260 loc) · 6.27 KB
/
monstring.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
[inherit ('monconst','montype','sys$library:starlet')]
MODULE Monstring(OUTPUT);
%include 'headers.txt'
TYPE
$UBYTE = [BYTE] 0..255;
(* -------------------------------------------------------------------------- *)
[asynchronous,external(str$trim)]
function str$trim(
VAR destination_string:[class_s,VOLATILE] PACKED ARRAY [$l1..$u1:integer] of char;
source_string : [CLASS_S] PACKED ARRAY [$l2..$u2:integer] OF CHAR;
VAR resultant_length : [VOLATILE] $UWORD) : INTEGER;
external;
[asynchronous,external(lib$signal)]
function lib$signal (%ref status:[unsafe] unsigned):unsigned;
external;
[ASYNCHRONOUS] function lib$getjpi (
item_code : INTEGER;
VAR process_id : [VOLATILE] UNSIGNED := %IMMED 0;
process_name : [CLASS_S] PACKED ARRAY [$l3..$u3:INTEGER] OF CHAR := %IMMED 0;
%REF resultant_value : [VOLATILE,UNSAFE] ARRAY [$l4..$u4:INTEGER] OF $UBYTE := %IMMED 0;
VAR resultant_string : [CLASS_S] PACKED ARRAY [$l5..$u5:INTEGER] OF CHAR := %IMMED 0;
VAR resultant_length : [VOLATILE] $UWORD := %IMMED 0) : INTEGER; EXTERNAL;
(* -------------------------------------------------------------------------- *)
[ASYNCHRONOUS]
PROCEDURE SysCall(S : [UNSAFE] Unsigned);
BEGIN
IF NOT ODD(S) THEN
Lib$Signal(s);
END;
(* ------------------------------------------------------------------------- *)
[GLOBAL]
FUNCTION Spaces(num : INTEGER := 0) : String;
VAR
i : INTEGER;
st : String;
BEGIN
st := '';
FOR i := 0 TO num DO
st := st + ' ';
Spaces := st;
END;
[ASYNCHRONOUS]
FUNCTION Slead(S : String) : String;
(* take off leading spaces and tabs *)
VAR
I : INTEGER;
Going : BYTE_BOOL;
BEGIN
I := 1;
Going := TRUE;
WHILE Going DO
BEGIN
IF I > Length(S) THEN
Going := FALSE
ELSE
IF (S[I]=' ') OR (S[I]=chr(9)) THEN
I := I + 1
ELSE
Going := FALSE;
END;
IF I > Length(S) THEN
Slead := ''
ELSE
Slead := SubStr(S, I, Length(S)+1-I);
END;
[global]
procedure mytrim(var s : string);
var
loop, cc, endd, indx : integer;
begin
indx := 1;
endd := s.length;
while (((s[indx] = ' ') or (s[indx] = chr(9))) and (indx < s.length)) do
indx := indx + 1;
while (((s[endd] = ' ') or (s[endd] = chr(9))) and (endd > indx)) do
endd := endd - 1;
if (indx = 1) then
s.length := endd
else
begin
cc := 1;
for loop := indx to endd do
begin
s[cc] := s[loop];
cc := cc + 1;
end;
s.length := endd-indx+1;
end;
end;
[GLOBAL]
FUNCTION Trim(S : String) : String;
(* Take off leading/trailing spaces/tabs *)
VAR
Tmp : String := '';
BEGIN
S := Slead(S);
SysCall(STR$Trim(Tmp.Body, S, Tmp.Length));
Trim := Tmp;
END;
[GLOBAL]
FUNCTION Userid : String;
VAR
Id : PACKED ARRAY[1..100] OF Char;
Length : $UWORD;
BEGIN
SysCall(LIB$GetJpi(JPI$_USERNAME, , , , Id, Length));
Userid := Trim(SubStr(Id, 1, Length));
END;
[GLOBAL]
FUNCTION Bite(VAR S : String) : String;
(* Return the first word (space terminated) in bite, and the rest of the *)
(* string in S *)
VAR
I : INTEGER := 0;
BEGIN
IF Length(S) <> 0 THEN
I := Index(S,' ');
CASE I OF
0 : BEGIN
Bite := S;
S := '';
END;
OTHERWISE BEGIN
Bite := SubStr(S, 1, I-1);
S := Trim(SubStr(S, I+1, Length(S)-I));
END;
END;
END;
[GLOBAL]
FUNCTION LowCase(S : String) : String;
CONST
Diff = 32; (* The diff in ascii value between 'a' and 'A' *)
VAR
Sprime : String;
Index : INTEGER;
BEGIN
Sprime := S;
FOR Index := 1 to LENGTH(S) DO
IF Sprime[Index] IN ['A'..'Z'] THEN
Sprime[Index] := CHR(ORD(Sprime[Index])+Diff);
LowCase := Sprime;
END;
[GLOBAL]
FUNCTION UpCase(S : String) : String;
CONST
Diff = 32; (* The diff in ascii value between 'a' and 'A' *)
VAR
Sprime : String;
Index : INTEGER;
BEGIN
Sprime := S;
FOR Index := 1 to LENGTH(S) DO
IF Sprime[Index] IN ['a'..'z'] THEN
Sprime[Index] := CHR(ORD(Sprime[Index])-Diff);
UpCase := Sprime;
END;
[GLOBAL]
FUNCTION IsValid(S : String; SType : INTEGER := vt_alphanum) : BYTE_BOOL;
VAR
i : INTEGER;
BEGIN
IsValid := TRUE;
FOR i := 1 TO Length(S) DO
BEGIN
CASE SType OF
(* vt_alphanum:
vt_numeric: *)
vt_alpha:
IF NOT( (S[i] IN ['a'..'z']) OR
(S[i] IN ['A'..'Z']) OR
(S[i] = ' ') ) THEN
IsValid := FALSE;
END; (* Case *)
END; (* FOR *)
END; (* Is Valid *)
[GLOBAL]
FUNCTION IsNum(S : String) : BYTE_BOOL;
VAR
Temp : INTEGER;
BEGIN
Readv(S, Temp, ERROR := CONTINUE);
IsNum := StatusV = 0;
END;
[GLOBAL]
FUNCTION Number(S : String) : INTEGER;
(* Assumption : S is a valid integer. Example - (1234) or (-121) *)
VAR
THENumber : INTEGER;
BEGIN
Readv(S, THENumber);
Number := THENumber;
END;
[GLOBAL]
FUNCTION SysTime : String;
VAR
HourString : String;
Hours : INTEGER;
TheTime : PACKED ARRAY[1..11] OF CHAR;
DayOrNite : String;
BEGIN
Time(TheTime);
IF TheTime[1] = ' ' THEN
Hours := ORD(TheTime[2]) - ORD('0')
ELSE
Hours := (ORD(TheTime[1]) - ORD('0'))*10 + (ORD(TheTime[2]) -ORD('0'));
IF Hours < 12 THEN
DayOrNite := 'am'
ELSE
DayOrNite := 'pm';
IF Hours >= 13 THEN
Hours := Hours - 12;
IF Hours = 0 THEN
Hours := 12;
Writev(HourString, Hours:2);
SysTime := HourString + ':' + TheTime[4] + TheTime[5] + DayOrNite;
END;
[GLOBAL]
FUNCTION SysDate : String;
VAR
TheDate : PACKED ARRAY[1..11] OF CHAR;
BEGIN
Date(TheDate);
SysDate := TheDate;
END;
[GLOBAL]
FUNCTION Styler(S : String; Style : INTEGER := 0) : String;
BEGIN
CASE Style OF
Style_Normal:
Styler := S;
Style_Capitalized:
Styler := Capitalize(S); { currently unsupported }
Style_AllCaps:
Styler := UpCase(S);
Style_AllLower:
Styler := LowCase(S);
OTHERWISE Styler := '%Styler: Unknown Style';
END;
END;
[GLOBAL]
FUNCTION Capitalize(S : String; AllWords : BYTE_BOOL := TRUE) : String;
(* Will either capitalize the first word in S or all of the words in S *)
CONST
Diff = 32; (* The diff in ascii value between 'a' and 'A' *)
VAR
Sprime : String;
Index : INTEGER;
First : BYTE_BOOL;
BEGIN
First := FALSE;
Sprime := S;
FOR Index := 1 to LENGTH(S) DO
IF (Sprime[Index] IN ['a'..'z']) AND (First = FALSE) THEN
BEGIN
Sprime[Index] := CHR(ORD(Sprime[Index])-Diff);
First := TRUE;
END;
Capitalize := Sprime;
END;
END.