-
Notifications
You must be signed in to change notification settings - Fork 3
/
misc_utils_unit.pas
911 lines (796 loc) · 28.1 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
{$I RSS_PLUGIN_DEFINES.INC}
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;
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 URLEncodeUTF8(stInput : widestring) : string;
function HTMLUnicodeToUTF8(const AStr: 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 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 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;
implementation
uses
SysUtils, SyncObjs, TNTSysUtils, wininet, dateutils, graphics ,forms, tntforms, stdctrls, tntStdCtrls, controls;
const
// You must obtain your own key, it's free
URLIdentifier : String = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
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 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;
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]);
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
'a': if AnsiStrPos(Sp, 'amp;') = Sp then { do not localize }
begin
Inc(Sp,3);
Result := Result+'&';
end;
'l',
'g': if (AnsiStrPos(Sp, 'lt;') = Sp) or (AnsiStrPos(Sp, 'gt;') = Sp) then { do not localize }
begin
Cp := Sp;
Inc(Sp, 2);
while (Sp^ <> ';') and (Sp^ <> #0) do Inc(Sp);
if Cp^ = 'l' then Result := Result+'<' else Result := Result+'>';
end;
'q': if AnsiStrPos(Sp, 'quot;') = Sp then { do not localize }
begin
Inc(Sp,4);
Result := Result+'"';
end;
'#': begin
Tp := Sp;
Inc(Tp);
while (Sp^ <> ';') and (Sp^ <> #0) do Inc(Sp);
SetString(S, Tp, Sp - Tp);
Val(S, I, Code);
sWide := WideChar(I);
Result := Result+UTF8Encode(sWide);
end;
else
begin
Result := '';
Exit;
end;
end;
end
else ;
Result := Result+Sp^;
end;
Inc(Sp);
end;
except
Result := '';
end;
end;
procedure Split(S : String; Ch : Char; sList : TStrings); overload;
var
I : Integer;
begin
While Pos(Ch,S) > 0 do
Begin
I := Pos(Ch,S);
sList.Add(Copy(S,1,I-1));
Delete(S,1,I);
End;
If Length(S) > 0 then sList.Add(S);
end;
procedure Split(S : WideString; Ch : Char; sList : TTNTStrings); overload;
var
I : Integer;
begin
While Pos(Ch,S) > 0 do
Begin
I := Pos(Ch,S);
sList.Add(Copy(S,1,I-1));
Delete(S,1,I);
End;
If Length(S) > 0 then sList.Add(S);
end;
initialization
QueryPerformanceFrequency(qTimer64Freq);
QueryPerformanceCounter(DebugStartTime);
csDebug := TCriticalSection.Create;
finalization
csDebug.Free;
end.