summaryrefslogtreecommitdiff
path: root/plugins/Chess4Net/lib/TntUnicodeControls/Source/TntIniFiles.pas
blob: 72199508657fcff3028abb9cfac905201ea6e244 (plain)
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
{*****************************************************************************}
{                                                                             }
{    Tnt Delphi Unicode Controls                                              }
{                                                                             }
{    Portions created by Wild Hunter are                                      }
{    Copyright (c) 2003 Wild Hunter (raguotis@freemail.lt)                    }
{                                                                             }
{    Portions created by Stanley Xu are                                       }
{    Copyright (c) 1999-2006 Stanley Xu                                       }
{      (http://gosurfbrowser.com/?go=supportFeedback&ln=en)                   }
{                                                                             }
{    Portions created by Borland Software Corporation are                     }
{    Copyright (c) 1995-2001 Borland Software Corporation                     }
{                                                                             }
{*****************************************************************************}

unit TntIniFiles;

{$R-,T-,H+,X+}
{$INCLUDE TntCompilers.inc}

interface

uses
  Classes, IniFiles,
  TntClasses;

type

  TTntCustomIniFile = class({TCustomIniFile}TObject{TNT-ALLOW TObject})
  private
    FFileName: WideString;
  public
    constructor Create(const FileName: WideString);
    function SectionExists(const Section: WideString): Boolean;
    function ReadString(const Section, Ident, Default: WideString): WideString; virtual; abstract;
    procedure WriteString(const Section, Ident, Value: WideString); virtual; abstract;
    function ReadInteger(const Section, Ident: WideString; Default: Longint): Longint; virtual;
    procedure WriteInteger(const Section, Ident: WideString; Value: Longint); virtual;
    function ReadBool(const Section, Ident: WideString; Default: Boolean): Boolean; virtual;
    procedure WriteBool(const Section, Ident: WideString; Value: Boolean); virtual;
    function ReadBinaryStream(const Section, Name: WideString; Value: TStream): Integer; virtual;
    function ReadDate(const Section, Name: WideString; Default: TDateTime): TDateTime; virtual;
    function ReadDateTime(const Section, Name: WideString; Default: TDateTime): TDateTime; virtual;
    function ReadFloat(const Section, Name: WideString; Default: Double): Double; virtual;
    function ReadTime(const Section, Name: WideString; Default: TDateTime): TDateTime; virtual;
    procedure WriteBinaryStream(const Section, Name: WideString; Value: TStream); virtual;
    procedure WriteDate(const Section, Name: WideString; Value: TDateTime); virtual;
    procedure WriteDateTime(const Section, Name: WideString; Value: TDateTime); virtual;
    procedure WriteFloat(const Section, Name: WideString; Value: Double); virtual;
    procedure WriteTime(const Section, Name: WideString; Value: TDateTime); virtual;
    procedure ReadSection(const Section: WideString; Strings: TTntStrings); virtual; abstract;
    procedure ReadSections(Strings: TTntStrings); virtual; abstract;
    procedure ReadSectionValues(const Section: WideString; Strings: TTntStrings); virtual; abstract;
    procedure EraseSection(const Section: WideString); virtual; abstract;
    procedure DeleteKey(const Section, Ident: WideString); virtual; abstract;
    procedure UpdateFile; virtual; abstract;
    function ValueExists(const Section, Ident: WideString): Boolean;
    property FileName: WideString read FFileName;
  end;

  { TTntStringHash - used internally by TTntMemIniFile to optimize searches. }

  PPTntHashItem = ^PTntHashItem;
  PTntHashItem = ^TTntHashItem;
  TTntHashItem = record
    Next: PTntHashItem;
    Key: WideString;
    Value: Integer;
  end;

  TTntStringHash = class
  private
    Buckets: array of PTntHashItem;
  protected
    function Find(const Key: WideString): PPTntHashItem;
    function HashOf(const Key: WideString): Cardinal; virtual;
  public
    constructor Create(Size: Integer = 256);
    destructor Destroy; override;
    procedure Add(const Key: WideString; Value: Integer);
    procedure Clear;
    procedure Remove(const Key: WideString);
    function Modify(const Key: WideString; Value: Integer): Boolean;
    function ValueOf(const Key: WideString): Integer;
  end;

  { TTntHashedStringList - A TTntStringList that uses TTntStringHash to improve the
    speed of Find }

  TTntHashedStringList = class(TTntStringList)
  private
    FValueHash: TTntStringHash;
    FNameHash: TTntStringHash;
    FValueHashValid: Boolean;
    FNameHashValid: Boolean;
    procedure UpdateValueHash;
    procedure UpdateNameHash;
  protected
    procedure Changed; override;
  public
    destructor Destroy; override;
    function IndexOf(const S: WideString): Integer; override;
    function IndexOfName(const Name: WideString): Integer; override;
  end;

  { TTntMemIniFile - loads and entire ini file into memory and allows all
    operations to be performed on the memory image.  The image can then
    be written out to the disk file }

  TTntMemIniFile = class(TTntCustomIniFile)
  private
    FSections: TTntStringList;
    function AddSection(const Section: WideString): TTntStrings;
    function GetCaseSensitive: Boolean;
    procedure SetCaseSensitive(Value: Boolean);
    procedure LoadValues;
  public
    constructor Create(const FileName: WideString); virtual;
    destructor Destroy; override;
    procedure Clear;
    procedure DeleteKey(const Section, Ident: WideString); override;
    procedure EraseSection(const Section: WideString); override;
    procedure GetStrings(List: TTntStrings);
    procedure ReadSection(const Section: WideString; Strings: TTntStrings); override;
    procedure ReadSections(Strings: TTntStrings); override;
    procedure ReadSectionValues(const Section: WideString; Strings: TTntStrings); override;
    function ReadString(const Section, Ident, Default: WideString): WideString; override;
    procedure Rename(const FileName: WideString; Reload: Boolean);
    procedure SetStrings(List: TTntStrings);
    procedure UpdateFile; override;
    procedure WriteString(const Section, Ident, Value: WideString); override;
    property CaseSensitive: Boolean read GetCaseSensitive write SetCaseSensitive;
  end;

{$IFDEF MSWINDOWS}
  { TTntIniFile - Encapsulates the Windows INI file interface
    (Get/SetPrivateProfileXXX functions) }

  TTntIniFile = class(TTntCustomIniFile)
  private
    FAnsiIniFile: TIniFile; // For compatibility with Windows 95/98/Me
  public
    constructor Create(const FileName: WideString); virtual;
    destructor Destroy; override;
    function ReadString(const Section, Ident, Default: WideString): WideString; override;
    procedure WriteString(const Section, Ident, Value: WideString); override;
    procedure ReadSection(const Section: WideString; Strings: TTntStrings); override;
    procedure ReadSections(Strings: TTntStrings); override;
    procedure ReadSectionValues(const Section: WideString; Strings: TTntStrings); override;
    procedure EraseSection(const Section: WideString); override;
    procedure DeleteKey(const Section, Ident: WideString); override;
    procedure UpdateFile; override;
  end;
{$ELSE}
  TTntIniFile = class(TTntMemIniFile)
  public
    destructor Destroy; override;
  end;
{$ENDIF}


implementation

uses                                                    
  RTLConsts, SysUtils, TntSysUtils
{$IFDEF COMPILER_9_UP} , WideStrUtils {$ELSE} , TntWideStrUtils {$ENDIF}
{$IFDEF MSWINDOWS}     , Windows      {$ENDIF};

{ TTntCustomIniFile }

constructor TTntCustomIniFile.Create(const FileName: WideString);
begin
  FFileName := FileName;
end;

function TTntCustomIniFile.SectionExists(const Section: WideString): Boolean;
var
  S: TTntStrings;
begin
  S := TTntStringList.Create;
  try
    ReadSection(Section, S);
    Result := S.Count > 0;
  finally
    S.Free;
  end;
end;

function TTntCustomIniFile.ReadInteger(const Section, Ident: WideString;
  Default: Longint): Longint;
var
  IntStr: WideString;
begin
  IntStr := ReadString(Section, Ident, '');
  if (Length(IntStr) > 2) and (IntStr[1] = WideChar('0')) and
    ((IntStr[2] = WideChar('X')) or (IntStr[2] = WideChar('x'))) then
    IntStr := WideString('$') + Copy(IntStr, 3, Maxint);
  Result := StrToIntDef(IntStr, Default);
end;

procedure TTntCustomIniFile.WriteInteger(const Section, Ident: WideString; Value: Longint);
begin
  WriteString(Section, Ident, IntToStr(Value));
end;

function TTntCustomIniFile.ReadBool(const Section, Ident: WideString;
  Default: Boolean): Boolean;
begin
  Result := ReadInteger(Section, Ident, Ord(Default)) <> 0;
end;

function TTntCustomIniFile.ReadDate(const Section, Name: WideString; Default: TDateTime): TDateTime;
var
  DateStr: WideString;
begin
  DateStr := ReadString(Section, Name, '');
  Result := Default;
  if DateStr <> '' then
  try
    Result := StrToDate(DateStr);
  except
    on EConvertError do
    else raise;
  end;
end;

function TTntCustomIniFile.ReadDateTime(const Section, Name: WideString; Default: TDateTime): TDateTime;
var
  DateStr: WideString;
begin
  DateStr := ReadString(Section, Name, '');
  Result := Default;
  if DateStr <> '' then
  try
    Result := StrToDateTime(DateStr);
  except
    on EConvertError do
    else raise;
  end;
end;

function TTntCustomIniFile.ReadFloat(const Section, Name: WideString; Default: Double): Double;
var
  FloatStr: WideString;
begin
  FloatStr := ReadString(Section, Name, '');
  Result := Default;
  if FloatStr <> '' then
  try
    Result := StrToFloat(FloatStr);
  except
    on EConvertError do
    else raise;
  end;
end;

function TTntCustomIniFile.ReadTime(const Section, Name: WideString; Default: TDateTime): TDateTime;
var
  TimeStr: WideString;
begin
  TimeStr := ReadString(Section, Name, '');
  Result := Default;
  if TimeStr <> '' then
  try
    Result := StrToTime(TimeStr);
  except
    on EConvertError do
    else raise;
  end;
end;

procedure TTntCustomIniFile.WriteDate(const Section, Name: WideString; Value: TDateTime);
begin
  WriteString(Section, Name, DateToStr(Value));
end;

procedure TTntCustomIniFile.WriteDateTime(const Section, Name: WideString; Value: TDateTime);
begin
  WriteString(Section, Name, DateTimeToStr(Value));
end;

procedure TTntCustomIniFile.WriteFloat(const Section, Name: WideString; Value: Double);
begin
  WriteString(Section, Name, FloatToStr(Value));
end;

procedure TTntCustomIniFile.WriteTime(const Section, Name: WideString; Value: TDateTime);
begin
  WriteString(Section, Name, TimeToStr(Value));
end;

procedure TTntCustomIniFile.WriteBool(const Section, Ident: WideString; Value: Boolean);
const
  Values: array[Boolean] of WideString = ('0', '1');
begin
  WriteString(Section, Ident, Values[Value]);
end;

function TTntCustomIniFile.ValueExists(const Section, Ident: WideString): Boolean;
var
  S: TTntStrings;
begin
  S := TTntStringList.Create;
  try
    ReadSection(Section, S);
    Result := S.IndexOf(Ident) > -1;
  finally
    S.Free;
  end;
end;

function TTntCustomIniFile.ReadBinaryStream(const Section, Name: WideString;
  Value: TStream): Integer;
var
  Text: String; // Not Unicode: Due to HexToBin is not Unicode
  Stream: TMemoryStream;
  Pos: Integer;
begin
  Text := ReadString(Section, Name, '');
  if Text <> '' then
  begin
    if Value is TMemoryStream then
      Stream := TMemoryStream(Value)
    else Stream := TMemoryStream.Create;
    try
      Pos := Stream.Position;
      Stream.SetSize(Stream.Size + Length(Text) div 2);
      HexToBin(PChar(Text), PChar(Integer(Stream.Memory) + Stream.Position), Length(Text) div 2);
      Stream.Position := Pos;
      if Value <> Stream then Value.CopyFrom(Stream, Length(Text) div 2);
      Result := Stream.Size - Pos;
    finally
      if Value <> Stream then Stream.Free;
    end;
  end else Result := 0;
end;

procedure TTntCustomIniFile.WriteBinaryStream(const Section, Name: WideString;
  Value: TStream);
var
  Text: string; // Not Unicode: Due to BinToHex is not Unicode
  Stream: TMemoryStream;
begin
  SetLength(Text, (Value.Size - Value.Position) * 2);
  if Length(Text) > 0 then
  begin
    if Value is TMemoryStream then
      Stream := TMemoryStream(Value)
    else Stream := TMemoryStream.Create;
    try
      if Stream <> Value then
      begin
        Stream.CopyFrom(Value, Value.Size - Value.Position);
        Stream.Position := 0;
      end;
      BinToHex(PChar(Integer(Stream.Memory) + Stream.Position), PChar(Text),
        Stream.Size - Stream.Position);
    finally
      if Value <> Stream then Stream.Free;
    end;
  end;
  WriteString(Section, Name, Text);
end;

{ TTntStringHash }

procedure TTntStringHash.Add(const Key: WideString; Value: Integer);
var
  Hash: Integer;
  Bucket: PTntHashItem;
begin
  Hash := HashOf(Key) mod Cardinal(Length(Buckets));
  New(Bucket);
  Bucket^.Key := Key;
  Bucket^.Value := Value;
  Bucket^.Next := Buckets[Hash];
  Buckets[Hash] := Bucket;
end;

procedure TTntStringHash.Clear;
var
  I: Integer;
  P, N: PTntHashItem;
begin
  for I := 0 to Length(Buckets) - 1 do
  begin
    P := Buckets[I];
    while P <> nil do
    begin
      N := P^.Next;
      Dispose(P);
      P := N;
    end;
    Buckets[I] := nil;
  end;
end;

constructor TTntStringHash.Create(Size: Integer);
begin
  inherited Create;
  SetLength(Buckets, Size);
end;

destructor TTntStringHash.Destroy;
begin
  Clear;
  inherited;
end;

function TTntStringHash.Find(const Key: WideString): PPTntHashItem;
var
  Hash: Integer;
begin
  Hash := HashOf(Key) mod Cardinal(Length(Buckets));
  Result := @Buckets[Hash];
  while Result^ <> nil do
  begin
    if Result^.Key = Key then
      Exit
    else
      Result := @Result^.Next;
  end;
end;

function TTntStringHash.HashOf(const Key: WideString): Cardinal;
var
  I: Integer;
begin
  Result := 0;
  for I := 1 to Length(Key) do
    Result := ((Result shl 2) or (Result shr (SizeOf(Result) * 8 - 2))) xor
      Ord(Key[I]); // Is it OK for WideChar?
end;

function TTntStringHash.Modify(const Key: WideString; Value: Integer): Boolean;
var
  P: PTntHashItem;
begin
  P := Find(Key)^;
  if P <> nil then
  begin
    Result := True;
    P^.Value := Value;
  end
  else
    Result := False;
end;

procedure TTntStringHash.Remove(const Key: WideString);
var
  P: PTntHashItem;
  Prev: PPTntHashItem;
begin
  Prev := Find(Key);
  P := Prev^;
  if P <> nil then
  begin
    Prev^ := P^.Next;
    Dispose(P);
  end;
end;

function TTntStringHash.ValueOf(const Key: WideString): Integer;
var
  P: PTntHashItem;
begin
  P := Find(Key)^;
  if P <> nil then
    Result := P^.Value else
    Result := -1;
end;

{ TTntHashedStringList }

procedure TTntHashedStringList.Changed;
begin
  inherited;
  FValueHashValid := False;
  FNameHashValid := False;
end;

destructor TTntHashedStringList.Destroy;
begin
  FValueHash.Free;
  FNameHash.Free;
  inherited;
end;

function TTntHashedStringList.IndexOf(const S: WideString): Integer;
begin
  UpdateValueHash;
  if not CaseSensitive then
    Result :=  FValueHash.ValueOf(WideUpperCase(S))
  else
    Result :=  FValueHash.ValueOf(S);
end;

function TTntHashedStringList.IndexOfName(const Name: WideString): Integer;
begin
  UpdateNameHash;
  if not CaseSensitive then
    Result := FNameHash.ValueOf(WideUpperCase(Name))
  else
    Result := FNameHash.ValueOf(Name);
end;

procedure TTntHashedStringList.UpdateNameHash;
var
  I: Integer;
  P: Integer;
  Key: WideString;
begin
  if FNameHashValid then Exit;
  if FNameHash = nil then
    FNameHash := TTntStringHash.Create
  else
    FNameHash.Clear;
  for I := 0 to Count - 1 do
  begin
    Key := Get(I);
    P := Pos(NameValueSeparator, Key);
    if P <> 0 then
    begin
      if not CaseSensitive then
        Key := WideUpperCase(Copy(Key, 1, P - 1))
      else
        Key := Copy(Key, 1, P - 1);
      FNameHash.Add(Key, I);
    end;
  end;
  FNameHashValid := True;
end;

procedure TTntHashedStringList.UpdateValueHash;
var
  I: Integer;
begin
  if FValueHashValid then Exit;
  if FValueHash = nil then
    FValueHash := TTntStringHash.Create
  else
    FValueHash.Clear;
  for I := 0 to Count - 1 do
    if not CaseSensitive then
      FValueHash.Add(WideUpperCase(Self[I]), I)
    else
      FValueHash.Add(Self[I], I);
  FValueHashValid := True;
end;

{ TTntMemIniFile }

constructor TTntMemIniFile.Create(const FileName: WideString);
begin
  inherited Create(FileName);
  FSections := TTntHashedStringList.Create;
  FSections.NameValueSeparator := '=';
{$IFDEF LINUX}
  FSections.CaseSensitive := True;
{$ELSE}
  FSections.CaseSensitive := False;
{$ENDIF}
  LoadValues;
end;

destructor TTntMemIniFile.Destroy;
begin
  if FSections <> nil then Clear;
  FSections.Free;
  inherited;
end;

function TTntMemIniFile.AddSection(const Section: WideString): TTntStrings;
begin
  Result := TTntHashedStringList.Create;
  try
    TTntHashedStringList(Result).CaseSensitive := CaseSensitive;
    FSections.AddObject(Section, Result);
  except
    Result.Free;
    raise;
  end;
end;

procedure TTntMemIniFile.Clear;
var
  I: Integer;
begin
  for I := 0 to FSections.Count - 1 do
    TObject(FSections.Objects[I]).Free;
  FSections.Clear;
end;

procedure TTntMemIniFile.DeleteKey(const Section, Ident: WideString);
var
  I, J: Integer;
  Strings: TTntStrings;
begin
  I := FSections.IndexOf(Section);
  if I >= 0 then
  begin
    Strings := TTntStrings(FSections.Objects[I]);
    J := Strings.IndexOfName(Ident);
    if J >= 0 then Strings.Delete(J);
  end;
end;

procedure TTntMemIniFile.EraseSection(const Section: WideString);
var
  I: Integer;
begin
  I := FSections.IndexOf(Section);
  if I >= 0 then
  begin
    TStrings(FSections.Objects[I]).Free;
    FSections.Delete(I);
  end;
end;

function TTntMemIniFile.GetCaseSensitive: Boolean;
begin
  Result := FSections.CaseSensitive;
end;

procedure TTntMemIniFile.GetStrings(List: TTntStrings);
var
  I, J: Integer;
  Strings: TTntStrings;
begin
  List.BeginUpdate;
  try
    for I := 0 to FSections.Count - 1 do
    begin
      List.Add('[' + FSections[I] + ']');
      Strings := TTntStrings(FSections.Objects[I]);
      for J := 0 to Strings.Count - 1 do
        List.Add(Strings[J]);
      List.Add('');
    end;
  finally
    List.EndUpdate;
  end;
end;

procedure TTntMemIniFile.LoadValues;
var
  List: TTntStringList;
begin
  if (FileName <> '') and WideFileExists(FileName) then
  begin
    List := TTntStringList.Create;
    try
      List.LoadFromFile(FileName);
      SetStrings(List);
    finally
      List.Free;
    end;
  end else
    Clear;
end;

procedure TTntMemIniFile.ReadSection(const Section: WideString;
  Strings: TTntStrings);
var
  I, J: Integer;
  SectionStrings: TTntStrings;
begin
  Strings.BeginUpdate;
  try
    Strings.Clear;
    I := FSections.IndexOf(Section);
    if I >= 0 then
    begin
      SectionStrings := TTntStrings(FSections.Objects[I]);
      for J := 0 to SectionStrings.Count - 1 do
        Strings.Add(SectionStrings.Names[J]);
    end;
  finally
    Strings.EndUpdate;
  end;
end;

procedure TTntMemIniFile.ReadSections(Strings: TTntStrings);
begin
  Strings.Assign(FSections);
end;

procedure TTntMemIniFile.ReadSectionValues(const Section: WideString;
  Strings: TTntStrings);
var
  I: Integer;
begin
  Strings.BeginUpdate;
  try
    Strings.Clear;
    I := FSections.IndexOf(Section);
    if I >= 0 then Strings.Assign(TTntStrings(FSections.Objects[I]));
  finally
    Strings.EndUpdate;
  end;
end;

function TTntMemIniFile.ReadString(const Section, Ident,
  Default: WideString): WideString;
var
  I: Integer;
  Strings: TTntStrings;
begin
  I := FSections.IndexOf(Section);
  if I >= 0 then
  begin
    Strings := TTntStrings(FSections.Objects[I]);
    I := Strings.IndexOfName(Ident);
    if I >= 0 then
    begin
      Result := Copy(Strings[I], Length(Ident) + 2, Maxint);
      Exit;
    end;
  end;
  Result := Default;
end;

procedure TTntMemIniFile.Rename(const FileName: WideString; Reload: Boolean);
begin
  FFileName := FileName;
  if Reload then LoadValues;
end;

procedure TTntMemIniFile.SetCaseSensitive(Value: Boolean);
var
  I: Integer;
begin
  if Value <> FSections.CaseSensitive then
  begin
    FSections.CaseSensitive := Value;
    for I := 0 to FSections.Count - 1 do
      with TTntHashedStringList(FSections.Objects[I]) do
      begin
        CaseSensitive := Value;
        Changed;
      end;
    TTntHashedStringList(FSections).Changed;
  end;
end;

procedure TTntMemIniFile.SetStrings(List: TTntStrings);
var
  I, J: Integer;
  S: WideString;
  Strings: TTntStrings;
begin
  Clear;
  Strings := nil;
  for I := 0 to List.Count - 1 do
  begin
    S := Trim(List[I]);
    if (S <> '') and (S[1] <> ';') then
      if (S[1] = '[') and (S[Length(S)] = ']') then
      begin
        Delete(S, 1, 1);
        SetLength(S, Length(S)-1);
        Strings := AddSection(Trim(S));
      end
      else
        if Strings <> nil then
        begin
          J := Pos(FSections.NameValueSeparator, S);
          if J > 0 then // remove spaces before and after NameValueSeparator
            Strings.Add(Trim(Copy(S, 1, J-1)) + FSections.NameValueSeparator + TrimRight(Copy(S, J+1, MaxInt)) )
          else
            Strings.Add(S);
        end;
  end;
end;

procedure TTntMemIniFile.UpdateFile;
var
  List: TTntStringList;
begin
  List := TTntStringList.Create;
  try
    GetStrings(List);
    List.SaveToFile(FFileName);
  finally
    List.Free;
  end;
end;

procedure TTntMemIniFile.WriteString(const Section, Ident, Value: WideString);
var
  I: Integer;
  S: WideString;
  Strings: TTntStrings;
begin
  I := FSections.IndexOf(Section);
  if I >= 0 then
    Strings := TTntStrings(FSections.Objects[I]) else
    Strings := AddSection(Section);
  S := Ident + FSections.NameValueSeparator + Value;
  I := Strings.IndexOfName(Ident);
  if I >= 0 then Strings[I] := S else Strings.Add(S);
end;



{$IFDEF MSWINDOWS}
{ TTntIniFile }

constructor TTntIniFile.Create(const FileName: WideString);
begin
  inherited Create(FileName);
  if (not Win32PlatformIsUnicode) then
    FAnsiIniFile := TIniFile.Create(FileName);
end;

destructor TTntIniFile.Destroy;
begin
  UpdateFile; // flush changes to disk
  if (not Win32PlatformIsUnicode) then
    FAnsiIniFile.Free;
  inherited Destroy;
end;

function TTntIniFile.ReadString(const Section, Ident, Default: WideString): WideString;
var
  Buffer: array[0..2047] of WideChar;
begin
  if (not Win32PlatformIsUnicode) then
    { Windows 95/98/Me }
    Result := FAnsiIniFile.ReadString(Section, Ident, Default)
  else begin
    { Windows NT/2000/XP and later }
    GetPrivateProfileStringW(PWideChar(Section),
      PWideChar(Ident), PWideChar(Default), Buffer, Length(Buffer), PWideChar(FFileName));
    Result := WideString(Buffer);
  end;
end;

procedure TTntIniFile.WriteString(const Section, Ident, Value: WideString);
begin
  if (not Win32PlatformIsUnicode) then
    { Windows 95/98/Me }
    FAnsiIniFile.WriteString(Section, Ident, Value)
  else begin
    { Windows NT/2000/XP and later }
    if not WritePrivateProfileStringW(PWideChar(Section), PWideChar(Ident),
      PWideChar(Value), PWideChar(FFileName)) then
      raise EIniFileException.CreateResFmt(@SIniFileWriteError, [FileName]);
  end;
end;

procedure TTntIniFile.ReadSections(Strings: TTntStrings);
const
  BufSize = 16384 * SizeOf(WideChar);
var
  Buffer, P: PWideChar;
begin
  if (not Win32PlatformIsUnicode) then
  begin
    { Windows 95/98/Me }
    FAnsiIniFile.ReadSections(Strings.AnsiStrings);
  end else
  begin
    { Windows NT/2000/XP and later }
    GetMem(Buffer, BufSize);
    try
      Strings.BeginUpdate;
      try
        Strings.Clear;
        if GetPrivateProfileStringW(nil, nil, nil, Buffer, BufSize,
          PWideChar(FFileName)) <> 0 then
        begin
          P := Buffer;
          while P^ <> WideChar(#0) do
          begin
            Strings.Add(P);
            Inc(P, WStrLen(P) + 1);
          end;
        end;
      finally
        Strings.EndUpdate;
      end;
    finally
      FreeMem(Buffer, BufSize);
    end;
  end; {else}
end;

procedure TTntIniFile.ReadSection(const Section: WideString; Strings: TTntStrings);
const
  BufSize = 16384 * SizeOf(WideChar);
var
  Buffer, P: PWideChar;
begin
  if (not Win32PlatformIsUnicode) then
  begin
    { Windows 95/98/Me }
    FAnsiIniFile.ReadSection(Section, Strings.AnsiStrings);
  end else
  begin
    { Windows NT/2000/XP and later }
    GetMem(Buffer, BufSize);
    try
      Strings.BeginUpdate;
      try
        Strings.Clear;
        if GetPrivateProfileStringW(PWideChar(Section), nil, nil, Buffer, BufSize,
          PWideChar(FFileName)) <> 0 then
        begin
          P := Buffer;
          while P^ <> #0 do
          begin
            Strings.Add(P);
            Inc(P, WStrLen(P) + 1);
          end;
        end;
      finally
        Strings.EndUpdate;
      end;
    finally
      FreeMem(Buffer, BufSize);
    end;
  end;
end;

procedure TTntIniFile.ReadSectionValues(const Section: WideString; Strings: TTntStrings);
var
  KeyList: TTntStringList;
  I: Integer;
begin
  if (not Win32PlatformIsUnicode) then
  begin
    { Windows 95/98/Me }
    FAnsiIniFile.ReadSectionValues(Section, Strings.AnsiStrings);
  end else
  begin
    { Windows NT/2000/XP and later }
    KeyList := TTntStringList.Create;
    try
      ReadSection(Section, KeyList);
      Strings.BeginUpdate;
      try
        Strings.Clear;
        for I := 0 to KeyList.Count - 1 do
          Strings.Add(KeyList[I] + '=' + ReadString(Section, KeyList[I], ''))
      finally
        Strings.EndUpdate;
      end;
    finally
      KeyList.Free;
    end;
  end; {if}
end;

procedure TTntIniFile.EraseSection(const Section: WideString);
begin
  if (not Win32PlatformIsUnicode) then
  begin
    { Windows 95/98/Me }
    FAnsiIniFile.EraseSection(Section);
  end
  else begin
    { Windows NT/2000/XP and later }
    if not WritePrivateProfileStringW(PWideChar(Section), nil, nil,
      PWideChar(FFileName)) then
      raise EIniFileException.CreateResFmt(@SIniFileWriteError, [FileName]);
  end; {if}
end;

procedure TTntIniFile.DeleteKey(const Section, Ident: WideString);
begin
  if (not Win32PlatformIsUnicode) then
  begin
    { Windows 95/98/Me }
    FAnsiIniFile.DeleteKey(Section, Ident);
  end
  else begin
    { Windows NT/2000/XP and later }
    WritePrivateProfileStringW(PWideChar(Section), PWideChar(Ident), nil,
      PWideChar(FFileName));
  end; {if}
end;

procedure TTntIniFile.UpdateFile;
begin
  if (not Win32PlatformIsUnicode) then
  begin
    { Windows 95/98/Me }
    FAnsiIniFile.UpdateFile
  end
  else begin
    { Windows NT/2000/XP and later }
    WritePrivateProfileStringW(nil, nil, nil, PWideChar(FFileName));
  end; {if}
end;

{$ELSE}

destructor TTntIniFile.Destroy;
begin
  UpdateFile;
  inherited Destroy;
end;

{$ENDIF}




end.