-
Notifications
You must be signed in to change notification settings - Fork 31
/
delphifiles.inc
1163 lines (1079 loc) · 30.3 KB
/
delphifiles.inc
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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
This part of the unit modified by Tim Slusher and Vladimir Kladov.
}
{* Set of utility methods to work with files
and reqistry.
When programming KOL, which is Windows API-oriented, You should
avoid alien (for Windows) embedded Pascal files handling, and
use API-calls which implemented very well. This set of functions
is intended to make this easier.
Also TDirList object implementation present here and some registry
access functions, which allow to make code more elegant.
}
{$UNDEF ASM_LOCAL}
{$IFDEF ASM_VERSION}
{$DEFINE ASM_LOCAL}
{$ENDIF ASM_VERSION}
{$IFDEF ASM_VERSION}
function FileCreate(const FileName: string; OpenFlags: DWord): THandle;
asm
XOR ECX, ECX
PUSH ECX
MOV ECX, EDX
SHR ECX, 16
AND CX, $1FFF
JNZ @@1
MOV CL, FILE_ATTRIBUTE_NORMAL
@@1: PUSH ECX
MOV CL, DH
PUSH ECX // CreationMode
PUSH 0
MOV CL, DL
PUSH ECX // ShareMode
MOV DX, 0
PUSH EDX // AccessMode
//CALL System.@LStrToPChar // FileName must not be ''
PUSH EAX
CALL CreateFile
end;
{$ELSE ASM_VERSION} //Pascal
function FileCreate(const FileName: string; OpenFlags: DWord): THandle;
var Attr: DWORD;
begin
Attr := (OpenFlags shr 16) and $1FFF;
if Attr = 0 then Attr := FILE_ATTRIBUTE_NORMAL;
Result := CreateFile( PChar(FileName), OpenFlags and $F0000000,
OpenFlags and $F, nil, (OpenFlags shr 8) and $F,
Attr, 0 );
end;
{$ENDIF ASM_VERSION}
{$IFDEF ASM_VERSION}
function FileClose(Handle: THandle): Boolean;
asm
PUSH EAX
CALL CloseHandle
TEST EAX, EAX
SETNZ AL
end;
{$ELSE ASM_VERSION} //Pascal
function FileClose(Handle: THandle): boolean;
begin
Result := CloseHandle(Handle);
end;
{$ENDIF ASM_VERSION}
{$IFDEF ASM_VERSION}
function FileExists( const FileName : String ) : Boolean;
const size_TWin32FindData = sizeof( TWin32FindData );
asm
CALL EAX2PChar
PUSH EAX
CALL GetFileAttributes
INC EAX
JZ @@exit
DEC EAX
{$IFDEF PARANOIA}
DB $24, FILE_ATTRIBUTE_DIRECTORY
{$ELSE}
AND AL, FILE_ATTRIBUTE_DIRECTORY
{$ENDIF}
SETZ AL
@@exit:
end;
{$ELSE ASM_VERSION} //Pascal
function FileExists( const FileName : String ) : Boolean;
var
Code: Integer;
begin
Code := GetFileAttributes(PChar(FileName));
Result := (Code <> -1) and (FILE_ATTRIBUTE_DIRECTORY and Code = 0);
end;
{$ENDIF ASM_VERSION}
{$IFDEF ASM_VERSION}
function FileSeek(Handle: THandle; MoveTo: integer; MoveMethod: TMoveMethod): DWord;
asm
MOVZX ECX, CL
PUSH ECX
PUSH 0
PUSH EDX
PUSH EAX
CALL SetFilePointer
end;
{$ELSE ASM_VERSION} //Pascal
function FileSeek(Handle: THandle; MoveTo: integer; MoveMethod: TMoveMethod): DWord;
begin
Result := SetFilePointer(Handle, MoveTo, nil, Ord( MoveMethod ) );
end;
{$ENDIF ASM_VERSION}
{$IFDEF ASM_VERSION}
function FileRead(Handle: THandle; var Buffer; Count: DWord): DWord;
asm
PUSH EBP
PUSH 0
MOV EBP, ESP
PUSH 0
PUSH EBP
PUSH ECX
PUSH EDX
PUSH EAX
CALL ReadFile
TEST EAX, EAX
POP EAX
JNZ @@exit
XOR EAX, EAX
@@exit:
POP EBP
end;
{$ELSE ASM_VERSION} //Pascal
function FileRead(Handle: THandle; var Buffer; Count: DWord): DWord;
begin
if not ReadFile(Handle, Buffer, Count, Result, nil) then
Result := 0;
end;
{$ENDIF ASM_VERSION}
{$IFDEF ASM_VERSION}
function File2Str(Handle: THandle): String;
asm
PUSH EDX
TEST EAX, EAX
JZ @@exit // return ''
PUSH EBX
MOV EBX, EAX // EBX = Handle
XOR EDX, EDX
XOR ECX, ECX
INC ECX
CALL FileSeek
PUSH EAX // Pos
PUSH 0
PUSH EBX
CALL GetFileSize
POP EDX
SUB EAX, EDX // EAX = Size - Pos
JZ @@exitEBX
PUSH EAX
CALL System.@GetMem
XCHG EAX, EBX
MOV EDX, EBX
POP ECX
PUSH ECX
CALL FileRead
POP ECX
MOV EDX, EBX
POP EBX
POP EAX
PUSH EDX
{$IFDEF _D2}
CALL _LStrFromPCharLen
{$ELSE}
CALL System.@LStrFromPCharLen
{$ENDIF}
JMP @@freebuf
@@exitEBX:
POP EBX
@@exit:
XCHG EDX, EAX
POP EAX // @Result
PUSH EDX
CALL System.@LStrFromPChar
@@freebuf:
POP EAX
TEST EAX, EAX
JZ @@fin
CALL System.@FreeMem
@@fin:
end;
{$ELSE ASM_VERSION} //Pascal
function File2Str(Handle: THandle): String;
var Pos, Size: DWORD;
begin
Result := '';
if Handle = 0 then Exit;
Pos := FileSeek( Handle, 0, spCurrent );
Size := GetFileSize( Handle, nil );
SetString( Result, nil, Size - Pos + 1 );
FileRead( Handle, Result[ 1 ], Size - Pos );
Result[ Size - Pos + 1 ] := #0;
end;
{$ENDIF ASM_VERSION}
{$IFDEF ASM_VERSION}
function FileWrite(Handle: THandle; const Buffer; Count: DWord): DWord;
asm
PUSH EBP
PUSH EBP
MOV EBP, ESP
PUSH 0
PUSH EBP
PUSH ECX
PUSH EDX
PUSH EAX
CALL WriteFile
TEST EAX, EAX
POP EAX
JNZ @@exit
XOR EAX, EAX
@@exit:
POP EBP
end;
{$ELSE ASM_VERSION} //Pascal
function FileWrite(Handle: THandle; const Buffer; Count: DWord): DWord;
begin
if not WriteFile(Handle, Buffer, Count, Result, nil) then
Result := 0;
end;
{$ENDIF ASM_VERSION}
{$IFDEF ASM_VERSION}
function FileEOF( Handle: THandle ) : Boolean;
asm
PUSH EAX
PUSH 0
PUSH EAX
CALL GetFileSize
XCHG EAX, [ESP]
MOV CL, spCurrent
XOR EDX, EDX
CALL FileSeek
POP EDX
CMP EAX, EDX
SETGE AL
end;
{$ELSE ASM_VERSION} //Pascal
function FileEOF( Handle: THandle ) : Boolean;
var Siz, Pos : DWord;
begin
Siz := GetFileSize( Handle, nil );
Pos := FileSeek( Handle, 0, spCurrent );
Result := Pos >= Siz;
end;
{$ENDIF ASM_VERSION}
{$IFDEF ASM_noVERSION}
function FileFullPath( const FileName: String ) : String;
const
BkSlash: String = '\';
szTShFileInfo = sizeof( TShFileInfo );
asm
PUSH EBX
PUSH ESI
MOV EBX, EDX
PUSH EAX
XCHG EAX, EDX
CALL System.@LStrClr
POP EDX
PUSH 0
MOV EAX, ESP
CALL System.@LStrAsg
MOV ESI, ESP
@@loo: CMP dword ptr [ESI], 0
JZ @@fin
MOV EAX, ESI
MOV EDX, [BkSlash]
PUSH 0
MOV ECX, ESP
CALL Parse
CMP dword ptr [EBX], 0
JE @@1
MOV EAX, EBX
MOV EDX, [BkSlash]
CALL System.@LStrCat
JMP @@2
@@1:
POP EAX
PUSH EAX
CALL System.@LStrLen
CMP EAX, 2
JNE @@2
POP EAX
PUSH EAX
CMP byte ptr [EAX+1], ':'
JNE @@2
MOV EAX, EBX
POP EDX
PUSH EDX
CALL System.@LStrAsg
JMP @@3
@@2:
PUSH 0
MOV EAX, ESP
MOV EDX, [EBX]
CALL System.@LStrAsg
MOV EAX, ESP
MOV EDX, [ESP+4]
CALL System.@LStrCat
POP EAX
PUSH EAX
SUB ESP, szTShFileInfo
MOV EDX, ESP
PUSH SHGFI_DISPLAYNAME
PUSH szTShFileInfo
PUSH EDX
PUSH 0
PUSH EAX
CALL ShGetFileInfo
LEA EDX, [ESP].TShFileInfo.szDisplayName
CMP byte ptr [EDX], 0
JE @@clr_stk
LEA EAX, [ESP+szTShFileInfo+4]
CALL System.@LStrFromPChar
@@clr_stk:
ADD ESP, szTShFileInfo
CALL RemoveStr
POP EDX
PUSH EDX
MOV EAX, EBX
CALL System.@LStrCat
@@3: CALL RemoveStr
JMP @@loo
@@fin: CALL RemoveStr
POP ESI
POP EBX
end;
{$ELSE ASM_VERSION} //Pascal
function FileFullPath( const FileName: String ) : String;
var SFI: TShFileInfo;
Src, S: String;
begin
Result := '';
Src := FileName;
while Src <> '' do
begin
S := Parse( Src, '\' );
if Result <> '' then
Result := Result + '\';
if (Result = '') and (Length( S ) = 2) and (S[ 2 ] = ':') then
Result := S
else
begin
ShGetFileInfo( PChar( Result + S ), 0, SFI, Sizeof( SFI ),
SHGFI_DISPLAYNAME );
if SFI.szDisplayName[ 0 ] <> #0 then
S := SFI.szDisplayName;
Result := Result + S;
end;
end;
if ExtractFileExt( Result ) = '' then
// case when flag 'Hide extensions for registered file types' is set on
// in the Explorer:
Result := Result + ExtractFileExt( FileName );
end;
{$ENDIF ASM_VERSION}
function FileShortPath( const FileName: String ): String;
var Buf: array[ 0..MAX_PATH ] of Char;
begin
GetShortPathName( PChar( FileName ), Buf, Sizeof( Buf ) );
Result := Buf;
end;
function FileIconSystemIdx( const Path: String ): Integer;
var SFI: TShFileInfo;
begin
SFI.iIcon := 0; // Bartov
ShGetFileInfo( PChar( Path ), 0, SFI, sizeof( SFI ),
//-- Babenko Alexey: -----------------//
// SHGFI_ICON or //
//----------------------------------//
SHGFI_SMALLICON or SHGFI_SYSICONINDEX );
Result := SFI.iIcon;
end;
function FileIconSysIdxOffline( const Path: String ): Integer;
var SFI: TShFileInfo;
begin
SFI.iIcon := 0; // Bartov
ShGetFileInfo( PChar( Path ), FILE_ATTRIBUTE_NORMAL, SFI, sizeof( SFI ),
//-- Babenko Alexey: -----------------//
// SHGFI_ATTRIBUTES or SHGFI_ICON or //
//----------------------------------//
SHGFI_SMALLICON or SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES );
Result := SFI.iIcon;
end;
procedure LogFileOutput( const filepath, str: String );
var F: HFile;
begin
F := FileCreate( filepath, ofOpenWrite or ofOpenAlways );
if F = INVALID_HANDLE_VALUE then Exit;
FileSeek( F, 0, spEnd );
FileWrite( F, {$IFNDEF _D2} String {$ENDIF}
( str + #13#10 )[ 1 ], Length( str ) + 2 );
FileClose( F );
end;
function StrSaveToFile( const Filename, Str: String ): Boolean;
var F: HFile;
begin
Result := FALSE;
F := FileCreate( Filename, ofOpenWrite or ofOpenAlways );
if F = INVALID_HANDLE_VALUE then Exit;
FileWrite( F, Str[ 1 ], Length( Str ) );
FileClose( F );
Result := TRUE;
end;
function StrLoadFromFile( const Filename: String ): String;
var F: HFile;
begin
Result := '';
F := FileCreate( Filename, ofOpenRead or ofOpenExisting or ofShareDenyWrite );
if F = INVALID_HANDLE_VALUE then Exit;
Result := File2Str( F );
FileClose( F ); {??ee(zhog); Dark Knight}
end;
{$IFDEF ASM_VERSION}
function DirectoryExists(const Name: string): Boolean;
asm
//CALL System.@LStrToPChar // Name must not be ''
PUSH EAX
CALL GetFileAttributes
INC EAX
JZ @@exit
DEC EAX
{$IFDEF PARANOIA}
DB $24, FILE_ATTRIBUTE_DIRECTORY
{$ELSE}
AND AL, FILE_ATTRIBUTE_DIRECTORY
{$ENDIF}
SETNZ AL
@@exit:
end;
{$ELSE ASM_VERSION} //Pascal
function DirectoryExists(const Name: string): Boolean;
var
Code: Integer;
begin
Code := GetFileAttributes(PChar(Name));
Result := (Code <> -1) and (FILE_ATTRIBUTE_DIRECTORY and Code <> 0);
end;
{$ENDIF ASM_VERSION}
function CheckDirectoryContent( const Name: String; SubDirsOnly: Boolean; const Mask: String ): Boolean;
var FD: TWin32FindData;
FH: THandle;
begin
if not DirectoryExists( Name ) then
Result := TRUE
else
begin
FH := Windows.FindFirstFile( PChar( IncludeTrailingPathDelimiter( Name )
+ Mask ), FD );
if FH = INVALID_HANDLE_VALUE then
Result := TRUE
else
begin
Result := TRUE;
repeat
if not StrIn( FD.cFileName, ['.','..'] ) then
begin
if SubDirsOnly and LongBool(FD.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY)
or not SubDirsOnly then
begin
Result := FALSE;
break;
end;
end;
until not Windows.FindNextFile( FH, FD );
Windows.FindClose( FH );
end;
end;
end;
function DirectoryEmpty(const Name: String): Boolean;
begin
Result := CheckDirectoryContent( Name, FALSE, '*.*' );
end;
{-}
function DirectorySize( const Path: String ): I64;
var DirList: PDirList;
I: Integer;
begin
Result := MakeInt64( 0, 0 );
DirList := NewDirList( Path, '*.*', 0 );
for I := 0 to DirList.Count-1 do
begin
if LongBool( DirList.Items[ I ].dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY ) then
Result := Add64( Result, DirectorySize( DirList.Path + DirList.Names[ I ] ) )
else
Result := Add64( Result, MakeInt64( DirList.Items[ I ].nFileSizeLow,
DirList.Items[ I ].nFileSizeHigh ) );
end;
DirList.Free;
end;
{+}
function DirectoryHasSubdirs( const Path: String ): Boolean;
begin
Result := not CheckDirectoryContent( Path, TRUE, '*.*' );
end;
function GetFileList(const dir: string): PStrList;
var
Srch: TWin32FindData;
flag: Integer;
succ: boolean;
begin
result := nil;
flag := FindFirstFile(PChar(dir), Srch);
succ := flag <> 0;
while succ do begin
if (not (Srch.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY > 0))then begin
if Result = nil then begin
Result := NewStrList;
end;
Result.Add(Srch.cFileName);
end;
succ := FindNextFile(Flag, Srch);
end;
FindClose(Flag);
end;
function ExcludeTrailingChar( const S: String; C: Char ): String;
begin
Result := S;
if Result <> '' then
if Result[ Length( Result ) ] = C then
Delete( Result, Length( Result ), 1 );
end;
function IncludeTrailingChar( const S: String; C: Char ): String;
begin
Result := S;
if (Result = '') or (Result[ Length( Result ) ] <> C) then
Result := Result + C;
end;
//---------------------------------------------------------
// Following functions/procedures are created by Edward Aretino:
// IncludeTrailingPathDelimiter, ExcludeTrailingPathDelimiter,
// ForceDirectories, CreateDir, ChangeFileExt
//---------------------------------------------------------
function IncludeTrailingPathDelimiter(const S: string): string;
begin
{if CopyTail(S, 1) <> '\' then
Result := S + '\'
else
Result := S;}
Result := IncludeTrailingChar( S, '\' );
end;
function ExcludeTrailingPathDelimiter(const S: string): string;
begin
{Result := S;
if Length(Result) = 0 then Exit;
if (CopyTail(Result, 1) = '\') then
DeleteTail(Result, 1);}
Result := ExcludeTrailingChar( S, '\' );
end;
function ForceDirectories(Dir: string): Boolean;
begin
Result := Length(Dir) > 0; {Centronix}
If not Result then Exit;
Dir := ExcludeTrailingPathDelimiter(Dir);
If (Length(Dir) < 3) or DirectoryExists(Dir) or
(ExtractFilePath(Dir) = Dir) then Exit; // avoid 'xyz:\' problem.
Result := ForceDirectories(ExtractFilePath(Dir)) and CreateDir(Dir);
end;
function CreateDir(const Dir: string): Boolean;
begin
Result := Windows.CreateDirectory(PChar(Dir), nil);
end;
function ChangeFileExt(FileName: String; const Extension: string): string;
var
FileExt: String;
begin
FileExt := ExtractFileExt(FileName);
DeleteTail(FileName, Length(FileExt));
Result := FileName+ Extension;
end;
{$IFDEF ASM_VERSION}
{$IFNDEF _D2}
{$DEFINE ASM_LStrFromPCharLen}
{$ENDIF}
{$ENDIF ASM_VERSION}
{$IFDEF ASM_LStrFromPCharLen}
{$DEFINE ASM_DIRDelimiters}
{$ENDIF}
{$IFDEF ASM_VERSION}
{$DEFINE ASM_DIRDelimiters}
{$ENDIF ASM_VERSION}
{$IFDEF ASM_DIRDelimiters}
const
DirDelimiters: PChar = ':\';
{$ENDIF}
{$IFDEF ASM_VERSION}
function ExtractFileName( const Path : String ) : String;
asm
PUSH EDX
PUSH EAX
MOV EDX, [DirDelimiters]
CALL __DelimiterLast
POP EDX
CMP byte ptr [EAX], 0
JZ @@1
XCHG EDX, EAX
INC EDX
@@1: POP EAX
CALL System.@LStrFromPChar
end;
{$ELSE ASM_VERSION} //Pascal
function ExtractFileName( const Path : String ) : String;
var P: PChar;
begin
P := __DelimiterLast( PChar( Path ), ':\' );
if P^ = #0 then
Result := Path
else
Result := P + 1;
end;
{$ENDIF ASM_VERSION}
{$IFDEF ASM_LStrFromPCharLen} // LStrFromPCharLen - there are no in D2
function ExtractFilePath( const Path : String ) : String;
asm
PUSH EDX
MOV EDX, [DirDelimiters]
CALL EAX2PChar
PUSH EAX
CALL __DelimiterLast
XCHG EDX, EAX
XOR ECX, ECX
POP EAX
CMP byte ptr [EDX], CL
JZ @@ret_0
SUB EDX, EAX
INC EDX
XCHG EDX, EAX
XCHG ECX, EAX
@@ret_0:
POP EAX
CALL System.@LStrFromPCharLen
end;
{$ELSE} //Pascal
function ExtractFilePath( const Path : String ) : String;
//var I : Integer;
var P, P0: PChar;
begin
P0 := PChar( Path );
P := __DelimiterLast( P0, ':\' );
if P^ = #0 then
Result := ''
else
Result := Copy( Path, 1, P - P0 + 1 );
end;
{$ENDIF}
function ExtractFileNameWOext( const Path : String ) : String;
begin
Result := ExtractFileName( Path );
Result := Copy( Result, 1, Length( Result ) - Length( ExtractFileExt( Result ) ) );
end;
{$IFDEF ASM_VERSION}
const
ExtDelimeters: PChar = '.';
function ExtractFileExt( const Path : String ) : String;
asm
PUSH EDX
MOV EDX, [ExtDelimeters]
CALL EAX2PChar
CALL __DelimiterLast
@@1: XCHG EDX, EAX
POP EAX
CALL System.@LStrFromPChar
end;
{$ELSE ASM_VERSION} //Pascal
function ExtractFileExt( const Path : String ) : String;
var P: PChar;
begin
P := __DelimiterLast( PChar( Path ), '.' );
Result := P;
end;
{$ENDIF ASM_VERSION}
function ReplaceFileExt( const Path, NewExt: String ): String;
begin
Result := ExtractFilePath( Path ) +
ExtractFileNameWOext( ExtractFileName( Path ) ) +
NewExt;
end;
function ExtractShortPathName( const Path: String ): String;
var
Buffer: array[0..MAX_PATH - 1] of Char;
begin
SetString(Result, Buffer,
GetShortPathName(PChar(Path), Buffer, SizeOf(Buffer)));
end;
function FilePathShortened( const Path: String; MaxLen: Integer ): String;
begin
Result := FilePathShortenPixels( Path, 0, MaxLen );
end;
function PixelsLength( DC: HDC; const Text: String ): Integer;
var Sz: TSize;
begin
if DC = 0 then
Result := Length( Text )
else
begin
Windows.GetTextExtentPoint32( DC, PChar( Text ), Length( Text ), Sz );
Result := Sz.cx;
end;
end;
function FilePathShortenPixels( const Path: String; DC: HDC; MaxPixels: Integer ): String;
var L0, L1: Integer;
Prev: String;
begin
Result := Path;
L0 := PixelsLength( DC, Result );
while L0 > MaxPixels do
begin
Prev := Result;
L1 := pos( '\...\', Result );
if L1 <= 0 then
Result := ExcludeTrailingPathDelimiter( ExtractFilePath( Result ) )
else
Result := Copy( Result, 1, L1 - 1 );
if Result <> '' then
Result := IncludeTrailingPathDelimiter( ExtractFilePath( Result ) ) + '...\' + ExtractFileName( Path );
if (Result = '') or (Result = Prev) then
begin
L1 := Length( ExtractFilePath( Result ) );
while (PixelsLength( DC, Result ) > MaxPixels) and (L1 > 1) do
begin
Dec( L1 );
Result := Copy( Result, 1, L1 ) + '...\' + ExtractFileName( Result );
end;
if PixelsLength( DC, Result ) > MaxPixels then
begin
L1 := MaxPixels + 1;
while ((MaxPixels > 0) and (L1 > 1) or (MaxPixels = 0) and (L1 > 0)) and
(PixelsLength( DC, Result ) > MaxPixels) do
begin
Dec( L1 );
Result := Copy( ExtractFileName( Path ), 1, L1 ) + '...';
end;
end;
break;
end;
L0 := PixelsLength( DC, Result );
end;
end;
procedure CutFirstDirectory(var S: String);
var
Root: Boolean;
P: Integer;
begin
if S = '\' then
S := ''
else
begin
if S[1] = '\' then
begin
Root := True;
Delete(S, 1, 1);
end
else
Root := False;
if S[1] = '.' then
Delete(S, 1, 4);
P := pos('\',S);
if P <> 0 then
begin
Delete(S, 1, P);
S := '...\' + S;
end
else
S := '';
if Root then
S := '\' + S;
end;
end;
function MinimizeName( const Path: String; DC: HDC; MaxPixels: Integer ): String;
var
Drive, Dir, Name: String;
begin
Result := Path;
Dir := ExtractFilePath(Result);
Name := ExtractFileName(Result);
if (Length(Dir) >= 2) and (Dir[2] = ':') then
begin
Drive := Copy(Dir, 1, 2);
Delete(Dir, 1, 2);
end
else
Drive := '';
while ((Dir <> '') or (Drive <> '')) and (PixelsLength(DC, Result) > MaxPixels) do
begin
if Dir = '\...\' then
begin
Drive := '';
Dir := '...\';
end
else if Dir = '' then
Drive := ''
else
CutFirstDirectory(Dir);
Result := Drive + Dir + Name;
end;
end;
{$IFDEF ASM_VERSION}
function FileSize( const Path : String ) : Integer;
const size_TWin32FindData = sizeof( TWin32FindData );
asm
ADD ESP, - size_TWin32FindData
PUSH ESP
//CALL System.@LStrToPChar // Path must not be ''
PUSH EAX
CALL FindFirstFile
INC EAX
JZ @@exit
DEC EAX
PUSH EAX
CALL FindClose
MOV EAX, [ESP].TWin32FindData.nFileSizeLow
@@exit:
ADD ESP, size_TWin32FindData
end;
{$ELSE ASM_VERSION} //Pascal
function FileSize( const Path : String ) : Integer;
var FD : TWin32FindData;
FH : THandle;
begin
FH := FindFirstFile( PChar( Path ), FD );
Result := 0;
if FH = INVALID_HANDLE_VALUE then exit;
Result := FD.nFileSizeLow;
if ((FD.nFileSizeLow and $80000000) <> 0) or
(FD.nFileSizeHigh <> 0) then Result := -1;
FindClose( FH );
end;
{$ENDIF ASM_VERSION}
//*
function FileTimeCompare( const FT1, FT2 : TFileTime ) : Integer;
var ST1, ST2 : TSystemTime;
begin
FileTimeToSystemTime( FT1, ST1 );
FileTimeToSystemTime( FT2, ST2 );
Result := CompareSystemTime( ST1, ST2 );
end;
function GetSystemDir: String;
var Buf: array[ 0..MAX_PATH ] of Char;
begin
GetSystemDirectory( @ Buf[ 0 ], MAX_PATH + 1 );
Result := IncludeTrailingPathDelimiter( PChar( @ Buf[ 0 ] ) );
end;
//*
function GetWindowsDir : string;
var Buf : array[ 0..MAX_PATH ] of Char;
begin
GetWindowsDirectory( @Buf[ 0 ], MAX_PATH + 1 );
Result := IncludeTrailingPathDelimiter( PChar( @ Buf[ 0 ] ) );
end;
function GetWorkDir : string;
var Buf: array[ 0..MAX_PATH ] of Char;
begin
GetCurrentDirectory( MAX_PATH + 1, @ Buf[ 0 ] );
Result := IncludeTrailingPathDelimiter( PChar( @ Buf[ 0 ] ) );
end;
//*
function GetTempDir : string;
var Buf : array[ 0..MAX_PATH ] of Char;
begin
Windows.GetTempPath( MAX_PATH + 1, @Buf[ 0 ] );
Result := IncludeTrailingPathDelimiter( PChar( @ Buf[ 0 ] ) );
end;
function CreateTempFile( const DirPath, Prefix: String ): String;
var Buf: array[ 0..MAX_PATH ] of Char;
begin
GetTempFileName( PChar( DirPath ), PChar( Prefix ), 0, Buf );
Result := Buf;
end;
function GetFileListStr(FPath{e.g.'c:\tmp\'}, FMask{e.g.'*.*'}: string): string;
{* List of files in string, separating each path from others with semicolon (';').
E.g.: 'c:\tmp\unit1.dcu;c:\tmp\unit1.~pa' (for use with DeleteFile2Recycle())}
var
Srch: TWin32FindData;
flag: Integer;
succ: boolean;
dir:string;
begin
result := '';
if (FPath<>'') and (FPath[length(FPath)]<>'\') then FPath:=FPath+'\';
if (FMask<>'') and (FMask[1]='\') then FMask:=CopyEnd(FMask,2);
dir:=FPath+FMask;
flag := FindFirstFile(PChar(dir), Srch);
succ := flag <> 0;
while succ do begin
if (not (Srch.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY > 0))then begin
if Result<>''then Result:=Result+';';
Result:=Result+FPath+Srch.cFileName;
end;
succ := FindNextFile(Flag, Srch);
end;
FindClose(Flag);
end;
function DeleteFiles( const DirPath: String ): Boolean;
var Files, Name: String;
begin
Files := GetFileListStr( ExtractFilePath( DirPath ), ExtractFileName( DirPath ) );
Result := TRUE;
while Files <> '' do
begin
Name := Parse( Files, ';' );
Result := Result and DeleteFile( PChar( Name ) );
end;
end;
//*
function DeleteFile2Recycle( const Filename : String ) : Boolean;
var FOS : TSHFileOpStruct;
Buf : PChar;
L : Integer;
begin
L := Length( Filename );
GetMem( Buf, L + 2 );
StrCopy( Buf, PChar( Filename ) );
Buf[ L + 1 ] := #0;
for L := L downto 0 do
if Buf[ L ] = ';' then Buf[ L ] := #0;
FillChar( FOS, Sizeof( FOS ), 0 );
if Applet <> nil then
FOS.Wnd := Applet.Handle;
FOS.wFunc := FO_DELETE;
FOS.pFrom := Buf;
FOS.fFlags := FOF_ALLOWUNDO or FOF_NOCONFIRMATION;
FOS.fAnyOperationsAborted := True;
FOS.lpszProgressTitle := PChar( 'Delete ' + Filename + ' to Recycle bin' );
Result := SHFileOperation( FOS ) = 0;
if Result then
Result := not FOS.fAnyOperationsAborted;