-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmisc_utils_unit.pas
1719 lines (1532 loc) · 52.7 KB
/
misc_utils_unit.pas
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 misc_utils_unit;
{********************************************************************
| This Source Code is subject to the terms of the |
| Mozilla Public License, v. 2.0. If a copy of the MPL was not |
| distributed with this file, You can obtain one at |
| https://mozilla.org/MPL/2.0/. |
| |
| Software distributed under the License is distributed on an |
| "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or |
| implied. See the License for the specific language governing |
| rights and limitations under the License. |
********************************************************************}
{ This sample code uses the TNT Delphi Unicode Controls (compatiable
with the last free version) to handle a few unicode tasks. }
interface
uses
Windows, Classes, TNTClasses, ShellAPI, shlobj;
const
// You must obtain your own key, it's free
URLIdentifier : String = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
typeMedia = 0;
typeFolder = 1;
typeAppend = 2;
typeRefresh = 3;
typeLiveStream = 100;
typePendingStream = 101;
type
Fixed_IShellLinkW = interface(IUnknown) { sl }
[SID_IShellLinkW]
function GetPath(pszFile: PWideChar; cchMaxPath: Integer;
var pfd: TWin32FindDataW; fFlags: DWORD): HResult; stdcall; // was "TWin32FindData", which is wrong.
function GetIDList(var ppidl: PItemIDList): HResult; stdcall;
function SetIDList(pidl: PItemIDList): HResult; stdcall;
function GetDescription(pszName: PWideChar; cchMaxName: Integer): HResult; stdcall;
function SetDescription(pszName: PWideChar): HResult; stdcall;
function GetWorkingDirectory(pszDir: PWideChar; cchMaxPath: Integer): HResult; stdcall;
function SetWorkingDirectory(pszDir: PWideChar): HResult; stdcall;
function GetArguments(pszArgs: PWideChar; cchMaxPath: Integer): HResult; stdcall;
function SetArguments(pszArgs: PWideChar): HResult; stdcall;
function GetHotkey(var pwHotkey: Word): HResult; stdcall;
function SetHotkey(wHotkey: Word): HResult; stdcall;
function GetShowCmd(out piShowCmd: Integer): HResult; stdcall;
function SetShowCmd(iShowCmd: Integer): HResult; stdcall;
function GetIconLocation(pszIconPath: PWideChar; cchIconPath: Integer;
out piIcon: Integer): HResult; stdcall;
function SetIconLocation(pszIconPath: PWideChar; iIcon: Integer): HResult; stdcall;
function SetRelativePath(pszPathRel: PWideChar; dwReserved: DWORD): HResult; stdcall;
function Resolve(Wnd: HWND; fFlags: DWORD): HResult; stdcall;
function SetPath(pszFile: PWideChar): HResult; stdcall;
end;
function TickCount64 : Int64;
procedure DebugMsgF(FileName : WideString; Txt : WideString);
procedure DebugMsgFT(FileName : WideString; Txt : WideString);
function DownloadFileToStringList(URL : String; fStream : TStringList; var Status : String; var ErrorCode: Integer; TimeOut : DWord) : Boolean; overload;
function DownloadFileToStream(URL : String; fStream : TMemoryStream; var Status : String; var ErrorCode: Integer; TimeOut : DWord) : Boolean; overload;
function DownloadImageToFile(URL : String; ImageFilePath, ImageFileName : WideString; var Status : String; var ErrorCode: Integer; TimeOut : DWord) : Boolean; overload;
procedure DownloadImageToFileThreaded(URL : String; ImageFilePath, ImageFileName : WideString; var Status : String; var ErrorCode: Integer; TimeOut : DWord; var SuccessCode, DownloadEnded : Boolean);
function EncodeFileName(S : WideString) : WideString;
function DecodeFileName(S : WideString) : WideString;
function URLEncodeUTF8(stInput : widestring) : string;
function HTMLUnicodeToUTF8(const AStr: String): String;
function EncodeURIComponent(const ASrc: string): UTF8String;
function SetRegString(BaseKey : HKey; SubKey : String; KeyEntry : String; KeyValue : String) : Boolean;
function GetRegString(BaseKey : HKey; SubKey : String; KeyEntry : String) : String;
function SetRegDWord(BaseKey : HKey; SubKey : String; KeyEntry : String; KeyValue : Integer) : Boolean;
function GetRegDWord(BaseKey : HKey; SubKey : String; KeyEntry : String) : Integer;
function AddBackSlash(S : WideString) : WideString; Overload;
function ConvertCharsToSpaces(S : WideString) : WideString;
function DecodeTextTags(S : WideString; RemoveSuffix : Boolean) : WideString;
function EncodeTextTags(S : WideString; AddSuffix : Boolean) : WideString;
function EncodePipe(S : WideString; AddSuffix : Boolean) : WideString;
function StripURLHash(sURL : String) : String;
procedure FileExtIntoStringList(fPath,fExt : WideString; fList : TTNTStrings; Recursive : Boolean);
function EncodeDuration(Dur : Integer) : WideString;
function TimeDifferenceToStr(nowTS,thenTS : TDateTime) : String;
function UTF8StringToWideString(Const S : UTF8String) : WideString;
function WideStringToUTF8String(Const S : WideString) : UTF8String;
function IntToStrDelimiter(iSrc : Int64; dChar : Char) : String;
function DosToAnsi(S: String): String;
function WidePosEx(SubStr, S : WideString; Offset : Cardinal = 1) : Integer;
function EnDeCrypt(const Value : String) : String;
function EraseFile(FileName : WideString) : Boolean;
function InputComboW(ownerWindow: THandle; const ACaption, APrompt: Widestring; const AList: TTNTStrings; var AOutput : WideString) : Boolean;
procedure Split(S : String; Ch : Char; sList : TStrings); overload;
procedure Split(S : WideString; Ch : Char; sList : TTNTStrings); overload;
function sParamCount(S : WideString) : Integer;
function GetSParam(PItem : Integer; PList : WideString; StripSpace : Boolean) : WideString; Overload;
function GetSLeftParam(S : String) : String;
function GetSRightParam(S : WideString; StripSpace : Boolean) : WideString;
Function ExtractFileNameNoExt(FileName : WideString) : WideString;
function WidePosRev(SubStr, S : WideString) : Integer;
function WideExtractFilePathEx(FileName : WideString) : WideString;
function StringToFloat(S : String) : Double;
function StringToFloatDef(S : String; dValue : Double) : Double;
function FileAgeW(const FileName: widestring): Integer;
function GetFileSize(FileName : Widestring) : Int64;
function WideExtractFileNameEx(FileName : WideString) : WideString;
function GetCurrentDLLPath : WideString;
function HashWideString(S : WideString) : Integer;
function WinExecAndWait32(FileName : WideString; Visibility : integer; waitforexec,console : boolean) : Integer;
procedure GetShortCutFileName(FileName : WideString; var NewFileName,NewParameters : Widestring);
function EntryTypeToString(eType : Integer) : String;
implementation
uses
SysUtils, SyncObjs, TNTSysUtils, wininet, dateutils, graphics ,forms, tntforms, stdctrls, tntStdCtrls, controls, ActiveX;
type
TDownloadThread = Class(TThread)
procedure execute; override;
public
DownloadEnded : PBoolean;
SuccessCode : PBoolean;
URL : String;
ImageFilePath : WideString;
ImageFileName : WideString;
Status : PString;
ErrorCode : PInteger;
TimeOut : DWord;
end;
var
TickCountLast : DWORD = 0;
TickCountBase : Int64 = 0;
DebugStartTime : Int64 = -1;
qTimer64Freq : Int64;
csDebug : TCriticalSection;
function TickCount64 : Int64;
begin
Result := GetTickCount;
If Result < TickCountLast then TickCountBase := TickCountBase+$100000000;
TickCountLast := Result;
Result := Result+TickCountBase;
end;
procedure DebugMsgFT(FileName : WideString; Txt : WideString);
var
S,S1 : String;
i64 : Int64;
begin
If FileName <> '' then
Begin
QueryPerformanceCounter(i64);
S := FloatToStrF(((i64-DebugStartTime)*1000) / qTimer64Freq,ffFixed,15,3);
While Length(S) < 12 do S := ' '+S;
S1 := DateToStr(Date)+' '+TimeToStr(Time);
DebugMsgF(FileName,S1+' ['+S+'] : '+Txt);
End;
end;
procedure DebugMsgF(FileName : WideString; Txt : WideString);
var
fStream : TTNTFileStream;
S : String;
begin
If FileName <> '' then
Begin
csDebug.Enter;
Try
If WideFileExists(FileName) = True then
Begin
Try
fStream := TTNTFileStream.Create(FileName,fmOpenWrite);
Except
fStream := nil;
End;
End
else
Begin
Try
fStream := TTNTFileStream.Create(FileName,fmCreate);
Except
fStream := nil;
End;
End;
If fStream <> nil then
Begin
S := UTF8Encode(Txt)+CRLF;
fStream.Seek(0,soFromEnd);
fStream.Write(S[1],Length(S));
fStream.Free;
End;
Finally
csDebug.Leave;
End;
End;
end;
function DownloadFileToStringList(URL : String; fStream : TStringList; var Status : String; var ErrorCode: Integer; TimeOut : DWord) : Boolean;
var
MemStream : TMemoryStream;
begin
Result := False;
If fStream <> nil then
Begin
MemStream := TMemoryStream.Create;
Result := DownloadFileToStream(URL,MemStream,Status,ErrorCode,TimeOut);
MemStream.Position := 0;
fStream.LoadFromStream(MemStream);
MemStream.Free;
End;
end;
(*
function DownloadFileToStringList(URL : String; fStream : TStringList) : Boolean;
var
Status : String;
ErrorCode : DWord;
begin
Result := DownloadFileToStringList(URL,fStream,Status,ErrorCode,0);
end;
(**)
function DownloadFileToStream(URL : String; fStream : TMemoryStream; var Status : String; var ErrorCode: Integer; TimeOut : DWord) : Boolean;
type
DLBufType = Array[0..1024] of Char;
const
MaxRetryAttempts = 5;
RetryInterval = 1; //seconds
var
NetHandle : HINTERNET;
URLHandle : HINTERNET;
DLBuf : ^DLBufType;
BytesRead : DWord;
infoBuffer : Array [0..512] of char;
bufLen : DWORD;
Tmp : DWord;
iAttemptsLeft : Integer;
AttemptAgain : Boolean;
RetryAfter: String;
begin
Result := False;
Status := '';
ErrorCode := 0;
If fStream <> nil then
Begin
NetHandle := InternetOpen(PChar(URLIdentifier),INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
If Assigned(NetHandle) then
Begin
If TimeOut > 0 then
Begin
InternetSetOption(NetHandle,INTERNET_OPTION_CONNECT_TIMEOUT,@TimeOut,Sizeof(TimeOut));
InternetSetOption(NetHandle,INTERNET_OPTION_SEND_TIMEOUT ,@TimeOut,Sizeof(TimeOut));
InternetSetOption(NetHandle,INTERNET_OPTION_RECEIVE_TIMEOUT,@TimeOut,Sizeof(TimeOut));
End;
iAttemptsLeft := MaxRetryAttempts;
repeat
AttemptAgain := False;
UrlHandle := InternetOpenUrl(NetHandle,PChar(URL),nil,0,INTERNET_FLAG_RELOAD,0);
If Assigned(UrlHandle) then
Begin
tmp := 0;
bufLen := Length(infoBuffer);
If HttpQueryInfo(UrlHandle,HTTP_QUERY_STATUS_CODE,@infoBuffer[0],bufLen,tmp) = True then
Begin
Status := infoBuffer;
RetryAfter := '';
If Status = '429' then
Begin
//To get all headers use the following code
// HttpQueryInfo(UrlHandle,HTTP_QUERY_RAW_HEADERS_CRLF,@Headers[0],bufLen,tmp);
//for guidance and hints on buffer sizes and in/out params see:
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa385373%28v=vs.85%29.aspx
//Retry-After
//X-RateLimit-Limit: 40
//X-RateLimit-Remaining: 39
//X-RateLimit-Reset: 1453056622
bufLen := Length(infoBuffer);
infoBuffer := 'Retry-After';
if HttpQueryInfo(UrlHandle,HTTP_QUERY_CUSTOM,@infoBuffer[0],bufLen,tmp) then
RetryAfter := infoBuffer
else RetryAfter := '';
End;
New(DLBuf);
fStream.Clear;
Repeat
ZeroMemory(DLBuf,Sizeof(DLBufType));
If InternetReadFile(UrlHandle,DLBuf,SizeOf(DLBufType),BytesRead) = True then
If BytesRead > 0 then fStream.Write(DLBuf^,BytesRead);
Until (BytesRead = 0);
Dispose(DLBuf);
If Status = '200' then Result := True
else If Status = '429' then // 429 - Too Many Requests
Begin
AttemptAgain := True;
Dec(iAttemptsLeft);
Sleep(1000 * StrToIntDef(RetryAfter, RetryInterval));
End
End;
InternetCloseHandle(UrlHandle);
End
else ErrorCode := GetLastError;
until
not (AttemptAgain and (iAttemptsLeft > 0));
InternetCloseHandle(NetHandle);
End;
End;
end;
(*
function DownloadFileToStream(URL : String; fStream : TMemoryStream) : Boolean;
var
Status : String;
ErrorCode : DWord;
begin
Result := DownloadFileToStream(URL,fStream,Status,ErrorCode,0);
end;
(**)
procedure TDownloadThread.execute;
begin
SuccessCode^ := DownloadImageToFile(URL,ImageFilePath,ImageFileName,Status^,ErrorCode^,TimeOut);
DownloadEnded^ := True;
end;
procedure DownloadImageToFileThreaded(URL : String; ImageFilePath, ImageFileName : WideString; var Status : String; var ErrorCode: Integer; TimeOut : DWord; var SuccessCode, DownloadEnded : Boolean);
var
DownloadThread : TDownloadthread;
begin
DownloadThread := TDownloadThread.Create(True);
DownloadThread.Priority := tpIdle;
DownloadThread.FreeOnTerminate := True;
DownloadThread.URL := URL;
DownloadThread.ImageFilePath := ImageFilePath;
DownloadThread.ImageFileName := ImageFileName;
DownloadThread.Status := @Status;
DownloadThread.ErrorCode := @ErrorCode;
DownloadThread.TimeOut := TimeOut;
DownloadThread.SuccessCode := @SuccessCode;
DownloadThread.SuccessCode^ := False;
DownloadThread.DownloadEnded := @DownloadEnded;
DownloadThread.DownloadEnded^ := False;
DownloadThread.Resume;
end;
function DownloadImageToFile(URL : String; ImageFilePath, ImageFileName : WideString; var Status : String; var ErrorCode: Integer; TimeOut : DWord) : Boolean;
var
iStream : TMemoryStream;
fStream : TTNTFileStream;
begin
Result := False;
// Download image to memory stream
iStream := TMemoryStream.Create;
iStream.Clear;
If DownloadFileToStream(URL,iStream,Status,ErrorCode,TimeOut) = True then
Begin
If iStream.Size > 0 then
Begin
// Create the destination folder if it doesn't exist
If WideDirectoryExists(ImageFilePath) = False then WideForceDirectories(ImageFilePath);
// Save the source image to disk
Try
fStream := TTNTFileStream.Create(ImageFilePath+ImageFileName,fmCreate);
Except
fStream := nil
End;
If fStream <> nil then
Begin
iStream.Position := 0;
Try
fStream.CopyFrom(iStream,iStream.Size);
Result := True;
Finally
fStream.Free;
End;
End;
End;
End;
iStream.Free;
end;
(*
function DownloadImageToFile(URL : String; ImageFilePath, ImageFileName : WideString) : Boolean;
var
Status : String;
ErrorCode : DWord;
begin
Result := DownloadImageToFile(URL,ImageFilePath,ImageFileName,Status,ErrorCode,0);
end;
(**)
function URLEncodeUTF8(stInput : widestring) : string;
const
Hex : array[0..255] of string = (
'%00', '%01', '%02', '%03', '%04', '%05', '%06', '%07',
'%08', '%09', '%0a', '%0b', '%0c', '%0d', '%0e', '%0f',
'%10', '%11', '%12', '%13', '%14', '%15', '%16', '%17',
'%18', '%19', '%1a', '%1b', '%1c', '%1d', '%1e', '%1f',
'%20', '%21', '%22', '%23', '%24', '%25', '%26', '%27',
'%28', '%29', '%2a', '%2b', '%2c', '%2d', '%2e', '%2f',
'%30', '%31', '%32', '%33', '%34', '%35', '%36', '%37',
'%38', '%39', '%3a', '%3b', '%3c', '%3d', '%3e', '%3f',
'%40', '%41', '%42', '%43', '%44', '%45', '%46', '%47',
'%48', '%49', '%4a', '%4b', '%4c', '%4d', '%4e', '%4f',
'%50', '%51', '%52', '%53', '%54', '%55', '%56', '%57',
'%58', '%59', '%5a', '%5b', '%5c', '%5d', '%5e', '%5f',
'%60', '%61', '%62', '%63', '%64', '%65', '%66', '%67',
'%68', '%69', '%6a', '%6b', '%6c', '%6d', '%6e', '%6f',
'%70', '%71', '%72', '%73', '%74', '%75', '%76', '%77',
'%78', '%79', '%7a', '%7b', '%7c', '%7d', '%7e', '%7f',
'%80', '%81', '%82', '%83', '%84', '%85', '%86', '%87',
'%88', '%89', '%8a', '%8b', '%8c', '%8d', '%8e', '%8f',
'%90', '%91', '%92', '%93', '%94', '%95', '%96', '%97',
'%98', '%99', '%9a', '%9b', '%9c', '%9d', '%9e', '%9f',
'%a0', '%a1', '%a2', '%a3', '%a4', '%a5', '%a6', '%a7',
'%a8', '%a9', '%aa', '%ab', '%ac', '%ad', '%ae', '%af',
'%b0', '%b1', '%b2', '%b3', '%b4', '%b5', '%b6', '%b7',
'%b8', '%b9', '%ba', '%bb', '%bc', '%bd', '%be', '%bf',
'%c0', '%c1', '%c2', '%c3', '%c4', '%c5', '%c6', '%c7',
'%c8', '%c9', '%ca', '%cb', '%cc', '%cd', '%ce', '%cf',
'%d0', '%d1', '%d2', '%d3', '%d4', '%d5', '%d6', '%d7',
'%d8', '%d9', '%da', '%db', '%dc', '%dd', '%de', '%df',
'%e0', '%e1', '%e2', '%e3', '%e4', '%e5', '%e6', '%e7',
'%e8', '%e9', '%ea', '%eb', '%ec', '%ed', '%ee', '%ef',
'%f0', '%f1', '%f2', '%f3', '%f4', '%f5', '%f6', '%f7',
'%f8', '%f9', '%fa', '%fb', '%fc', '%fd', '%fe', '%ff');
var
iLen,iIndex : integer;
stEncoded : string;
ch : widechar;
begin
iLen := Length(stInput);
stEncoded := '';
for iIndex := 1 to iLen do
begin
ch := stInput[iIndex];
If (ch >= 'A') and (ch <= 'Z') then stEncoded := stEncoded + ch
else
If (ch >= 'a') and (ch <= 'z') then stEncoded := stEncoded + ch
else
If (ch >= '0') and (ch <= '9') then stEncoded := stEncoded + ch
else
If (ch = ' ') then stEncoded := stEncoded + '%20'//'+'
else
If ((ch = '-') or (ch = '_') or (ch = '.') or (ch = '!') or (ch = '*') or (ch = '~') or (ch = '\') or (ch = '(') or (ch = ')')) then stEncoded := stEncoded + ch
else
If (Ord(ch) <= $07F) then stEncoded := stEncoded + hex[Ord(ch)]
else
If (Ord(ch) <= $7FF) then
begin
stEncoded := stEncoded + hex[$c0 or (Ord(ch) shr 6)];
stEncoded := stEncoded + hex[$80 or (Ord(ch) and $3F)];
end
else
begin
stEncoded := stEncoded + hex[$e0 or (Ord(ch) shr 12)];
stEncoded := stEncoded + hex[$80 or ((Ord(ch) shr 6) and ($3F))];
stEncoded := stEncoded + hex[$80 or ((Ord(ch)) and ($3F))];
end;
end;
result := (stEncoded);
end;
function EncodeURIComponent(const ASrc: string): UTF8String;
const
HexMap: UTF8String = '0123456789ABCDEF';
function IsSafeChar(ch: Integer): Boolean;
begin
if (ch >= 48) and (ch <= 57) then Result := True // 0-9
else if (ch >= 65) and (ch <= 90) then Result := True // A-Z
else if (ch >= 97) and (ch <= 122) then Result := True // a-z
else if (ch = 33) then Result := True // !
else if (ch >= 39) and (ch <= 42) then Result := True // '()*
else if (ch >= 45) and (ch <= 46) then Result := True // -.
else if (ch = 95) then Result := True // _
else if (ch = 126) then Result := True // ~
else Result := False;
end;
var
I, J: Integer;
ASrcUTF8: UTF8String;
begin
Result := ''; {Do not Localize}
ASrcUTF8 := UTF8Encode(ASrc);
// UTF8Encode call not strictly necessary but
// prevents implicit conversion warning
I := 1; J := 1;
SetLength(Result, Length(ASrcUTF8) * 3); // space to %xx encode every byte
while I <= Length(ASrcUTF8) do
begin
if IsSafeChar(Ord(ASrcUTF8[I])) then
begin
Result[J] := ASrcUTF8[I];
Inc(J);
end
else if ASrcUTF8[I] = ' ' then
begin
Result[J] := '+';
Inc(J);
end
else
begin
Result[J] := '%';
Result[J+1] := HexMap[(Ord(ASrcUTF8[I]) shr 4) + 1];
Result[J+2] := HexMap[(Ord(ASrcUTF8[I]) and 15) + 1];
Inc(J,3);
end;
Inc(I);
end;
SetLength(Result, J-1);
end;
function GetRegString(BaseKey : HKey; SubKey : String; KeyEntry : String) : String;
var
RegHandle : HKey;
RegType : LPDWord;
BufSize : LPDWord;
KeyValue : String;
begin
Result := '';
If RegOpenKeyEx(BaseKey,PChar(SubKey),0,KEY_READ,RegHandle) = ERROR_SUCCESS then
Begin
New(RegType);
New(BufSize);
RegType^ := Reg_SZ;
BufSize^ := 65535;
SetLength(KeyValue,65535);
If RegQueryValueEx(RegHandle,PChar(KeyEntry),nil,RegType,@KeyValue[1],BufSize) = ERROR_SUCCESS then
Begin
If BufSize^ > 0 then SetLength(KeyValue,BufSize^-1) else KeyValue := '';
Result := KeyValue;
End;
Dispose(BufSize);
Dispose(RegType);
RegCloseKey(RegHandle);
End;
end;
function SetRegString(BaseKey : HKey; SubKey : String; KeyEntry : String; KeyValue : String) : Boolean;
var
RegHandle : HKey;
S : String;
I : Integer;
begin
Result := False;
If RegCreateKeyEx(BaseKey,PChar(SubKey),0,nil,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,nil,RegHandle,@I) = ERROR_SUCCESS then
Begin
If KeyValue = '' then S := #0 else S := KeyValue;
Result := RegSetValueEx(RegHandle,@KeyEntry[1],0,REG_SZ,@S[1],Length(S)) = ERROR_SUCCESS;
RegCloseKey(RegHandle);
End;
end;
function SetRegDWord(BaseKey : HKey; SubKey : String; KeyEntry : String; KeyValue : Integer) : Boolean;
var
RegHandle : HKey;
I : Integer;
begin
Result := False;
If RegCreateKeyEx(BaseKey,PChar(SubKey),0,nil,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,nil,RegHandle,@I) = ERROR_SUCCESS then
Begin
If RegSetValueEx(RegHandle,PChar(KeyEntry),0,REG_DWORD,@KeyValue,4) = ERROR_SUCCESS then Result := True;
RegCloseKey(RegHandle);
End;
end;
function GetRegDWord(BaseKey : HKey; SubKey : String; KeyEntry : String) : Integer;
var
RegHandle : HKey;
RegType : LPDWord;
BufSize : LPDWord;
KeyValue : Integer;
begin
Result := -1;
If RegOpenKeyEx(BaseKey,PChar(SubKey),0,KEY_READ,RegHandle) = ERROR_SUCCESS then
Begin
New(RegType);
New(BufSize);
RegType^ := Reg_DWORD;
BufSize^ := 4;
If RegQueryValueEx(RegHandle,PChar(KeyEntry),nil,RegType,@KeyValue,BufSize) = ERROR_SUCCESS then
Begin
Result := KeyValue;
End;
Dispose(BufSize);
Dispose(RegType);
RegCloseKey(RegHandle);
End;
end;
function AddBackSlash(S : WideString) : WideString; Overload;
var I : Integer;
begin
I := Length(S);
If I > 0 then If (S[I] <> '\') and (S[I] <> '/') then S := S+'\';
Result := S;
end;
function ConvertCharsToSpaces(S : WideString) : WideString;
begin
Result := TNT_WideStringReplace(TNT_WideStringReplace(TNT_WideStringReplace(S,'-', ' ', [rfReplaceAll]), '.', ' ', [rfReplaceAll]), '_', ' ', [rfReplaceAll]);
end;
function StripURLHash(sURL : String) : String;
var
iPos : Integer;
begin
iPos := Pos('#',sURL);
If iPos > 0 then
Begin
Result := Copy(sURL,1,iPos-1);
End
Else Result := sURL;
end;
procedure FileExtIntoStringList(fPath,fExt : WideString; fList : TTNTStrings; Recursive : Boolean);
var
sRec : TSearchRecW;
begin
If WideFindFirst(fPath+'*.*',faAnyFile,sRec) = 0 then
Begin
Repeat
If (Recursive = True) and (sRec.Attr and faDirectory = faDirectory) and (sRec.Name <> '.') and (sRec.Name <> '..') then
Begin
FileExtIntoStringList(AddBackSlash(fPath+sRec.Name),fExt,fList,Recursive);
End
else
If (sRec.Attr and faVolumeID = 0) and (sRec.Attr and faDirectory = 0) then
Begin
If WideCompareText(WideExtractFileExt(sRec.Name),fExt) = 0 then
fList.Add(fPath+sRec.Name);
End;
Until WideFindNext(sRec) <> 0;
WideFindClose(sRec);
End;
end;
function DecodeTextTags(S : WideString; RemoveSuffix : Boolean) : WideString;
var
S1 : WideString;
begin
If RemoveSuffix = True then S1 := ';' else S1 := '';
S := TNT_WideStringReplace(S,'&apos' +S1,'''',[rfReplaceAll]);
S := TNT_WideStringReplace(S,'&comma'+S1,',' ,[rfReplaceAll]);
S := TNT_WideStringReplace(S,'"' +S1,'"' ,[rfReplaceAll]);
S := TNT_WideStringReplace(S,'<' +S1,'<' ,[rfReplaceAll]);
S := TNT_WideStringReplace(S,'>' +S1,'>' ,[rfReplaceAll]);
S := TNT_WideStringReplace(S,'&' +S1,'&' ,[rfReplaceAll]);
S := TNT_WideStringReplace(S,'&pipe' +S1,'|' ,[rfReplaceAll]);
Result := S;
end;
function EncodeTextTags(S : WideString; AddSuffix : Boolean) : WideString;
var
S1 : WideString;
I : Integer;
begin
If AddSuffix = True then S1 := ';' else S1 := '';
S := TNT_WideStringReplace(S,'&' ,'&' +S1,[rfReplaceAll]);
S := TNT_WideStringReplace(S,'''','&apos' +S1,[rfReplaceAll]);
S := TNT_WideStringReplace(S,',' ,'&comma'+S1,[rfReplaceAll]);
S := TNT_WideStringReplace(S,'"' ,'"' +S1,[rfReplaceAll]);
S := TNT_WideStringReplace(S,'<' ,'<' +S1,[rfReplaceAll]);
S := TNT_WideStringReplace(S,'>' ,'>' +S1,[rfReplaceAll]);
S := TNT_WideStringReplace(S,'|' ,'&pipe' +S1,[rfReplaceAll]);
//for I := 1 to Length(S) do If Ord(S[I]) = $2028 then S[I] := #32;
for I := 1 to Length(S) do If Word(S[I]) = $2028 then S[I] := #32;
Result := S;
end;
function EncodePipe(S : WideString; AddSuffix : Boolean) : WideString;
var
S1 : WideString;
I : Integer;
begin
If AddSuffix = True then S1 := ';' else S1 := '';
S := TNT_WideStringReplace(S,'|' ,'&pipe' +S1,[rfReplaceAll]);
for I := 1 to Length(S) do If Word(S[I]) = $2028 then S[I] := #32;
Result := S;
end;
function EncodeDuration(Dur : Integer) : WideString;
var
dHours : Integer;
dMinutes : Integer;
dSeconds : Integer;
begin
dHours := Dur div 3600;
Dec(Dur,dHours*3600);
dMinutes := Dur div 60;
Dec(Dur,dMinutes*60);
dSeconds := Dur;
If dHours > 0 then
Result := IntToStr(dHours) +'h '+IntToStr(dMinutes)+'m' else
Result := IntToStr(dMinutes)+'m '+IntToStr(dSeconds)+'s';
end;
function TimeDifferenceToStr(nowTS,thenTS : TDateTime) : String;
const
yearInSec : Int64 = 31557600;
monthInSec : Int64 = 2629800;
dayInSec : Int64 = 86400;
hourInSec : Int64 = 3600;
minInSec : Int64 = 60;
var
secDiff : Int64;
minDiff : Int64;
hourDiff : Int64;
dayDiff : Int64;
monthDiff : Int64;
yearDiff : Int64;
begin
secDiff := SecondsBetween(nowTS,thenTS);
yearDiff := secDiff div yearInSec;
Dec(secDiff,yearDiff*yearInSec);
monthDiff := secDiff div monthInSec;
Dec(secDiff,monthDiff*monthInSec);
dayDiff := secDiff div dayInSec;
Dec(secDiff,dayDiff*dayInSec);
hourDiff := secDiff div hourInSec;
Dec(secDiff,hourDiff*hourInSec);
minDiff := secDiff div minInSec;
Dec(secDiff,minDiff*minInSec);
Result := '';
If yearDiff > 0 then
Begin
If yearDiff = 1 then
Result := IntToStr(yearDiff)+' year' else
Result := IntToStr(yearDiff)+' years';
End
else
If monthDiff > 0 then
Begin
If monthDiff = 1 then
Result := IntToStr(monthDiff)+' month' else
Result := IntToStr(monthDiff)+' months';
End
else
If dayDiff > 0 then
Begin
If dayDiff = 1 then
Result := IntToStr(dayDiff)+' day' else
Result := IntToStr(dayDiff)+' days';
End
else
If hourDiff > 0 then
Begin
If hourDiff = 1 then
Result := IntToStr(hourDiff)+' hour' else
Result := IntToStr(hourDiff)+' hours';
End
else
If minDiff > 0 then
Begin
If minDiff = 1 then
Result := IntToStr(minDiff)+' minute' else
Result := IntToStr(minDiff)+' minutes';
End
else
If secDiff > 0 then
Begin
If secDiff = 1 then
Result := IntToStr(secDiff)+' second' else
Result := IntToStr(secDiff)+' seconds';
End
end;
function UTF8StringToWideString(Const S : UTF8String) : WideString;
var
iLen :Integer;
sw :WideString;
begin
Result := '';
if Length(S) = 0 then Exit;
iLen := MultiByteToWideChar(CP_UTF8,0,PAnsiChar(s),-1,nil,0);
SetLength(sw,iLen);
MultiByteToWideChar(CP_UTF8,0,PAnsiChar(s),-1,PWideChar(sw),iLen);
iLen := Pos(#0,sw);
If iLen > 0 then SetLength(sw,iLen-1);
Result := sw;
end;
function WideStringToUTF8String(Const S : WideString) : UTF8String;
var
iLen:Integer;
su :UTF8String;
begin
result := '';
if Length(s)=0 then Exit;
iLen :=WideCharToMultiByte(CP_UTF8,0,PWideChar(s),-1,nil,0,nil,nil);
SetLength(su,iLen);
WideCharToMultiByte(CP_UTF8,0,PWideChar(s),-1,PAnsiChar(su),iLen,nil,nil);
Result:=su;
end;
function IntToStrDelimiter(iSrc : Int64; dChar : Char) : String;
var
I : Integer;
S : String;
begin
S := IntToStr(iSrc);
Result := S;
I := Length(S)-2;
While I > 1 do
Begin
Insert(dChar,Result,I);
Dec(I,3);
End;
end;
function DosToAnsi(S: String): String;
begin
SetLength(Result, Length(S));
OEMToCharBuff(PChar(S), PChar(Result), Length(S));
end;
function InputComboW(ownerWindow: THandle; const ACaption, APrompt: Widestring; const AList: TTNTStrings; var AOutput : WideString) : Boolean;
function GetCharSize(Canvas: TCanvas): TPoint;
var
I: Integer;
Buffer: array[0..51] of Char;
begin
for I := 0 to 25 do Buffer[I] := Chr(I + Ord('A'));
for I := 0 to 25 do Buffer[I + 26] := Chr(I + Ord('a'));
GetTextExtentPoint(Canvas.Handle, Buffer, 52, TSize(Result));
Result.X := Result.X div 52;
end;
var
Form : TTNTForm;
Prompt : TTNTLabel;
Combo : TTNTComboBox;
DialogUnits : TPoint;
ButtonTop,
ButtonWidth,
ButtonHeight : Integer;
CenterOnRect : TRect;
begin
AOutput := '';
Result := False;
Form := TTNTForm.Create(nil);
with Form do
try
Canvas.Font := Font;
DialogUnits := GetCharSize(Canvas);
BorderStyle := bsDialog;
Caption := ACaption;
ClientWidth := MulDiv(180, DialogUnits.X, 4);
Prompt := TTNTLabel.Create(Form);
with Prompt do
begin
Parent := Form;
Caption := APrompt;
Left := MulDiv(8, DialogUnits.X, 4);
Top := MulDiv(8, DialogUnits.Y, 8);
Constraints.MaxWidth := MulDiv(164, DialogUnits.X, 4);
WordWrap := True;
end;
Combo := TTNTComboBox.Create(Form);
with Combo do
begin
Parent := Form;
Style := csDropDownList;
Items.Assign(AList);
ItemIndex := 0;
Left := Prompt.Left;
Top := Prompt.Top + Prompt.Height + 5;
Width := MulDiv(164, DialogUnits.X, 4);
end;
ButtonTop := Combo.Top + Combo.Height + 15;
ButtonWidth := MulDiv(50, DialogUnits.X, 4);
ButtonHeight := MulDiv(14, DialogUnits.Y, 8);
with TTNTButton.Create(Form) do
begin
Parent := Form;
Caption := 'OK';
ModalResult := mrOk;
default := True;
SetBounds(MulDiv(38, DialogUnits.X, 4), ButtonTop, ButtonWidth, ButtonHeight);
end;
with TTNTButton.Create(Form) do
begin
Parent := Form;
Caption := 'Cancel';
ModalResult := mrCancel;
Cancel := True;
SetBounds(MulDiv(92, DialogUnits.X, 4), Combo.Top + Combo.Height + 15, ButtonWidth, ButtonHeight);
Form.ClientHeight := Top + Height + 13;
end;
If GetWindowRect(ownerWindow,CenterOnRect) = False then
GetWindowRect(0,CenterOnRect); // Can't find window, center on screen
SetBounds(CenterOnRect.Left+(((CenterOnRect.Right-CenterOnRect.Left)-Width) div 2),CenterOnRect.Top+(((CenterOnRect.Bottom-CenterOnRect.Top)-Height) div 2),Width,Height);
if ShowModal = mrOk then
begin
AOutput := Combo.Text;
Result := True;
end;
finally
Form.Free;
end;
end;
// Based on : http://stackoverflow.com/questions/1657105/delphi-html-decode
// With modifications to maintain UTF8Encoding while converting � type unicode characters
function HTMLUnicodeToUTF8(const AStr: String): String;
var
Sp, Cp, Tp : PChar;
S : String;
I, Code : Integer;
sWide : WideString;
begin
Result := '';
Sp := PChar(AStr);
Cp := Sp;
try
while Sp^ <> #0 do
begin
case Sp^ of
'&': begin
Cp := Sp;
Inc(Sp);
case Sp^ of