summaryrefslogtreecommitdiff
path: root/plugins/Chess4Net/lib/TntUnicodeControls/Source/TntIniFilesEx.pas
blob: 87ec6139763f28715156b5f1030ed89e1c6b1fb9 (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

{*****************************************************************************}
{                                                                             }
{    Tnt Delphi Unicode Controls                                              }
{      http://www.tntware.com/delphicontrols/unicode/                         }
{        Extended TTntMemIniFile  (compatible with all versions)              }
{                                                                             }
{    Copyright (c) 1999-2007 Stanley Xu                                       }
{      http://getgosurf.com/?go=supportFeedback&ln=en                         }
{                                                                             }
{*****************************************************************************}

{*****************************************************************************}
{                                                                             }
{  BACKGROUND:                                                                }
{    TTntMemIniFile buffers all changes to the INI file in memory. To write   }
{    the data from memory back to the associated INI file, call the           }
{    UpdateFile() method. However, the whole content of this INI file will    }
{    be overwritten. Even those sections that are not used. This will make    }
{    troubles, if two instances try to change the same file at the same       }
{    time, without some method of managing access the instances may well end  }
{    up overwriting each other's work.                                        }
{                                                                             }
{  IDEA:                                                                      }
{    TTntMemIniFileEx implementes a simple idea: To check the timestamp       }
{    before each operation. If the file is modified, TTntMemIniFileEx will    }
{    reload the file to keep the content updated.                             }
{                                                                             }
{  CONCLUSION:                                                                }
{    #  TTntMemIniFileEx and TTntMemIniFile are ideal for read-only access.   }
{       For instance: To read localization files, etc.                        }
{    #  To perform mass WriteString() operations, please use the following    }
{       code.                                                                 }
{             BeginUpdate();                                                  }
{             try                                                             }
{               for I := 0 to 10000 do                                        }
{                 WriteString(...);                                           }
{             finally;                                                        }
{               EndUpdate();                                                  }
{               UpdateFile;                                                   }
{             end;                                                            }
{                                                                             }
{*****************************************************************************}

unit TntIniFilesEx;

{$INCLUDE TntCompilers.inc}

interface

uses
  TntClasses, TntIniFiles;

type
  TTntMemIniFileEx = class(TTntMemIniFile)
  private
    FUpdateCount: Integer;
    FModified: Boolean;
    FLastAccessed: Integer;
    function FileRealLastAccessedTime: Integer;
    procedure GetLatestVersion;
  protected
    procedure LoadValues; // Extended
  public
    constructor Create(const FileName: WideString); override;
    procedure BeginUpdate; virtual;
    procedure EndUpdate; virtual;
    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 DeleteKey(const Section, Ident: WideString); override;
    procedure EraseSection(const Section: WideString); override;
    procedure UpdateFile; override;
  end;



implementation

uses
  SysUtils, TntSysUtils;


{ TTntMemIniFileEx }

function TTntMemIniFileEx.FileRealLastAccessedTime: Integer;
var
  H: Integer;   // file handle
begin
  Result := 0;
  H := WideFileOpen(FileName, fmOpenWrite); //fmOpenRead (?)
  if H <> -1 then
  try
    Result := FileGetDate(H);
  finally
    FileClose(H);
  end;
end;

procedure TTntMemIniFileEx.GetLatestVersion;
begin
  if FLastAccessed = FileRealLastAccessedTime then
    Exit;

  LoadValues;
  // FLastAccess will be updated in LoadValues(...)
end;

procedure TTntMemIniFileEx.LoadValues; // Copied from TntIniFiles.pas
var
  List: TTntStringList;
begin
  if (FileName <> '') and WideFileExists(FileName) then
  begin
    List := TTntStringList.Create;
    try
      List.LoadFromFile(FileName);
      FLastAccessed := FileRealLastAccessedTime;  // Extra
      FModified := False;                         //
      SetStrings(List);
    finally
      List.Free;
    end;
  end else
    Clear;
end;

constructor TTntMemIniFileEx.Create(const FileName: WideString);
begin
  inherited Create(FileName);
  FUpdateCount := 0;
end;

procedure TTntMemIniFileEx.BeginUpdate;
begin
  Inc(FUpdateCount);
end;

procedure TTntMemIniFileEx.EndUpdate;
begin
  Dec(FUpdateCount);
end;

function TTntMemIniFileEx.ReadString(const Section, Ident, Default: WideString): WideString;
begin
  GetLatestVersion;
  Result := inherited ReadString(Section, Ident, Default);
end;

procedure TTntMemIniFileEx.WriteString(const Section, Ident, Value: WideString);
begin
  GetLatestVersion;
  inherited WriteString(Section, Ident, Value);
  FModified := True;
  UpdateFile; // Flush changes to disk
end;

procedure TTntMemIniFileEx.ReadSection(const Section: WideString; Strings: TTntStrings);
begin
  GetLatestVersion;
  inherited ReadSection(Section, Strings);
end;

procedure TTntMemIniFileEx.ReadSections(Strings: TTntStrings);
begin
  GetLatestVersion;
  inherited ReadSections(Strings);
end;

procedure TTntMemIniFileEx.ReadSectionValues(const Section: WideString; Strings: TTntStrings);
begin
  GetLatestVersion;
  inherited ReadSectionValues(Section, Strings);
end;

procedure TTntMemIniFileEx.DeleteKey(const Section, Ident: WideString);
begin
  GetLatestVersion;
  inherited DeleteKey(Section, Ident);
  FModified := True;
  UpdateFile; // Flush changes to disk
end;

procedure TTntMemIniFileEx.EraseSection(const Section: WideString);
begin
  GetLatestVersion;
  inherited EraseSection(Section);
  FModified := True;
  UpdateFile; // Flush changes to disk
end;

procedure TTntMemIniFileEx.UpdateFile;
begin
  if not FModified or (FUpdateCount > 0) then
    Exit;
  inherited;
end;





end.