-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIUTILS.PAS
More file actions
776 lines (536 loc) · 18.3 KB
/
IUTILS.PAS
File metadata and controls
776 lines (536 loc) · 18.3 KB
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
unit IUTILS;
(* Information
ßßßßßßßßßßß
Program Title : NASM-IDE General System Components
External name : IUTILS.TPU
Version : 1.3
Start date : 08/06/1998
Last update : 08/06/1999
Author : Rob Anderton
Description : General system components.
Incorporates most of the procedures/functions found in
SUTILS.PAS from the S-Library.
*)
interface
uses DOS, OBJECTS;
{*** Data types ***}
{*** TListBoxRec - used to set/get data with dialog boxes containing a
TListBox descendant. ***}
type PListBoxRec = ^TListBoxRec;
TListBoxRec = record
pList : PCollection;
wSelection : word;
end;
{*** Useful data types (for typecasting etc.) ***}
type PByteArray = ^TByteArray;
TByteArray = array[0..65534] of byte;
PCharArray = ^TCharArray;
TCharArray = array[0..65534] of char;
PWordArray = ^TWordArray;
TWordArray = array[0..32766] of word;
{*** Equivalent to a QWORD or COMP data type ***}
PQuadRec = ^TQuadRec;
TQuadRec = record
Lo : longint;
Hi : longint;
end;
{*** Procedures ***}
procedure RegisterAll;
function HexToDec(sHex : string) : longint;
function ByteToHex(bVal : byte;
bLeadingZero : boolean) : string;
function WordToHex(wVal : word;
bLeadingZero : boolean) : string;
function LongToHex(lVal : longint;
bLeadingZero : boolean) : string;
function IntToStr(lVal : longint): string;
function StrUpper(sText : string) : string;
function StrLower(sText : string) : string;
function LoCase(cUpper : char) : char;
function GetLinearAddr(pPtr : pointer) : longint;
function NormalisePtr(pPtr : pointer) : pointer;
function Min(lValA, lValB : longint) : longint;
function Max(lValA, lValB : longint) : longint;
procedure Swap(var lValA, lValB : longint);
function Power(lN, lP : longint) : longint;
function InpLong(wReg : word) : longint;
procedure OutpLong(wReg : word; lVal : longint);
function GetLongTime : longint;
{*** File management ***}
function FileExists(sFilename : string) : boolean;
function IsDir(const sPath : string) : boolean;
function IsWild(const sPath : string) : boolean;
function GetCurDir : DirStr;
function DriveValid(cDrive : char) : boolean;
function PathValid(var sPath : PathStr) : boolean;
function ValidFileName(var sFileName : PathStr) : boolean;
{******}
implementation
uses APP, DIALOGS, MENUS, VIEWS, ICONTROL;
{******}
(* RegisterAll - registers all existing and new NASM-IDE objects for
stream I/O.
*)
procedure RegisterAll;
begin
{*** Register standard Turbo Vision objects ***}
RegisterApp;
RegisterDialogs;
RegisterMenus;
RegisterViews;
RegisterType(RCollection);
{*** ICONTROL unit ***}
RegisterControls;
end;
{*** Following functions formerly in SUTILS.PAS ***}
(* HexToDec - converts a string of hexadecimal characters into a decimal
representation.
sHex - the hexadecimal string to convert.
Returns a longint containing the decimal value, conversion
is stopped when a non-hexadecimal character is detected, and
the current working value is returned.
*)
function HexToDec(sHex : string) : longint;
var NumChars : integer; {Number of characters processed}
Value : longint; {Temporary value of conversion}
i : word; {Loop control variable}
begin
{*** Initialise variables ***}
NumChars:= 0;
Value:= 0;
{*** Only process non-empty strings ***}
if sHex <> '' then
begin
{*** Skip any leading spaces ***}
i:= 1;
while (sHex[i] = ' ') and (i <= Length(sHex)) do Inc(i);
{*** Process remaining characters ***}
while i <= Length(sHex) do
begin
case UpCase(sHex[i]) of
'0'..'9' : begin
{*** Convert value ***}
Value:= Value * $10;
Value:= Value + INTEGER(sHex[i]) -
INTEGER('0');
NumChars:= Succ(NumChars);
end;
'A'..'F' : begin
{*** Convert value ***}
Value:= Value * $10;
Value:= Value + INTEGER(UpCase(sHex[i])) -
INTEGER('A') + 10;
NumChars:= Succ(NumChars);
end;
else begin
{*** Stop processing when invalid
character detected ***}
if NumChars <> 0 then HexToDec:= Value
else HexToDec:= 0;
Exit;
end;
end;
Inc(i);
end;
{*** Return converted value ***}
HexToDec:= Value;
end
else HexToDec:= 0; {Return zero if sHex is empty}
end;
(* ByteToHex - converts a byte value into a string of hexadecimal
characters.
bVal - the decimal value to convert
bLeadingZero - if true the returned string is padded
with zeros to always make the string
have a length of 2 characters
Returns the converted string.
*)
function ByteToHex(bVal : byte; bLeadingZero : boolean) : string;
{*** Hexadecimal characters ***}
const HexChars : array [0..$F] of char = '0123456789ABCDEF';
var HexStr : string; {Temporary string}
begin
{*** Convert to string ***}
HexStr:= (HexChars[bVal shr 4] + HexChars[bVal and $F]);
{*** Remove leading zero ***}
if not bLeadingZero then
if HexStr[1] = '0' then
begin
HexStr[1]:= HexStr[2];
HexStr[0]:= Chr(1);
end;
{*** Return string ***}
ByteToHex:= HexStr;
end;
(* WordToHex - converts a word value into a string of hexadecimal
characters.
wVal - the decimal value to convert
bLeadingZero - if true the returned string is padded
with zeros to always make the string
have a length of 4 characters
Returns the converted string.
*)
function WordToHex(wVal : word; bLeadingZero : boolean) : string;
{*** Hexadecimal characters ***}
const HexChars : array [0..$F] of char = '0123456789ABCDEF';
var HexStr : string; {Temporary string}
i, j : byte; {Loop control variables}
begin
{*** Convert to string ***}
HexStr:= (HexChars[Hi(wVal) shr 4] + HexChars[Hi(wVal) and $F] +
HexChars[Lo(wVal) shr 4] + HexChars[Lo(wVal) and $F]);
{*** Remove leading zeros ***}
if not bLeadingZero then
begin
i:= 1;
{*** Find position of first non-zero character ***}
while HexStr[i] = '0' do Inc(i);
{*** Remove zeros if necessary ***}
if i > 1 then
begin
Move(HexStr[i], HexStr[1], 5 - i);
HexStr[0]:= Chr(5 - i);
end;
end;
{*** Return string ***}
WordToHex:= HexStr;
end;
(* LongToHex - converts a longint value into a string of hexadecimal
characters.
lVal - the decimal value to convert
bLeadingZero - if true the returned string is padded
with zeros to always make the string
have a length of 8 characters
Returns the converted string.
*)
function LongToHex(lVal : longint; bLeadingZero : boolean) : string;
{*** Hexadecimal characters ***}
const HexChars : array [0..$F] of char = '0123456789ABCDEF';
var HexStr : string; {Temporary string}
w1, w2 : word; {Store high and low words of DWORD}
i : byte; {Loop control variable}
begin
{*** Get high and low words ***}
w1:= WORD(lVal);
w2:= WORD(lVal shr 16);
{*** Convert to string ***}
HexStr:= (HexChars[Hi(w2) shr 4] + HexChars[Hi(w2) and $F] +
HexChars[Lo(w2) shr 4] + HexChars[Lo(w2) and $F]) +
(HexChars[Hi(w1) shr 4] + HexChars[Hi(w1) and $F] +
HexChars[Lo(w1) shr 4] + HexChars[Lo(w1) and $F]);
{*** Remove leading zeros ***}
if not bLeadingZero then
begin
i:= 1;
{*** Find position of first non-zero character ***}
while HexStr[i] = '0' do Inc(i);
{*** Remove zeros if necessary ***}
if i > 1 then
begin
Move(HexStr[i], HexStr[1], 9 - i);
HexStr[0]:= Chr(9 - i);
end;
end;
{*** Return string ***}
LongToHex:= HexStr;
end;
(* IntToStr - converts a decimal value into a string. This is a functional
wrapper for the STR procecedure.
lVal - the number to convert to a string.
Returns a string containing the converted value.
*)
function IntToStr(lVal : longint): string;
var S : string[11]; {Temporary string for conversion}
begin
{*** Call standard STR procedure ***}
Str(lVal, S);
{*** Return string ***}
IntToStr:= S;
end;
(* StrUpper - converts an entire string to upper case.
sText - the string to convert.
Returns the upper case string.
*)
function StrUpper(sText : string) : string;
var Loop : byte; {Used to process characters in a string}
Temp : string; {Holds converted string}
begin
{*** Set length ***}
Temp[0]:= sText[0];
{*** Convert individual characters to upper case ***}
for Loop:= 1 to Length(sText) do Temp[Loop]:= Upcase(sText[Loop]);
{*** Return the converted string ***}
StrUpper:= Temp;
end;
(* StrLower - converts an entire string to lower case.
sText - the string to be converted.
Returns the converted string.
*)
function StrLower(sText : string) : string;
var Loop : byte; {Used to process individual characters}
Temp : string; {Holds the converted string}
begin
{*** Set string length ***}
Temp[0]:= sText[0];
{*** Convert individual characters to lower case ***}
for Loop:= 1 to Length(sText) do Temp[Loop]:= LoCase(sText[Loop]);
{*** Return the converted string ***}
StrLower:= Temp;
end;
(* LoCase - converts an alphabetical character to lower case. Does not
alter lower case, numeric or other characters.
cUpper - the character to process.
Returns the converted character.
*)
function LoCase(cUpper : char) : char;
begin
{*** If cUpper is an upper case letter then convert ***}
if cUpper in ['A'..'Z'] then LoCase:= Chr(Ord(cUpper) + 32)
else LoCase:= cUpper; {Otherwise do nothing}
end;
(* GetLinearAddr - converts a segment:offset pointer into a 32 bit linear
address.
pPtr - the pointer to convert.
Returns the 32 bit linear address.
*)
function GetLinearAddr(pPtr : pointer) : longint;
begin
{*** Convert pointer and return ***}
GetLinearAddr:= LONGINT(Seg(pPtr^)) shl 4 + LONGINT(Ofs(pPtr^));
end;
(* NormalisePtr - normalises a pointer so that the offset address is always
between 0 and 15.
pPtr - the pointer to normalise.
Returns the normalised pointer.
*)
function NormalisePtr(pPtr : pointer) : pointer;
var LinearAddr : longint; {Holds the 32 bit linear address of pPtr}
begin
{*** Convert pointer to a linear address ***}
LinearAddr:= GetLinearAddr(pPtr);
{*** Normalise and return the pointer ***}
NormalisePtr:= Ptr(LinearAddr div 16, LinearAddr mod 16);
end;
(* Min - compares two values and returns the lowest.
If lValA and lValB are equal then lValB is returned.
lValA, lValB - the two numbers to be compared.
Returns the minimum value of the two.
*)
function Min(lValA, lValB : longint) : longint;
begin
if lValA < lValB then Min:= lValA
else Min:= lValB;
end;
(* Max - compares two values and returns the highest.
If lValA and lValB are equal then lValB is returned.
lValA, lValB - the two numbers to be compared.
Returns the maximum value of the two.
*)
function Max(lValA, lValB : longint) : longint;
begin
if lValA > lValB then Max:= lValA
else Max:= lValB;
end;
(* Swap - swaps the values of two numbers, so that lValA becomes the value
of lValB and lValB takes on the value of lValA.
lValA, lValB - the two numbers to be swapped.
*)
procedure Swap(var lValA, lValB : longint);
var t : longint; {Temporary store for lValA}
begin
t:= lValA; {Store lValA}
lValA:= lValB; {Set lValA to lValB}
lValB:= t; {Set lValB to the original value of lValA}
end;
(* Power - raises lN to the power lP.
For example: lN = 2, lP = 3, returns 2^3 = 8.
lN - the number to be raised to the power.
lP - the power.
Returns lN ^ lP.
*)
function Power(lN, lP : longint) : longint;
begin
{*** if lP = 0 then lN ^ lP is always 1 ***}
if lP = 0 then Power:= 1
{*** if lN is 0 then lN ^ lP is always 0 ***}
else if lN = 0 then Power:= 0
{*** Otherwise recursively call Power ***}
else Power:= lN * Power(lN, (lP - 1))
end;
(* InpLong - inputs a 32 bit value from the specified register. This is
equivalent to a single IN EAX, DX instruction and requires
a 386 or better processor.
wReg - the register to read from.
Returns the value read from wReg.
This function is taken from the VGADOC 4.0 package.
*)
function InpLong(wReg : word) : longint;
var l : longint; {Temporary register value}
begin
inline($8B/$56/<wReg/$66/$ED/$66/$89/$46/<l);
InpLong:= l;
end;
{******}
(* OutpLong - outputs a 32 bit value to the specified register. This is
equivalent to a single OUT EAX, DX instruction and requires
a 386 or better processor.
wReg - the register to write to.
lVal - the value to write.
This procedure is taken from the VGADOC 4.0 package.
*)
procedure OutpLong(wReg : word; lVal : longint);
begin
(* mov dx, [BP + wReg]
mov eax, [BP + lVal]
out dx, eax
*)
inline($8B/$56/<wReg/$66/$8B/$46/<lVal/$66/$EF);
end;
(* FileExists - checks a given file name, returning true if the file exists
and false if it does not.
sFilename - the name of the file to check.
Returns result of check.
*)
function FileExists(sFilename : string) : boolean;
var SearchInfo : SearchRec;
begin
{*** Find matching file if one exists ***}
FindFirst(FExpand(sFileName), AnyFile, SearchInfo);
FileExists:= (DosError = 0);
end;
(* IsDir - returns true if the given path is a directory, false if not.
sPath - path to check.
*)
function IsDir(const sPath : string) : boolean;
var SearchData : SearchRec;
begin
FindFirst(sPath, Directory, SearchData);
if DosError = 0 then
IsDir:= SearchData.Attr and Directory <> 0
else
IsDir:= false;
end;
(* IsWild - returns true if the given string contains any wild card
characters ('*' or '?').
sPath - string to check.
*)
function IsWild(const sPath : string) : boolean;
begin
IsWild:= (Pos('?', sPath) > 0) or (Pos('*', sPath) > 0);
end;
(* GetCurDir - returns the current directory including trailing '\' *)
function GetCurDir : DirStr;
var CurDir : DirStr;
begin
GetDir(0, CurDir);
if Length(CurDir) > 3 then
begin
Inc(CurDir[0]);
CurDir[Length(CurDir)]:= '\';
end;
GetCurDir:= CurDir;
end;
(* DriveValid - returns true if the given drive letter is valid.
cDrive - drive letter to check.
*)
function DriveValid(cDrive : char) : boolean; assembler;
asm
mov ah, $19 {Save the current drive in BL}
int $21
mov bl, al
mov dl, cDrive {Select the given drive}
sub dl, 'A'
mov ah, $0E
int $21
mov ah, $19 {Retrieve what DOS thinks is current}
int $21
mov cx, 0 {Assume false}
cmp al, dl {Is the current drive the given drive?}
jne @@1
mov cx, 1 {It is, so the drive is valid}
mov dl, bl {Restore the old drive}
mov ah, $0E
int $21
@@1:
xchg ax, cx {Put the return value into AX}
end;
(* PathValid - returns true if the given path is valid.
sPath - path to check.
*)
function PathValid(var sPath : PathStr) : boolean;
var ExpPath : PathStr;
SearchData : SearchRec;
begin
{*** Expand to fully qualified path ***}
ExpPath:= FExpand(sPath);
if Length(ExpPath) <= 3 then
{*** Validate disk drive ***}
PathValid:= DriveValid(ExpPath[1])
else
begin
{*** Validate full path ***}
if ExpPath[Length(ExpPath)] = '\' then Dec(ExpPath[0]);
FindFirst(ExpPath, Directory, SearchData);
PathValid:= (DosError = 0) and (SearchData.Attr and Directory <> 0);
end;
end;
(* ValidFileName - returns true if the given filename contains no illegal
characters.
sFileName - the filename to validate.
*)
function ValidFileName(var sFileName : PathStr) : boolean;
const IllegalChars = ';,=+<>|"[] \';
var Dir : DirStr;
Name : NameStr;
Ext : ExtStr;
{*** Contains - returns true if S1 contains any characters in S2 ***}
function Contains(S1, S2 : string) : boolean; near; assembler;
asm
push ds
cld
lds si, S1
les di, S2
mov dx, di
xor ah, ah
lodsb
mov bx, ax
or bx, bx
jz @@2
mov al, es:[di]
xchg ax, cx
@@1:
push cx
mov di, dx
lodsb
repne scasb
pop cx
je @@3
dec bx
jnz @@1
@@2:
xor al, al
jmp @@4
@@3:
mov al, 1
@@4:
pop ds
end;
{******}
begin
ValidFileName:= true;
{*** Extract filename ***}
FSplit(sFileName, Dir, Name, Ext);
{*** Check for illegal characters ***}
if not ((Dir = '') or PathValid(Dir)) or
Contains(Name, IllegalChars) or
Contains(Dir, IllegalChars) then ValidFileName:= false;
end;
{******}
function GetLongTime : longint;
var Hour, Min, Sec, Sec100 : word;
begin
GetTime(Hour, Min, Sec, Sec100);
GetLongTime:= (LONGINT(Hour) * 3600) + (LONGINT(Min) * 60) + LONGINT(Sec);
end;
{******}
end.