forked from MarkHofmann11/WWIVEdit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WESTRING.PAS
285 lines (252 loc) · 5.75 KB
/
WESTRING.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
UNIT WEString;
{$I WEGLOBAL.PAS}
{ -- This is the Low Level Strings unit of WWIVEdit 2.2
-- Last Updated : 3/22/92
-- Written By:
-- Adam Caldwell
--
-- This code is Public Domain
--
-- Purpose : Provide Low level string manipulation routines
--
-- Know Errors: None
--
-- Planned Enhancements : Improvement of Go_Left and Go_Right
--
-- }
INTERFACE
FUNCTION cstr(i:longint):string; { Converts a number to a string }
FUNCTION ZExpand(i:LongInt; { Converts an integer into a string and }
len : byte):string;{ and adds enough zeros at the beginning }
{ to make is LEN characters long }
FUNCTION Dup(c:char; n:integer) { Returns a string of length "n" full of }
: string; { character "c" }
FUNCTION value(I : string) : integer;
FUNCTION TransformString(s:string):string;
PROCEDURE StripEndString(VAR s:string);
FUNCTION StripEndS(s:string):string;
FUNCTION GoLeft(s:string; i:byte):byte;
FUNCTION GoRight(s:string; i:byte):byte;
FUNCTION LengthW(s:string):integer;
FUNCTION LeftS(s:string; n:integer):string; {Returns the Left N characters }
FUNCTION RightS(s:string; n:integer):string;{Returns the Right N characters }
FUNCTION CmpLeft(source, Target:string):boolean;
FUNCTION CmpLeftI(source,target:string):boolean;
FUNCTION ToLower(s:string):String;
FUNCTION LJustify(s:string; width:integer):string;
FUNCTION Fname(s:string):string;
PROCEDURE StripEndC(VAR s:string; ch:char);
FUNCTION HexStr(n:LongInt; Len:Byte):string;
FUNCTION HexVal(s:string):longint;
FUNCTION WCopy(s:string; len:integer):string;
IMPLEMENTATION
FUNCTION Fname(s:string):string;
VAR
p:byte;
BEGIN
p:=pos('.',s);
IF p>0 THEN
Fname:=copy(s,1,pos('.',s)-1)
ELSE
Fname:=s;
END;
FUNCTION LJustify(s:string; width:integer):string;
VAR
t:string;
i:integer;
BEGIN
t:=dup(#32,width);
FOR i:=1 TO length(s) DO
t[i]:=s[i];
LJustify:=t;
END;
FUNCTION CmpLeft(source,target:string):boolean;
BEGIN
CmpLeft:=LeftS(source,Length(target))=target;
END;
FUNCTION CmpLeftI(source,target:string):boolean;
BEGIN
CmpLeftI:=CmpLeft(TransformString(source),TransformString(target));
END;
FUNCTION LeftS(s:string; n:integer):string;
BEGIN
LeftS:=copy(s,1,n);
END;
FUNCTION RightS(s:string; n:integer):string;
BEGIN
RightS:=copy(s,length(s)-n+1,n);
END;
FUNCTION value(I : string) : integer;
VAR n,n1 : integer;
BEGIN
val(i,n,n1);
IF n1 <> 0 THEN BEGIN
i := copy(i,1,n1-1);
val(i,n,n1)
END;
value := n;
END;
FUNCTION TransformString(s:string):string;
VAR x:integer;
BEGIN
StripEndString(s);
FOR x:=1 TO Length(s) DO
s[x]:=upcase(s[x]);
TransformString:=s;
END;
FUNCTION ToLower(s:string):String;
VAR x:integer;
BEGIN
FOR x:=1 TO Length(s) DO
IF s[x] IN ['A'..'Z'] THEN
s[x]:=chr(ord(s[x])+32);
ToLower:=s;
END;
PROCEDURE StripEndString(VAR s:string);
{ Strips off the end of a string }
BEGIN
WHILE (length(s)>0) AND (s[length(s)]=' ') DO
delete(s,length(s),1);
END;
FUNCTION StripEndS(s:string):string;
{ Strips off the end of a string }
BEGIN
WHILE (length(s)>0) AND (s[length(s)]=' ') DO
delete(s,length(s),1);
StripEndS:=s;
END;
FUNCTION cstr(i : longint) : string;
VAR c : string;
BEGIN
str(i,c);
cstr := c;
END;
FUNCTION ZExpand(i:LongInt; len:byte):string;
VAR
negative:boolean;
result:string;
x:byte;
BEGIN
Negative:=i<0;
IF negative then i:=-i;
result:=cstr(i);
FOR x:=length(result) TO len-1 DO
result:='0'+result;
IF negative THEN result[1]:='-';
ZExpand:=Result
END;
FUNCTION Dup(c:char; n:integer):string;
VAR
s:string;
BEGIN
IF n IN [1..255] THEN
BEGIN
FillChar(s[1],n,c);
s[0]:=chr(n);
Dup:=s;
END ELSE dup:='';
END;
FUNCTION GoLeft(s:string; i:byte):byte;
{ A simple word left function... needs improvement }
BEGIN
IF i>length(s) THEN i:=length(s);
IF i>2 THEN
REPEAT
dec(i);
UNTIL (i=1) OR (s[i-1] = ' ')
ELSE i:=1;
GoLeft:=i;
END;
FUNCTION GoRight(s:string; i:byte):byte;
{ A very simple Word Right function... Could be improved }
BEGIN
IF i<=length(s) THEN
REPEAT
inc(i)
UNTIL (i=length(s)) OR (s[i] =' ');
GoRight:=i+1
END;
FUNCTION LengthW(s:string):integer;
VAR l,i:integer;
BEGIN
l:=0;
FOR i:=1 TO Length(s) DO
BEGIN
l:=l+1;
IF s[i]=^C THEN l:=l-2
ELSE IF s[i]=^H THEN l:=l-2
END;
Lengthw:=l;
END;
FUNCTION WCopy(s:string; len:integer):string;
VAR
i:byte;
cc:char;
BEGIN
i:=1;
cc:='0';
WHILE ((len>1) AND (i<=length(s))) DO
BEGIN
IF s[i] IN [^C,^H] THEN inc(len,2);
IF s[i]=^C THEN cc:=s[i+1];
dec(len);
inc(i);
END;
s:=copy(s,1,i);
IF cc<>'0' THEN s:=s+^C'0';
WCopy:=s
END;
PROCEDURE StripEndC(VAR s:string; ch:char);
VAR
i:integer;
BEGIN
i:=length(s);
WHILE (i>1) AND (s[i]<>ch) DO
dec(i);
Delete(s,i,length(s)-i+1);
END;
FUNCTION HexStr(n:LongInt; Len:Byte):string;
CONST
HexDigit : ARRAY[0..15] OF CHAR =
('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
VAR
s:string;
zero:string;
done:boolean;
BEGIN
s:='';
Done:=false;
WHILE NOT Done DO
BEGIN
s:=Hexdigit[n mod 16]+s;
n:=(n-(n mod 16)) div 16;
done:=n=0;
END;
IF len=0 THEN zero:='' ELSE BEGIN
fillchar(zero[1],len-Length(S),'0');
zero[0]:=chr(len-Length(s));
END;
HexStr:=Zero+s;
END;
FUNCTION HexVal(s:string):LongInt;
VAR
result:LongInt;
dig : byte;
x:byte;
BEGIN
result:=0;
FOR x:=1 TO length(s) DO
BEGIN
IF (s[x]>='0') AND (s[x]<='9') THEN
dig := ord(s[x])-ord('0')
ELSE IF (upcase(s[x])>='A') AND (upcase(s[x])<='F') THEN
dig := ord(upcase(s[x])) - ord('A') + 10
ELSE BEGIN
writeln('Hex Error!');
halt;
END;
result:=result * 16 + dig;
END;
HexVal:=Result;
END;
END.