-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUTILS.PAS
218 lines (185 loc) · 5.22 KB
/
UTILS.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
{
Utils Unit
Stuff that doesn't fit anywhere else: type conversions, compare functions
2022 LRT
}
unit
utils;
interface
uses
consts, types, locale;
{ basic data types to string }
function boolToStr(b: boolean): string5;
function longToStr(i: longint): string11;
function intToStr(i: integer): string6;
{ numbers to hexadecimal string }
function byteToHex(i: byte): string2;
function wordToHex(i: word): string4;
function longToHex(i: longint): string8;
function hexToByte(s: string2): byte;
function hexToWord(s: string4): word;
function hexToLong(s: string8): longint;
{ pointer to string in segment:offset format }
function ptrToStr(p: pointer): string9;
{ string to number }
function strToInt(s: string): integer;
{ comparison functions }
function compareBuffers(a, b: pchar; size: word): ECompareResult;
function compareLong(a, b: pointer): ECompareResult;
function compareStr(a, b: pointer): ECompareResult;
{ file path parsing }
function getFilename(path: string; extension: boolean): string;
{ converts record types to readable strings}
function sizeToStr(size: PSize): string;
function pointToStr(point: PPoint): string;
function frameToStr(frame: PFrame): string;
function isPointInFrame(p: PPoint; f: PFrame): boolean;
function lowcase(ch: char): char;
implementation
const
C_HEX_DIGITS: array [0..15] of char = '0123456789ABCDEF';
function boolToStr(b: boolean): string5;
begin
if b then
boolToStr := S_TRUE
else
boolToStr := S_FALSE;
end;
function longToStr(i: longint): string11;
var s: string;
begin
Str(i, s);
LongToStr := s;
end;
function intToStr(i: integer): string6;
var
s: string6;
begin
Str(i, s);
IntToStr := s;
end;
function byteToHex(i: byte): string2;
begin
byteToHex := C_HEX_DIGITS[i shr 4] + C_HEX_DIGITS[i and $0F];
end;
function wordToHex(i: word): string4;
begin
wordToHex := ByteToHex(hi(i)) + ByteToHex(lo(i));
end;
function longToHex(i: longint): string8;
begin
LongToHex := ByteToHex(i shr 24) +
ByteToHex((i and $00FF0000) shr 16) +
ByteToHex((i and $0000FF00) shr 8) +
ByteToHex(i and $000000FF);
end;
function hexToByte(s: string2): byte;
begin
hexToByte := hexToLong(s) and $FF;
end;
function hexToWord(s: string4): word;
begin
hexToWord := hexToLong(s) and $FFFF;
end;
function hexToLong(s: string8): longint;
var
i: byte;
c: char;
v: longint;
begin
v := 0;
for i := 1 to length(s) do
begin
v := v shl 4;
c := upcase(s[i]);
if (c >= '0') and (c <= '9') then
v := v or (ord(c) - ord('0'))
else if (c >= 'A') and (c <= 'F') then
v := v or (10 + ord(c) - ord('A'));
end;
hexToLong := v;
end;
function ptrToStr(p: pointer): string9;
begin
if p = nil then
ptrToStr := 'nil'
else
ptrToStr := WordToHex(Seg(p^)) + ':' + WordToHex(Ofs(p^));
end;
function strToInt(s: string): integer;
var i, c: integer;
begin
Val(s, i, c);
StrToInt := i;
end;
function compareBuffers(a, b: pchar; size: word): ECompareResult;
begin
while (size>0) and (a^=b^) do
begin
inc(a);
inc(b);
dec(size);
end;
if size=0 then
compareBuffers := ECompareEqual
else
if a^<b^ then
compareBuffers := ECompareLesser
else
compareBuffers := ECompareGreater;
end;
function compareLong(a, b: pointer): ECompareResult;
begin
if plong(a)^ > plong(b)^ then compareLong := ECompareGreater else
if plong(a)^ < plong(b)^ then compareLong := ECompareLesser else
compareLong := ECompareEqual;
end;
function compareStr(a, b: pointer): ECompareResult;
begin
if pstr(a)^ > pstr(b)^ then compareStr := ECompareGreater else
if pstr(a)^ < pstr(b)^ then compareStr := ECompareLesser else
compareStr := ECompareEqual;
end;
function getFilename(path: string; extension: boolean): string;
var
i: byte;
s: string;
begin
i := length(path);
if not extension then dec(i, 4);
s := '';
while path[i] <> C_FILE_PATH_SEPARATOR do
begin
s := path[i] + s;
dec(i);
end;
getFilename := s;
end;
function sizeToStr(size: PSize): string;
begin
with size^ do
sizeToStr := 'W:' + inttostr(width) + ', H:' + inttostr(height);
end;
function pointToStr(point: PPoint): string;
begin
with point^ do
pointToStr := 'X:' + inttostr(x) + ', Y:' + inttostr(y);
end;
function frameToStr(frame: PFrame): string;
begin
with frame^ do
frameToStr := pointToStr(@point) + ', ' + sizeToStr(@size);
end;
function isPointInFrame(p: PPoint; f: PFrame): boolean;
begin
with f^, point, size do
isPointInFrame := (p^.x >= x) and (p^.x <= (x+width)) and (p^.y >= y) and (p^.y <= (y+height));
end;
function lowcase(ch: char): char;
begin
if ch in ['A'..'Z'] then
lowcase := chr(ord(ch) + 32)
else
lowcase := ch;
end;
end.