-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathSysUtils.pas
3413 lines (3158 loc) · 98.1 KB
/
SysUtils.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 SysUtils;
{
LVCL - Very LIGHT VCL routines
------------------------------
Tiny replacement for the standard VCL SysUtils.pas
Just put the LVCL directory in your Project/Options/Directories/SearchPath
and your .EXE will shrink from 300KB to 30KB
Notes:
- Some routines are improved/faster: EncodeDate, DecodeDate, DecodeTime,
IntToStr, HexToStr, UpperCase, CompareText, StrCopy, StrLen, StrComp,
FileExists, CompareMem...
- Date strings have a fixed format: 'YYYY/MM/DD hh:mm:ss'
- format() supports quite all usual format (%% %s %d %x %.prec? %index:?),
but without floating point args (saves 3KB on EXE size -> use str() + %s)
- slow MBCS Ansi*() function mostly removed
- support Win NT 4.0 and Win95 OSR2 minimum
- Cross-Platform: this SysUtils unit can be used on (Cross)Kylix under Linux
The contents of this file are subject to the Mozilla Public License
Version 1.1 (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.mozilla.org/MPL
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.
The Initial Developer of the Original Code is Arnaud Bouchez.
This work is Copyright (c)2008 Arnaud Bouchez - http://bouchez.info
Emulates the original Delphi/Kylix Cross-Platform Runtime Library
(c)2000,2001 Borland Software Corporation
Portions created by Paul Toth are (c)2001 Paul Toth - http://tothpaul.free.fr
All Rights Reserved.
Contributors:
- Vadim (pult)
}
{ $D-,L-}
Interface
uses
{$ifdef MSWINDOWS}
Windows;
{$else}
Types,
LibC;
{$endif}
{$WARNINGS OFF}
{$IF CompilerVersion >= 24.00}
{$ZEROBASEDSTRINGS OFF}
{$IFEND}
type
{$IFNDEF UNICODE}
{$IFDEF FPC}
NativeInt = PtrInt;
NativeUInt = PtrUInt;
{$ELSE}
{.$IFDEF CPUX86}
NativeInt = Integer;
NativeUInt = Cardinal;
{.$ENDIF}
{$ENDIF}
IntPtr = NativeInt;
UIntPtr = NativeUInt;
{$ENDIF}
TMethod = record
Code, Data: Pointer;
end;
LongRec = packed record
case integer of
0: (Lo, Hi: Word);
1: (Words: array [0..1] of Word);
2: (Bytes: array [0..3] of Byte);
end;
Int64Rec = packed record
case integer of
0: (Lo, Hi: Cardinal);
1: (Cardinals: array [0..1] of Cardinal);
2: (Words: array [0..3] of Word);
3: (Bytes: array [0..7] of Byte);
end;
PByteArray = ^TByteArray;
TByteArray = array[0..32767] of Byte;
PWordArray = ^TWordArray;
TWordArray = array[0..32767] of Word;
TSysCharSet = set of AnsiChar;
{$IFDEF UNICODE}
TBytes = TArray<Byte>;
{$ELSE}
TBytes = array of Byte;
RawByteString = AnsiString;
{$ENDIF}
type
PDayTable = ^TDayTable;
TDayTable = array[1..12] of Word;
TTimeStamp = record
Time: integer; { Number of milliseconds since midnight }
Date: integer; { One plus number of days since 1/1/0001 }
end;
const
MonthDays: array [Boolean] of TDayTable =
((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31));
HoursPerDay = 24;
MinsPerHour = 60;
SecsPerMin = 60;
MSecsPerSec = 1000;
MinsPerDay = HoursPerDay * MinsPerHour;
SecsPerDay = MinsPerDay * SecsPerMin;
MSecsPerDay = SecsPerDay * MSecsPerSec;
FMSecsPerDay: Single = MSecsPerDay;
IMSecsPerDay: integer = MSecsPerDay;
DateDelta = 693594;
UnixDateDelta = 25569;
{$ifdef MSWINDOWS}
const
clBlack = $000000;
clMaroon = $000080;
clGreen = $008000;
clOlive = $008080;
clNavy = $800000;
clPurple = $800080;
clTeal = $808000;
clGray = $808080;
clSilver = $C0C0C0;
clRed = $0000FF;
clLime = $00FF00;
clYellow = $00FFFF;
clBlue = $FF0000;
clFuchsia = $FF00FF;
clAqua = $FFFF00;
clLtGray = $C0C0C0;
clDkGray = $808080;
clWhite = $FFFFFF;
clNone = $1FFFFFFF;
clDefault = $20000000;
clScrollBar = COLOR_SCROLLBAR or $80000000;
clBackground = COLOR_BACKGROUND or $80000000;
clActiveCaption = COLOR_ACTIVECAPTION or $80000000;
clInactiveCaption = COLOR_INACTIVECAPTION or $80000000;
clMenu = COLOR_MENU or $80000000;
clWindow = COLOR_WINDOW or $80000000;
clWindowFrame = COLOR_WINDOWFRAME or $80000000;
clMenuText = COLOR_MENUTEXT or $80000000;
clWindowText = COLOR_WINDOWTEXT or $80000000;
clCaptionText = COLOR_CAPTIONTEXT or $80000000;
clActiveBorder = COLOR_ACTIVEBORDER or $80000000;
clInactiveBorder = COLOR_INACTIVEBORDER or $80000000;
clAppWorkSpace = COLOR_APPWORKSPACE or $80000000;
clHighlight = COLOR_HIGHLIGHT or $80000000;
clHighlightText = COLOR_HIGHLIGHTTEXT or $80000000;
clBtnFace = COLOR_BTNFACE or $80000000;
clBtnShadow = COLOR_BTNSHADOW or $80000000;
clGrayText = COLOR_GRAYTEXT or $80000000;
clBtnText = COLOR_BTNTEXT or $80000000;
clInactiveCaptionText = COLOR_INACTIVECAPTIONTEXT or $80000000;
clBtnHighlight = COLOR_BTNHIGHLIGHT or $80000000;
cl3DDkShadow = COLOR_3DDKSHADOW or $80000000;
cl3DLight = COLOR_3DLIGHT or $80000000;
clInfoText = COLOR_INFOTEXT or $80000000;
clInfoBk = COLOR_INFOBK or $80000000;
const
PathDelim = '\';
DriveDelim = ':';
PathSep = ';';
procedure Error(const Msg: string; Value: integer = -1);
procedure MsgBox(const Msg: string);
function IdentToColor(Ident: PChar): integer;
{$endif}
function Rect(Left, Top, Width, Height: integer): TRect;
function StringIndex(const str: string; const p: array of PChar):integer;
function IntToHex(Value: cardinal; Digits: integer): string;
function StrToInt(const S: string): integer;
function StrToIntDef(const S: string; Default: integer): integer;
function TryStrToInt(const S: string; out Value: integer): Boolean;
function GUIDToString(const GUID: TGUID): string;
function StrLen(S: PChar): integer; overload;
{$IFDEF UNICODE}
function StrLen(S: PAnsiChar): integer; overload;
{$ENDIF}
function StrLCopy(Dest: PChar; const Source: PChar; MaxLen: Cardinal): PChar;
function StrComp(Str1, Str2: PChar): integer;
function StrIComp(Str1, Str2: PChar): Integer;
function StrEnd(Str: PChar): PChar;
function StrCopy(Dest: PChar; const Source: PChar): PChar;
function StrCat(Dest: PChar; const Source: PChar): PChar;
function StrPCopy(Dest: PChar; const Source: string): PChar;
function StrScan(Str: PChar; Chr: Char): PChar;
function AnsiStrScan(Str: PChar; Chr: Char): PChar; {$ifdef UNICODE}inline;{$endif}
function Trim(const S: string): string; overload;
function TrimLeft(const S: string): string; overload;
function TrimRight(const S: string): string; overload;
{$IFDEF UNICODE}
function Trim(const S: AnsiString): AnsiString; overload;
function TrimLeft(const S: AnsiString): AnsiString; overload;
function TrimRight(const S: AnsiString): AnsiString; overload;
{$ENDIF}
function QuotedStr(const S: string): string;
function AnsiQuotedStr(const S: string; Quote: Char): string;
function AnsiExtractQuotedStr(var Src: PChar; Quote: Char): string;
function AnsiDequotedStr(const S: string; AQuote: Char): string;
function ExtractFilePath(const FileName: string): string;
function ExtractFileDir(const FileName: string): string;
function ExtractFileName(const FileName: string): string;
function ExtractFileExt(const FileName: string): string;
function ChangeFileExt(const FileName, Extension: string): string;
function ExtractFileDrive(const FileName: string): string;
function IncludeTrailingPathDelimiter(const S: string): string;
function ExcludeTrailingPathDelimiter(const S: string): string;
function LastDelimiter(const Delimiters, S: string): integer;
function UpperCase(const S: string): string;
function LowerCase(const S: string): string;
{$ifdef MSWINDOWS}
function DiskFree(Drive: Byte): Int64;
function DiskSize(Drive: Byte): Int64;
{$endif}
type
TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase);
function StringReplace(const S, OldPattern, NewPattern: string;
Flags: TReplaceFlags): string;
function CompareStr(const S1, S2: string): Integer;
function CompareText(const S1, S2: string): integer;
function SameText(const S1, S2: string): Boolean;
// Borland's code
const
faReadOnly = $00000001;
faHidden = $00000002;
faSysFile = $00000004;
faVolumeID = $00000008;
faDirectory = $00000010;
faArchive = $00000020;
faSymLink = $00000040;
faAnyFile = $0000003F;
{ File open modes }
{$ifdef MSWINDOWS}
fmOpenRead = $0000;
fmOpenWrite = $0001;
fmOpenReadWrite = $0002;
fmShareCompat = $0000;
fmShareExclusive = $0010;
fmShareDenyWrite = $0020;
fmShareDenyRead = $0030;
fmShareDenyNone = $0040;
function ExpandFileName(const FileName: string): string; // not relevant
{$else}
fmOpenRead = O_RDONLY;
fmOpenWrite = O_WRONLY;
fmOpenReadWrite = O_RDWR;
// fmShareCompat not supported
fmShareExclusive = $0010;
fmShareDenyWrite = $0020;
// fmShareDenyRead not supported
fmShareDenyNone = $0030;
const
PathDelim = '/';
DriveDelim = '';
PathSep = ':';
{$endif} // Linux
type
TFileName = type string;
function FileCreate(const FileName: TFileName): THandle;
function FileOpen(const FileName: TFileName; Mode: LongWord): THandle;
procedure FileClose(Handle: THandle);
function FileSeek(Handle: THandle; Offset, Origin: integer): integer;
function FileRead(Handle: THandle; var Buffer; Count: LongWord): integer;
function FileWrite(Handle: THandle; const Buffer; Count: LongWord): integer;
function FileDateToDateTime(FileDate: integer): TDateTime;
function DateTimeToFileDate(DateTime: TDateTime): integer;
function DirectoryExists(const Directory: TFileName): Boolean;
function ForceDirectories(Dir: string): Boolean;
function GetCurrentDir: TFileName;
function CreateDir(const Dir: TFileName): Boolean;
function RemoveDir(const Dir: TFileName): Boolean;
function SafeLoadLibrary(const Filename: TFileName): HMODULE;
function FileExists(const FileName: TFileName): Boolean;
function FileAge(const FileName: TFileName): integer; overload;
function FileAge(const FileName: string; out FileDateTime: TDateTime): Boolean; overload;
function FileSetDate(F: THandle; Age: integer): integer; overload;
function FileSetDate(const FileName: TFileName; Age: integer): integer; overload;
function RenameFile(const OldName, NewName: TFileName): Boolean;
function DeleteFile(const FileName: TFileName): Boolean;
{$ifdef MSWINDOWS}
function GetModuleName(Module: HMODULE): TFileName;
{$endif}
var
lastFileAgeSize: integer; // updated with FileAge(fileName)
type
TSearchRec = record
Time: integer;
Size: integer;
Attr: integer;
Name: string;
ExcludeAttr: integer;
{$ifdef MSWINDOWS}
FindHandle: THandle;
FindData: TWin32FindData;
{$else}
Mode: mode_t;
FindHandle: Pointer;
PathOnly: string;
Pattern: string;
{$endif}
end;
function FindFirst(const Path: string; Attr: integer;
var F: TSearchRec): integer;
function FindNext(var F: TSearchRec): integer;
procedure FindClose(var F: TSearchRec);
function Now: TDateTime;
function Time: TDateTime;
function FileGetDate(Handle: THandle): Integer;
function EncodeTime(Hour, Min, Sec, MSec: Word): TDateTime; overload;
function EncodeTime(Hour, Min, Sec, MSec: cardinal): TDateTime; overload;
function TryEncodeDate(Year, Month, Day: Word; out Date: TDateTime): Boolean;
function TryEncodeTime(Hour, Min, Sec, MSec: Word; out Time: TDateTime): Boolean;
procedure DecodeTime(DateTime: TDateTime; var Hour, Min, Sec, MSec: Word);
function DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;
function TimeStampToDateTime(const TimeStamp: TTimeStamp): TDateTime;
function SystemTimeToDateTime(const SystemTime: TSystemTime): TDateTime;
function TrySystemTimeToDateTime(const SystemTime: TSystemTime; out DateTime: TDateTime): Boolean;
function SystemTimeToDosDateTime(const SystemTime: TSystemTime): integer;
function NowToDosDateTime: integer;
procedure DosDateTimeToSystemTime(DosDateTime: integer; out SystemTime: TSystemTime);
// warning: wDayOfWeek is not set
function DateTimeToStr(const DateTime: TDateTime): string;
function TryStrToDateTime(const S: string; out Value: TDateTime): Boolean;
// date format is fixed to 'YYYY/MM/DD hh:mm:ss' string layout
/// fast write Y (as 4 chars) in P^
procedure YearToPAnsiChar(Y: Word; P: PAnsiChar);
{$ifdef MSWINDOWS}
(*function SelectDirectory(Handle: integer; const Caption: string;
var Directory: string): Boolean;*)
procedure DateTimeToSystemTime(DateTime: TDateTime; var SystemTime: TSystemTime);
function GetFileVersion(const aFileName: string): Cardinal;
{$endif}
function AllocMem(Size: integer): pointer;
procedure FreeAndNil(var Obj); {$ifdef UNICODE}inline;{$endif}
function CompareMem(P1, P2: Pointer; Length: integer): Boolean;
function Format(const Format: string; const Args: array of const): string;
// supported: %% %s %d %x %.prec? %index:?
function IntToStr(Value: integer): string; overload;
function IntToStr(Value: Int64): string; overload;
function EncodeDate(Year, Month, Day: Word): TDateTime;
procedure DecodeDate(const DT: TDateTime; out Year, Month, Day: word); overload;
function IsLeapYear(Year: cardinal): Boolean;
type
Exception = class(TObject)
private
FMessage: string;
public
constructor Create(const Msg: string);
constructor CreateFmt(const Msg: string; const Args: array of const);
property Message: string read FMessage write FMessage;
end;
EAssertionFailed = class(Exception);
EStreamError = class(Exception);
EExternal = class(Exception);
EExternalException = class(EExternal);
EIntError = class(EExternal);
EDivByZero = class(EIntError);
ERangeError = class(EIntError);
EIntOverflow = class(EIntError);
EMathError = class(EExternal);
EInvalidOp = class(EMathError);
EZeroDivide = class(EMathError);
EOverflow = class(EMathError);
EUnderflow = class(EMathError);
EAccessViolation = class(EExternal);
ExceptClass = class of Exception;
EAbort = class(Exception);
EOSError = class(EExternal)
public
ErrorCode: cardinal;
end;
EInOutError = class(Exception)
public
ErrorCode: Integer;
end;
EConvertError = class(Exception);
procedure OutOfMemoryError;
function SysErrorMessage(ErrorCode: Integer): string;
procedure RaiseLastOSError;
procedure Abort;
{$ifdef MSWINDOWS}
var
Win32Platform: integer = 0;
Win32MajorVersion: integer = 0;
Win32MinorVersion: integer = 0;
Win32BuildNumber: integer = 0;
Win32CSDVersion: string = '';
{$endif}
procedure Sleep(milliseconds: Cardinal);{$ifdef MSWINDOWS} stdcall; {$endif}
function AnsiSameText(const S1, S2: string): Boolean;
function AnsiCompareText(const S1, S2: string): Integer;
function AnsiSameStr(const S1, S2: string): Boolean;
function AnsiCompareStr(const S1, S2: string): Integer;
function AnsiUpperCase(const S: string): string;
function AnsiLowerCase(const S: string): string;
const
// our customs SysUtils.pas (normal and LVCL) contains the same array
TwoDigitLookup: packed array[0..99] of array[1..2] of AnsiChar =
('00','01','02','03','04','05','06','07','08','09',
'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');
HexChars: array[0..15] of Char = '0123456789ABCDEF';
implementation
uses
Classes;
procedure AddCharLS(var S: string; c: char);
var L: integer;
begin
L := integer(s);
if L<>0 then
L := pInteger(L-4)^; // L := length(S)
SetLength(s,L+1); // with FastMM4, this SetLength() is almost immediate
{$ifdef UNICODE}
PWordArray(s)[L] := ord(c);
{$else}
PByteArray(s)[L] := ord(c);
{$endif}
end;
procedure DecodeDate(const DT: TDateTime; out Year, Month, Day: word);
var J: integer;
begin
J := pred((Trunc (DT) + 693900) shl 2);
Year := J div 146097;
Day := (J - 146097 * Year) shr 2;
J := (Day shl 2 + 3) div 1461;
Day := (Day shl 2 + 7 - 1461 * J) shr 2;
Month := (5 * Day - 3) div 153;
Day := (5 * Day + 2 - 153 * Month) div 5;
Year := 100 * Year + J;
if Month < 10 then
Inc (Month, 3)
else begin
Dec (Month, 9);
Inc (Year);
end;
end;
function IsLeapYear(Year: cardinal): Boolean;
asm
test al,3
jz @@CheckCentury
xor eax,eax {Return False}
ret
@@CheckCentury:
mov edx,$028F5C29 {((2^32)+100-1)/100}
mov ecx,eax
mul edx {EDX = Year DIV 100}
mov eax,edx
imul edx,100 {EDX = (Year DIV 100) * 100}
cmp ecx,edx
je @@Century {Year is Divisible by 100}
mov al,true {Return True}
ret
@@Century:
test al,3 {Check if Divisible by 400}
setz al {Set Result}
end;
type TWordRec = packed record YDiv100, YMod100: byte; end;
function Div100(Y: Word): TWordRec;
asm
mov cl,100
div cl // ah=remainder=Y mod 100, al=quotient=Year div 100
end;
function EncodeDate(Year, Month, Day: Word): TDateTime;
begin
result := 0;
if (Month < 1) or (Month > 12) then exit;
if (Day <= MonthDays[true][Month]) and // test worse case = leap year
(Year >= 1) and (Year < 10000) and
(Month < 13) and (Day > 0) then begin
if Month > 2 then
Dec(Month, 3) else
if Month > 0 then begin
Inc(Month, 9);
Dec(Year);
end
else // Month <= 0
exit;
with Div100(Year) do
result := (146097 * YDiv100) shr 2 + (1461 * YMod100) shr 2 +
(153 * Month + 2) div 5 + Day - 693900;
end;
end;
function IntToStr(Value : integer): string;
{$ifdef UNICODE}
begin
str(Value,result);
end;
{$else}
// 3x faster than SysUtils.IntToStr
// from IntToStr32_JOH_IA32_6_a
asm
push ebx
push edi
push esi
mov ebx,eax {Value}
sar ebx,31 {0 for +ve Value or -1 for -ve Value}
xor eax,ebx
sub eax,ebx {ABS(Value)}
mov esi,10 {Max Digits in result}
mov edi,edx {@result}
cmp eax,10; sbb esi, 0
cmp eax,100; sbb esi, 0
cmp eax,1000; sbb esi, 0
cmp eax,10000; sbb esi, 0
cmp eax,100000; sbb esi, 0
cmp eax,1000000; sbb esi, 0
cmp eax,10000000; sbb esi, 0
cmp eax,100000000; sbb esi, 0
cmp eax,1000000000; sbb esi, ebx {Digits (Including Sign Character)}
mov ecx,[edx] {result}
test ecx,ecx
je @@NewStr {Create New string for result}
cmp dword ptr [ecx-8], 1
jne @@ChangeStr {Reference Count<>1}
cmp esi,[ecx-4]
je @@LengthOk {Existing Length = Required Length}
sub ecx,8 {Allocation Address}
push eax {ABS(Value)}
push ecx
mov eax,esp
lea edx,[esi+9] {New Allocation Size}
call system.@ReallocMem {Reallocate result string}
pop ecx
pop eax {ABS(Value)}
add ecx,8 {result}
mov [ecx-4],esi {Set New Length}
mov byte ptr [ecx+esi],0 {Add Null Terminator}
mov [edi],ecx {Set result Address}
jmp @@LengthOk
@@ChangeStr:
mov edx,dword ptr [ecx-8] {Reference Count}
add edx,1
jz @@NewStr {RefCount = -1 (string Constant)}
lock dec dword ptr [ecx-8] {Decrement Existing Reference Count}
@@NewStr:
push eax {ABS(Value)}
mov eax,esi {Length}
call system.@NewAnsiString
mov [edi],eax {Set result Address}
mov ecx,eax {result}
pop eax {ABS(Value)}
@@LengthOk:
mov byte ptr [ecx],'-' {Store '-' Character (May be Overwritten)}
add esi,ebx {Digits (Excluding Sign Character)}
sub ecx,ebx {Destination of 1st Digit}
sub esi,2 {Digits (Excluding Sign Character) - 2}
jle @@FinalDigits {1 or 2 Digit Value}
cmp esi,8 {10 Digit Value?}
jne @@SetResult {Not a 10 Digit Value}
sub eax,2000000000 {Digit 10 must be either '1' or '2'}
mov dl,'2'
jnc @@SetDigit10 {Digit 10 = '2'}
mov dl,'1' {Digit 10 = '1'}
add eax,1000000000
@@SetDigit10:
mov [ecx],dl {Save Digit 10}
mov esi,7 {9 Digits Remaining}
add ecx,1 {Destination of 2nd Digit}
@@SetResult:
mov edi,$28F5C29 {((2^32)+100-1)/100}
@@Loop:
mov ebx,eax {Dividend}
mul edi {EDX = Dividend DIV 100}
mov eax,edx {Set Next Dividend}
imul edx,-200 {-2 * (100 * Dividend DIV 100)}
movzx edx,word ptr [TwoDigitLookup+ebx*2+edx] {Dividend MOD 100 in ASCII}
mov [ecx+esi],dx
sub esi,2
jg @@Loop {Loop until 1 or 2 Digits Remaining}
@@FinalDigits:
pop esi
pop edi
pop ebx
jnz @@LastDigit
movzx eax,word ptr [TwoDigitLookup+eax*2]
mov [ecx],ax {Save Final 2 Digits}
ret
@@LastDigit:
or al,'0' {Ascii Adjustment}
mov [ecx],al {Save Final Digit}
end;
{$endif}
function IntToStr(Value: Int64): string;
{$ifdef UNICODE}
begin
str(Value,result);
end;
{$else}
// from IntToStr64_JOH_IA32_6_b
asm
push ebx
mov ecx, [ebp+8] {Low Integer of Value}
mov edx, [ebp+12] {High Integer of Value}
xor ebp, ebp {Clear Sign Flag (EBP Already Pushed)}
mov ebx, ecx {Low Integer of Value}
test edx, edx
jnl @@AbsValue
mov ebp, 1 {EBP = 1 for -ve Value or 0 for +ve Value}
neg ecx
adc edx, 0
neg edx
@@AbsValue: {EDX:ECX = Abs(Value)}
jnz @@Large
test ecx, ecx
js @@Large
mov edx, eax {@Result}
mov eax, ebx {Low Integer of Value}
call IntToStr {Call Fastest Integer IntToStr Function}
pop ebx
@@Exit:
pop ebp {Restore Stack and Exit}
ret 8
@@Large:
push edi
push esi
mov edi, eax
xor ebx, ebx
xor eax, eax
@@Test15: {Test for 15 or More Digits}
cmp edx, $00005af3 {100000000000000 div $100000000}
jne @@Check15
cmp ecx, $107a4000 {100000000000000 mod $100000000}
@@Check15:
jb @@Test13
@@Test17: {Test for 17 or More Digits}
cmp edx, $002386f2 {10000000000000000 div $100000000}
jne @@Check17
cmp ecx, $6fc10000 {10000000000000000 mod $100000000}
@@Check17:
jb @@Test15or16
@@Test19: {Test for 19 Digits}
cmp edx, $0de0b6b3 {1000000000000000000 div $100000000}
jne @@Check19
cmp ecx, $a7640000 {1000000000000000000 mod $100000000}
@@Check19:
jb @@Test17or18
mov al, 19
jmp @@SetLength
@@Test17or18: {17 or 18 Digits}
mov bl, 18
cmp edx, $01634578 {100000000000000000 div $100000000}
jne @@SetLen
cmp ecx, $5d8a0000 {100000000000000000 mod $100000000}
jmp @@SetLen
@@Test15or16: {15 or 16 Digits}
mov bl, 16
cmp edx, $00038d7e {1000000000000000 div $100000000}
jne @@SetLen
cmp ecx, $a4c68000 {1000000000000000 mod $100000000}
jmp @@SetLen
@@Test13: {Test for 13 or More Digits}
cmp edx, $000000e8 {1000000000000 div $100000000}
jne @@Check13
cmp ecx, $d4a51000 {1000000000000 mod $100000000}
@@Check13:
jb @@Test11
@@Test13or14: {13 or 14 Digits}
mov bl, 14
cmp edx, $00000918 {10000000000000 div $100000000}
jne @@SetLen
cmp ecx, $4e72a000 {10000000000000 mod $100000000}
jmp @@SetLen
@@Test11: {10, 11 or 12 Digits}
cmp edx, $02 {10000000000 div $100000000}
jne @@Check11
cmp ecx, $540be400 {10000000000 mod $100000000}
@@Check11:
mov bl, 11
jb @@SetLen {10 Digits}
@@Test11or12: {11 or 12 Digits}
mov bl, 12
cmp edx, $17 {100000000000 div $100000000}
jne @@SetLen
cmp ecx, $4876e800 {100000000000 mod $100000000}
@@SetLen:
sbb eax, 0 {Adjust for Odd/Evem Digit Count}
add eax, ebx
@@SetLength: {Abs(Value) in EDX:ECX, Digits in EAX}
push ecx {Save Abs(Value)}
push edx
lea edx, [eax+ebp] {Digits Needed (Including Sign Character)}
mov ecx, [edi] {@Result}
mov esi, edx {Digits Needed (Including Sign Character)}
test ecx, ecx
je @@NewStr {Create New AnsiString for Result}
cmp dword ptr [ecx-8], 1
jne @@ChangeStr {Reference Count<>1}
cmp esi, [ecx-4]
je @@LengthOk {Existing Length = Required Length}
sub ecx, 8 {Allocation Address}
push eax {ABS(Value)}
push ecx
mov eax, esp
lea edx, [esi+9] {New Allocation Size}
call system.@ReallocMem {Reallocate Result AnsiString}
pop ecx
pop eax {ABS(Value)}
add ecx, 8 {@Result}
mov [ecx-4], esi {Set New Length}
mov byte ptr [ecx+esi], 0 {Add Null Terminator}
mov [edi], ecx {Set Result Address}
jmp @@LengthOk
@@ChangeStr:
mov edx, dword ptr [ecx-8] {Reference Count}
add edx, 1
jz @@NewStr {RefCount = -1 (AnsiString Constant)}
lock dec dword ptr [ecx-8] {Decrement Existing Reference Count}
@@NewStr:
push eax {ABS(Value)}
mov eax, esi {Length}
call system.@NewAnsiString
mov [edi], eax {Set Result Address}
mov ecx, eax {@Result}
pop eax {ABS(Value)}
@@LengthOk:
mov edi, [edi] {@Result}
sub esi, ebp {Digits Needed (Excluding Sign Character)}
mov byte ptr [edi], '-' {Store '-' Character (May be Overwritten)}
add edi, ebp {Destination of 1st Digit}
pop edx {Restore Abs(Value)}
pop eax
cmp esi, 17
jl @@LessThan17Digits {Digits < 17}
je @@SetDigit17 {Digits = 17}
cmp esi, 18
je @@SetDigit18 {Digits = 18}
mov cl, '0' - 1
mov ebx, $a7640000 {1000000000000000000 mod $100000000}
mov ebp, $0de0b6b3 {1000000000000000000 div $100000000}
@@CalcDigit19:
add ecx, 1
sub eax, ebx
sbb edx, ebp
jnc @@CalcDigit19
add eax, ebx
adc edx, ebp
mov [edi], cl
add edi, 1
@@SetDigit18:
mov cl, '0' - 1
mov ebx, $5d8a0000 {100000000000000000 mod $100000000}
mov ebp, $01634578 {100000000000000000 div $100000000}
@@CalcDigit18:
add ecx, 1
sub eax, ebx
sbb edx, ebp
jnc @@CalcDigit18
add eax, ebx
adc edx, ebp
mov [edi], cl
add edi, 1
@@SetDigit17:
mov cl, '0' - 1
mov ebx, $6fc10000 {10000000000000000 mod $100000000}
mov ebp, $002386f2 {10000000000000000 div $100000000}
@@CalcDigit17:
add ecx, 1
sub eax, ebx
sbb edx, ebp
jnc @@CalcDigit17
add eax, ebx
adc edx, ebp
mov [edi], cl
add edi, 1 {Update Destination}
mov esi, 16 {Set 16 Digits Left}
@@LessThan17Digits: {Process Next 8 Digits}
mov ecx, 100000000 {EDX:EAX = Abs(Value) = Dividend}
div ecx
mov ebp, eax {Dividend DIV 100000000}
mov ebx, edx
mov eax, edx {Dividend MOD 100000000}
mov edx, $51EB851F
mul edx
shr edx, 5 {Dividend DIV 100}
mov eax, edx {Set Next Dividend}
lea edx, [edx*4+edx]
lea edx, [edx*4+edx]
shl edx, 2 {Dividend DIV 100 * 100}
sub ebx, edx {Remainder (0..99)}
movzx ebx, word ptr [TwoDigitLookup+ebx*2]
shl ebx, 16
mov edx, $51EB851F
mov ecx, eax {Dividend}
mul edx
shr edx, 5 {Dividend DIV 100}
mov eax, edx
lea edx, [edx*4+edx]
lea edx, [edx*4+edx]
shl edx, 2 {Dividend DIV 100 * 100}
sub ecx, edx {Remainder (0..99)}
or bx, word ptr [TwoDigitLookup+ecx*2]
mov [edi+esi-4], ebx {Store 4 Digits}
mov ebx, eax
mov edx, $51EB851F
mul edx
shr edx, 5 {EDX = Dividend DIV 100}
lea eax, [edx*4+edx]
lea eax, [eax*4+eax]
shl eax, 2 {EAX = Dividend DIV 100 * 100}
sub ebx, eax {Remainder (0..99)}
movzx ebx, word ptr [TwoDigitLookup+ebx*2]
movzx ecx, word ptr [TwoDigitLookup+edx*2]
shl ebx, 16
or ebx, ecx
mov [edi+esi-8], ebx {Store 4 Digits}
mov eax, ebp {Remainder}
sub esi, 10 {Digits Left - 2}
jz @@Last2Digits
@@SmallLoop: {Process Remaining Digits}
mov edx, $28F5C29 {((2^32)+100-1)/100}
mov ebx, eax {Dividend}
mul edx
mov eax, edx {Set Next Dividend}
imul edx, -200
movzx edx, word ptr [TwoDigitLookup+ebx*2+edx] {Dividend MOD 100 in ASCII}
mov [edi+esi], dx
sub esi, 2
jg @@SmallLoop {Repeat Until Less than 2 Digits Remaining}
jz @@Last2Digits
or al , '0' {Ascii Adjustment}
mov [edi], al {Save Final Digit}
jmp @@Done
@@Last2Digits:
movzx eax, word ptr [TwoDigitLookup+eax*2]
mov [edi], ax {Save Final 2 Digits}
@@Done:
pop esi
pop edi
pop ebx
end;
{$endif}
{$ifdef MSWINDOWS}
procedure Error(const Msg:string; Value: integer = -1);
begin
if Value=-1 then
MessageBox(0,pointer(Msg),nil,MB_ICONSTOP or MB_TASKMODAL or MB_DEFAULT_DESKTOP_ONLY) else
MessageBox(0,pointer(Msg+' '+IntToStr(Value)),nil,MB_ICONSTOP or MB_TASKMODAL or MB_DEFAULT_DESKTOP_ONLY);
end;
procedure MsgBox(const Msg:string);
begin
MessageBox(0,pointer(Msg),nil,0);
end;
{$endif}
function Rect(Left,Top,Width,Height:integer):TRect;
begin
result.Left := Left;
result.Top := Top;
result.Right := Left+Width;
result.Bottom := Top+Height;
end;
function StringIndex(const str: string; const p: array of PChar): integer;
begin
result := High(p);
while (result>=0) and (StrComp(p[result],pointer(str))<>0) do
dec(result);
end;
{$ifdef MSWINDOWS}
{ Color mapping routines }
function IdentToColor(Ident: PChar): integer;
type
TIdentMapEntry = packed record
Value: integer;
Name: PChar;
end;
const // Value are integer, not enumerates -> no RTTI trick possible
Colors: array[0..41] of TIdentMapEntry = (
(Value: clBlack; Name: 'Black'),
(Value: clMaroon; Name: 'Maroon'),
(Value: clGreen; Name: 'Green'),
(Value: clOlive; Name: 'Olive'),
(Value: clNavy; Name: 'Navy'),
(Value: clPurple; Name: 'Purple'),
(Value: clTeal; Name: 'Teal'),
(Value: clGray; Name: 'Gray'),
(Value: clSilver; Name: 'Silver'),
(Value: clRed; Name: 'Red'),
(Value: clLime; Name: 'Lime'),
(Value: clYellow; Name: 'Yellow'),
(Value: clBlue; Name: 'Blue'),
(Value: clFuchsia; Name: 'Fuchsia'),
(Value: clAqua; Name: 'Aqua'),
(Value: clWhite; Name: 'White'),
(Value: clScrollBar; Name: 'ScrollBar'),
(Value: clBackground; Name: 'Background'),
(Value: clActiveCaption; Name: 'ActiveCaption'),
(Value: clInactiveCaption; Name: 'InactiveCaption'),
(Value: clMenu; Name: 'Menu'),
(Value: clWindow; Name: 'Window'),
(Value: clWindowFrame; Name: 'WindowFrame'),
(Value: clMenuText; Name: 'MenuText'),
(Value: clWindowText; Name: 'WindowText'),
(Value: clCaptionText; Name: 'CaptionText'),
(Value: clActiveBorder; Name: 'ActiveBorder'),
(Value: clInactiveBorder; Name: 'InactiveBorder'),
(Value: clAppWorkSpace; Name: 'AppWorkSpace'),
(Value: clHighlight; Name: 'Highlight'),
(Value: clHighlightText; Name: 'HighlightText'),
(Value: clBtnFace; Name: 'BtnFace'),
(Value: clBtnShadow; Name: 'BtnShadow'),