FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
QuickLib/Quick.Commons.pas at master · ChrisAtXenos/QuickLib · GitHub
ChrisAtXenos
QuickLib
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
QuickLib
/
Quick.Commons.pas
Copy path
More file actions
More file actions
Latest commit
History
History
History
2438 lines (2209 loc) · 62.5 KB
Breadcrumbs
QuickLib
/
Quick.Commons.pas
Copy path
File metadata and controls
2438 lines (2209 loc) · 62.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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-2024 Kike P�rez
Unit : Quick.Commons
Description : Common functions
Author : Kike P�rez
Version : 2.0
Created : 14/07/2017
Modified : 14/03/2024
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,
{
$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,
{
$ENDIF
}
DateUtils;
type
TLogEventType = (etInfo, etSuccess, etWarning, etError, etDebug, etDone, etTrace, etCritical, etException);
TLogVerbose =
set
of
TLogEventType;
const
LOG_ONLYERRORS = [etInfo,etError];
LOG_ERRORSANDWARNINGS = [etInfo,etWarning,etError];
LOG_TRACE = [etInfo,etError,etWarning,etTrace];
LOG_ALL = [etInfo, etSuccess, etWarning, etError, etDebug, etDone, etTrace, etCritical, etException];
LOG_DEBUG = [etInfo,etSuccess,etWarning,etError,etDebug];
{
$IFDEF DELPHIXE7_UP
}
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
}
CRLF = #
13
#
10
;
type
TPasswordComplexity =
set
of
(pfIncludeNumbers,pfIncludeSigns);
TEnvironmentPath =
record
EXEPATH : string;
{
$IFDEF MSWINDOWS
}
WINDOWS : string;
SYSTEM : string;
PROGRAMFILES : string;
COMMONFILES : string;
HOMEDRIVE : string;
TEMP : string;
USERPROFILE : string;
INSTDRIVE : string;
DESKTOP : string;
STARTMENU : string;
DESKTOP_ALLUSERS : string;
STARTMENU_ALLUSERS : string;
STARTUP : string;
APPDATA : String;
PROGRAMDATA : string;
ALLUSERSPROFILE : string;
{
$ENDIF MSWINDOWS
}
end
;
{
$IFNDEF FPC
}
TFileHelper =
record
helper
for
TFile
{
$IF DEFINED(MSWINDOWS) OR DEFINED(DELPHILINUX)
}
class
function
IsInUse
(
const
FileName : string) : Boolean; static;
{
$ENDIF
}
class
function
GetSize
(
const
FileName: String): Int64; static;
end
;
TDirectoryHelper =
record
helper
for
TDirectory
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
}
TCounter =
record
private
fMaxValue : Integer;
fCurrentValue : Integer;
public
property
MaxValue : Integer read fMaxValue;
procedure
Init
(aMaxValue : Integer);
function
Count
: Integer;
function
CountIs
(aValue : Integer) : Boolean;
function
Check
: Boolean;
procedure
Reset
;
end
;
TTimeCounter =
record
private
fCurrentTime : TDateTime;
fDoneEvery : Integer;
public
property
DoneEvery : Integer read fDoneEvery;
procedure
Init
(MillisecondsToReach : Integer);
function
Check
: Boolean;
procedure
Reset
;
end
;
{
$IFNDEF FPC
}
{
$IFNDEF DELPHIXE7_UP
}
TArrayUtil<T> =
class
class
procedure
Delete
(
var
aArray : TArray<T>; aIndex : Integer);
end
;
{
$ENDIF
}
TArrayOfStringHelper =
record
helper
for
TArray<string>
public
function
Any
: Boolean; overload;
function
Any
(
const
aValue : string) : Boolean; overload;
function
Add
(
const
aValue : string) : Integer;
function
AddIfNotExists
(
const
aValue : string; aCaseSense : Boolean = False) : Integer;
function
Remove
(
const
aValue : string) : Boolean;
function
Exists
(
const
aValue : string) : Boolean;
function
Count
: Integer;
end
;
TDelegate<T> = reference
to
procedure(
Value
: T);
{
$ENDIF
}
TPairItem =
record
Name
: string;
Value
: string;
constructor
Create(
const
aName, aValue : string);
end
;
TPairList =
class
type
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
function
GetEnumerator
: TPairEnumerator;
function
GetValue
(
const
aName : string) : string;
function
GetPair
(
const
aName : string) : TPairItem;
function
Add
(aPair : TPairItem) : Integer; overload;
function
Add
(
const
aName, aValue : string) : Integer; overload;
procedure
AddOrUpdate
(
const
aName, aValue : string);
function
Exists
(
const
aName : string) : Boolean;
function
Remove
(
const
aName : string) : Boolean;
function
Count
: Integer;
property
Items[
const
aName : string] : string read GetValue write AddOrUpdate;
function
ToArray
: TArray<TPairItem>;
procedure
FromArray
(aValue : TArray<TPairItem>);
procedure
Clear
;
end
;
{
$IFDEF DELPHIXE7_UP
}
TDateTimeHelper =
record
helper
for
TDateTime
public
function
ToSQLString
: string;
procedure
FromNow
;
procedure
FromUTC
(
const
aUTCTime : TDateTime);
function
IncDay
(
const
aValue : Cardinal =
1
) : TDateTime;
function
DecDay
(
const
aValue : Cardinal =
1
) : TDateTime;
function
IncMonth
(
const
aValue : Cardinal =
1
) : TDateTime;
function
DecMonth
(
const
aValue : Cardinal =
1
) : TDateTime;
function
IncYear
(
const
aValue : Cardinal =
1
) : TDateTime;
function
DecYear
(
const
aValue : Cardinal =
1
) : TDateTime;
function
IsEqualTo
(
const
aDateTime : TDateTime) : Boolean;
function
IsAfter
(
const
aDateTime : TDateTime) : Boolean;
function
IsBefore
(
const
aDateTime : TDateTime) : Boolean;
function
IsSameDay
(
const
aDateTime : TDateTime) : Boolean;
function
IsSameTime
(
const
aTime : TTime) : Boolean;
function
DayOfTheWeek
: Word;
function
ToJsonFormat
: string;
function
ToGMTFormat
: string;
function
ToTimeStamp
: TTimeStamp;
function
ToUTC
: TDateTime;
function
ToMilliseconds
: Int64;
function
ToString
: string;
function
Date
: TDate;
function
Time
: TTime;
function
IsAM
: Boolean;
function
IsPM
: Boolean;
end
;
TDateHelper =
record
helper
for
TDate
public
function
ToString
: string;
end
;
TTimeHelper =
record
helper
for
TTime
public
function
ToString
: string;
end
;
{
$ENDIF
}
EEnvironmentPath =
class
(Exception);
EShellError =
class
(Exception);
//
generates a random password with complexity options
function
RandomPassword
(
const
PasswordLength : Integer; Complexity : TPasswordComplexity = [pfIncludeNumbers,pfIncludeSigns]) : string;
//
generates a random string
function
RandomString
(
const
aLength: Integer) : string;
//
extracts file extension from a filename
function
ExtractFileNameWithoutExt
(
const
FileName: string): string;
//
converts a Unix path to Windows path
function
UnixToWindowsPath
(
const
UnixPath: string): string;
//
converts a Windows path to Unix path
function
WindowsToUnixPath
(
const
WindowsPath: string): string;
//
corrects malformed urls
function
CorrectURLPath
(
const
cUrl : string) : string;
//
get url parts
function
UrlGetProtocol
(
const
aUrl : string) : string;
function
UrlGetHost
(
const
aUrl : string) : string;
function
UrlGetPath
(
const
aUrl : string) : string;
function
UrlGetQuery
(
const
aUrl : string) : string;
function
UrlRemoveProtocol
(
const
aUrl : string) : string;
function
UrlRemoveQuery
(
const
aUrl : string) : string;
function
UrlSimpleEncode
(
const
aUrl : string) : string;
//
get typical environment paths as temp, desktop, etc
procedure
GetEnvironmentPaths
;
{
$IFDEF MSWINDOWS
}
function
GetSpecialFolderPath
(folderID : Integer) : string;
//
checks if running on a 64bit OS
function
Is64bitOS
: Boolean;
//
checks if is a console app
function
IsConsole
: Boolean;
function
HasConsoleOutput
: Boolean;
//
checks if compiled in debug mode
{
$ENDIF
}
function
IsDebug
: Boolean;
{
$IFDEF MSWINDOWS
}
//
checks if running as a service
function
IsService
: Boolean;
//
gets number of seconds without user interaction (mouse, keyboard)
function
SecondsIdle
: DWord;
//
frees process memory not needed
procedure
FreeUnusedMem
;
//
changes screen resolution
function
SetScreenResolution
(Width, Height: integer): Longint;
{
$ENDIF MSWINDOWS
}
//
returns last day of current month
function
LastDayCurrentMonth
: TDateTime;
{
$IFDEF FPC
}
function
DateTimeInRange
(ADateTime: TDateTime; AStartDateTime, AEndDateTime: TDateTime; aInclusive: Boolean = True): Boolean;
{
$ENDIF
}
//
checks if two datetimes are in same day
function
IsSameDay
(cBefore, cNow : TDateTime) : Boolean;
//
change Time of a DateTime
function
ChangeTimeOfADay
(aDate : TDateTime; aHour, aMinute, aSecond : Word; aMilliSecond : Word =
0
) : TDateTime;
//
change Date of a DateTime
function
ChangeDateOfADay
(aDate : TDateTime; aYear, aMonth, aDay : Word) : TDateTime;
//
returns n times a char
function
FillStr
(
const
C : Char;
const
Count : Integer) : string;
function
FillStrEx
(
const
value
: string;
const
Count : Integer) : string;
//
checks if string exists in array of string
function
StrInArray
(
const
aValue : string;
const
aInArray :
array
of
string; aCaseSensitive : Boolean = True) : Boolean;
//
checks if integer exists in array of integer
function
IntInArray
(
const
aValue : Integer;
const
aInArray :
array
of
Integer) : Boolean;
//
check if array is empty
function
IsEmptyArray
(aArray : TArray<string>) : Boolean; overload;
function
IsEmptyArray
(aArray : TArray<Integer>) : Boolean; overload;
//
returns a number leading zero
function
Zeroes
(
const
Number, Len : Int64) : string;
//
converts a number to thousand delimeter string
function
NumberToStr
(
const
Number : Int64) : string;
//
returns n spaces
function
Spaces
(
const
Count : Integer) : string;
//
returns current date as a string
function
NowStr
: string;
//
returns a new GUID as string
function
NewGuidStr
: string;
//
compare a string with a wildcard pattern (? or *)
function
IsLike
(cText, Pattern: string) : Boolean;
//
Upper case for first letter
function
Capitalize
(s: string): string;
function
CapitalizeWords
(s: string): string;
//
returns current logged user
function
GetLoggedUserName
: string;
//
returns computer name
function
GetComputerName
: string;
//
check if remote desktop session
{
$IFDEF MSWINDOWS
}
function
IsRemoteSession
: Boolean;
{
$ENDIF
}
//
extract domain and user name from user login
function
ExtractDomainAndUser
(
const
aUser : string; out oDomain, oUser : string) : Boolean;
//
Changes incorrect delims in path
function
NormalizePathDelim
(
const
cPath : string;
const
Delim : Char) : string;
//
combine paths normalized with delim
function
CombinePaths
(
const
aFirstPath, aSecondPath: string; aDelim : Char): string;
//
Removes firs segment of a path
function
RemoveFirstPathSegment
(
const
cdir : string) : string;
//
Removes last segment of a path
function
RemoveLastPathSegment
(
const
cDir : string) : string;
//
returns path delimiter if found
function
GetPathDelimiter
(
const
aPath : string) : string;
//
returns first segment of a path
function
GetFirstPathSegment
(
const
aPath : string) : string;
//
returns last segment of a path
function
GetLastPathSegment
(
const
aPath : string) : string;
//
finds swith in commandline params
function
ParamFindSwitch
(
const
Switch : string) : Boolean;
//
gets value for a switch if exists
function
ParamGetSwitch
(
const
Switch : string;
var
cvalue : string) : Boolean;
//
returns app name (filename based)
function
GetAppName
: string;
//
returns app version (major & minor)
function
GetAppVersionStr
: string;
//
returns app version full (major, minor, release & compiled)
function
GetAppVersionFullStr
: string;
//
convert UTC DateTime to Local DateTime
function
UTCToLocalTime
(GMTTime: TDateTime): TDateTime;
//
convert Local DateTime to UTC DateTime
function
LocalTimeToUTC
(LocalTime : TDateTime): TDateTime;
//
convert DateTime to GTM Time string
function
DateTimeToGMT
(aDate : TDateTime) : string;
//
convert GMT Time string to DateTime
function
GMTToDateTime
(aDate : string) : TDateTime;
//
convert DateTime to Json Date format
function
DateTimeToJsonDate
(aDateTime : TDateTime) : string;
//
convert Json Date format to DateTime
function
JsonDateToDateTime
(
const
aJsonDate : string) : TDateTime;
//
count number of digits of a Integer
function
CountDigits
(anInt: Cardinal): Cardinal; inline;
//
count times a string is present in other string
function
CountStr
(
const
aFindStr, aSourceStr : string) : Integer;
//
save stream to file
procedure
SaveStreamToFile
(aStream : TStream;
const
aFilename : string);
//
save stream to string
function
StreamToString
(
const
aStream: TStream;
const
aEncoding: TEncoding): string;
function
StreamToStringEx
(aStream : TStream) : string;
//
save string to stream
procedure
StringToStream
(
const
aStr : string; aStream : TStream;
const
aEncoding: TEncoding);
procedure
StringToStreamEx
(
const
aStr : string; aStream : TStream);
//
returns a real comma separated text from stringlist
function
CommaText
(aList : TStringList) : string; overload;
//
returns a real comma separated text from array of string
function
CommaText
(aArray : TArray<string>) : string; overload;
//
returns a string CRLF separated from array of string
function
ArrayToString
(aArray : TArray<string>) : string; overload;
//
returns a string with separator from array of string
function
ArrayToString
(aArray : TArray<string>; aSeparator : string) : string; overload;
//
returns a string CRLF separated from array of Integer
function
ArrayToString
(aArray : TArray<Integer>) : string; overload;
//
returns a string with separator from array of Integer
function
ArrayToString
(aArray : TArray<Integer>; aSeparator : string) : string; overload;
//
converts TStrings to array
function
StringsToArray
(aStrings : TStrings) : TArray<string>; overload;
//
converts string comma or semicolon separated to array
function
StringsToArray
(
const
aString : string) : TArray<string>; overload;
{
$IFDEF MSWINDOWS
}
//
process messages on console applications
procedure
ProcessMessages
;
//
get last error message
function
GetLastOSError
: String;
{
$ENDIF
}
{
$IF DEFINED(FPC) AND DEFINED(MSWINDOWS)
}
function
GetLastInputInfo
(
var
plii: TLastInputInfo): BOOL;stdcall;
external
'
user32
'
name
'
GetLastInputInfo
'
;
{
$ENDIF
}
function
RemoveLastChar
(
const
aText : string) : string;
function
DateTimeToSQL
(aDateTime : TDateTime) : string;
function
IsInteger
(
const
aValue : string) : Boolean;
function
IsFloat
(
const
aValue : string) : Boolean;
function
IsBoolean
(
const
aValue : string) : Boolean;
//
extract a substring and deletes from source string
function
ExtractStr
(
var
vSource : string; aIndex : Integer; aCount : Integer) : string;
//
get first string between string delimiters
function
GetSubString
(
const
aSource, aFirstDelimiter, aLastDelimiter : string) : string;
//
get double quoted or dequoted string
function
DbQuotedStr
(
const
str : string): string;
function
UnDbQuotedStr
(
const
str: string) : string;
//
get simple quoted or dequoted string
function
SpQuotedStr
(
const
str : string): string;
function
UnSpQuotedStr
(
const
str : string): string;
function
UnQuotedStr
(
const
str : string;
const
aQuote : Char) : string;
//
ternary operator
function
Ifx
(aCondition : Boolean;
const
aIfIsTrue, aIfIsFalse : string) : string; overload;
function
Ifx
(aCondition : Boolean;
const
aIfIsTrue, aIfIsFalse : Integer) : Integer; overload;
function
Ifx
(aCondition : Boolean;
const
aIfIsTrue, aIfIsFalse : Extended) : Extended; overload;
function
Ifx
(aCondition : Boolean;
const
aIfIsTrue, aIfIsFalse : TObject) : TObject; overload;
var
path : TEnvironmentPath;
//
Enabled if QuickService is defined
IsQuickServiceApp : Boolean;
implementation
{
TFileHelper
}
{
$IFNDEF FPC
}
{
$IFDEF MSWINDOWS
}
class
function
TFileHelper.IsInUse
(
const
FileName : string) : Boolean;
var
HFileRes: HFILE;
begin
Result := False;
if
not
FileExists(FileName)
then
Exit;
try
HFileRes := CreateFile(PChar(FileName)
,GENERIC_READ
or
GENERIC_WRITE
,
0
,
nil
,OPEN_EXISTING
,FILE_ATTRIBUTE_NORMAL
,
0
);
Result := (HFileRes = INVALID_HANDLE_VALUE);
if
not
(Result)
then
begin
CloseHandle(HFileRes);
end
;
except
Result := True;
end
;
end
;
{
$ENDIF
}
{
$IFDEF DELPHILINUX
}
class
function
TFileHelper.IsInUse
(
const
FileName : string) : Boolean;
var
fs : TFileStream;
begin
try
fs := TFileStream.Create(FileName, fmOpenReadWrite, fmShareExclusive);
Result := True;
fs.Free;
except
Result := False;
end
;
end
;
{
$ENDIF
}
{
$IFDEF MSWINDOWS
}
class
function
TFileHelper.GetSize
(
const
FileName: String): Int64;
var
info: TWin32FileAttributeData;
begin
Result := -
1
;
if
not
GetFileAttributesEx(PWideChar(FileName), GetFileExInfoStandard, @info)
then
Exit;
Result := Int64(info.nFileSizeLow)
or
Int64(info.nFileSizeHigh
shl
32
);
end
;
{
$ELSE
}
class
function
TFileHelper.GetSize
(
const
FileName: String): Int64;
var
sr : TSearchRec;
begin
if
FindFirst(fileName, faAnyFile, sr ) =
0
then
Result := sr.Size
else
Result := -
1
;
end
;
{
$ENDIF
}
{
TDirectoryHelper
}
class
function
TDirectoryHelper.GetSize
(
const
Path: String): Int64;
var
filename : string;
begin
Result := -
1
;
for
filename
in
TDirectory.GetFiles(Path)
do
begin
Result := Result + TFile.GetSize(filename);
end
;
end
;
{
$ENDIF
}
{
other functions
}
function
RandomPassword
(
const
PasswordLength : Integer; Complexity : TPasswordComplexity = [pfIncludeNumbers,pfIncludeSigns]) : string;
const
PassAlpha =
'
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
'
;
PassSigns =
'
@!&$
'
;
PassNumbers =
'
1234567890
'
;
var
MinNumbers,
MinSigns : Integer;
NumNumbers,
NumSigns : Integer;
begin
Result :=
'
'
;
Randomize;
//
fill all alfa
repeat
Result := Result + PassAlpha[Random(Length(PassAlpha))+
1
];
until
(Length(Result) = PasswordLength);
//
checks if need include numbers
if
pfIncludeNumbers
in
Complexity
then
begin
MinNumbers := Round(PasswordLength /
10
*
2
);
NumNumbers :=
0
;
if
MinNumbers =
0
then
MinNumbers :=
1
;
repeat
Result[Random(PasswordLength)+
1
] := PassNumbers[Random(Length(PassNumbers))+
1
];
Inc(NumNumbers);
until
NumNumbers = MinNumbers;
end
;
//
checks if need include signs
if
pfIncludeSigns
in
Complexity
then
begin
MinSigns := Round(PasswordLength /
10
*
1
);
NumSigns :=
0
;
if
MinSigns =
0
then
MinSigns :=
1
;
repeat
Result[Random(PasswordLength)+
1
] := PassSigns[Random(Length(PassSigns))+
1
];
Inc(NumSigns);
until
NumSigns = MinSigns;
end
;
end
;
function
RandomString
(
const
aLength: Integer) : string;
const
chars : string =
'
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
'
;
var
i : Integer;
clong : Integer;
begin
clong := High(chars);
SetLength(Result, aLength);
for
i :=
1
to
aLength
do
begin
Result[i] := chars[Random(clong) +
1
];
end
;
end
;
function
ExtractFileNameWithoutExt
(
const
FileName: string): string;
begin
Result := TPath.GetFileNameWithoutExtension(FileName);
end
;
function
UnixToWindowsPath
(
const
UnixPath: string): string;
begin
Result := StringReplace(UnixPath,
'
/
'
,
'
\
'
,[rfReplaceAll, rfIgnoreCase]);
end
;
function
WindowsToUnixPath
(
const
WindowsPath: string): string;
begin
Result := StringReplace(WindowsPath,
'
\
'
,
'
/
'
,[rfReplaceAll, rfIgnoreCase]);
end
;
function
CorrectURLPath
(
const
cUrl : string) : string;
var
nurl : string;
begin
nurl := WindowsToUnixPath(cUrl);
nurl := StringReplace(nurl,
'
//
'
,
'
/
'
,[rfReplaceAll]);
Result := StringReplace(nurl,
'
'
,
'
%20
'
,[rfReplaceAll]);
//
TNetEncoding.Url.Encode()
end
;
function
UrlGetProtocol
(
const
aUrl : string) : string;
begin
Result := aUrl.SubString(
0
,aUrl.IndexOf(
'
://
'
));
end
;
function
UrlGetHost
(
const
aUrl : string) : string;
var
url : string;
len : Integer;
begin
url := UrlRemoveProtocol(aUrl);
if
url.Contains(
'
/
'
)
then
len := url.IndexOf(
'
/
'
)
else
len := url.Length;
Result := url.SubString(
0
,len);
end
;
function
UrlGetPath
(
const
aUrl : string) : string;
var
url : string;
len : Integer;
begin
url := UrlRemoveProtocol(aUrl);
if
not
url.Contains(
'
/
'
)
then
Exit(
'
'
);
len := url.IndexOf(
'
?
'
);
if
len <
0
then
len := url.Length
else
len := url.IndexOf(
'
?
'
) - url.IndexOf(
'
/
'
);
Result := url.Substring(url.IndexOf(
'
/
'
),len);
end
;
function
UrlGetQuery
(
const
aUrl : string) : string;
begin
if
not
aUrl.Contains(
'
?
'
)
then
Exit(
'
'
);
Result := aUrl.Substring(aUrl.IndexOf(
'
?
'
)+
1
);
end
;
function
UrlRemoveProtocol
(
const
aUrl : string) : string;
var
pos : Integer;
begin
pos := aUrl.IndexOf(
'
://
'
);
if
pos <
0
then
pos :=
0
else
pos := pos +
3
;
Result := aUrl.SubString(pos, aUrl.Length);
end
;
function
UrlRemoveQuery
(
const
aUrl : string) : string;
begin
if
not
aUrl.Contains(
'
?
'
)
then
Exit(aUrl);
Result := aUrl.Substring(
0
,aUrl.IndexOf(
'
?
'
));
end
;
function
UrlSimpleEncode
(
const
aUrl : string) : string;
begin
Result := StringReplace(aUrl,
'
'
,
'
%20
'
,[rfReplaceAll]);
end
;
procedure
GetEnvironmentPaths
;
begin
//
gets path
path.EXEPATH := TPath.GetDirectoryName(ParamStr(
0
));
{
$IFDEF MSWINDOWS
}
path.WINDOWS := SysUtils.GetEnvironmentVariable(
'
windir
'
);
path.PROGRAMFILES := SysUtils.GetEnvironmentVariable(
'
ProgramFiles
'
);
path.COMMONFILES := SysUtils.GetEnvironmentVariable(
'
CommonProgramFiles(x86)
'
);
path.HOMEDRIVE := SysUtils.GetEnvironmentVariable(
'
SystemDrive
'
);
path.USERPROFILE := SysUtils.GetEnvironmentVariable(
'
USERPROFILE
'
);
path.PROGRAMDATA := SysUtils.GetEnvironmentVariable(
'
ProgramData
'
);
path.ALLUSERSPROFILE := SysUtils.GetEnvironmentVariable(
'
AllUsersProfile
'
);
path.INSTDRIVE := path.HOMEDRIVE;
path.TEMP := SysUtils.GetEnvironmentVariable(
'
TEMP
'
);
//
these paths fail if user is SYSTEM
try
path.SYSTEM := GetSpecialFolderPath(CSIDL_SYSTEM);
path.APPDATA := GetSpecialFolderPath(CSIDL_APPDATA);
path.DESKTOP := GetSpecialFolderPath(CSIDL_DESKTOP);
path.DESKTOP_ALLUSERS := GetSpecialFolderPath(CSIDL_COMMON_DESKTOPDIRECTORY);
path.STARTMENU:=GetSpecialFolderPath(CSIDL_PROGRAMS);
path.STARTMENU_ALLUSERS:=GetSpecialFolderPath(CSIDL_COMMON_PROGRAMS);
path.STARTMENU_ALLUSERS := path.STARTMENU;
path.STARTUP:=GetSpecialFolderPath(CSIDL_STARTUP);
except
//
end
;
{
$ENDIF
}
end
;
{
$IFDEF MSWINDOWS
}
function
GetSpecialFolderPath
(folderID : Integer) : string;
var
shellMalloc: IMalloc;
ppidl: PItemIdList;
begin
ppidl :=
nil
;
try
if
SHGetMalloc(shellMalloc) = NOERROR
then
begin
SHGetSpecialFolderLocation(
0
, folderID, ppidl);
SetLength(Result, MAX_PATH);
if
not
SHGetPathFromIDList(ppidl,
{
$IFDEF FPC
}
PAnsiChar(Result)
{
$ELSE
}
PChar(Result)
{
$ENDIF
}
)
then
begin
raise EShellError.create(Format(
'
GetSpecialFolderPath: Invalid PIPL (%d)
'
,[folderID]));
end
;
SetLength(Result, lStrLen(
{
$IFDEF FPC
}
PAnsiChar(Result)
{
$ELSE
}
PChar(Result)
{
$ENDIF
}
));
end
;
finally
if
ppidl <>
nil
then
shellMalloc.Free(ppidl);
end
;
end
;
function
Is64bitOS
: Boolean;
begin
{
$IFDEF WIN64
}
Result := True;
{
$ELSE
}
Result := False;
{
$ENDIF WIN64
}
end
;
function
IsConsole
: Boolean;
begin
{
$IFDEF CONSOLE
}
Result := True;
{
$ELSE
}
Result := False;
{
$ENDIF CONSOLE
}
end
;
{
$ENDIF
}
function
HasConsoleOutput
: Boolean;
{
$IFDEF MSWINDOWS
}
var
stout : THandle;
begin
try
stout := GetStdHandle(Std_Output_Handle);
{
$WARN SYMBOL_PLATFORM OFF
}
//
Allready checked that we are on a windows platform
Win32Check(stout <> Invalid_Handle_Value);
{
$WARN SYMBOL_PLATFORM ON
}
Result := stout <>
0
;
except
Result := False;
end
;
end
;
{
$ELSE
}
begin
Result := IsConsole;
end
;
{
$ENDIF
}
function
IsDebug
: Boolean;
begin
{
$IFDEF DEBUG
}
Result := True;
{
$ELSE
}
Result := False;
{
$ENDIF DEBUG
}
end
;
{
$IFDEF MSWINDOWS
}
function
IsService
: Boolean;
begin
//
only working with my Quick.AppService unit
try
Result := (IsConsole)
and
(
not
HasConsoleOutput);
except
Result := False;
end
;
end
;
function
SecondsIdle
: DWord;
var
liInfo: TLastInputInfo;
begin
liInfo.cbSize := SizeOf(TLastInputInfo) ;
GetLastInputInfo(liInfo) ;
Result := (GetTickCount - liInfo.dwTime)
DIV
1000
;
end
;
procedure
FreeUnusedMem
;
begin
if
Win32Platform = VER_PLATFORM_WIN32_NT
then
SetProcessWorkingSetSize(GetCurrentProcess, $FFFFFFFF, $FFFFFFFF);
end
;
function
SetScreenResolution
(Width, Height: integer): Longint;
var
DeviceMode: TDeviceMode;
begin
with
DeviceMode
do
begin
dmSize := SizeOf(TDeviceMode);
dmPelsWidth := Width;
dmPelsHeight := Height;
dmFields := DM_PELSWIDTH
or
DM_PELSHEIGHT;
end
;
Result := ChangeDisplaySettings(DeviceMode, CDS_UPDATEREGISTRY);
end
;
{
$ENDIF MSWINDOWS
}
function
LastDayCurrentMonth
: TDateTime;
begin
Result := EncodeDate(YearOf(Now),MonthOf(Now), DaysInMonth(Now));
end
;
{
$IFDEF FPC
}
function
DateTimeInRange
(ADateTime: TDateTime; AStartDateTime, AEndDateTime: TDateTime; aInclusive: Boolean = True): Boolean;
begin
if
aInclusive
then
Result := (AStartDateTime <= ADateTime)
and
(ADateTime <= AEndDateTime)
else
Result := (AStartDateTime < ADateTime)
and
(ADateTime < AEndDateTime);
end
;
{
$ENDIF
}
function
IsSameDay
(cBefore, cNow : TDateTime) : Boolean;
begin
//
Test: Result := MinutesBetween(cBefore,cNow) < 1;
Result := DateTimeInRange(cNow,StartOfTheDay(cBefore),EndOfTheDay(cBefore),True);
end
;
function
ChangeTimeOfADay
(aDate : TDateTime; aHour, aMinute, aSecond : Word; aMilliSecond : Word =
0
) : TDateTime;
var
y, m, d : Word;
begin
DecodeDate(aDate,y,m,d);
Result := EncodeDateTime(y,m,d,aHour,aMinute,aSecond,aMilliSecond);
end
;
function
ChangeDateOfADay
(aDate : TDateTime; aYear, aMonth, aDay : Word) : TDateTime;
var
h, m, s, ms : Word;
begin
DecodeTime(aDate,h,m,s,ms);
Result := EncodeDateTime(aYear,aMonth,aDay,h,m,s,
0
);
end
;
function
FillStr
(
const
C : Char;
const
Count : Integer) : string;
var
i : Integer;
begin
Result :=
'
'
;
for
i :=
1
to
Count
do
Result := Result + C;
end
;
function
FillStrEx
(
const
value
: string;
const
Count : Integer) : string;
var
i : Integer;
begin
Result :=
'
'
;
for
i :=
1
to
Count
do
Result := Result +
value
;
end
;
function
StrInArray
(
const
aValue : string;
const
aInArray :
array
of
string; aCaseSensitive : Boolean = True) : Boolean;
var
s : string;
begin
for
s
in
aInArray
do
begin
if
aCaseSensitive
then
begin
if
s = aValue
then
Exit(True);
end
else
begin
if
CompareText(aValue,s) =
0
then
Exit(True);
end
;
end
;
Result := False;
end
;
function
IntInArray
(
const
aValue : Integer;
const
aInArray :
array
of
Integer) : Boolean;
var
i : Integer;
begin
for
i
in
aInArray
do
begin
if
i = aValue
then
Exit(True);
end
;
Result := False;
end
;
function
IsEmptyArray
(aArray : TArray<string>) : Boolean;
begin
Result := Length(aArray) =
0
;
end
;
function
IsEmptyArray
(aArray : TArray<Integer>) : Boolean;
begin
Result := Length(aArray) =
0
;
end
;
function
Zeroes
(
const
Number, Len : Int64) : string;
begin
if
Len > Length(IntToStr(Number))
then
Result := FillStr(
'
0
'
,Len - Length(IntToStr(Number))) + IntToStr(Number)
else
Result := IntToStr(Number);
end
;
function
NumberToStr
(
const
Number : Int64) : string;
begin
try
Result := FormatFloat(
'
0,
'
,Number);
except
Result :=
'
#Error
'
;
end
;
end
;
function
Spaces
(
const
Count : Integer) : string;
begin
Result := FillStr(
'
'
,Count);
end
;
function
NowStr
: string;
begin
Result := DateTimeToStr(Now());
end
;
function
NewGuidStr
: string;
{
$IFNDEF DELPHIRX10_UP
}
var
guid : TGUID;
{
$ENDIF
}
begin
{
$IFDEF DELPHIRX10_UP
}
Result := TGUID.NewGuid.ToString;
{
$ELSE
}
guid.NewGuid;
Result := guid.ToString
{
$ENDIF
}
end
;
function
IsLike
(cText, Pattern: string) : Boolean;
var
i, n : Integer;
match : Boolean;
wildcard : Boolean;
CurrentPattern : Char;
begin
Result := False;
wildcard := False;
cText := LowerCase(cText);
Pattern := LowerCase(Pattern);
match := False;
if
(Pattern.Length > cText.Length)
or
(Pattern =
'
'
)
then
Exit;
if
Pattern =
'
*
'
then
begin
Result := True;
Exit;
end
;
for
i :=
1
to
cText.Length
do
begin
CurrentPattern := Pattern[i];
if
CurrentPattern =
'
*
'
then
wildcard := True;
if
wildcard
then
begin
n := Pos(Copy(Pattern,i+
1
,Pattern.Length),cText);
if
(n > i)
or
(Pattern.Length = i)
then
begin
Result := True;
Exit;
end
;
end
else
begin
if
(cText[i] = CurrentPattern)
or
(CurrentPattern =
'
?
'
)
then
match := True
else
match := False;
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL