-
Notifications
You must be signed in to change notification settings - Fork 22
/
Classes.pas
2058 lines (1833 loc) · 53 KB
/
Classes.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 Classes;
{
LVCL - Very LIGHT VCL routines
------------------------------
Tiny replacement for the standard VCL Classes.pas
Just put the LVCL directory in your Project/Options/Directories/SearchPath
and your .EXE will shrink from 300KB to 30KB
Notes:
- implements TComponent+TFileStream+TList+TMemoryStream+TPersistent+TReader
+TResourceStream+TStream+TStringList
- compatible with the standard .DFM files
- only use existing properties in your DFM, otherwise you'll get error on startup
- TList and TStringList are simplier than standard ones
- TStrings is not implemented (but mapped to TStringList)
- TMemoryStream use faster Delphi heap manager, not the slow GlobalAlloc()
- TThread/TEvent simple implementation (on Windows only)
- Cross-Platform: it can be used on (Cross)Kylix under Linux (tested)
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)
Some modifications by Leonid Glazyrin, Feb 2012 <[email protected]>
* New types of DFM properties supported: List and Set
* Some (or maybe all) unsupported (sub)properties in DFM ignored without errors
}
{.$define debug} // send error messages from TReader in a Console window
{$ifdef debug} {$APPTYPE CONSOLE} {$endif}
{$IF CompilerVersion >= 24.00}
{$ZEROBASEDSTRINGS OFF}
{$IFEND}
{$R-,T-,X+,H+,B-}
{$WARNINGS OFF}
Interface
uses
SysUtils,
{$ifdef MSWINDOWS}
Windows;
{$else}
Types,
LibC;
{$endif}
type
EClassesError = class(Exception);
EResNotFound = class(Exception);
TNotifyEvent = procedure(Sender:TObject) of object;
TPointerList = array of Pointer;
TList = class
protected
fList: TPointerList;
private
fCount: integer;
fSize: integer;
fOwnObjects: boolean;
function GetItem(index: integer): pointer;
procedure SetItem(index: integer; value: pointer);
procedure Grow;
procedure FreeObjects;
procedure SetCount(number: integer);
procedure Error(index: integer);
public
destructor Destroy; override;
function Add(Item: pointer): integer;
procedure Insert(index: integer; item: pointer);
procedure Remove(item: pointer);
procedure Delete(index: integer);
function IndexOf(item: pointer): integer;
procedure Clear;
property Count: integer read fCount write SetCount;
property Items[index: integer]: pointer read GetItem write SetItem; default;
// can be used in order to speed up code a little bit (but no index check)
property List: TPointerList read fList;
end;
TObjectList = class(TList)
public
constructor Create(AOwnObjects: boolean=true);
end;
TStringList = class;
TStringListSortCompare = function(List: TStringList; Index1, Index2: Integer): Integer;
TStringList = class
private
fListStr: array of string;
// fListObj[] is allocated only if objects are used (not nil)
fListObj: array of TObject;
fCount: integer;
fSize : integer;
fCaseSensitive: boolean;
FDelimiter: Char;
FSorted: Boolean;
function GetItem(index: integer): string;
procedure SetItem(index: integer; const value: string);
function GetObject(index: integer): TObject;
procedure SetObject(index: integer; value: TObject);
function GetText: string;
procedure SetText(const Value: string);
function GetDelimitedText: string;
procedure SetDelimitedText(const Value: string);
procedure SetSorted(Value: Boolean);
protected
procedure QuickSort(L, R: Integer; SCompare: TStringListSortCompare);
function CompareStrings(const S1, S2: string): Integer; virtual;
public
constructor Create;
function Add(const s: string): integer;
function AddObject(const s: string; AObject: TObject): integer;
procedure AddStrings(Strings: TStringList);
procedure Delete(index: integer);
function IndexOf(const s: string): integer;
function IndexOfObject(item: pointer): integer;
function IndexOfName(const Name: string; const Separator: string='='): integer;
function ValueOf(const Name: string; const Separator: string='='): string;
function NameOf(const Value: string; const Separator: string='='): string;
procedure Clear;
function TextLen: integer;
procedure LoadFromFile(const FileName: string);
procedure SaveToFile(const FileName: string);
procedure CustomSort(Compare: TStringListSortCompare);
procedure Sort; virtual;
property Count: integer read fCount;
property Delimiter: Char read FDelimiter write FDelimiter;
property DelimitedText: string read GetDelimitedText write SetDelimitedText;
property Sorted: Boolean read FSorted write SetSorted;
property CaseSensitive: boolean read fCaseSensitive write fCaseSensitive;
property Strings[index: integer]: string read GetItem write SetItem; default;
property Objects[index: integer]: TObject read GetObject write SetObject;
property Text: string read GetText write SetText;
end;
TStrings = TStringList; // for easy debugging
TSeekOrigin = (soBeginning, soCurrent, soEnd);
const
fmCreate = $FFFF;
// used in TStream.Seek()
soFromBeginning = 0;
soFromCurrent = 1;
soFromEnd = 2;
type
TStream = class
protected
procedure SetPosition(value: integer); virtual;
function GetPosition: integer; virtual;
function GetSize: integer; virtual;
procedure SetSize(Value: integer); virtual;
public
function Read(var Buffer; Count: integer): integer; virtual; abstract;
procedure ReadBuffer(var Buffer; Count: integer);
function Write(const Buffer; Count: integer): integer; virtual; abstract;
procedure WriteBuffer(const Buffer; Count: integer);
function Seek(Offset: Longint; Origin: Word): Longint; overload; virtual;
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; overload; virtual;
procedure Clear;
procedure LoadFromStream(aStream: TStream); virtual;
procedure SaveToStream(aStream: TStream); virtual;
procedure LoadFromFile(const FileName: string);
procedure SaveToFile(const FileName: string);
function CopyFrom(Source: TStream; Count: integer): integer;
property Size: integer read GetSize write SetSize;
property Position: integer read GetPosition write SetPosition;
end;
THandleStream = class(TStream)
private
fHandle: THandle;
protected
procedure SetSize(Value: integer); override;
public
constructor Create(aHandle: THandle);
function Read(var Buffer; count: integer): integer; override;
function Write(const Buffer; Count: integer): integer; override;
function Seek(Offset: integer; Origin: Word): integer; override;
property Handle: THandle read fHandle;
end;
TFileStream = class(THandleStream)
private
fFileName: string;
protected
{$ifdef Linux} // this special function use stat() instead of seek()
function GetSize: cardinal; override; {$endif}
public
constructor Create(const FileName: string; Mode: Word);
destructor Destroy; override;
end;
TCustomMemoryStream = class(TStream)
protected
fPosition, fSize: integer;
fMemory: pointer;
procedure SetPosition(value: integer); override;
function GetPosition: integer; override;
function GetSize: integer; override;
procedure SetSize(Value: integer); override;
public
function Read(var Buffer; count: integer): integer; override;
procedure SetPointer(Buffer: pointer; Count: integer);
function Seek(Offset: integer; Origin: Word): integer; override;
procedure SaveToStream(aStream: TStream); override;
property Memory: pointer read fMemory;
end;
TResourceStream = class(TCustomMemoryStream)
private
HResInfo: THandle;
HGlobal: THandle;
procedure Initialize(Instance: THandle; Name, ResType: PChar; FromID: Boolean);
public
constructor Create(Instance: THandle; const ResName: string; ResType: PChar);
constructor CreateFromID(Instance: THandle; ResID: Integer; ResType: PChar);
destructor Destroy; override;
function Write(const Buffer; Count: Longint): Longint; override;
end;
TStreamOwnership = (soReference, soOwned);
TMemoryStream = class(TCustomMemoryStream)
protected
fCapacity: integer;
procedure SetSize(Value: integer); override;
procedure SetCapacity(Value: integer);
public
destructor Destroy; override;
function Write(const Buffer; Count: integer): integer; override;
procedure LoadFromStream(aStream: TStream); override;
end;
{$ifdef MSWINDOWS}
TFilerFlag = (ffInherited, ffChildPos, ffInline);
TFilerFlags = set of TFilerFlag;
PValueType = ^TValueType;
TValueType = (vaNull, vaList, vaInt8, vaInt16, vaInt32, vaExtended,
vaString, vaIdent, vaFalse, vaTrue, vaBinary, vaSet, vaLString,
vaNil, vaCollection, vaSingle, vaCurrency, vaDate, vaWString, vaInt64,
vaUTF8String);
TComponent = class;
TReader = class
private
fHandle: HGlobal;
fStart: integer;
fPointer: pByte;
fSize: integer;
fPosition: integer;
fNotifyLoaded: TList;
procedure SetPosition(Value: integer);
public
constructor Create(const ResourceName: string);
destructor Destroy; override;
procedure Loading(AComponent: TComponent);
function Read(var Data; DataSize: integer): integer;
function EndOfList: boolean;
function ReadValueType: TValueType;
function BooleanProperty: boolean;
function IntegerProperty: integer;
function StringProperty: string;
function ColorProperty: integer;
function BinaryProperty(var Size: integer):pointer;
procedure IdentProperty(var aValue; aTypeInfo: pointer);
procedure SetProperty(var ASet; aTypeInfo: pointer);
function ReadByte: byte;
function ReadWord: word;
function ReadInteger: integer;
function ReadString: string;
function ReadShortString: shortstring;
function ReadUTF8String: string;
function ReadWString: string;
procedure ReadPrefix(var Flags: TFilerFlags; var AChildPos: integer);
procedure ReadList;
procedure ReadSet;
procedure ReadStrings(Strings: TStrings);
procedure AnyProperty;
property Size: integer read fSize;
property Position: integer read fPosition write SetPosition;
end;
/// in LVCL, TPersistent don't have any RTTI information compiled within
// - RTTI is not needed with LVCL and will increase code size
// - if you need RTTI, you should use {$M+} explicitely
TPersistent = class
protected
function SubProperty(const Name: string): TPersistent; virtual;
procedure ReadProperty(const Name: string; Reader: TReader); virtual;
end;
TPersistentClass = class of TPersistent;
TOperation = (opInsert, opRemove);
TComponent = class(TPersistent)
private
fOwner: TComponent;
fComponents: TObjectList;
fTag: integer;
protected
fCompName: string;
/// Provides the interface for a method that changes the parent of the component
procedure SetParentComponent(Value: TComponent); virtual;
/// Returns the parent of the component
function GetParentComponent: TComponent; virtual;
public
/// Allocates memory and constructs a safely initialized instance of a component
constructor Create(AOwner: TComponent); virtual;
destructor Destroy; override;
procedure ReadProperties(Reader: TReader);
procedure Loaded; virtual;
/// Indicates the component that is responsible for streaming and freeing this component
property Owner: TComponent read fOwner;
/// Get of set the parent of the component
property ParentComponent: TComponent read GetParentComponent write SetParentComponent;
/// Indicates the number of components owned by the component
function ComponentCount: integer;
/// if not nil, lists all components owned by the component
property Components: TObjectList read fComponents;
/// the component name
property Name: string read fCompName;
/// some generic numeric container
property Tag: Integer read fTag write fTag;
end;
TComponentClass = class of TComponent;
TThread = class;
TThreadMethod = procedure of object;
TNotifyThreadEvent = procedure(Sender: TThread) of object;
/// minimal Threading implementation, using direct Windows API
TThread = class
private
FHandle: THandle;
FThreadID: cardinal;
FFinished,
FTerminated,
FSuspended,
FCreateSuspended,
FFreeOnTerminate: Boolean;
procedure SetSuspended(Value: Boolean);
protected
FOnTerminate: TNotifyThreadEvent;
procedure Execute; virtual; abstract;
public
constructor Create(CreateSuspended: Boolean);
procedure AfterConstruction; override;
destructor Destroy; override;
procedure Resume;
procedure Suspend;
function WaitFor: LongWord;
procedure Terminate;
property Handle: THandle read FHandle;
property ThreadID: cardinal read FThreadID;
property Suspended: Boolean read FSuspended write SetSuspended;
property Terminated: Boolean read FTerminated;
property FreeOnTerminate: Boolean read FFreeOnTerminate write FFreeOnTerminate;
property OnTerminate: TNotifyThreadEvent read FOnTerminate write FOnTerminate;
end;
TWaitResult = (wrSignaled, wrTimeout, wrAbandoned, wrError);
TEvent = class
protected
FHandle: THandle;
public
constructor Create(EventAttributes: PSecurityAttributes; ManualReset,
InitialState: Boolean; const Name: string);
function WaitFor(Timeout: LongWord): TWaitResult;
procedure SetEvent;
procedure ResetEvent;
destructor Destroy; override;
property Handle: THandle read FHandle;
end;
function GetClass(const AClassName: ShortString): TPersistentClass; overload;
function GetClass(const AClassName: string): TPersistentClass; overload;
function FindClass(const AClassName: string): TPersistentClass;
procedure RegisterClass(AClass: TPersistentClass);
procedure RegisterClasses(const AClasses: array of TPersistentClass);
{$endif}
// if the beginning of p^ is same as up^ (ignore case - up^ must be already Upper)
// (only exists in LVCL)
function IdemPChar(p, up: PChar): boolean;
implementation
procedure TList.FreeObjects;
var i: integer;
begin
if fOwnObjects then
for i := 0 to fCount-1 do
TObject(fList[i]).Free;
end;
destructor TList.Destroy;
begin
FreeObjects;
inherited; // will do Finalize(fList) in FinalizeRecord
end;
function TList.GetItem(index: integer): pointer;
// default VCL raise an exception in case of out of range index
// -> LVCL just return nil without any exception
{$ifdef PUREPASCAL}
begin
if (self=nil) or (cardinal(index)>=cardinal(fCount)) then
result := nil else
result := fList[Index];
end;
{$else}
asm
cmp edx,[eax].TList.fCount
mov eax,[eax].TList.fList
jae @e
mov eax,[eax+edx*4]
ret
@e: xor eax,eax
end;
{$endif}
procedure TList.SetItem(index: integer; value: pointer);
begin
if (self=nil) or (cardinal(index)>=cardinal(fCount)) then
exit;
if fOwnObjects then
TObject(fList[Index]).Free;
fList[Index] := Value;
end;
procedure TList.Grow;
begin
if fSize>64 then
inc(fSize,fSize shr 2) else
inc(fSize,16);
Setlength(fList,fSize); // will set all new entries to nil
end;
function TList.Add(Item: pointer): integer;
begin
if fCount=fSize then
Grow;
fList[fCount] := Item;
result := fCount;
inc(fCount);
end;
procedure TList.Insert(index: integer; item: pointer);
begin
if (self=nil) or (cardinal(index)>cardinal(fCount)) then
exit;
if fCount=fSize then
Grow;
if index < FCount then
Move(FList[index], FList[index+1], (FCount-index)*SizeOf(item));
fList[index] := Item;
inc(fCount);
end;
procedure TList.Remove(item: pointer);
begin
Delete(IndexOf(item));
end;
procedure TList.Delete(index: integer);
begin
if (self=nil) or (cardinal(index)>=cardinal(fCount)) then
exit;
if fOwnObjects then
TObject(fList[index]).Free;
Dec(fCount);
if index < FCount then
Move(fList[index + 1],fList[index],(fCount-index)*SizeOf(Pointer));
end;
procedure TList.SetCount(number: integer);
var i: integer;
begin
if number<0 then
number := 0;
if fOwnObjects then
for i := number to fCount-1 do
TObject(fList[i]).Free;
fSize := number;
fCount := number;
Grow;
end;
function TList.IndexOf(item: pointer): integer;
begin
if self<>nil then
for result := 0 to fCount-1 do
if fList[result]=item then exit;
result := -1;
end;
procedure TList.Clear;
begin
if fOwnObjects then
FreeObjects;
fCount := 0;
fSize := 0;
SetLength(fList,0);
end;
procedure TList.Error(index: integer);
function ReturnAddr: Pointer;
asm
MOV EAX,[EBP+4]
end;
begin
// since we use "jae ListErrorIndex" to jump here, we still have a valid
// return address for ReturnAddr (i.e. direct jump without stack)
raise EClassesError.CreateFmt('Index %d out of range',[index]) at ReturnAddr;
end;
{ TObjectList }
constructor TObjectList.Create(AOwnObjects: boolean=true);
begin
inherited Create;
fOwnObjects := AOwnObjects; // do all the magic :)
end;
{ TStringList }
constructor TStringList.Create;
begin
inherited Create;
FDelimiter := ',';
end;
function TStringList.GetItem(index: integer): string;
{$ifdef PUREPASCAL}
begin
if cardinal(index)>=cardinal(FCount) then
TList.Error(index);
Result := fListStr[index];
end;
{$else}
asm // eax=self, edx=index, ecx=result
cmp edx,[eax].TStringList.fCount
mov eax,[eax].TStringList.fListStr
jae TList.Error
mov edx,[eax+edx*4]
mov eax,ecx
jmp System.@LStrLAsg
end;
{$endif}
procedure TStringList.SetItem(index: integer; const value: string);
begin
if (self<>nil) and (cardinal(index)<cardinal(fCount)) then
fListStr[index] := value;
end;
function TStringList.GetObject(index: integer): TObject;
begin
if (self=nil) or (cardinal(index)>=cardinal(fCount)) or
(index>=length(fListObj)) then
result := nil else
result := fListObj[index];
end;
procedure TStringList.SetObject(index: integer; value: TObject);
begin
if (self<>nil) and (cardinal(index)<cardinal(fCount)) and (value<>nil) then begin
if high(fListObj)<>fSize then
SetLength(fListObj,fSize+1);
fListObj[index] := value;
end;
end;
function TStringList.Add(const s: string): integer;
begin
result := AddObject(s,nil);
end;
function TStringList.AddObject(const s: string; AObject: TObject): integer;
begin
if fCount=fSize then begin
if fSize>64 then
inc(fSize,fSize shr 2) else
inc(fSize,16);
Setlength(fListStr,fSize+1);
end;
fListStr[fCount] := s;
result := fCount;
inc(fCount);
if AObject<>nil then
Objects[result] := AObject;
end;
procedure TStringList.AddStrings(Strings: TStringList);
var i: integer;
begin
for i := 0 to Strings.Count-1 do
AddObject(Strings[i],Objects[i]);
end;
procedure TStringList.Delete(index: integer);
var L: integer;
begin
if (self=nil) or (cardinal(index)>=cardinal(fCount)) then
exit;
fListStr[index] := ''; // avoid GPF
Dec(FCount);
if index<FCount then begin
L := (FCount-index)*4;
Move(FListStr[index + 1], FListStr[index], L);
if FListObj<>nil then
Move(FListObj[index + 1], FListObj[index], L);
end;
pointer(fListStr[FCount]) := nil; // avoid GPF
end;
function TStringList.IndexOf(const s: string): integer;
begin
if fCaseSensitive then begin
for result := 0 to fCount-1 do
if fListStr[result]=s then
exit;
end else
for result := 0 to fCount-1 do
if SameText(fListStr[result],s) then
exit;
result := -1;
end;
function TStringList.IndexOfObject(item: pointer): integer;
begin
if fListObj<>nil then
for result := 0 to fCount-1 do
if fListObj[result]=item then
exit;
result := -1;
end;
function IdemPChar(p, up: PChar): boolean;
// if the beginning of p^ is same as up^ (ignore case - up^ must be already Upper)
var c: char;
begin
result := false;
if (p=nil) or (up=nil) then
exit;
while up^<>#0 do begin
c := p^;
if up^<>c then
if c in ['a'..'z'] then begin
dec(c,32);
if up^<>c then
exit;
end else exit;
inc(up);
inc(p);
end;
result := true;
end;
function TStringList.IndexOfName(const Name: string; const Separator: string='='): integer;
var L: integer;
Tmp: string;
begin
if self<>nil then begin
Tmp := UpperCase(Name)+Separator;
L := length(Tmp);
if L>1 then
for result := 0 to fCount-1 do
if IdemPChar(pointer(fListStr[result]),pointer(Tmp)) then
exit;
end;
result := -1;
end;
function TStringList.ValueOf(const Name: string; const Separator: string='='): string;
var i: integer;
begin
i := IndexOfName(Name,Separator);
if i>=0 then
result := copy(fListStr[i],length(Name)+length(Separator)+1,maxInt) else
result := '';
end;
function TStringList.NameOf(const Value: string; const Separator: string='='): string;
var i,j,L: integer;
P: PChar;
begin
L := length(Separator)-1;
for i := 0 to fCount-1 do begin
j := pos(Separator,fListStr[i]);
if j=0 then continue;
P := PChar(pointer(fListStr[i]))+j+L;
while P^=' ' do inc(P); // trim left value
if StrIComp(P,PChar(Value))=0 then begin
result := copy(fListStr[i],1,j-1);
exit;
end;
end;
result := '';
end;
procedure TStringList.Clear;
begin
if (self=nil) or (fCount<=0) then exit;
fCount := 0;
fSize := 0;
Finalize(fListStr);
Finalize(fListObj);
end;
procedure TStringList.LoadFromFile(const FileName: string);
var F: system.text;
s: string;
buf: array[0..4095] of byte;
begin
Clear;
{$I-}
Assign(F,FileName);
SetTextBuf(F,buf);
Reset(F);
if ioresult<>0 then exit;
while not eof(F) do begin
readln(F,s);
Add(s);
end;
ioresult;
Close(F);
ioresult;
{$I+}
end;
procedure TStringList.SaveToFile(const FileName: string);
var F: system.text;
i: integer;
buf: array[0..4095] of byte;
begin
{$I-}
Assign(F,FileName);
SetTextBuf(F,buf);
rewrite(F);
if ioresult<>0 then exit;
for i := 0 to FCount-1 do
writeln(F,FListStr[i]);
ioresult;
Close(F);
ioresult; // ignore any error
{$I+}
end;
procedure TStringList.QuickSort(L, R: Integer; SCompare: TStringListSortCompare);
var I, J, P, Tmp: Integer;
begin
repeat
I := L;
J := R;
P := (L+R) shr 1;
repeat
while SCompare(Self,I,P)<0 do Inc(I);
while SCompare(Self,J,P)>0 do Dec(J);
if I <= J then begin
Tmp := integer(fListObj[I]);
fListObj[I] := fListObj[J];
fListObj[J] := pointer(Tmp);
Tmp := integer(FListStr[I]);
integer(FListStr[I]) := integer(FListStr[J]);
integer(FListStr[J]) := Tmp;
if P=I then
P := J else
if P=J then
P := I;
Inc(I);
Dec(J);
end;
until I>J;
if L<J then QuickSort(L,J,SCompare);
L := I;
until I>=R;
end;
procedure TStringList.SetSorted(Value: Boolean);
begin
if FSorted <> Value then begin
if Value then
Sort;
FSorted := Value;
end;
end;
function StringListCompareStrings(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result := List.CompareStrings(List.fListStr[Index1],List.fListStr[Index2]);
end;
procedure TStringList.Sort;
begin
CustomSort(StringListCompareStrings);
end;
procedure TStringList.CustomSort(Compare: TStringListSortCompare);
begin
if (self=nil) or (FCount<=1) then
exit;
QuickSort(0,FCount-1,Compare);
end;
function TStringList.CompareStrings(const S1, S2: string): Integer;
begin
if CaseSensitive then
Result := AnsiCompareStr(S1, S2) else
Result := AnsiCompareText(S1, S2);
end;
function TStringList.TextLen: integer;
var i: integer;
begin
result := fCount*2; // #13#10 size
for i := 0 to fCount-1 do
if integer(fListStr[i])<>0 then
inc(result,pInteger(integer(fListStr[i])-4)^); // fast add length(List[i])
end;
function TStringList.GetText: string;
var i,V,L: integer;
P: PChar;
begin
// much faster than for i := 0 to Count-1 do result := result+List[i]+#13#10;
result := '';
if fCount=0 then exit;
SetLength(result,TextLen);
P := pointer(result);
for i := 0 to fCount-1 do begin
V := integer(fListStr[i]);
if V<>0 then begin
L := pInteger(V-4)^; // L := length(List[i])
move(pointer(V)^,P^,L);
inc(P,L);
end;
PWord(P)^ := 13+10 shl 8;
inc(P,2);
end;
end;
procedure TStringList.SetText(const Value: string);
function GetNextLine(d: pChar; out next: pChar): string;
begin
next := d;
while not (d^ in [#0,#10,#13]) do inc(d);
SetString(result,next,d-next);
if d^=#13 then inc(d);
if d^=#10 then inc(d);
if d^=#0 then
next := nil else
next := d;
end;
var P: PChar;
begin
Clear;
P := pointer(Value);
while P<>nil do
Add(GetNextLine(P,P));
end;
function TStrings.GetDelimitedText: string;
var
S: string;
P: PChar;
I: Integer;
LDelimiters: TSysCharSet;
QuoteChar: Char;
begin
QuoteChar := '"';
if (Count = 1) and (fListStr[0] = '') then
Result := QuoteChar + QuoteChar
else
begin
Result := '';
LDelimiters := [#0, QuoteChar, Delimiter];
//if not StrictDelimiter then
LDelimiters := LDelimiters + [#1..' '];
for I := 0 to Count - 1 do
begin
S := fListStr[I];
P := PChar(S);
while not (P^ in LDelimiters) do
{$IFDEF MSWINDOWS}
P := CharNext(P);
{$ELSE}
Inc(P);
{$ENDIF}
if (P^ <> #0) then S := AnsiQuotedStr(S, QuoteChar);
Result := Result + S + Delimiter;
end;
System.Delete(Result, Length(Result), 1);
end;
end;
procedure TStrings.SetDelimitedText(const Value: string);
var
P, P1: PChar;
S: string;
QuoteChar: Char;
begin
QuoteChar := '"';
//BeginUpdate;
//try
Clear;
P := PChar(Value);
//if not StrictDelimiter then
while P^ in [#1..' '] do
{$IFDEF MSWINDOWS}
P := CharNext(P);
{$ELSE}
Inc(P);
{$ENDIF}
while P^ <> #0 do
begin
if P^ = QuoteChar then
S := AnsiExtractQuotedStr(P, QuoteChar)
else
begin
P1 := P;
while (({not FStrictDelimiter and} (P^ > ' ')) {or
(FStrictDelimiter and (P^ <> #0))}) and (P^ <> Delimiter) do
{$IFDEF MSWINDOWS}
P := CharNext(P);
{$ELSE}
Inc(P);
{$ENDIF}
SetString(S, P1, P - P1);
end;
Add(S);
//if not FStrictDelimiter then
while P^ in [#1..' '] do
{$IFDEF MSWINDOWS}
P := CharNext(P);
{$ELSE}
Inc(P);
{$ENDIF}
if P^ = Delimiter then
begin
P1 := P;
{$IFDEF MSWINDOWS}
if CharNext(P1)^ = #0 then
{$ELSE}
Inc(P1);
if P1^ = #0 then
{$ENDIF}
Add('');
repeat
{$IFDEF MSWINDOWS}
P := CharNext(P);
{$ELSE}
Inc(P);
{$ENDIF}
until not ({not FStrictDelimiter and} (P^ in [#1..' ']));
end;
end;
//finally
// EndUpdate;