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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
|
{*****************************************************************************}
{ }
{ Tnt Delphi Unicode Controls }
{ http://www.tntware.com/delphicontrols/unicode/ }
{ Version: 2.3.0 }
{ }
{ Copyright (c) 2002-2007, Troy Wolbrink (troy.wolbrink@tntware.com) }
{ }
{*****************************************************************************}
unit TntClasses;
{$INCLUDE TntCompilers.inc}
interface
{ TODO: Consider: TTntRegIniFile, TTntMemIniFile (consider if UTF8 fits into this solution). }
{***********************************************}
{ WideChar-streaming implemented by Maël Hörz }
{***********************************************}
uses
Classes, SysUtils, Windows,
{$IFNDEF COMPILER_10_UP}
TntWideStrings,
{$ELSE}
WideStrings,
{$ENDIF}
ActiveX, Contnrs;
// ......... introduced .........
type
TTntStreamCharSet = (csAnsi, csUnicode, csUnicodeSwapped, csUtf8);
function AutoDetectCharacterSet(Stream: TStream): TTntStreamCharSet;
//---------------------------------------------------------------------------------------------
// Tnt - Classes
//---------------------------------------------------------------------------------------------
{TNT-WARN ExtractStrings}
{TNT-WARN LineStart}
{TNT-WARN TStringStream} // TODO: Implement a TWideStringStream
// A potential implementation of TWideStringStream can be found at:
// http://kdsxml.cvs.sourceforge.net/kdsxml/Global/KDSClasses.pas?revision=1.10&view=markup
procedure TntPersistent_AfterInherited_DefineProperties(Filer: TFiler; Instance: TPersistent);
type
{TNT-WARN TFileStream}
TTntFileStream = class(THandleStream)
public
constructor Create(const FileName: WideString; Mode: Word);
destructor Destroy; override;
end;
{TNT-WARN TMemoryStream}
TTntMemoryStream = class(TMemoryStream{TNT-ALLOW TMemoryStream})
public
procedure LoadFromFile(const FileName: WideString);
procedure SaveToFile(const FileName: WideString);
end;
{TNT-WARN TResourceStream}
TTntResourceStream = class(TCustomMemoryStream)
private
HResInfo: HRSRC;
HGlobal: THandle;
procedure Initialize(Instance: THandle; Name, ResType: PWideChar);
public
constructor Create(Instance: THandle; const ResName: WideString; ResType: PWideChar);
constructor CreateFromID(Instance: THandle; ResID: Word; ResType: PWideChar);
destructor Destroy; override;
function Write(const Buffer; Count: Longint): Longint; override;
procedure SaveToFile(const FileName: WideString);
end;
TTntStrings = class;
{TNT-WARN TAnsiStrings}
TAnsiStrings{TNT-ALLOW TAnsiStrings} = class(TStrings{TNT-ALLOW TStrings})
public
procedure LoadFromFile(const FileName: WideString); reintroduce;
procedure SaveToFile(const FileName: WideString); reintroduce;
procedure LoadFromFileEx(const FileName: WideString; CodePage: Cardinal);
procedure SaveToFileEx(const FileName: WideString; CodePage: Cardinal);
procedure LoadFromStreamEx(Stream: TStream; CodePage: Cardinal); virtual; abstract;
procedure SaveToStreamEx(Stream: TStream; CodePage: Cardinal); virtual; abstract;
end;
TAnsiStringsForWideStringsAdapter = class(TAnsiStrings{TNT-ALLOW TAnsiStrings})
private
FWideStrings: TTntStrings;
FAdapterCodePage: Cardinal;
protected
function Get(Index: Integer): AnsiString; override;
procedure Put(Index: Integer; const S: AnsiString); override;
function GetCount: Integer; override;
function GetObject(Index: Integer): TObject; override;
procedure PutObject(Index: Integer; AObject: TObject); override;
procedure SetUpdateState(Updating: Boolean); override;
function AdapterCodePage: Cardinal; dynamic;
public
constructor Create(AWideStrings: TTntStrings; _AdapterCodePage: Cardinal = 0);
procedure Clear; override;
procedure Delete(Index: Integer); override;
procedure Insert(Index: Integer; const S: AnsiString); override;
procedure LoadFromStreamEx(Stream: TStream; CodePage: Cardinal); override;
procedure SaveToStreamEx(Stream: TStream; CodePage: Cardinal); override;
end;
{TNT-WARN TStrings}
TTntStrings = class(TWideStrings)
private
FLastFileCharSet: TTntStreamCharSet;
FAnsiStrings: TAnsiStrings{TNT-ALLOW TAnsiStrings};
procedure SetAnsiStrings(const Value: TAnsiStrings{TNT-ALLOW TAnsiStrings});
procedure ReadData(Reader: TReader);
procedure ReadDataUTF7(Reader: TReader);
procedure ReadDataUTF8(Reader: TReader);
procedure WriteDataUTF7(Writer: TWriter);
protected
procedure DefineProperties(Filer: TFiler); override;
public
constructor Create;
destructor Destroy; override;
procedure LoadFromFile(const FileName: WideString); override;
procedure LoadFromStream(Stream: TStream); override;
procedure LoadFromStream_BOM(Stream: TStream; WithBOM: Boolean); virtual;
procedure SaveToFile(const FileName: WideString); override;
procedure SaveToStream(Stream: TStream); override;
procedure SaveToStream_BOM(Stream: TStream; WithBOM: Boolean); virtual;
property LastFileCharSet: TTntStreamCharSet read FLastFileCharSet;
published
property AnsiStrings: TAnsiStrings{TNT-ALLOW TAnsiStrings} read FAnsiStrings write SetAnsiStrings stored False;
end;
{ TTntStringList class }
TTntStringList = class;
TWideStringListSortCompare = function(List: TTntStringList; Index1, Index2: Integer): Integer;
{TNT-WARN TStringList}
TTntStringList = class(TTntStrings)
private
FUpdating: Boolean;
FList: PWideStringItemList;
FCount: Integer;
FCapacity: Integer;
FSorted: Boolean;
FDuplicates: TDuplicates;
FCaseSensitive: Boolean;
FOnChange: TNotifyEvent;
FOnChanging: TNotifyEvent;
procedure ExchangeItems(Index1, Index2: Integer);
procedure Grow;
procedure QuickSort(L, R: Integer; SCompare: TWideStringListSortCompare);
procedure SetSorted(Value: Boolean);
procedure SetCaseSensitive(const Value: Boolean);
protected
procedure Changed; virtual;
procedure Changing; virtual;
function Get(Index: Integer): WideString; override;
function GetCapacity: Integer; override;
function GetCount: Integer; override;
function GetObject(Index: Integer): TObject; override;
procedure Put(Index: Integer; const S: WideString); override;
procedure PutObject(Index: Integer; AObject: TObject); override;
procedure SetCapacity(NewCapacity: Integer); override;
procedure SetUpdateState(Updating: Boolean); override;
function CompareStrings(const S1, S2: WideString): Integer; override;
procedure InsertItem(Index: Integer; const S: WideString; AObject: TObject); virtual;
public
destructor Destroy; override;
function Add(const S: WideString): Integer; override;
function AddObject(const S: WideString; AObject: TObject): Integer; override;
procedure Clear; override;
procedure Delete(Index: Integer); override;
procedure Exchange(Index1, Index2: Integer); override;
function Find(const S: WideString; var Index: Integer): Boolean; virtual;
function IndexOf(const S: WideString): Integer; override;
function IndexOfName(const Name: WideString): Integer; override;
procedure Insert(Index: Integer; const S: WideString); override;
procedure InsertObject(Index: Integer; const S: WideString;
AObject: TObject); override;
procedure Sort; virtual;
procedure CustomSort(Compare: TWideStringListSortCompare); virtual;
property Duplicates: TDuplicates read FDuplicates write FDuplicates;
property Sorted: Boolean read FSorted write SetSorted;
property CaseSensitive: Boolean read FCaseSensitive write SetCaseSensitive;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
property OnChanging: TNotifyEvent read FOnChanging write FOnChanging;
end;
// ......... introduced .........
type
TListTargetCompare = function (Item, Target: Pointer): Integer;
function FindSortedListByTarget(List: TList; TargetCompare: TListTargetCompare;
Target: Pointer; var Index: Integer): Boolean;
function ClassIsRegistered(const clsid: TCLSID): Boolean;
var
RuntimeUTFStreaming: Boolean;
type
TBufferedAnsiString = class(TObject)
private
FStringBuffer: AnsiString;
LastWriteIndex: Integer;
public
procedure Clear;
procedure AddChar(const wc: AnsiChar);
procedure AddString(const s: AnsiString);
procedure AddBuffer(Buff: PAnsiChar; Chars: Integer);
function Value: AnsiString;
function BuffPtr: PAnsiChar;
end;
TBufferedWideString = class(TObject)
private
FStringBuffer: WideString;
LastWriteIndex: Integer;
public
procedure Clear;
procedure AddChar(const wc: WideChar);
procedure AddString(const s: WideString);
procedure AddBuffer(Buff: PWideChar; Chars: Integer);
function Value: WideString;
function BuffPtr: PWideChar;
end;
TBufferedStreamReader = class(TStream)
private
FStream: TStream;
FStreamSize: Integer;
FBuffer: array of Byte;
FBufferSize: Integer;
FBufferStartPosition: Integer;
FVirtualPosition: Integer;
procedure UpdateBufferFromPosition(StartPos: Integer);
public
constructor Create(Stream: TStream; BufferSize: Integer = 1024);
function Read(var Buffer; Count: Longint): Longint; override;
function Write(const Buffer; Count: Longint): Longint; override;
function Seek(Offset: Longint; Origin: Word): Longint; override;
end;
// "synced" wide string
type TSetAnsiStrEvent = procedure(const Value: AnsiString) of object;
function GetSyncedWideString(var WideStr: WideString; const AnsiStr: AnsiString): WideString;
procedure SetSyncedWideString(const Value: WideString; var WideStr: WideString;
const AnsiStr: AnsiString; SetAnsiStr: TSetAnsiStrEvent);
type
TWideComponentHelper = class(TComponent)
private
FComponent: TComponent;
protected
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
public
constructor Create(AOwner: TComponent); override;
constructor CreateHelper(AOwner: TComponent; ComponentHelperList: TComponentList);
end;
function FindWideComponentHelper(ComponentHelperList: TComponentList; Component: TComponent): TWideComponentHelper;
implementation
uses
RTLConsts, ComObj, Math,
Registry, TypInfo, TntSystem, TntSysUtils;
{ TntPersistent }
//===========================================================================
// The Delphi 5 Classes.pas never supported the streaming of WideStrings.
// The Delphi 6 Classes.pas supports WideString streaming. But it's too bad that
// the Delphi 6 IDE doesn't use the updated Classes.pas. Switching between Form/Text
// mode corrupts extended characters in WideStrings even under Delphi 6.
// Delphi 7 seems to finally get right. But let's keep the UTF7 support at design time
// to enable sharing source code with previous versions of Delphi.
//
// The purpose of this solution is to store WideString properties which contain
// non-ASCII chars in the form of UTF7 under the old property name + '_UTF7'.
//
// Special thanks go to Francisco Leong for helping to develop this solution.
//
{ TTntWideStringPropertyFiler }
type
TTntWideStringPropertyFiler = class
private
FInstance: TPersistent;
FPropInfo: PPropInfo;
procedure ReadDataUTF8(Reader: TReader);
procedure ReadDataUTF7(Reader: TReader);
procedure WriteDataUTF7(Writer: TWriter);
public
procedure DefineProperties(Filer: TFiler; Instance: TPersistent; PropName: AnsiString);
end;
function ReaderNeedsUtfHelp(Reader: TReader): Boolean;
begin
if Reader.Owner = nil then
Result := False { designtime - visual form inheritance ancestor }
else if csDesigning in Reader.Owner.ComponentState then
{$IFDEF COMPILER_7_UP}
Result := False { Delphi 7+: designtime - doesn't need UTF help. }
{$ELSE}
Result := True { Delphi 6: designtime - always needs UTF help. }
{$ENDIF}
else
Result := RuntimeUTFStreaming; { runtime }
end;
procedure TTntWideStringPropertyFiler.ReadDataUTF8(Reader: TReader);
begin
if ReaderNeedsUtfHelp(Reader) then
SetWideStrProp(FInstance, FPropInfo, UTF8ToWideString(Reader.ReadString))
else
Reader.ReadString; { do nothing with Result }
end;
procedure TTntWideStringPropertyFiler.ReadDataUTF7(Reader: TReader);
begin
if ReaderNeedsUtfHelp(Reader) then
SetWideStrProp(FInstance, FPropInfo, UTF7ToWideString(Reader.ReadString))
else
Reader.ReadString; { do nothing with Result }
end;
procedure TTntWideStringPropertyFiler.WriteDataUTF7(Writer: TWriter);
begin
Writer.WriteString(WideStringToUTF7(GetWideStrProp(FInstance, FPropInfo)));
end;
procedure TTntWideStringPropertyFiler.DefineProperties(Filer: TFiler; Instance: TPersistent;
PropName: AnsiString);
{$IFNDEF COMPILER_7_UP}
function HasData: Boolean;
var
CurrPropValue: WideString;
begin
// must be stored
Result := IsStoredProp(Instance, FPropInfo);
if Result
and (Filer.Ancestor <> nil)
and (GetPropInfo(Filer.Ancestor, PropName, [tkWString]) <> nil) then
begin
// must be different than ancestor
CurrPropValue := GetWideStrProp(Instance, FPropInfo);
Result := CurrPropValue <> GetWideStrProp(Filer.Ancestor, GetPropInfo(Filer.Ancestor, PropName));
end;
if Result then begin
// must be non-blank and different than UTF8 (implies all ASCII <= 127)
CurrPropValue := GetWideStrProp(Instance, FPropInfo);
Result := (CurrPropValue <> '') and (WideStringToUTF8(CurrPropValue) <> CurrPropValue);
end;
end;
{$ENDIF}
begin
FInstance := Instance;
FPropInfo := GetPropInfo(Instance, PropName, [tkWString]);
if FPropInfo <> nil then begin
// must be published (and of type WideString)
Filer.DefineProperty(PropName + 'W', ReadDataUTF8, nil, False);
{$IFDEF COMPILER_7_UP}
Filer.DefineProperty(PropName + '_UTF7', ReadDataUTF7, WriteDataUTF7, False);
{$ELSE}
Filer.DefineProperty(PropName + '_UTF7', ReadDataUTF7, WriteDataUTF7, HasData);
{$ENDIF}
end;
FInstance := nil;
FPropInfo := nil;
end;
{ TTntWideCharPropertyFiler }
type
TTntWideCharPropertyFiler = class
private
FInstance: TPersistent;
FPropInfo: PPropInfo;
{$IFNDEF COMPILER_9_UP}
FWriter: TWriter;
procedure GetLookupInfo(var Ancestor: TPersistent;
var Root, LookupRoot, RootAncestor: TComponent);
{$ENDIF}
procedure ReadData_W(Reader: TReader);
procedure ReadDataUTF7(Reader: TReader);
procedure WriteData_W(Writer: TWriter);
function ReadChar(Reader: TReader): WideChar;
public
procedure DefineProperties(Filer: TFiler; Instance: TPersistent; PropName: AnsiString);
end;
{$IFNDEF COMPILER_9_UP}
type
TGetLookupInfoEvent = procedure(var Ancestor: TPersistent;
var Root, LookupRoot, RootAncestor: TComponent) of object;
function AncestorIsValid(Ancestor: TPersistent; Root, RootAncestor: TComponent): Boolean;
begin
Result := (Ancestor <> nil) and (RootAncestor <> nil) and
Root.InheritsFrom(RootAncestor.ClassType);
end;
function IsDefaultOrdPropertyValue(Instance: TObject; PropInfo: PPropInfo;
OnGetLookupInfo: TGetLookupInfoEvent): Boolean;
var
Ancestor: TPersistent;
LookupRoot: TComponent;
RootAncestor: TComponent;
Root: TComponent;
AncestorValid: Boolean;
Value: Longint;
Default: LongInt;
begin
Ancestor := nil;
Root := nil;
LookupRoot := nil;
RootAncestor := nil;
if Assigned(OnGetLookupInfo) then
OnGetLookupInfo(Ancestor, Root, LookupRoot, RootAncestor);
AncestorValid := AncestorIsValid(Ancestor, Root, RootAncestor);
Result := True;
if (PropInfo^.GetProc <> nil) and (PropInfo^.SetProc <> nil) then
begin
Value := GetOrdProp(Instance, PropInfo);
if AncestorValid then
Result := Value = GetOrdProp(Ancestor, PropInfo)
else
begin
Default := PPropInfo(PropInfo)^.Default;
Result := (Default <> LongInt($80000000)) and (Value = Default);
end;
end;
end;
procedure TTntWideCharPropertyFiler.GetLookupInfo(var Ancestor: TPersistent;
var Root, LookupRoot, RootAncestor: TComponent);
begin
Ancestor := FWriter.Ancestor;
Root := FWriter.Root;
LookupRoot := FWriter.LookupRoot;
RootAncestor := FWriter.RootAncestor;
end;
{$ENDIF}
function TTntWideCharPropertyFiler.ReadChar(Reader: TReader): WideChar;
var
Temp: WideString;
begin
case Reader.NextValue of
vaWString:
Temp := Reader.ReadWideString;
vaString:
Temp := Reader.ReadString;
else
raise EReadError.Create(SInvalidPropertyValue);
end;
if Length(Temp) > 1 then
raise EReadError.Create(SInvalidPropertyValue);
Result := Temp[1];
end;
procedure TTntWideCharPropertyFiler.ReadData_W(Reader: TReader);
begin
SetOrdProp(FInstance, FPropInfo, Ord(ReadChar(Reader)));
end;
procedure TTntWideCharPropertyFiler.ReadDataUTF7(Reader: TReader);
var
S: WideString;
begin
S := UTF7ToWideString(Reader.ReadString);
if S = '' then
SetOrdProp(FInstance, FPropInfo, 0)
else
SetOrdProp(FInstance, FPropInfo, Ord(S[1]))
end;
type TAccessWriter = class(TWriter);
procedure TTntWideCharPropertyFiler.WriteData_W(Writer: TWriter);
var
L: Integer;
Temp: WideString;
begin
Temp := WideChar(GetOrdProp(FInstance, FPropInfo));
TAccessWriter(Writer).WriteValue(vaWString);
L := Length(Temp);
Writer.Write(L, SizeOf(Integer));
Writer.Write(Pointer(@Temp[1])^, L * 2);
end;
procedure TTntWideCharPropertyFiler.DefineProperties(Filer: TFiler;
Instance: TPersistent; PropName: AnsiString);
{$IFNDEF COMPILER_9_UP}
function HasData: Boolean;
var
CurrPropValue: Integer;
begin
// must be stored
Result := IsStoredProp(Instance, FPropInfo);
if Result and (Filer.Ancestor <> nil) and
(GetPropInfo(Filer.Ancestor, PropName, [tkWChar]) <> nil) then
begin
// must be different than ancestor
CurrPropValue := GetOrdProp(Instance, FPropInfo);
Result := CurrPropValue <> GetOrdProp(Filer.Ancestor, GetPropInfo(Filer.Ancestor, PropName));
end;
if Result and (Filer is TWriter) then
begin
FWriter := TWriter(Filer);
Result := not IsDefaultOrdPropertyValue(Instance, FPropInfo, GetLookupInfo);
end;
end;
{$ENDIF}
begin
FInstance := Instance;
FPropInfo := GetPropInfo(Instance, PropName, [tkWChar]);
if FPropInfo <> nil then
begin
// must be published (and of type WideChar)
{$IFDEF COMPILER_9_UP}
Filer.DefineProperty(PropName + 'W', ReadData_W, WriteData_W, False);
{$ELSE}
Filer.DefineProperty(PropName + 'W', ReadData_W, WriteData_W, HasData);
{$ENDIF}
Filer.DefineProperty(PropName + '_UTF7', ReadDataUTF7, nil, False);
end;
FInstance := nil;
FPropInfo := nil;
end;
procedure TntPersistent_AfterInherited_DefineProperties(Filer: TFiler; Instance: TPersistent);
var
I, Count: Integer;
PropInfo: PPropInfo;
PropList: PPropList;
WideStringFiler: TTntWideStringPropertyFiler;
WideCharFiler: TTntWideCharPropertyFiler;
begin
Count := GetTypeData(Instance.ClassInfo)^.PropCount;
if Count > 0 then
begin
WideStringFiler := TTntWideStringPropertyFiler.Create;
try
WideCharFiler := TTntWideCharPropertyFiler.Create;
try
GetMem(PropList, Count * SizeOf(Pointer));
try
GetPropInfos(Instance.ClassInfo, PropList);
for I := 0 to Count - 1 do
begin
PropInfo := PropList^[I];
if (PropInfo = nil) then
break;
if (PropInfo.PropType^.Kind = tkWString) then
WideStringFiler.DefineProperties(Filer, Instance, PropInfo.Name)
else if (PropInfo.PropType^.Kind = tkWChar) then
WideCharFiler.DefineProperties(Filer, Instance, PropInfo.Name)
end;
finally
FreeMem(PropList, Count * SizeOf(Pointer));
end;
finally
WideCharFiler.Free;
end;
finally
WideStringFiler.Free;
end;
end;
end;
{ TTntFileStream }
constructor TTntFileStream.Create(const FileName: WideString; Mode: Word);
var
CreateHandle: Integer;
{$IFDEF DELPHI_7_UP}
ErrorMessage: WideString;
{$ENDIF}
begin
if Mode = fmCreate then
begin
CreateHandle := WideFileCreate(FileName);
if CreateHandle < 0 then begin
{$IFDEF DELPHI_7_UP}
ErrorMessage := WideSysErrorMessage(GetLastError);
raise EFCreateError.CreateFmt(SFCreateErrorEx, [WideExpandFileName(FileName), ErrorMessage]);
{$ELSE}
raise EFCreateError.CreateFmt(SFCreateError, [WideExpandFileName(FileName)]);
{$ENDIF}
end;
end else
begin
CreateHandle := WideFileOpen(FileName, Mode);
if CreateHandle < 0 then begin
{$IFDEF DELPHI_7_UP}
ErrorMessage := WideSysErrorMessage(GetLastError);
raise EFOpenError.CreateFmt(SFOpenErrorEx, [WideExpandFileName(FileName), ErrorMessage]);
{$ELSE}
raise EFOpenError.CreateFmt(SFOpenError, [WideExpandFileName(FileName)]);
{$ENDIF}
end;
end;
inherited Create(CreateHandle);
end;
destructor TTntFileStream.Destroy;
begin
if Handle >= 0 then FileClose(Handle);
end;
{ TTntMemoryStream }
procedure TTntMemoryStream.LoadFromFile(const FileName: WideString);
var
Stream: TStream;
begin
Stream := TTntFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
procedure TTntMemoryStream.SaveToFile(const FileName: WideString);
var
Stream: TStream;
begin
Stream := TTntFileStream.Create(FileName, fmCreate);
try
SaveToStream(Stream);
finally
Stream.Free;
end;
end;
{ TTntResourceStream }
constructor TTntResourceStream.Create(Instance: THandle; const ResName: WideString;
ResType: PWideChar);
begin
inherited Create;
Initialize(Instance, PWideChar(ResName), ResType);
end;
constructor TTntResourceStream.CreateFromID(Instance: THandle; ResID: Word;
ResType: PWideChar);
begin
inherited Create;
Initialize(Instance, PWideChar(ResID), ResType);
end;
procedure TTntResourceStream.Initialize(Instance: THandle; Name, ResType: PWideChar);
procedure Error;
begin
raise EResNotFound.CreateFmt(SResNotFound, [Name]);
end;
begin
HResInfo := FindResourceW(Instance, Name, ResType);
if HResInfo = 0 then Error;
HGlobal := LoadResource(Instance, HResInfo);
if HGlobal = 0 then Error;
SetPointer(LockResource(HGlobal), SizeOfResource(Instance, HResInfo));
end;
destructor TTntResourceStream.Destroy;
begin
UnlockResource(HGlobal);
FreeResource(HGlobal); { Technically this is not necessary (MS KB #193678) }
inherited Destroy;
end;
function TTntResourceStream.Write(const Buffer; Count: Longint): Longint;
begin
raise EStreamError.CreateRes(PResStringRec(@SCantWriteResourceStreamError));
end;
procedure TTntResourceStream.SaveToFile(const FileName: WideString);
var
Stream: TStream;
begin
Stream := TTntFileStream.Create(FileName, fmCreate);
try
SaveToStream(Stream);
finally
Stream.Free;
end;
end;
{ TAnsiStrings }
procedure TAnsiStrings{TNT-ALLOW TAnsiStrings}.LoadFromFile(const FileName: WideString);
var
Stream: TStream;
begin
Stream := TTntFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
procedure TAnsiStrings{TNT-ALLOW TAnsiStrings}.SaveToFile(const FileName: WideString);
var
Stream: TStream;
begin
Stream := TTntFileStream.Create(FileName, fmCreate);
try
SaveToStream(Stream);
finally
Stream.Free;
end;
end;
procedure TAnsiStrings{TNT-ALLOW TAnsiStrings}.LoadFromFileEx(const FileName: WideString; CodePage: Cardinal);
var
Stream: TStream;
begin
Stream := TTntFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
LoadFromStreamEx(Stream, CodePage);
finally
Stream.Free;
end;
end;
procedure TAnsiStrings{TNT-ALLOW TAnsiStrings}.SaveToFileEx(const FileName: WideString; CodePage: Cardinal);
var
Stream: TStream;
begin
Stream := TTntFileStream.Create(FileName, fmCreate);
try
if (CodePage = CP_UTF8) then
Stream.WriteBuffer(PAnsiChar(UTF8_BOM)^, Length(UTF8_BOM));
SaveToStreamEx(Stream, CodePage);
finally
Stream.Free;
end;
end;
{ TAnsiStringsForWideStringsAdapter }
constructor TAnsiStringsForWideStringsAdapter.Create(AWideStrings: TTntStrings; _AdapterCodePage: Cardinal);
begin
inherited Create;
FWideStrings := AWideStrings;
FAdapterCodePage := _AdapterCodePage;
end;
function TAnsiStringsForWideStringsAdapter.AdapterCodePage: Cardinal;
begin
if FAdapterCodePage = 0 then
Result := TntSystem.DefaultSystemCodePage
else
Result := FAdapterCodePage;
end;
procedure TAnsiStringsForWideStringsAdapter.Clear;
begin
FWideStrings.Clear;
end;
procedure TAnsiStringsForWideStringsAdapter.Delete(Index: Integer);
begin
FWideStrings.Delete(Index);
end;
function TAnsiStringsForWideStringsAdapter.Get(Index: Integer): AnsiString;
begin
Result := WideStringToStringEx(FWideStrings.Get(Index), AdapterCodePage);
end;
procedure TAnsiStringsForWideStringsAdapter.Put(Index: Integer; const S: AnsiString);
begin
FWideStrings.Put(Index, StringToWideStringEx(S, AdapterCodePage));
end;
function TAnsiStringsForWideStringsAdapter.GetCount: Integer;
begin
Result := FWideStrings.GetCount;
end;
procedure TAnsiStringsForWideStringsAdapter.Insert(Index: Integer; const S: AnsiString);
begin
FWideStrings.Insert(Index, StringToWideStringEx(S, AdapterCodePage));
end;
function TAnsiStringsForWideStringsAdapter.GetObject(Index: Integer): TObject;
begin
Result := FWideStrings.GetObject(Index);
end;
procedure TAnsiStringsForWideStringsAdapter.PutObject(Index: Integer; AObject: TObject);
begin
FWideStrings.PutObject(Index, AObject);
end;
procedure TAnsiStringsForWideStringsAdapter.SetUpdateState(Updating: Boolean);
begin
FWideStrings.SetUpdateState(Updating);
end;
procedure TAnsiStringsForWideStringsAdapter.LoadFromStreamEx(Stream: TStream; CodePage: Cardinal);
var
Size: Integer;
S: AnsiString;
begin
BeginUpdate;
try
Size := Stream.Size - Stream.Position;
SetString(S, nil, Size);
Stream.Read(Pointer(S)^, Size);
FWideStrings.SetTextStr(StringToWideStringEx(S, CodePage));
finally
EndUpdate;
end;
end;
procedure TAnsiStringsForWideStringsAdapter.SaveToStreamEx(Stream: TStream; CodePage: Cardinal);
var
S: AnsiString;
begin
S := WideStringToStringEx(FWideStrings.GetTextStr, CodePage);
Stream.WriteBuffer(Pointer(S)^, Length(S));
end;
{ TTntStrings }
constructor TTntStrings.Create;
begin
inherited;
FAnsiStrings := TAnsiStringsForWideStringsAdapter.Create(Self);
FLastFileCharSet := csUnicode;
end;
destructor TTntStrings.Destroy;
begin
FreeAndNil(FAnsiStrings);
inherited;
end;
procedure TTntStrings.SetAnsiStrings(const Value: TAnsiStrings{TNT-ALLOW TAnsiStrings});
begin
FAnsiStrings.Assign(Value);
end;
procedure TTntStrings.DefineProperties(Filer: TFiler);
{$IFNDEF COMPILER_7_UP}
function DoWrite: Boolean;
begin
if Filer.Ancestor <> nil then
begin
Result := True;
if Filer.Ancestor is TWideStrings then
Result := not Equals(TWideStrings(Filer.Ancestor))
end
else Result := Count > 0;
end;
function DoWriteAsUTF7: Boolean;
var
i: integer;
begin
Result := False;
for i := 0 to Count - 1 do begin
if (Strings[i] <> '') and (WideStringToUTF8(Strings[i]) <> Strings[i]) then begin
Result := True;
break; { found a string with non-ASCII chars (> 127) }
end;
end;
end;
{$ENDIF}
begin
inherited DefineProperties(Filer); { Handles main 'Strings' property.' }
Filer.DefineProperty('WideStrings', ReadData, nil, False);
Filer.DefineProperty('WideStringsW', ReadDataUTF8, nil, False);
{$IFDEF COMPILER_7_UP}
Filer.DefineProperty('WideStrings_UTF7', ReadDataUTF7, WriteDataUTF7, False);
{$ELSE}
Filer.DefineProperty('WideStrings_UTF7', ReadDataUTF7, WriteDataUTF7, DoWrite and DoWriteAsUTF7);
{$ENDIF}
end;
procedure TTntStrings.LoadFromFile(const FileName: WideString);
var
Stream: TStream;
begin
Stream := TTntFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
FLastFileCharSet := AutoDetectCharacterSet(Stream);
Stream.Position := 0;
LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
procedure TTntStrings.LoadFromStream(Stream: TStream);
begin
LoadFromStream_BOM(Stream, True);
end;
procedure TTntStrings.LoadFromStream_BOM(Stream: TStream; WithBOM: Boolean);
var
DataLeft: Integer;
StreamCharSet: TTntStreamCharSet;
SW: WideString;
SA: AnsiString;
begin
BeginUpdate;
try
if WithBOM then
StreamCharSet := AutoDetectCharacterSet(Stream)
else
StreamCharSet := csUnicode;
DataLeft := Stream.Size - Stream.Position;
if (StreamCharSet in [csUnicode, csUnicodeSwapped]) then
begin
// BOM indicates Unicode text stream
if DataLeft < SizeOf(WideChar) then
SW := ''
else begin
SetLength(SW, DataLeft div SizeOf(WideChar));
Stream.Read(PWideChar(SW)^, DataLeft);
if StreamCharSet = csUnicodeSwapped then
StrSwapByteOrder(PWideChar(SW));
end;
SetTextStr(SW);
end
else if StreamCharSet = csUtf8 then
begin
// BOM indicates UTF-8 text stream
SetLength(SA, DataLeft div SizeOf(AnsiChar));
Stream.Read(PAnsiChar(SA)^, DataLeft);
SetTextStr(UTF8ToWideString(SA));
end
else
begin
// without byte order mark it is assumed that we are loading ANSI text
SetLength(SA, DataLeft div SizeOf(AnsiChar));
Stream.Read(PAnsiChar(SA)^, DataLeft);
SetTextStr(SA);
end;
finally
EndUpdate;
end;
end;
procedure TTntStrings.ReadData(Reader: TReader);
begin
if Reader.NextValue in [vaString, vaLString] then
SetTextStr(Reader.ReadString) {JCL compatiblity}
else if Reader.NextValue = vaWString then
SetTextStr(Reader.ReadWideString) {JCL compatiblity}
else begin
BeginUpdate;
try
Clear;
Reader.ReadListBegin;
while not Reader.EndOfList do
if Reader.NextValue in [vaString, vaLString] then
Add(Reader.ReadString) {TStrings compatiblity}
else
Add(Reader.ReadWideString);
Reader.ReadListEnd;
finally
EndUpdate;
end;
end;
end;
procedure TTntStrings.ReadDataUTF7(Reader: TReader);
begin
Reader.ReadListBegin;
if ReaderNeedsUtfHelp(Reader) then
begin
BeginUpdate;
try
Clear;
while not Reader.EndOfList do
Add(UTF7ToWideString(Reader.ReadString))
finally
EndUpdate;
end;
end else begin
while not Reader.EndOfList do
Reader.ReadString; { do nothing with Result }
end;
Reader.ReadListEnd;
end;
procedure TTntStrings.ReadDataUTF8(Reader: TReader);
begin
Reader.ReadListBegin;
if ReaderNeedsUtfHelp(Reader)
or (Count = 0){ Legacy support where 'WideStrings' was never written in lieu of WideStringsW }
then begin
BeginUpdate;
try
Clear;
while not Reader.EndOfList do
Add(UTF8ToWideString(Reader.ReadString))
finally
EndUpdate;
end;
end else begin
while not Reader.EndOfList do
Reader.ReadString; { do nothing with Result }
end;
Reader.ReadListEnd;
end;
procedure TTntStrings.SaveToFile(const FileName: WideString);
var
Stream: TStream;
begin
Stream := TTntFileStream.Create(FileName, fmCreate);
try
SaveToStream(Stream);
finally
Stream.Free;
end;
end;
procedure TTntStrings.SaveToStream(Stream: TStream);
begin
SaveToStream_BOM(Stream, True);
end;
procedure TTntStrings.SaveToStream_BOM(Stream: TStream; WithBOM: Boolean);
// Saves the currently loaded text into the given stream.
// WithBOM determines whether to write a byte order mark or not.
var
SW: WideString;
BOM: WideChar;
begin
if WithBOM then begin
BOM := UNICODE_BOM;
Stream.WriteBuffer(BOM, SizeOf(WideChar));
end;
SW := GetTextStr;
Stream.WriteBuffer(PWideChar(SW)^, Length(SW) * SizeOf(WideChar));
end;
procedure TTntStrings.WriteDataUTF7(Writer: TWriter);
var
I: Integer;
begin
Writer.WriteListBegin;
for I := 0 to Count-1 do
Writer.WriteString(WideStringToUTF7(Get(I)));
Writer.WriteListEnd;
end;
{ TTntStringList }
destructor TTntStringList.Destroy;
begin
FOnChange := nil;
FOnChanging := nil;
inherited Destroy;
if FCount <> 0 then Finalize(FList^[0], FCount);
FCount := 0;
SetCapacity(0);
end;
function TTntStringList.Add(const S: WideString): Integer;
begin
Result := AddObject(S, nil);
end;
function TTntStringList.AddObject(const S: WideString; AObject: TObject): Integer;
begin
if not Sorted then
Result := FCount
else
if Find(S, Result) then
case Duplicates of
dupIgnore: Exit;
dupError: Error(PResStringRec(@SDuplicateString), 0);
end;
InsertItem(Result, S, AObject);
end;
procedure TTntStringList.Changed;
begin
if (not FUpdating) and Assigned(FOnChange) then
FOnChange(Self);
end;
procedure TTntStringList.Changing;
begin
if (not FUpdating) and Assigned(FOnChanging) then
FOnChanging(Self);
end;
procedure TTntStringList.Clear;
begin
if FCount <> 0 then
begin
Changing;
Finalize(FList^[0], FCount);
FCount := 0;
SetCapacity(0);
Changed;
end;
end;
procedure TTntStringList.Delete(Index: Integer);
begin
if (Index < 0) or (Index >= FCount) then Error(PResStringRec(@SListIndexError), Index);
Changing;
Finalize(FList^[Index]);
Dec(FCount);
if Index < FCount then
System.Move(FList^[Index + 1], FList^[Index],
(FCount - Index) * SizeOf(TWideStringItem));
Changed;
end;
procedure TTntStringList.Exchange(Index1, Index2: Integer);
begin
if (Index1 < 0) or (Index1 >= FCount) then Error(PResStringRec(@SListIndexError), Index1);
if (Index2 < 0) or (Index2 >= FCount) then Error(PResStringRec(@SListIndexError), Index2);
Changing;
ExchangeItems(Index1, Index2);
Changed;
end;
procedure TTntStringList.ExchangeItems(Index1, Index2: Integer);
var
Temp: Integer;
Item1, Item2: PWideStringItem;
begin
Item1 := @FList^[Index1];
Item2 := @FList^[Index2];
Temp := Integer(Item1^.FString);
Integer(Item1^.FString) := Integer(Item2^.FString);
Integer(Item2^.FString) := Temp;
Temp := Integer(Item1^.FObject);
Integer(Item1^.FObject) := Integer(Item2^.FObject);
Integer(Item2^.FObject) := Temp;
end;
function TTntStringList.Find(const S: WideString; var Index: Integer): Boolean;
var
L, H, I, C: Integer;
begin
Result := False;
L := 0;
H := FCount - 1;
while L <= H do
begin
I := (L + H) shr 1;
C := CompareStrings(FList^[I].FString, S);
if C < 0 then L := I + 1 else
begin
H := I - 1;
if C = 0 then
begin
Result := True;
if Duplicates <> dupAccept then L := I;
end;
end;
end;
Index := L;
end;
function TTntStringList.Get(Index: Integer): WideString;
begin
if (Index < 0) or (Index >= FCount) then Error(PResStringRec(@SListIndexError), Index);
Result := FList^[Index].FString;
end;
function TTntStringList.GetCapacity: Integer;
begin
Result := FCapacity;
end;
function TTntStringList.GetCount: Integer;
begin
Result := FCount;
end;
function TTntStringList.GetObject(Index: Integer): TObject;
begin
if (Index < 0) or (Index >= FCount) then Error(PResStringRec(@SListIndexError), Index);
Result := FList^[Index].FObject;
end;
procedure TTntStringList.Grow;
var
Delta: Integer;
begin
if FCapacity > 64 then Delta := FCapacity div 4 else
if FCapacity > 8 then Delta := 16 else
Delta := 4;
SetCapacity(FCapacity + Delta);
end;
function TTntStringList.IndexOf(const S: WideString): Integer;
begin
if not Sorted then Result := inherited IndexOf(S) else
if not Find(S, Result) then Result := -1;
end;
function TTntStringList.IndexOfName(const Name: WideString): Integer;
var
NameKey: WideString;
begin
if not Sorted then
Result := inherited IndexOfName(Name)
else begin
// use sort to find index more quickly
NameKey := Name + NameValueSeparator;
Find(NameKey, Result);
if (Result < 0) or (Result > Count - 1) then
Result := -1
else if CompareStrings(NameKey, Copy(Strings[Result], 1, Length(NameKey))) <> 0 then
Result := -1
end;
end;
procedure TTntStringList.Insert(Index: Integer; const S: WideString);
begin
InsertObject(Index, S, nil);
end;
procedure TTntStringList.InsertObject(Index: Integer; const S: WideString;
AObject: TObject);
begin
if Sorted then Error(PResStringRec(@SSortedListError), 0);
if (Index < 0) or (Index > FCount) then Error(PResStringRec(@SListIndexError), Index);
InsertItem(Index, S, AObject);
end;
procedure TTntStringList.InsertItem(Index: Integer; const S: WideString; AObject: TObject);
begin
Changing;
if FCount = FCapacity then Grow;
if Index < FCount then
System.Move(FList^[Index], FList^[Index + 1],
(FCount - Index) * SizeOf(TWideStringItem));
with FList^[Index] do
begin
Pointer(FString) := nil;
FObject := AObject;
FString := S;
end;
Inc(FCount);
Changed;
end;
procedure TTntStringList.Put(Index: Integer; const S: WideString);
begin
if Sorted then Error(PResStringRec(@SSortedListError), 0);
if (Index < 0) or (Index >= FCount) then Error(PResStringRec(@SListIndexError), Index);
Changing;
FList^[Index].FString := S;
Changed;
end;
procedure TTntStringList.PutObject(Index: Integer; AObject: TObject);
begin
if (Index < 0) or (Index >= FCount) then Error(PResStringRec(@SListIndexError), Index);
Changing;
FList^[Index].FObject := AObject;
Changed;
end;
procedure TTntStringList.QuickSort(L, R: Integer; SCompare: TWideStringListSortCompare);
var
I, J, P: 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
ExchangeItems(I, J);
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 TTntStringList.SetCapacity(NewCapacity: Integer);
begin
ReallocMem(FList, NewCapacity * SizeOf(TWideStringItem));
FCapacity := NewCapacity;
end;
procedure TTntStringList.SetSorted(Value: Boolean);
begin
if FSorted <> Value then
begin
if Value then Sort;
FSorted := Value;
end;
end;
procedure TTntStringList.SetUpdateState(Updating: Boolean);
begin
FUpdating := Updating;
if Updating then Changing else Changed;
end;
function WideStringListCompareStrings(List: TTntStringList; Index1, Index2: Integer): Integer;
begin
Result := List.CompareStrings(List.FList^[Index1].FString,
List.FList^[Index2].FString);
end;
procedure TTntStringList.Sort;
begin
CustomSort(WideStringListCompareStrings);
end;
procedure TTntStringList.CustomSort(Compare: TWideStringListSortCompare);
begin
if not Sorted and (FCount > 1) then
begin
Changing;
QuickSort(0, FCount - 1, Compare);
Changed;
end;
end;
function TTntStringList.CompareStrings(const S1, S2: WideString): Integer;
begin
if CaseSensitive then
Result := WideCompareStr(S1, S2)
else
Result := WideCompareText(S1, S2);
end;
procedure TTntStringList.SetCaseSensitive(const Value: Boolean);
begin
if Value <> FCaseSensitive then
begin
FCaseSensitive := Value;
if Sorted then Sort;
end;
end;
//------------------------- TntClasses introduced procs ----------------------------------
function AutoDetectCharacterSet(Stream: TStream): TTntStreamCharSet;
var
ByteOrderMark: WideChar;
BytesRead: Integer;
Utf8Test: array[0..2] of AnsiChar;
begin
// Byte Order Mark
ByteOrderMark := #0;
if (Stream.Size - Stream.Position) >= SizeOf(ByteOrderMark) then begin
BytesRead := Stream.Read(ByteOrderMark, SizeOf(ByteOrderMark));
if (ByteOrderMark <> UNICODE_BOM) and (ByteOrderMark <> UNICODE_BOM_SWAPPED) then begin
ByteOrderMark := #0;
Stream.Seek(-BytesRead, soFromCurrent);
if (Stream.Size - Stream.Position) >= Length(Utf8Test) * SizeOf(AnsiChar) then begin
BytesRead := Stream.Read(Utf8Test[0], Length(Utf8Test) * SizeOf(AnsiChar));
if Utf8Test <> UTF8_BOM then
Stream.Seek(-BytesRead, soFromCurrent);
end;
end;
end;
// Test Byte Order Mark
if ByteOrderMark = UNICODE_BOM then
Result := csUnicode
else if ByteOrderMark = UNICODE_BOM_SWAPPED then
Result := csUnicodeSwapped
else if Utf8Test = UTF8_BOM then
Result := csUtf8
else
Result := csAnsi;
end;
function FindSortedListByTarget(List: TList; TargetCompare: TListTargetCompare;
Target: Pointer; var Index: Integer): Boolean;
var
L, H, I, C: Integer;
begin
Result := False;
L := 0;
H := List.Count - 1;
while L <= H do
begin
I := (L + H) shr 1;
C := TargetCompare(List[i], Target);
if C < 0 then L := I + 1 else
begin
H := I - 1;
if C = 0 then
begin
Result := True;
L := I;
end;
end;
end;
Index := L;
end;
function ClassIsRegistered(const clsid: TCLSID): Boolean;
var
OleStr: POleStr;
Reg: TRegIniFile;
Key, Filename: WideString;
begin
// First, check to see if there is a ProgID. This will tell if the
// control is registered on the machine. No ProgID, control won't run
Result := ProgIDFromCLSID(clsid, OleStr) = S_OK;
if not Result then Exit; //Bail as soon as anything goes wrong.
// Next, make sure that the file is actually there by rooting it out
// of the registry
Key := WideFormat('\SOFTWARE\Classes\CLSID\%s', [GUIDToString(clsid)]);
Reg := TRegIniFile.Create;
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
Result := Reg.OpenKeyReadOnly(Key);
if not Result then Exit; // Bail as soon as anything goes wrong.
FileName := Reg.ReadString('InProcServer32', '', EmptyStr);
if (Filename = EmptyStr) then // try another key for the file name
begin
FileName := Reg.ReadString('InProcServer', '', EmptyStr);
end;
Result := Filename <> EmptyStr;
if not Result then Exit;
Result := WideFileExists(Filename);
finally
Reg.Free;
end;
end;
{ TBufferedAnsiString }
procedure TBufferedAnsiString.Clear;
begin
LastWriteIndex := 0;
if Length(FStringBuffer) > 0 then
FillChar(FStringBuffer[1], Length(FStringBuffer) * SizeOf(AnsiChar), 0);
end;
procedure TBufferedAnsiString.AddChar(const wc: AnsiChar);
const
MIN_GROW_SIZE = 32;
MAX_GROW_SIZE = 256;
var
GrowSize: Integer;
begin
Inc(LastWriteIndex);
if LastWriteIndex > Length(FStringBuffer) then begin
GrowSize := Max(MIN_GROW_SIZE, Length(FStringBuffer));
GrowSize := Min(GrowSize, MAX_GROW_SIZE);
SetLength(FStringBuffer, Length(FStringBuffer) + GrowSize);
FillChar(FStringBuffer[LastWriteIndex], GrowSize * SizeOf(AnsiChar), 0);
end;
FStringBuffer[LastWriteIndex] := wc;
end;
procedure TBufferedAnsiString.AddString(const s: AnsiString);
var
LenS: Integer;
BlockSize: Integer;
AllocSize: Integer;
begin
LenS := Length(s);
if LenS > 0 then begin
Inc(LastWriteIndex);
if LastWriteIndex + LenS - 1 > Length(FStringBuffer) then begin
// determine optimum new allocation size
BlockSize := Length(FStringBuffer) div 2;
if BlockSize < 8 then
BlockSize := 8;
AllocSize := ((LenS div BlockSize) + 1) * BlockSize;
// realloc buffer
SetLength(FStringBuffer, Length(FStringBuffer) + AllocSize);
FillChar(FStringBuffer[Length(FStringBuffer) - AllocSize + 1], AllocSize * SizeOf(AnsiChar), 0);
end;
CopyMemory(@FStringBuffer[LastWriteIndex], @s[1], LenS * SizeOf(AnsiChar));
Inc(LastWriteIndex, LenS - 1);
end;
end;
procedure TBufferedAnsiString.AddBuffer(Buff: PAnsiChar; Chars: Integer);
var
i: integer;
begin
for i := 1 to Chars do begin
if Buff^ = #0 then
break;
AddChar(Buff^);
Inc(Buff);
end;
end;
function TBufferedAnsiString.Value: AnsiString;
begin
Result := PAnsiChar(FStringBuffer);
end;
function TBufferedAnsiString.BuffPtr: PAnsiChar;
begin
Result := PAnsiChar(FStringBuffer);
end;
{ TBufferedWideString }
procedure TBufferedWideString.Clear;
begin
LastWriteIndex := 0;
if Length(FStringBuffer) > 0 then
FillChar(FStringBuffer[1], Length(FStringBuffer) * SizeOf(WideChar), 0);
end;
procedure TBufferedWideString.AddChar(const wc: WideChar);
const
MIN_GROW_SIZE = 32;
MAX_GROW_SIZE = 256;
var
GrowSize: Integer;
begin
Inc(LastWriteIndex);
if LastWriteIndex > Length(FStringBuffer) then begin
GrowSize := Max(MIN_GROW_SIZE, Length(FStringBuffer));
GrowSize := Min(GrowSize, MAX_GROW_SIZE);
SetLength(FStringBuffer, Length(FStringBuffer) + GrowSize);
FillChar(FStringBuffer[LastWriteIndex], GrowSize * SizeOf(WideChar), 0);
end;
FStringBuffer[LastWriteIndex] := wc;
end;
procedure TBufferedWideString.AddString(const s: WideString);
var
i: integer;
begin
for i := 1 to Length(s) do
AddChar(s[i]);
end;
procedure TBufferedWideString.AddBuffer(Buff: PWideChar; Chars: Integer);
var
i: integer;
begin
for i := 1 to Chars do begin
if Buff^ = #0 then
break;
AddChar(Buff^);
Inc(Buff);
end;
end;
function TBufferedWideString.Value: WideString;
begin
Result := PWideChar(FStringBuffer);
end;
function TBufferedWideString.BuffPtr: PWideChar;
begin
Result := PWideChar(FStringBuffer);
end;
{ TBufferedStreamReader }
constructor TBufferedStreamReader.Create(Stream: TStream; BufferSize: Integer = 1024);
begin
// init stream
FStream := Stream;
FStreamSize := Stream.Size;
// init buffer
FBufferSize := BufferSize;
SetLength(FBuffer, BufferSize);
FBufferStartPosition := -FBufferSize; { out of any useful range }
// init virtual position
FVirtualPosition := 0;
end;
function TBufferedStreamReader.Seek(Offset: Integer; Origin: Word): Longint;
begin
case Origin of
soFromBeginning: FVirtualPosition := Offset;
soFromCurrent: Inc(FVirtualPosition, Offset);
soFromEnd: FVirtualPosition := FStreamSize + Offset;
end;
Result := FVirtualPosition;
end;
procedure TBufferedStreamReader.UpdateBufferFromPosition(StartPos: Integer);
begin
try
FStream.Position := StartPos;
FStream.Read(FBuffer[0], FBufferSize);
FBufferStartPosition := StartPos;
except
FBufferStartPosition := -FBufferSize; { out of any useful range }
raise;
end;
end;
function TBufferedStreamReader.Read(var Buffer; Count: Integer): Longint;
var
BytesLeft: Integer;
FirstBufferRead: Integer;
StreamDirectRead: Integer;
Buf: PAnsiChar;
begin
if (FVirtualPosition >= 0) and (Count >= 0) then
begin
Result := FStreamSize - FVirtualPosition;
if Result > 0 then
begin
if Result > Count then
Result := Count;
Buf := @Buffer;
BytesLeft := Result;
// try to read what is left in buffer
FirstBufferRead := FBufferStartPosition + FBufferSize - FVirtualPosition;
if (FirstBufferRead < 0) or (FirstBufferRead > FBufferSize) then
FirstBufferRead := 0;
FirstBufferRead := Min(FirstBufferRead, Result);
if FirstBufferRead > 0 then begin
Move(FBuffer[FVirtualPosition - FBufferStartPosition], Buf[0], FirstBufferRead);
Dec(BytesLeft, FirstBufferRead);
end;
if BytesLeft > 0 then begin
// The first read in buffer was not enough
StreamDirectRead := (BytesLeft div FBufferSize) * FBufferSize;
FStream.Position := FVirtualPosition + FirstBufferRead;
FStream.Read(Buf[FirstBufferRead], StreamDirectRead);
Dec(BytesLeft, StreamDirectRead);
if BytesLeft > 0 then begin
// update buffer, and read what is left
UpdateBufferFromPosition(FStream.Position);
Move(FBuffer[0], Buf[FirstBufferRead + StreamDirectRead], BytesLeft);
end;
end;
Inc(FVirtualPosition, Result);
Exit;
end;
end;
Result := 0;
end;
function TBufferedStreamReader.Write(const Buffer; Count: Integer): Longint;
begin
raise ETntInternalError.Create('Internal Error: class can not write.');
Result := 0;
end;
//-------- synced wide string -----------------
function GetSyncedWideString(var WideStr: WideString; const AnsiStr: AnsiString): WideString;
begin
if AnsiString(WideStr) <> (AnsiStr) then begin
WideStr := AnsiStr; {AnsiStr changed. Keep WideStr in sync.}
end;
Result := WideStr;
end;
procedure SetSyncedWideString(const Value: WideString; var WideStr: WideString;
const AnsiStr: AnsiString; SetAnsiStr: TSetAnsiStrEvent);
begin
if Value <> GetSyncedWideString(WideStr, AnsiStr) then
begin
if (not WideSameStr(Value, AnsiString(Value))) {unicode chars lost in conversion}
and (AnsiStr = AnsiString(Value)) {AnsiStr is not going to change}
then begin
SetAnsiStr(''); {force the change}
end;
WideStr := Value;
SetAnsiStr(Value);
end;
end;
{ TWideComponentHelper }
function CompareComponentHelperToTarget(Item, Target: Pointer): Integer;
begin
if Integer(TWideComponentHelper(Item).FComponent) < Integer(Target) then
Result := -1
else if Integer(TWideComponentHelper(Item).FComponent) > Integer(Target) then
Result := 1
else
Result := 0;
end;
function FindWideComponentHelperIndex(ComponentHelperList: TComponentList; Component: TComponent; var Index: Integer): Boolean;
begin
// find Component in sorted wide caption list (list is sorted by TWideComponentHelper.FComponent)
Result := FindSortedListByTarget(ComponentHelperList, CompareComponentHelperToTarget, Component, Index);
end;
constructor TWideComponentHelper.Create(AOwner: TComponent);
begin
raise ETntInternalError.Create('TNT Internal Error: TWideComponentHelper.Create should never be encountered.');
end;
constructor TWideComponentHelper.CreateHelper(AOwner: TComponent; ComponentHelperList: TComponentList);
var
Index: Integer;
begin
// don't use direct ownership for memory management
inherited Create(nil);
FComponent := AOwner;
FComponent.FreeNotification(Self);
// insert into list according to sort
FindWideComponentHelperIndex(ComponentHelperList, FComponent, Index);
ComponentHelperList.Insert(Index, Self);
end;
procedure TWideComponentHelper.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited;
if (AComponent = FComponent) and (Operation = opRemove) then begin
FComponent := nil;
Free;
end;
end;
function FindWideComponentHelper(ComponentHelperList: TComponentList; Component: TComponent): TWideComponentHelper;
var
Index: integer;
begin
if FindWideComponentHelperIndex(ComponentHelperList, Component, Index) then begin
Result := TWideComponentHelper(ComponentHelperList[Index]);
Assert(Result.FComponent = Component, 'TNT Internal Error: FindWideComponentHelperIndex failed.');
end else
Result := nil;
end;
initialization
RuntimeUTFStreaming := False; { Delphi 6 and higher don't need UTF help at runtime. }
end.
|