-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxLogic.ioUtils.pas
More file actions
1233 lines (1057 loc) · 35 KB
/
Copy pathMaxLogic.ioUtils.pas
File metadata and controls
1233 lines (1057 loc) · 35 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
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
unit maxLogic.IOUtils;
{
work in progress: for now only some methods exported from old big the pawel1.pas
Version: 0.20
}
{$IF DEFINED(FRAMEWORK_VCL) OR DEFINED(FRAMEWORK_FMXs)}
{$IFNDEF CONSOLE}
{$DEFINE CanUseApplicationInstance}
{$ENDIF}
{$IFEND}
interface
uses
{$IFDEF MadExcept}madExcept, {$ENDIF}
{$IF DEFINED( MsWINDOWS)}
winApi.Windows, System.Win.ComObj, winApi.WinInet, winApi.ShLwApi, winApi.ShlObj, winApi.SHFolder,
maxConsoleRunner,
{$ELSEIF DEFINED(POSIX)}
Posix.Stdlib,
Posix.Unistd,
Posix.Dlfcn,
{$IFEND}
System.Classes, System.SysUtils, generics.collections, generics.defaults;
function GetCurrentDLLName: string;
{$IFDEF MSWINDOWS}
function GetBuildInfo: string; overload;
function GetBuildInfo(const FileName: string; out V1, V2, v3, v4: Word): string; overload;
function GetVersionString(const prefix: string = ' v'): string;
// as GetBuildInfo but returns only the last number which is responsible for the build version or build number
function GetBuildNumber: Word; overload;
function GetBuildNumber(const FileName: string): Word; overload;
{$ENDIF}
{$IFDEF MSWINDOWS}
{ Default Encoding is utf8
include in tc file like that:
<ResName>RCDATA <FileName>
}
function LoatStringFromResource(const aResName: string; out aValue: string; const aDefault: string = ''; aEncoding: TEncoding = nil): boolean; overload;
// Default is ''
function LoatStringFromResource(const aResName: string; aEncoding: TEncoding = nil): string; overload;
{$ENDIF}
// works also for dll files
function GetInstallDir: string;
{$IFDEF MSWINDOWS}
procedure exec(const FileName: string; const Parameter: string = ''; StartDir: string = '');
procedure ExecuteFile(const aFileName: string;
const AParameter, ACurrentDir: string; AWait: boolean;
aRunHidden: boolean = False); overload;
procedure ExecuteFile(const Cmd, ACurrentDir: string; AWait: boolean;
aRunHidden: boolean = False); overload;
type
TStrProc = maxConsoleRunner.TDataReadyProc;
// similar to the above, but now we also get the stdOut and errOut and the exit code
procedure ExecuteFile(const Cmd, ACurrentDir: string;
out aExitCode: integer;
aOnStdOut: TStrProc = nil;
aOnErrOut: TStrProc = nil;
aRunHidden: boolean = True); overload;
{$ENDIF}
// default encoding is utf8
// aWaitBetweenRetries is in milliseconds
// returns true if the write was successful false otherwise
function SafeAppendToFile(const aFileName, aText: string; aRetryCount: integer = 5; aWaitBetweenRetries: integer = 10; aEncoding: TEncoding = nil): boolean;
{$IFDEF MSWINDOWS}
function RecycleItem(const ItemName: string; const DeleteToRecycle: boolean = True; const ShowConfirm: boolean = True; const TotalSilence: boolean = False): boolean;
{$ENDIF}
function CombinePath(const aParts: array of string; aAddFinalTrailingPathDelimiter: boolean = False): string;
/// <summary>
/// <b>OBSOLETE.</b> Alias for <see cref="SanitizeFileName" />.
/// </summary>
/// <remarks>
/// *Kept for backward compatibility only.* New code should call
/// <see cref="SanitizeFileName" /> directly. The implementation
/// simply forwards to the new helper, so behaviour is identical.
/// </remarks>
{$WARN SYMBOL_DEPRECATED ON}
function ConvertToValidDirectoryName(const aText: string;
aReplaceInvalidCharsWith: char = '_'): string;
deprecated 'ConvertToValidDirectoryName is obsolete. Use SanitizeFileName instead.';
/// <summary>
/// Makes a file or directory *name* safe for all supported platforms.
/// Optionally processes a full path while preserving path separators.
/// </summary>
/// <param name="aRawName">
/// Untrusted input: either a single name (default mode) or a full path.
/// </param>
/// <param name="aReplacement">
/// Replacement for each invalid character (defaults to '_').
/// If set to #0, invalid characters are removed.
/// </param>
/// <param name="aHonorSeparators">
/// When *true*, treats directory separators as boundaries and sanitizes each segment;
/// when *false* (default), sanitizes the whole string as a single name (no separators allowed).
/// </param>
/// <returns>
/// A safe name/path; never empty (falls back to 'untitled').
/// On Windows, segments are also stripped of trailing dot/space and
/// avoid reserved device names.
/// </returns>
function SanitizeFileName(const aRawName: string;
const aReplacement: char = '_';
const aHonorSeparators: boolean = False): string;
/// <summary>
/// This function checks the provided filename to determine if it exceeds the Windows path length limit. It then appropriately prefixes the filename to support extended-length paths. The function also distinguishes between local and network paths, applying a different prefix based on the path type. Note that for network paths, the initial double backslashes (`\\`) are replaced with the `\\?\UNC\` prefix, while local paths simply receive the `\\?\` prefix.
/// Remember, even with these prefixes, some Windows APIs and applications may not support extended-length paths or may require certain group policy settings to be enabled for support.
/// on non windows systems the result is the same as the input
/// </summary>
function LongFileNameFix(const aFileName: string): string;
// ensures the last char is TPath.DirectorySeparatorChar
function slash(const aPath: string): string;
function UnSlash(const aPath: string): string;
// NOTE: Environment Changes Scope: As on Windows, changes made by setenv in this way only affect the current process and its child processes. They do not affect the system-wide environment or persist after the application terminates.
procedure SetEnvironmentPath(const aPath: string);
function FilePathToURL(const aFilePath: string): string;
// for windows it will exchange "/" with "\"
// it will also replace "//" with "/" with a single /
function NormalizePath(const aPath: string): string;
// This forces the path into a system-compatible ANSI encoding.
// on non windows OS just returns the input
function GetAnsiFileName(const aLongPath: string): string;
// ATTENTION: shortens the path only on windows, on other systems it just returns the input
function GetShortPath(const aLongPath: string): string;
/// <summary>
/// Properly quotes a string for use as a command-line argument in the current platform's shell.
/// Automatically selects the appropriate quoting method based on the platform (Windows or Unix/Linux).
/// </summary>
/// <param name="aValue">The string to be quoted for shell use</param>
/// <returns>A properly quoted string safe to use as a shell command argument</returns>
function QuoteShellArgument(const aValue: string): string;
/// <summary>
/// Properly quotes a string for use in Unix/Linux shell commands using single quotes.
/// Handles special characters and escapes embedded single quotes according to bash conventions.
/// Example: test\'1 becomes 'test'\''1' which is the proper Linux way to escape single quotes.
/// </summary>
/// <param name="aValue">The string to be quoted for Unix shell use</param>
/// <returns>A properly quoted string safe to use in bash/sh command lines</returns>
function QuoteUnixShellArgument(const aValue: string): string;
/// <summary>
/// Properly quotes a string for use in Windows command shell (cmd.exe).
/// Handles Windows' complex escaping rules, with special treatment for:
/// - Backslashes before quotes (doubled)
/// - Double quotes (escaped with backslashes)
/// - Spaces and other special characters
/// </summary>
/// <param name="aValue">The string to be quoted for Windows shell use</param>
/// <returns>A properly quoted string safe to use in Windows command lines</returns>
function QuoteWindowsShellArgument(const aValue: string): string;
function GetLocalAppDataPath: string;
{
lProgramFiles := GetSpecialWindowsFolder(CSIDL_PROGRAM_FILES);
lProgramFilesX86 := GetSpecialWindowsFolder(CSIDL_PROGRAM_FILESX86);
lAppData:= GetSpecialWindowsFolder(CSIDL_APPDATA);
}
{$IFDEF MSWINDOWS}
function GetSpecialWindowsFolder(aFolder: integer): string;
{$ENDIF}
function IsInPath(const aPath, aFileName: string): boolean;
function GetFileDate(out aCreateTime, aLastAccess, aLastWriteTime: TDateTime; const aFileName: string): boolean; overload;
function GetFileDate(const aFileName: string): TDateTime; overload;
// smark BOM detection. If no bom preset, checks if file is utf8.
function LoadStrFromFile(const aFileName: string): string;
// normalizes the path, then compares
function SamePath(const aPath1, aPath2: string): boolean;
type
// keep your existing enum as-is
TSpecialFolderKind = (
sfkCurUser_My_Documents,
sfkAllUsers_Application_Data,
sfkUserSpecific_Application_Data,
sfkProgramFiles,
sfkAllUsers_Documents
// no good cross-platform analogue for Desktop and Drives
{$IFDEF MSWINDOWS}
, sfkDesktop,
sfkDrives
{$ENDIF}
);
function GetSpecialFolderPath(FolderKind: TSpecialFolderKind): string;
// some bytes read/write helpers
procedure writeToBytesAndIncOffset(const Buf; BuffSize: integer;
var Bytes: TBytes; var offset: integer);
procedure writeToBytes(const Buf; BufSize: integer; var Bytes: TBytes;
offset: integer);
procedure readFromBytesAndIncOffset(var Buf; BuffSize: integer;
const Bytes: TBytes; var offset: integer);
procedure readFromBytes(var Buf; BufSize: integer; const Bytes: TBytes;
offset: integer);
procedure FillZeroBytes(var Bytes: TBytes);
{$IFNDEF MsWIndows}
procedure ZeroMemory(aPtr: Pointer; aSize: NativeInt); inline;
{$ENDIF}
procedure GetFileList(aFileList: TStringList; const aDirectory: String; const aFileMask: String = '*.*'; aIncludeSubDirectories: Boolean = false);
implementation
uses
{$IFDEF MSWINDOWS}
ShellApi,
{$ENDIF}
{$IFDEF CanUseApplicationInstance}
Forms,
{$ENDIF}
System.IOUtils, System.StrUtils, AutoFree;
{$IFDEF MSWINDOWS}
function LoatStringFromResource(const aResName: string; out aValue: string; const aDefault: string = ''; aEncoding: TEncoding = nil): boolean;
var
RS: TResourceStream;
Bytes: TBytes;
begin
aValue := aDefault;
Result := False;
if aEncoding = nil then
aEncoding := TEncoding.UTF8;
RS := TResourceStream.Create(HInstance, aResName, RT_RCDATA);
try
if RS.Size > 0 then
begin
SetLength(Bytes, RS.Size);
RS.ReadBuffer(Bytes[0], RS.Size);
aValue := aEncoding.GetString(Bytes);
Result := True;
end;
finally
RS.Free;
end;
end;
{$ENDIF}
{$IFDEF MSWINDOWS}
function LoatStringFromResource(const aResName: string; aEncoding: TEncoding = nil): string;
begin
if not LoatStringFromResource(aResName, Result) then
Result := '';
end;
{$ENDIF}
{$IFDEF MSWINDOWS}
function GetVersionString(const prefix: string = ' v'): string;
var
s: string;
begin
s := GetBuildInfo;
if s = '' then
Result := ''
else
Result := { Application.Title + }prefix + s;
end;
{$ENDIF}
{$IFDEF MSWINDOWS}
function GetBuildNumber: Word;
begin
Result := GetBuildNumber(GetCurrentDLLName);
end;
{$ENDIF}
{$IFDEF MSWINDOWS}
function GetBuildNumber(const FileName: string): Word;
var
V1, V2, v3: Word;
begin
GetBuildInfo(FileName, V1, V2, v3, Result);
end;
{$ENDIF}
{$IFDEF MSWINDOWS}
function GetBuildInfo: string;
var
V1, V2, v3, v4: Word;
begin
V1 := 0;
V2 := 0;
v3 := 0;
v4 := 0;
try
Result := GetBuildInfo(GetCurrentDLLName(), V1, V2, v3, v4);
except
Result := '';
end;
end;
{$ENDIF}
{$IFDEF MSWINDOWS}
function GetBuildInfo(const FileName: string; out V1, V2, v3, v4: Word): string;
var
VerInfoSize: Cardinal;
VerInfo: Pointer;
VerValueSize: Cardinal;
VerValue: PVSFixedFileInfo;
dummy: Cardinal;
begin
VerInfoSize := GetFileVersionInfoSize(PChar(FileName), dummy);
if VerInfoSize = 0 then
begin
dummy := GetLastError;
Result := '';
exit;
end; { if }
GetMem(VerInfo, VerInfoSize);
GetFileVersionInfo(PChar(FileName), 0, VerInfoSize, VerInfo);
VerQueryValue(VerInfo, '\', Pointer(VerValue), VerValueSize);
with VerValue^ do
begin
V1 := dwFileVersionMS shr 16;
V2 := dwFileVersionMS and $FFFF;
v3 := dwFileVersionLS shr 16;
v4 := dwFileVersionLS and $FFFF;
end;
FreeMem(VerInfo, VerInfoSize);
Result := IntToStr(V1) + '.' + IntToStr(V2) + '.' + IntToStr(v3) + '.' +
IntToStr(v4);
end;
{$ENDIF}
function GetCurrentDLLName: string;
var
lPath: string;
lLength: Integer;
begin
{$IFDEF MSWINDOWS}
SetLength(lPath, max_path);
lLength := GetModuleFileName(HInstance, @lPath[1], max_path);
if lLength > 0 then
begin
SetLength(lPath, lLength);
Result := lPath;
end
else
Result := '';
{$ELSE}
lPath := ParamStr(0);
if lPath = '' then
Exit('');
try
Result := TPath.GetFullPath(lPath);
except
Result := lPath;
end;
{$ENDIF}
end;
function GetInstallDir: string;
begin
Result := GetCurrentDLLName;
if Result <> '' then
Result := ExtractFilePath(Result);
if Result = '' then
begin
Result := ParamStr(0);
if Result <> '' then
begin
try
Result := TPath.GetFullPath(Result);
except
end;
Result := ExtractFilePath(Result);
end;
end;
end;
{$IFDEF MSWINDOWS}
procedure exec(const FileName: string; const Parameter: string = ''; StartDir: string = '');
begin
if StartDir = '' then
StartDir := ExtractFilePath(FileName);
ShellExecute(0, nil, PChar(FileName), PChar(Parameter), PChar(StartDir),
SW_NORMAL);
end;
{$ENDIF}
{$IFDEF MSWINDOWS}
procedure ExecuteFile(const Cmd, ACurrentDir: string;
out aExitCode: integer;
aOnStdOut: TStrProc = nil;
aOnErrOut: TStrProc = nil;
aRunHidden: boolean = True);
var
lRunner: TmaxConsoleRunner;
begin
lRunner := TmaxConsoleRunner.Create;
try
lRunner.DecodeCommand(Cmd);
lRunner.OnStdDataRead := aOnStdOut;
lRunner.OnErrorDataRead := aOnErrOut;
lRunner.RunHidden := aRunHidden;
lRunner.Execute;
aExitCode := lRunner.ExitCode;
finally
lRunner.Free;
end;
end;
procedure ExecuteFile(
const aFileName: string;
const AParameter, ACurrentDir: string; AWait: boolean;
aRunHidden: boolean = False);
var
Cmd: string;
dir: string;
begin
dir := ACurrentDir;
if dir = '' then
dir := ExtractFilePath(aFileName);
Cmd := aFileName;
if (Cmd <> '') and (Cmd[1] <> '"') then
Cmd := '"' + Cmd + '"';
if AParameter <> '' then
Cmd := Cmd + ' ' + AParameter;
ExecuteFile(Cmd, dir, AWait, aRunHidden);
end;
{$ENDIF}
{$IFDEF MSWINDOWS}
procedure ExecuteFile(
const Cmd, ACurrentDir: string; AWait: boolean;
aRunHidden: boolean = False); overload;
var
si: TStartupInfo;
pi: TProcessInformation;
lCmd, lDir: string;
begin
lDir := ACurrentDir;
ZeroMemory(@si, SizeOf(si));
with si do
begin
cb := SizeOf(si);
dwFlags := STARTF_USESHOWWINDOW;
if aRunHidden then
wShowWindow := SW_HIDE
else
wShowWindow := SW_NORMAL;
end;
ZeroMemory(@pi, SizeOf(pi));
lCmd := Cmd;
UniqueString(lCmd);
if CreateProcess(nil, PChar(lCmd), nil, nil, False,
CREATE_DEFAULT_ERROR_MODE or CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS,
nil, PChar(lDir), si, pi) then
begin
try
if AWait then
begin
{$IFDEF MadExcept}madExcept.PauseFreezeCheck(True);
{$ENDIF}
WaitForSingleObject(pi.hProcess, INFINITE);
{$IFDEF MadExcept}madExcept.PauseFreezeCheck(False);
{$ENDIF}
end;
finally
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
end;
end;
end;
{$ENDIF}
{$IFDEF MSWINDOWS}
function RecycleItem(const ItemName: string; const DeleteToRecycle: boolean = True; const ShowConfirm: boolean = True; const TotalSilence: boolean = False): boolean;
var
SHFileOpStruct: TSHFileOpStruct;
begin
fillchar(SHFileOpStruct, SizeOf(SHFileOpStruct), #0);
{$IFDEF CanUseApplicationInstance}
if assigned(application) and assigned(application.MainForm) then
SHFileOpStruct.wnd := application.MainForm.Handle { Others are using 0. But Application.MainForm.Handle is better because otherwise, the 'Are you sure you want to delete' will be hidden under program's window }
else
{$ENDIF}
SHFileOpStruct.wnd := 0;
SHFileOpStruct.wFunc := FO_DELETE;
SHFileOpStruct.pFrom := PChar(ItemName + #0);
SHFileOpStruct.pTo := nil;
SHFileOpStruct.hNameMappings := nil;
if DeleteToRecycle then
SHFileOpStruct.fFlags := SHFileOpStruct.fFlags or FOF_ALLOWUNDO;
if TotalSilence then
SHFileOpStruct.fFlags := SHFileOpStruct.fFlags or FOF_NO_UI
else
if not ShowConfirm then
SHFileOpStruct.fFlags := SHFileOpStruct.fFlags or FOF_NOCONFIRMATION;
Result := SHFileOperation(SHFileOpStruct) = 0;
end;
{$ENDIF}
function SafeAppendToFile(const aFileName, aText: string; aRetryCount: integer = 5; aWaitBetweenRetries: integer = 10; aEncoding: TEncoding = nil): boolean;
var
RetryCounter: integer;
begin
Result := False;
RetryCounter := 0;
if aRetryCount < 1 then
aRetryCount := 1;
if aEncoding = nil then
aEncoding := TEncoding.UTF8;
repeat
try
TFile.AppendAllText(aFileName, aText, aEncoding);
exit(True);
except
Inc(RetryCounter);
Sleep(aWaitBetweenRetries);
end;
until RetryCounter >= aRetryCount;
end;
function CombinePath(const aParts: array of string; aAddFinalTrailingPathDelimiter: boolean = False): string;
var
X: integer;
begin
if length(aParts) = 0 then
exit('');
Result := aParts[0];
for X := 1 to length(aParts) - 1 do
Result := TPath.Combine(Result, aParts[X]);
if aAddFinalTrailingPathDelimiter then
Result := IncludeTrailingPathDelimiter(Result);
end;
function SanitizeFileName(const aRawName: string;
const aReplacement: char;
const aHonorSeparators: boolean): string;
function ReplaceOrRemoveInvalid(const s: string; const repl: char): string;
var
i: integer;
begin
Result := s;
for i := 1 to length(Result) do
if not TPath.IsValidFileNameChar(Result[i]) then // cross-platform invalid-char check
begin
if repl = #0 then
Result[i] := #0 // mark for deletion, compact later
else
Result[i] := repl;
end;
if repl = #0 then
begin
// compact string by removing #0 markers
Result := Result.Replace(#0, '');
end;
end;
{$IFDEF MSWINDOWS}
function WindowsFixups(const seg: string): string;
const
// Windows reserved device names (case-insensitive)
CReserved: array[0..21] of string = (
'CON', 'PRN', 'AUX', 'NUL',
'COM1', 'COM2', 'COM3', 'COM4', 'COM5', 'COM6', 'COM7', 'COM8', 'COM9',
'LPT1', 'LPT2', 'LPT3', 'LPT4', 'LPT5', 'LPT6', 'LPT7', 'LPT8', 'LPT9'
);
var
u: string;
begin
Result := seg;
// strip trailing space or dot (Windows UI/Win32 rules)
while (Result <> '') and ((Result[High(Result)] = ' ') or (Result[High(Result)] = '.')) do
delete(Result, length(Result), 1);
// avoid reserved DOS device names
u := UpperCase(Result);
if matchText(u, CReserved) then
Result := '_' + Result;
end;
{$ELSE}
function WindowsFixups(const seg: string): string;
begin
// no-op on non-Windows
Result := seg;
end;
{$ENDIF}
function FixEmptyToUntitled(const s: string): string;
const
cUntitled = 'untitled';
begin
if s.IsEmpty then
Result := cUntitled
else
Result := s;
end;
function SanitizeSegment(const Raw: string; const repl: char): string;
begin
Result := ReplaceOrRemoveInvalid(Raw, repl);
Result := WindowsFixups(Result);
Result := FixEmptyToUntitled(Result);
end;
function IsDirSep(const ch: char): boolean;
begin
// Accept both primary and alternate separators on all platforms
Result := (ch = TPath.DirectorySeparatorChar) or (ch = TPath.AltDirectorySeparatorChar);
end;
function SanitizeFullPath(const Raw: string; const repl: char): string;
var
i, segStart: integer;
seg, sep: string;
c: char;
// helper to flush the current segment to output
procedure FlushSegment;
begin
seg := copy(Raw, segStart, i - segStart);
Result := Result + SanitizeSegment(seg, repl) + sep;
sep := '';
segStart := i + 1;
end;
function IsVolumeSepHere: boolean;
begin
// treat "C:" drive as a boundary on Windows only
{$IFDEF MSWINDOWS}
Result := (i = 2) and (Raw[i] = TPath.VolumeSeparatorChar) and
(length(Raw) >= 2) and CharInSet(string(Raw[1]).ToUpper[1], ['A'..'Z']);
{$ELSE}
Result := False;
{$ENDIF}
end;
begin
Result := '';
if Raw = '' then
exit(FixEmptyToUntitled(Raw));
segStart := 1;
sep := '';
i := 1;
while i <= length(Raw) do
begin
c := Raw[i];
if IsDirSep(c) then
begin
sep := c; // keep the exact separator as in input
FlushSegment; // sanitize segment before the separator
end
else if IsVolumeSepHere then
begin
// keep "<drive>:"
Result := Result + copy(Raw, segStart, i - segStart + 1);
segStart := i + 1;
end;
Inc(i);
end;
// tail segment (no trailing separator)
seg := copy(Raw, segStart, length(Raw) - segStart + 1);
Result := Result + SanitizeSegment(seg, repl);
end;
begin
if not aHonorSeparators then
Result := SanitizeSegment(aRawName, aReplacement)
else
Result := SanitizeFullPath(aRawName, aReplacement);
end;
function ConvertToValidDirectoryName(const aText: string; aReplaceInvalidCharsWith: char): string;
begin
// Simple alias for backward compatibility
Result := SanitizeFileName(aText, aReplaceInvalidCharsWith);
end;
function LongFileNameFix(const aFileName: string): string;
const
MaxPathLength = 260;
LocalPrefix = '\\?\';
NetworkPrefix = '\\?\UNC\';
var
IsNetworkPath: boolean;
begin
Result := aFileName;
{$IFDEF MSWINDOWS}
// do we really have to use the long path format?
if length(Result) < MaxPathLength then
exit;
// Check if the filename is already prefixed
if startsText(LocalPrefix, Result) or startsText(NetworkPrefix, Result) then
exit;
IsNetworkPath := startsText('\\', Result);
if IsNetworkPath then
// Remove the leading '\\' for network paths to correctly append the UNC prefix
Result := copy(Result, 3, length(Result));
if IsNetworkPath then
Result := NetworkPrefix + Result
else
Result := LocalPrefix + Result;
{$ENDIF}
end;
function UnSlash(const aPath: string): string;
begin
if (aPath <> '') and (aPath[length(aPath)] = TPath.DirectorySeparatorChar) then
Result := copy(aPath, 1, length(aPath) - 1)
else
Result := aPath;
end;
function slash(const aPath: string): string;
begin
if (aPath <> '') and (aPath[length(aPath)] <> TPath.DirectorySeparatorChar) then
Result := aPath + TPath.DirectorySeparatorChar
else
Result := aPath;
end;
procedure SetEnvironmentPath(const aPath: string);
const
cName = 'PATH';
var
lEnvVarValue: string;
lPath: string;
begin
lPath := ExpandFileName(aPath);
lEnvVarValue := System.SysUtils.GetEnvironmentVariable(cName);
// Remove trailing semicolons
while endsText(';', lEnvVarValue) do
SetLength(lEnvVarValue, length(lEnvVarValue) - 1);
// Append new path
lEnvVarValue := lEnvVarValue + ';' + lPath;
// Use the appropriate function to set the environment variable
{$IFDEF MSWINDOWS}
if not winApi.Windows.SetEnvironmentVariable(PChar(cName), PChar(lEnvVarValue)) then
raise Exception.Create('Failed to set environment variable.');
{$ELSEIF DEFINED(POSIX)}
if setenv(pAnsiChar(AnsiString(cName)), pAnsiChar(AnsiString(lEnvVarValue)), 1) <> 0 then
raise Exception.Create('Failed to set environment variable.');
{$ELSE}
{$MESSAGE ERROR 'Not implemented for this platform!'}
{$ENDIF}
end;
{$IFDEF MSWINDOWS}
function FilePathToURL(const aFilePath: string): string;
var
lBufferLen: dword;
begin
lBufferLen := INTERNET_MAX_URL_LENGTH;
SetLength(Result, lBufferLen);
OleCheck(UrlCreateFromPath(PChar(aFilePath), PChar(Result), @lBufferLen, 0));
SetLength(Result, lBufferLen);
end;
{$ELSE}
function FilePathToURL(const aFilePath: string): string;
var
lLen: integer;
procedure append(const s: string);
begin
if length(Result) <= lLen + length(s) then
SetLength(Result, (lLen + length(s)) * 2);
for var X := 1 to length(s) do
Result[lLen + X] := s[X];
Inc(lLen, length(s));
end;
function makeHex(const c: char): string;
begin
Result := '%' + IntTohex(Ord(c));
end;
begin
Result := 'file:///';
lLen := length(Result);
SetLength(Result, length(aFilePath) * 3);
for var X := 1 to length(aFilePath) do
begin
if CharInSet(aFilePath[X], ['a'..'z', 'A'..'Z', '0'..'9', '-', '_', '/', ',', '.', ':', '!', '@']) then
append(aFilePath[X])
else if aFilePath[X] = '\' then
append('/')
else
append(makeHex(aFilePath[X]));
end;
SetLength(Result, lLen); // truncate
end;
{$ENDIF}
function NormalizePath(const aPath: string): string;
var
goodSlash: char;
s: string;
lDoubleSlash: string;
begin
s := aPath;
goodSlash := TPath.DirectorySeparatorChar;
lDoubleSlash := StringOfChar(goodSlash, 2);
if goodSlash = '/' then
s := Stringreplace(s, '\', goodSlash, [rfReplaceAll])
else
s := Stringreplace(s, '/', goodSlash, [rfReplaceAll]);
s := Stringreplace(s, lDoubleSlash, goodSlash, [rfReplaceAll]);
Result := s;
end;
function GetAnsiFileName(const aLongPath: string): string;
var
AnsiBuffer: array[0..max_path] of AnsiChar;
begin
{$IFNDEF MsWIndows}
Result := aLongPath;
{$ELSE}
WideCharToMultiByte(CP_ACP, 0, pWideChar(aLongPath), -1, AnsiBuffer, max_path, nil, nil);
Result := string(AnsiBuffer);
{$ENDIF}
end;
function GetShortPath(const aLongPath: string): string;
{$IFDEF MSWINDOWS}
var
Buffer: array[0..max_path] of char;
{$ENDIF}
begin
{$IFNDEF MsWIndows}
Result := aLongPath;
{$ELSE}
// Attempt to get the short path name
if GetShortPathName(PChar(aLongPath), Buffer, max_path) > 0 then
Result := Buffer
else
Result := aLongPath; // Fallback if conversion fails
{$ENDIF}
end;
function QuoteShellArgument(const aValue: string): string;
begin
{$IFDEF MSWINDOWS}
Result := QuoteWindowsShellArgument(aValue);
{$ELSE}
Result := QuoteUnixShellArgument(aValue);
{$ENDIF}
end;
function QuoteWindowsShellArgument(const aValue: string): string;
var
lNeedsQuoting: boolean;
i: integer;
begin
// Check if we need to quote the string at all
lNeedsQuoting := False;
for i := 1 to length(aValue) do
begin
if CharInSet(aValue[i], [' ', #9, #10, #13, '&', '|', '(', ')', '<', '>', '^', '"', '''']) then
begin
lNeedsQuoting := True;
break;
end;
end;
if not lNeedsQuoting and (aValue <> '') then
exit(aValue);
Result := QuotedStr(aValue);
end;
function QuoteUnixShellArgument(const aValue: string): string;
var
lNeedsQuoting: boolean;
i: integer;
c: char;
lBuilder: TStringBuilder;
begin
// Check if we need to quote the string at all
lNeedsQuoting := False;
for i := 1 to length(aValue) do
begin
c := aValue[i];
if CharInSet(c, [' ', #9, #10, #13, ';', '&', '|', '<', '>', '(', ')', '$', '`', '\', '"', '''', #0]) then
begin
lNeedsQuoting := True;
break;
end;
end;
if not lNeedsQuoting then
exit(aValue);
// Create string builder with extra capacity for escaping
gc(lBuilder, TStringBuilder.Create(length(aValue) * 2 + 2));
// Use single quotes for bash - simplest and safest escape method for most cases
lBuilder.append('''');
// Special handling for single quotes - bash can't escape within single quotes
// The technique is to close the quote, insert the quote with different quoting,
// then reopen the single quote
for i := 1 to length(aValue) do
begin
c := aValue[i];
if c = '''' then
lBuilder.append('''\''''') // Close quote, escaped quote, reopen quote
else
lBuilder.append(c);
end;
lBuilder.append('''');
Result := lBuilder.ToString;
end;
function GetLocalAppDataPath: string;
begin
{$IF Defined(MSWINDOWS)}
Result := GetEnvironmentVariable('AppData');
{$ELSEIF Defined(MACOS)}
Result := TPath.Combine(TPath.GetHomePath, 'Library/Application Support');
{$ELSEIF Defined(LINUX)}
Result := GetEnvironmentVariable('XDG_CONFIG_HOME');
if Result.IsEmpty then
Result := TPath.Combine(TPath.GetHomePath, '.config');
{$ELSEIF Defined(ANDROID)}
Result := TPath.GetDocumentsPath; // or use GetSharedDownloadsPath for shared app data
{$ELSE}
raise Exception.Create('Unsupported platform for GetLocalAppDataPath');
{$ENDIF}
end;
{$IFDEF MSWINDOWS}
function GetSpecialWindowsFolder(aFolder: integer): string;
var
lPath: array[0..max_path] of char;