-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathQuick.Commons.pas
More file actions
3171 lines (2845 loc) · 91.6 KB
/
Quick.Commons.pas
File metadata and controls
3171 lines (2845 loc) · 91.6 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
{ ***************************************************************************
Copyright (c) 2016-2025 Kike Pérez
Unit : Quick.Commons
Description : Common functions
Author : Kike Pérez
Version : 2.0
Created : 14/07/2017
Modified : 01/03/2026
This file is part of QuickLib: https://github.com/exilon/QuickLib
***************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************** }
unit Quick.Commons;
{$i QuickLib.inc}
interface
uses
Classes,
SysUtils,
Types,
{$IFDEF MSWINDOWS}
Windows,
ActiveX,
ShlObj,
{$ENDIF MSWINDOWS}
{$IFDEF FPC}
Quick.Files,
{$IFDEF LINUX}
FileInfo,
BaseUnix,
{$ENDIF}
{$ELSE}
IOUtils,
{$ENDIF}
{$IFDEF ANDROID}
Androidapi.JNI.Os,
Androidapi.Helpers,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.GraphicsContentViewText,
{$IFDEF DELPHIRX103_UP}
Androidapi.JNI.App,
{$ENDIF}
{$ENDIF}
{$IFDEF IOS}
iOSapi.UIKit,
Posix.SysSysctl,
Posix.StdDef,
iOSapi.Foundation,
Macapi.ObjectiveC,
Macapi.Helpers,
{$ENDIF}
{$IFDEF OSX}
Macapi.Foundation,
Macapi.Helpers,
FMX.Helpers.Mac,
Macapi.ObjectiveC,
{$ENDIF}
{$IFDEF POSIX}
Posix.Unistd,
Posix.Pwd,
{$ENDIF}
DateUtils;
type
/// <summary>
/// Defines the different types of log events available in the logging system.
/// </summary>
TLogEventType = (etInfo, etSuccess, etWarning, etError, etDebug, etDone, etTrace, etCritical, etException);
/// <summary>
/// Set of TLogEventType used to define which log event types should be processed.
/// </summary>
TLogVerbose = set of TLogEventType;
const
/// <summary>Log verbosity level that only captures informational and error events.</summary>
LOG_ONLYERRORS = [etInfo,etError];
/// <summary>Log verbosity level that captures informational, warning, and error events.</summary>
LOG_ERRORSANDWARNINGS = [etInfo,etWarning,etError];
/// <summary>Log verbosity level that captures informational, error, warning, and trace events.</summary>
LOG_TRACE = [etInfo,etError,etWarning,etTrace];
/// <summary>Log verbosity level that captures all event types.</summary>
LOG_ALL = [etInfo, etSuccess, etWarning, etError, etDebug, etDone, etTrace, etCritical, etException];
/// <summary>Log verbosity level that captures debug-related events.</summary>
LOG_DEBUG = [etInfo,etSuccess,etWarning,etError,etDebug];
{$IFDEF DELPHIXE7_UP}
/// <summary>Array of string representations for each TLogEventType.</summary>
EventStr : array of string = ['INFO','SUCC','WARN','ERROR','DEBUG','DONE','TRACE','CRITICAL','EXCEPTION'];
{$ELSE}
EventStr : array[0..8] of string = ('INFO','SUCC','WARN','ERROR','DEBUG','DONE','TRACE','CRITICAL','EXCEPTION');
{$ENDIF}
/// <summary>Carriage Return + Line Feed constant for line breaks.</summary>
CRLF = #13#10;
type
/// <summary>
/// Defines password complexity requirements.
/// </summary>
/// <remarks>
/// pfIncludeNumbers: Include numeric digits in the password
/// pfIncludeSigns: Include special characters in the password
/// </remarks>
TPasswordComplexity = set of (pfIncludeNumbers,pfIncludeSigns);
/// <summary>
/// Record containing paths to common environment and system directories.
/// </summary>
/// <remarks>
/// Provides easy access to frequently used system paths such as temp directories,
/// desktop, program files, etc. Automatically populated at initialization.
/// </remarks>
TEnvironmentPath = record
/// <summary>Path to the executable directory.</summary>
EXEPATH : string;
{$IFDEF MSWINDOWS}
/// <summary>Windows system directory path.</summary>
WINDOWS : string;
/// <summary>Windows system32 directory path.</summary>
SYSTEM : string;
/// <summary>Program Files directory path.</summary>
PROGRAMFILES : string;
/// <summary>Common Files directory path.</summary>
COMMONFILES : string;
/// <summary>System drive (typically C:).</summary>
HOMEDRIVE : string;
/// <summary>Temporary files directory path.</summary>
TEMP : string;
/// <summary>User profile directory path.</summary>
USERPROFILE : string;
/// <summary>Installation drive.</summary>
INSTDRIVE : string;
/// <summary>Current user's desktop directory path.</summary>
DESKTOP : string;
/// <summary>Current user's start menu directory path.</summary>
STARTMENU : string;
/// <summary>All users desktop directory path.</summary>
DESKTOP_ALLUSERS : string;
/// <summary>All users start menu directory path.</summary>
STARTMENU_ALLUSERS : string;
/// <summary>Startup directory path.</summary>
STARTUP : string;
/// <summary>Application data directory path.</summary>
APPDATA : String;
/// <summary>ProgramData directory path.</summary>
PROGRAMDATA : string;
/// <summary>All users profile directory path.</summary>
ALLUSERSPROFILE : string;
{$ENDIF MSWINDOWS}
end;
{$IFNDEF FPC}
/// <summary>
/// Helper record for TFile class providing additional file operations.
/// </summary>
TFileHelper = record helper for TFile
{$IF DEFINED(MSWINDOWS) OR DEFINED(DELPHILINUX)}
/// <summary>
/// Checks if a file is currently in use by another process.
/// </summary>
/// <param name="FileName">Full path to the file to check.</param>
/// <returns>True if the file is in use, False otherwise.</returns>
class function IsInUse(const FileName : string) : Boolean; static;
{$ENDIF}
/// <summary>
/// Gets the size of a file in bytes.
/// </summary>
/// <param name="FileName">Full path to the file.</param>
/// <returns>File size in bytes, or -1 if the file doesn't exist.</returns>
class function GetSize(const FileName: String): Int64; static;
end;
/// <summary>
/// Helper record for TDirectory class providing additional directory operations.
/// </summary>
TDirectoryHelper = record helper for TDirectory
/// <summary>
/// Calculates the total size of all files in a directory.
/// </summary>
/// <param name="Path">Full path to the directory.</param>
/// <returns>Total size in bytes of all files in the directory.</returns>
class function GetSize(const Path: String): Int64; static;
end;
{$ENDIF}
{$IFDEF FPC}
{$IFDEF LINUX}
UINT = cardinal;
{$ENDIF}
PLASTINPUTINFO = ^LASTINPUTINFO;
tagLASTINPUTINFO = record
cbSize: UINT;
dwTime: DWORD;
end;
LASTINPUTINFO = tagLASTINPUTINFO;
TLastInputInfo = LASTINPUTINFO;
type
TCmdLineSwitchType = (clstValueNextParam, clstValueAppended);
TCmdLineSwitchTypes = set of TCmdLineSwitchType;
{$ENDIF}
/// <summary>
/// Counter record that tracks iterations up to a maximum value.
/// </summary>
/// <remarks>
/// Provides a simple mechanism to count up to a specific value and check when
/// the maximum is reached. Automatically resets after reaching the maximum.
/// </remarks>
TCounter = record
private
fMaxValue : Integer;
fCurrentValue : Integer;
public
/// <summary>Gets the maximum value this counter can reach.</summary>
property MaxValue : Integer read fMaxValue;
/// <summary>
/// Initializes the counter with a maximum value.
/// </summary>
/// <param name="aMaxValue">Maximum count value.</param>
procedure Init(aMaxValue : Integer);
/// <summary>
/// Returns the current count value.
/// </summary>
/// <returns>Current count value.</returns>
function Count : Integer;
/// <summary>
/// Checks if current count equals a specific value.
/// </summary>
/// <param name="aValue">Value to compare against.</param>
/// <returns>True if current count equals aValue.</returns>
function CountIs(aValue : Integer) : Boolean;
/// <summary>
/// Increments the counter and checks if maximum is reached.
/// </summary>
/// <returns>True if maximum value is reached (counter resets), False otherwise.</returns>
function Check : Boolean;
/// <summary>
/// Resets the counter to zero.
/// </summary>
procedure Reset;
end;
/// <summary>
/// Time-based counter that tracks elapsed time in milliseconds.
/// </summary>
/// <remarks>
/// Useful for implementing time-based events or throttling operations.
/// Checks if a specific time interval has elapsed since last reset.
/// </remarks>
TTimeCounter = record
private
fCurrentTime : TDateTime;
fDoneEvery : Integer;
public
/// <summary>Gets the time interval in milliseconds.</summary>
property DoneEvery : Integer read fDoneEvery;
/// <summary>
/// Initializes the time counter with a millisecond interval.
/// </summary>
/// <param name="MillisecondsToReach">Time interval in milliseconds.</param>
procedure Init(MillisecondsToReach : Integer);
/// <summary>
/// Checks if the time interval has elapsed.
/// </summary>
/// <returns>True if the interval has passed (counter resets), False otherwise.</returns>
function Check : Boolean;
/// <summary>
/// Resets the counter to current time.
/// </summary>
procedure Reset;
end;
{$IFNDEF FPC}
{$IFNDEF DELPHIXE7_UP}
TArrayUtil<T> = class
class procedure Delete(var aArray : TArray<T>; aIndex : Integer);
end;
{$ENDIF}
/// <summary>
/// Helper record extending TArray<string> with utility methods.
/// </summary>
TArrayOfStringHelper = record helper for TArray<string>
public
/// <summary>Checks if the array has any elements.</summary>
/// <returns>True if array is not empty.</returns>
function Any : Boolean; overload;
/// <summary>Checks if the array contains a specific value.</summary>
/// <param name="aValue">Value to search for.</param>
/// <returns>True if value exists in array.</returns>
function Any(const aValue : string) : Boolean; overload;
/// <summary>Adds a value to the array.</summary>
/// <param name="aValue">Value to add.</param>
/// <returns>Index of the added element.</returns>
function Add(const aValue : string) : Integer;
/// <summary>Adds a value only if it doesn't already exist.</summary>
/// <param name="aValue">Value to add.</param>
/// <param name="aCaseSense">Whether comparison is case-sensitive.</param>
/// <returns>Index of the element (existing or newly added).</returns>
function AddIfNotExists(const aValue : string; aCaseSense : Boolean = False) : Integer;
/// <summary>Removes a value from the array.</summary>
/// <param name="aValue">Value to remove.</param>
/// <returns>True if value was found and removed.</returns>
function Remove(const aValue : string) : Boolean;
/// <summary>Checks if a value exists in the array.</summary>
/// <param name="aValue">Value to search for.</param>
/// <returns>True if value exists.</returns>
function Exists(const aValue : string) : Boolean;
/// <summary>Gets the number of elements in the array.</summary>
/// <returns>Number of elements.</returns>
function Count : Integer;
end;
TDelegate<T> = reference to procedure(Value : T);
{$ENDIF}
/// <summary>
/// Record representing a name-value pair.
/// </summary>
TPairItem = record
/// <summary>Name/key of the pair.</summary>
Name : string;
/// <summary>Value associated with the name.</summary>
Value : string;
/// <summary>
/// Creates a new pair item with specified name and value.
/// </summary>
/// <param name="aName">Name/key of the pair.</param>
/// <param name="aValue">Value of the pair.</param>
constructor Create(const aName, aValue : string);
end;
/// <summary>
/// List class for managing name-value pairs.
/// </summary>
/// <remarks>
/// Provides dictionary-like functionality using a dynamic array of TPairItem.
/// Supports enumeration and indexed access by name.
/// </remarks>
TPairList = class
type
/// <summary>
/// Enumerator for iterating over pair items.
/// </summary>
TPairEnumerator = class
private
fArray : ^TArray<TPairItem>;
fIndex : Integer;
function GetCurrent: TPairItem;
public
constructor Create(var aArray: TArray<TPairItem>);
property Current : TPairItem read GetCurrent;
function MoveNext: Boolean;
end;
private
fItems : TArray<TPairItem>;
public
/// <summary>Gets an enumerator for the list.</summary>
function GetEnumerator : TPairEnumerator;
/// <summary>Gets the value for a given name.</summary>
/// <param name="aName">Name to search for.</param>
/// <returns>Value associated with the name, or empty string if not found.</returns>
function GetValue(const aName : string) : string;
/// <summary>Gets the pair item for a given name.</summary>
/// <param name="aName">Name to search for.</param>
/// <returns>TPairItem with the specified name.</returns>
function GetPair(const aName : string) : TPairItem;
/// <summary>Adds a pair item to the list.</summary>
/// <param name="aPair">Pair item to add.</param>
/// <returns>Index of the added item.</returns>
function Add(aPair : TPairItem) : Integer; overload;
/// <summary>Adds a name-value pair to the list.</summary>
/// <param name="aName">Name/key of the pair.</param>
/// <param name="aValue">Value of the pair.</param>
/// <returns>Index of the added item.</returns>
function Add(const aName, aValue : string) : Integer; overload;
/// <summary>Adds or updates a name-value pair.</summary>
/// <param name="aName">Name/key of the pair.</param>
/// <param name="aValue">Value to set.</param>
procedure AddOrUpdate(const aName, aValue : string);
/// <summary>Checks if a name exists in the list.</summary>
/// <param name="aName">Name to search for.</param>
/// <returns>True if name exists.</returns>
function Exists(const aName : string) : Boolean;
/// <summary>Removes a pair by name.</summary>
/// <param name="aName">Name of the pair to remove.</param>
/// <returns>True if pair was found and removed.</returns>
function Remove(const aName : string) : Boolean;
/// <summary>Gets the number of pairs in the list.</summary>
/// <returns>Number of pairs.</returns>
function Count : Integer;
/// <summary>Default indexed property for accessing values by name.</summary>
property Items[const aName : string] : string read GetValue write AddOrUpdate;
/// <summary>Converts the list to an array of pairs.</summary>
/// <returns>Array containing all pair items.</returns>
function ToArray : TArray<TPairItem>;
/// <summary>Populates the list from an array of pairs.</summary>
/// <param name="aValue">Array of pair items to load.</param>
procedure FromArray(aValue : TArray<TPairItem>);
/// <summary>Removes all pairs from the list.</summary>
procedure Clear;
end;
{$IFDEF DELPHIXE7_UP}
/// <summary>
/// Helper record extending TDateTime with utility methods.
/// </summary>
TDateTimeHelper = record helper for TDateTime
public
/// <summary>Converts the datetime to SQL format string (YYYY-MM-DD hh:mm:ss).</summary>
function ToSQLString : string;
/// <summary>Sets the datetime to current time.</summary>
procedure FromNow;
/// <summary>Converts UTC time to local time.</summary>
/// <param name="aUTCTime">UTC datetime value.</param>
procedure FromUTC(const aUTCTime : TDateTime);
/// <summary>Increments the datetime by specified number of days.</summary>
/// <param name="aValue">Number of days to increment (default 1).</param>
function IncDay(const aValue : Cardinal = 1) : TDateTime;
/// <summary>Decrements the datetime by specified number of days.</summary>
/// <param name="aValue">Number of days to decrement (default 1).</param>
function DecDay(const aValue : Cardinal = 1) : TDateTime;
/// <summary>Increments the datetime by specified number of months.</summary>
/// <param name="aValue">Number of months to increment (default 1).</param>
function IncMonth(const aValue : Cardinal = 1) : TDateTime;
/// <summary>Decrements the datetime by specified number of months.</summary>
/// <param name="aValue">Number of months to decrement (default 1).</param>
function DecMonth(const aValue : Cardinal = 1) : TDateTime;
/// <summary>Increments the datetime by specified number of years.</summary>
/// <param name="aValue">Number of years to increment (default 1).</param>
function IncYear(const aValue : Cardinal = 1) : TDateTime;
/// <summary>Decrements the datetime by specified number of years.</summary>
/// <param name="aValue">Number of years to decrement (default 1).</param>
function DecYear(const aValue : Cardinal = 1) : TDateTime;
/// <summary>Checks if this datetime equals another datetime.</summary>
function IsEqualTo(const aDateTime : TDateTime) : Boolean;
/// <summary>Checks if this datetime is after another datetime.</summary>
function IsAfter(const aDateTime : TDateTime) : Boolean;
/// <summary>Checks if this datetime is before another datetime.</summary>
function IsBefore(const aDateTime : TDateTime) : Boolean;
/// <summary>Checks if this datetime is on the same day as another datetime.</summary>
function IsSameDay(const aDateTime : TDateTime) : Boolean;
/// <summary>Checks if this datetime has the same time as another time.</summary>
function IsSameTime(const aTime : TTime) : Boolean;
/// <summary>Gets the day of the week (1-7).</summary>
function DayOfTheWeek : Word;
/// <summary>Converts datetime to JSON/ISO 8601 format string.</summary>
function ToJsonFormat : string;
/// <summary>Converts datetime to GMT format string.</summary>
function ToGMTFormat: string;
/// <summary>Converts datetime to TimeStamp.</summary>
function ToTimeStamp : TTimeStamp;
/// <summary>Converts local datetime to UTC.</summary>
function ToUTC : TDateTime;
/// <summary>Converts datetime to milliseconds.</summary>
function ToMilliseconds : Int64;
/// <summary>Converts datetime to string.</summary>
function ToString : string;
/// <summary>Extracts the date part of the datetime.</summary>
function Date : TDate;
/// <summary>Extracts the time part of the datetime.</summary>
function Time : TTime;
/// <summary>Checks if time is in AM (ante meridiem).</summary>
function IsAM : Boolean;
/// <summary>Checks if time is in PM (post meridiem).</summary>
function IsPM : Boolean;
end;
/// <summary>
/// Helper record extending TDate with utility methods.
/// </summary>
TDateHelper = record helper for TDate
public
/// <summary>Converts date to string.</summary>
function ToString : string;
end;
/// <summary>
/// Helper record extending TTime with utility methods.
/// </summary>
TTimeHelper = record helper for TTime
public
/// <summary>Converts time to string.</summary>
function ToString : string;
end;
{$ENDIF}
/// <summary>Exception raised when environment path operations fail.</summary>
EEnvironmentPath = class(Exception);
/// <summary>Exception raised when shell operations fail.</summary>
EShellError = class(Exception);
/// <summary>
/// Generates a random password with specified complexity requirements.
/// </summary>
/// <param name="PasswordLength">Desired length of the password.</param>
/// <param name="Complexity">Set of complexity requirements (numbers, signs).</param>
/// <returns>Randomly generated password string.</returns>
function RandomPassword(const PasswordLength : Integer; Complexity : TPasswordComplexity = [pfIncludeNumbers,pfIncludeSigns]) : string;
/// <summary>
/// Generates a random alphanumeric string.
/// </summary>
/// <param name="aLength">Length of the string to generate.</param>
/// <returns>Random string containing letters and numbers.</returns>
function RandomString(const aLength: Integer) : string;
/// <summary>
/// Extracts filename without extension from a full path.
/// </summary>
/// <param name="FileName">Full path or filename.</param>
/// <returns>Filename without extension.</returns>
function ExtractFileNameWithoutExt(const FileName: string): string;
/// <summary>
/// Converts Unix-style path (/) to Windows-style path (\).
/// </summary>
/// <param name="UnixPath">Path with Unix separators.</param>
/// <returns>Path with Windows separators.</returns>
function UnixToWindowsPath(const UnixPath: string): string;
/// <summary>
/// Converts Windows-style path (\) to Unix-style path (/).
/// </summary>
/// <param name="WindowsPath">Path with Windows separators.</param>
/// <returns>Path with Unix separators.</returns>
function WindowsToUnixPath(const WindowsPath: string): string;
/// <summary>
/// Corrects malformed URLs by fixing slashes and encoding spaces.
/// </summary>
/// <param name="cUrl">URL to correct.</param>
/// <returns>Corrected URL.</returns>
function CorrectURLPath(const cUrl : string) : string;
/// <summary>Extracts the protocol from a URL (e.g., 'http', 'https').</summary>
function UrlGetProtocol(const aUrl : string) : string;
/// <summary>Extracts the host from a URL.</summary>
function UrlGetHost(const aUrl : string) : string;
/// <summary>Extracts the path from a URL.</summary>
function UrlGetPath(const aUrl : string) : string;
/// <summary>Extracts the query string from a URL.</summary>
function UrlGetQuery(const aUrl : string) : string;
/// <summary>Removes the protocol part from a URL.</summary>
function UrlRemoveProtocol(const aUrl : string) : string;
/// <summary>Removes the query string part from a URL.</summary>
function UrlRemoveQuery(const aUrl : string) : string;
/// <summary>Performs simple URL encoding (spaces to %20).</summary>
function UrlSimpleEncode(const aUrl : string) : string;
/// <summary>
/// Populates the global 'path' variable with common environment paths.
/// </summary>
/// <remarks>
/// Automatically called during unit initialization. Fills paths like TEMP, DESKTOP, etc.
/// </remarks>
procedure GetEnvironmentPaths;
{$IFDEF MSWINDOWS}
/// <summary>
/// Gets a special folder path by its CSIDL identifier.
/// </summary>
/// <param name="folderID">CSIDL folder identifier (e.g., CSIDL_DESKTOP).</param>
/// <returns>Full path to the special folder.</returns>
function GetSpecialFolderPath(folderID : Integer) : string;
/// <summary>Checks if the operating system is 64-bit.</summary>
/// <returns>True if running on 64-bit OS.</returns>
function Is64bitOS : Boolean;
/// <summary>Checks if the application is compiled as a console application.</summary>
/// <returns>True if console application.</returns>
function IsConsole : Boolean;
/// <summary>Checks if the application has console output available.</summary>
/// <returns>True if console output is available.</returns>
function HasConsoleOutput : Boolean;
{$ENDIF}
/// <summary>Checks if the application is compiled in debug mode.</summary>
/// <returns>True if compiled with DEBUG directive.</returns>
function IsDebug : Boolean;
{$IFDEF MSWINDOWS}
/// <summary>Checks if the application is running as a Windows service.</summary>
/// <returns>True if running as a service.</returns>
function IsService : Boolean;
/// <summary>
/// Gets the number of seconds since last user input (keyboard or mouse).
/// </summary>
/// <returns>Seconds of idle time.</returns>
function SecondsIdle: DWord;
/// <summary>
/// Frees unused process memory to reduce memory footprint.
/// </summary>
procedure FreeUnusedMem;
/// <summary>
/// Changes the screen resolution.
/// </summary>
/// <param name="Width">Desired screen width in pixels.</param>
/// <param name="Height">Desired screen height in pixels.</param>
/// <returns>Result code from ChangeDisplaySettings.</returns>
function SetScreenResolution(Width, Height: integer): Longint;
{$ENDIF MSWINDOWS}
/// <summary>Returns the last day of the current month.</summary>
/// <returns>TDateTime representing the last day of current month.</returns>
function LastDayCurrentMonth: TDateTime;
{$IFDEF FPC}
/// <summary>
/// Checks if a datetime is within a specified range.
/// </summary>
/// <param name="aInclusive">If true, range endpoints are included.</param>
function DateTimeInRange(ADateTime: TDateTime; AStartDateTime, AEndDateTime: TDateTime; aInclusive: Boolean = True): Boolean;
{$ENDIF}
/// <summary>
/// Checks if two datetimes are on the same day.
/// </summary>
/// <param name="cBefore">First datetime to compare.</param>
/// <param name="cNow">Second datetime to compare.</param>
/// <returns>True if both datetimes are on the same day.</returns>
function IsSameDay(cBefore, cNow : TDateTime) : Boolean;
/// <summary>
/// Changes the time portion of a datetime value.
/// </summary>
/// <param name="aDate">Original datetime.</param>
/// <param name="aHour">New hour value.</param>
/// <param name="aMinute">New minute value.</param>
/// <param name="aSecond">New second value.</param>
/// <param name="aMilliSecond">New millisecond value (default 0).</param>
/// <returns>DateTime with modified time.</returns>
function ChangeTimeOfADay(aDate : TDateTime; aHour, aMinute, aSecond : Word; aMilliSecond : Word = 0) : TDateTime;
/// <summary>
/// Changes the date portion of a datetime value.
/// </summary>
/// <param name="aDate">Original datetime.</param>
/// <param name="aYear">New year value.</param>
/// <param name="aMonth">New month value.</param>
/// <param name="aDay">New day value.</param>
/// <returns>DateTime with modified date.</returns>
function ChangeDateOfADay(aDate : TDateTime; aYear, aMonth, aDay : Word) : TDateTime;
/// <summary>
/// Creates a string by repeating a character.
/// </summary>
/// <param name="C">Character to repeat.</param>
/// <param name="Count">Number of repetitions.</param>
/// <returns>String containing the repeated character.</returns>
function FillStr(const C : Char; const Count : Integer) : string;
/// <summary>
/// Creates a string by repeating a string value.
/// </summary>
/// <param name="value">String to repeat.</param>
/// <param name="Count">Number of repetitions.</param>
/// <returns>String containing the repeated value.</returns>
function FillStrEx(const value : string; const Count : Integer) : string;
/// <summary>
/// Checks if a string exists in an array of strings.
/// </summary>
/// <param name="aValue">Value to search for.</param>
/// <param name="aInArray">Array to search in.</param>
/// <param name="aCaseSensitive">Whether comparison is case-sensitive (default True).</param>
/// <returns>True if value is found in array.</returns>
function StrInArray(const aValue : string; const aInArray : array of string; aCaseSensitive : Boolean = True) : Boolean;
/// <summary>
/// Checks if an integer exists in an array of integers.
/// </summary>
/// <param name="aValue">Value to search for.</param>
/// <param name="aInArray">Array to search in.</param>
/// <returns>True if value is found in array.</returns>
function IntInArray(const aValue : Integer; const aInArray : array of Integer) : Boolean;
/// <summary>Checks if a string array is empty.</summary>
function IsEmptyArray(aArray : TArray<string>) : Boolean; overload;
/// <summary>Checks if an integer array is empty.</summary>
function IsEmptyArray(aArray : TArray<Integer>) : Boolean; overload;
/// <summary>
/// Returns a number as a string with leading zeros.
/// </summary>
/// <param name="Number">Number to format.</param>
/// <param name="Len">Desired minimum length.</param>
/// <returns>String with leading zeros.</returns>
function Zeroes(const Number, Len : Int64) : string;
/// <summary>
/// Converts a number to a string with thousand delimiters.
/// </summary>
/// <param name="Number">Number to format.</param>
/// <returns>Formatted number string (e.g., "1,000,000").</returns>
function NumberToStr(const Number : Int64) : string;
/// <summary>
/// Creates a string of spaces.
/// </summary>
/// <param name="Count">Number of spaces.</param>
/// <returns>String containing spaces.</returns>
function Spaces(const Count : Integer) : string;
/// <summary>Returns the current date and time as a string.</summary>
function NowStr : string;
/// <summary>
/// Generates a new GUID and returns it as a string.
/// </summary>
/// <returns>GUID string in format {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}.</returns>
function NewGuidStr : string;
/// <summary>
/// Compares a string with a wildcard pattern.
/// </summary>
/// <param name="cText">Text to match.</param>
/// <param name="Pattern">Pattern with wildcards (* for multiple chars, ? for single char).</param>
/// <returns>True if text matches pattern.</returns>
function IsLike(cText, Pattern: string) : Boolean;
/// <summary>
/// Capitalizes the first letter of a string.
/// </summary>
/// <param name="s">String to capitalize.</param>
/// <returns>String with first letter uppercase.</returns>
function Capitalize(s: string): string;
/// <summary>
/// Capitalizes the first letter of each word in a string.
/// </summary>
/// <param name="s">String to capitalize.</param>
/// <returns>String with each word capitalized.</returns>
function CapitalizeWords(s: string): string;
/// <summary>Gets the currently logged-in username.</summary>
/// <returns>Username string.</returns>
function GetLoggedUserName : string;
/// <summary>Gets the computer name.</summary>
/// <returns>Computer name string.</returns>
function GetComputerName : string;
{$IFDEF MSWINDOWS}
/// <summary>Checks if the session is a remote desktop connection.</summary>
/// <returns>True if remote desktop session.</returns>
function IsRemoteSession : Boolean;
{$ENDIF}
/// <summary>
/// Extracts domain and username from a login string.
/// </summary>
/// <param name="aUser">User string (formats: DOMAIN\User or User@domain.com).</param>
/// <param name="oDomain">Output parameter for domain.</param>
/// <param name="oUser">Output parameter for username.</param>
/// <returns>True if domain was extracted, False otherwise.</returns>
function ExtractDomainAndUser(const aUser : string; out oDomain, oUser : string) : Boolean;
/// <summary>
/// Normalizes path delimiters to a specific character.
/// </summary>
/// <param name="cPath">Path to normalize.</param>
/// <param name="Delim">Desired delimiter character (\ or /).</param>
/// <returns>Path with normalized delimiters.</returns>
function NormalizePathDelim(const cPath : string; const Delim : Char) : string;
/// <summary>
/// Combines two paths with a specified delimiter.
/// </summary>
/// <param name="aFirstPath">First path segment.</param>
/// <param name="aSecondPath">Second path segment.</param>
/// <param name="aDelim">Path delimiter to use.</param>
/// <returns>Combined path.</returns>
function CombinePaths(const aFirstPath, aSecondPath: string; aDelim : Char): string;
/// <summary>
/// Removes the first segment from a path.
/// </summary>
/// <param name="cdir">Path to process.</param>
/// <returns>Path without first segment.</returns>
function RemoveFirstPathSegment(const cdir : string) : string;
/// <summary>
/// Removes the last segment from a path.
/// </summary>
/// <param name="cDir">Path to process.</param>
/// <returns>Path without last segment.</returns>
function RemoveLastPathSegment(const cDir : string) : string;
/// <summary>
/// Detects and returns the path delimiter used in a path.
/// </summary>
/// <param name="aPath">Path to analyze.</param>
/// <returns>Path delimiter (\ or /) or empty string if none found.</returns>
function GetPathDelimiter(const aPath : string) : string;
/// <summary>
/// Extracts the first segment from a path.
/// </summary>
/// <param name="aPath">Path to process.</param>
/// <returns>First path segment.</returns>
function GetFirstPathSegment(const aPath : string) : string;
/// <summary>
/// Extracts the last segment from a path.
/// </summary>
/// <param name="aPath">Path to process.</param>
/// <returns>Last path segment.</returns>
function GetLastPathSegment(const aPath : string) : string;
/// <summary>
/// Checks if a command-line switch exists.
/// </summary>
/// <param name="Switch">Switch name to find (without - or /).</param>
/// <returns>True if switch was found in command line parameters.</returns>
function ParamFindSwitch(const Switch : string) : Boolean;
/// <summary>
/// Gets the value of a command-line switch.
/// </summary>
/// <param name="Switch">Switch name (without - or /).</param>
/// <param name="cvalue">Output parameter for switch value.</param>
/// <returns>True if switch was found and value retrieved.</returns>
function ParamGetSwitch(const Switch : string; var cvalue : string) : Boolean;
/// <summary>
/// Gets the application name based on the executable filename.
/// </summary>
/// <returns>Application name without extension.</returns>
function GetAppName : string;
/// <summary>
/// Gets the application version string (major.minor).
/// </summary>
/// <returns>Version string (e.g., "1.0").</returns>
function GetAppVersionStr: string;
/// <summary>
/// Gets the full application version string (major.minor.release.build).
/// </summary>
/// <returns>Full version string (e.g., "1.0.0.0").</returns>
function GetAppVersionFullStr: string;
/// <summary>
/// Converts UTC datetime to local datetime.
/// </summary>
/// <param name="GMTTime">UTC datetime value.</param>
/// <returns>Local datetime value.</returns>
function UTCToLocalTime(GMTTime: TDateTime): TDateTime;
/// <summary>
/// Converts local datetime to UTC datetime.
/// </summary>
/// <param name="LocalTime">Local datetime value.</param>
/// <returns>UTC datetime value.</returns>
function LocalTimeToUTC(LocalTime : TDateTime): TDateTime;
/// <summary>
/// Converts datetime to GMT format string.
/// </summary>
/// <param name="aDate">DateTime to convert.</param>
/// <returns>GMT formatted string.</returns>
function DateTimeToGMT(aDate : TDateTime) : string;
/// <summary>
/// Converts GMT format string to datetime.
/// </summary>
/// <param name="aDate">GMT formatted string.</param>
/// <returns>TDateTime value.</returns>
function GMTToDateTime(aDate : string) : TDateTime;
/// <summary>
/// Converts datetime to JSON/ISO 8601 date format.
/// </summary>
/// <param name="aDateTime">DateTime to convert.</param>
/// <returns>ISO 8601 formatted string.</returns>
function DateTimeToJsonDate(aDateTime : TDateTime) : string;
/// <summary>
/// Converts JSON/ISO 8601 date format to datetime.
/// </summary>
/// <param name="aJsonDate">ISO 8601 formatted string.</param>
/// <returns>TDateTime value.</returns>
function JsonDateToDateTime(const aJsonDate : string) : TDateTime;
/// <summary>
/// Counts the number of digits in an integer.
/// </summary>
/// <param name="anInt">Integer to analyze.</param>
/// <returns>Number of digits.</returns>
function CountDigits(anInt: Cardinal): Cardinal; inline;
/// <summary>
/// Counts occurrences of a substring in a string.
/// </summary>
/// <param name="aFindStr">Substring to find.</param>
/// <param name="aSourceStr">String to search in.</param>
/// <returns>Number of occurrences.</returns>
function CountStr(const aFindStr, aSourceStr : string) : Integer;
/// <summary>
/// Saves a stream to a file.
/// </summary>
/// <param name="aStream">Stream to save.</param>
/// <param name="aFilename">Target filename.</param>
procedure SaveStreamToFile(aStream : TStream; const aFilename : string);
/// <summary>
/// Converts a stream to a string using specified encoding.
/// </summary>
/// <param name="aStream">Stream to convert.</param>
/// <param name="aEncoding">Text encoding to use.</param>
/// <returns>String content.</returns>
function StreamToString(const aStream: TStream; const aEncoding: TEncoding): string;
/// <summary>
/// Converts a stream to a string (auto-detecting type).
/// </summary>
/// <param name="aStream">Stream to convert.</param>
/// <returns>String content.</returns>
function StreamToStringEx(aStream : TStream) : string;
/// <summary>
/// Writes a string to a stream using specified encoding.
/// </summary>
/// <param name="aStr">String to write.</param>
/// <param name="aStream">Target stream.</param>
/// <param name="aEncoding">Text encoding to use.</param>
procedure StringToStream(const aStr : string; aStream : TStream; const aEncoding: TEncoding);
/// <summary>
/// Writes a string to a stream directly.
/// </summary>
/// <param name="aStr">String to write.</param>
/// <param name="aStream">Target stream.</param>
procedure StringToStreamEx(const aStr : string; aStream : TStream);
/// <summary>
/// Converts a TStringList to a comma-separated string.
/// </summary>
/// <param name="aList">StringList to convert.</param>
/// <returns>Comma-separated string.</returns>
function CommaText(aList : TStringList) : string; overload;
/// <summary>
/// Converts an array of strings to a comma-separated string.
/// </summary>
/// <param name="aArray">Array to convert.</param>
/// <returns>Comma-separated string.</returns>
function CommaText(aArray : TArray<string>) : string; overload;
/// <summary>
/// Converts an array of strings to a CRLF-separated string.
/// </summary>
/// <param name="aArray">Array to convert.</param>
/// <returns>CRLF-separated string.</returns>
function ArrayToString(aArray : TArray<string>) : string; overload;
/// <summary>
/// Converts an array of strings to a string with custom separator.
/// </summary>
/// <param name="aArray">Array to convert.</param>
/// <param name="aSeparator">Separator string.</param>
/// <returns>Separated string.</returns>
function ArrayToString(aArray : TArray<string>; aSeparator : string) : string; overload;
/// <summary>
/// Converts an array of integers to a CRLF-separated string.
/// </summary>
/// <param name="aArray">Array to convert.</param>