1 unit uMyIni;
2 
3 {$mode objfpc}{$H+}
4 
5 interface
6 
7 uses
8   Classes, SysUtils, iniFiles, dynlibs;
9 
10 type
11   TMyIni = class
12     ini  : TMemIniFile;
13     lini : TMemIniFile;
14     crit : TRTLCriticalSection;
15   private
16     LocalSections : String;
17     fIniFileName : String;
18   public
19     property IniFileName : String read fIniFileName;
20 
21     constructor Create(IniFile,LocalIniFile : String);
22     destructor  Destroy; override;
23 
ReadStringnull24     function  ReadString(const Section, Ident, Default: string): string;
ReadIntegernull25     function  ReadInteger(const Section, Ident: string; Default: Longint;ToLocal : Boolean=FALSE): Longint;
ReadBoolnull26     function  ReadBool(const Section, Ident: string; Default: Boolean;ToLocal : Boolean=FALSE): Boolean;
ReadFloatnull27     function  ReadFloat(const Section, Ident: string; Default: Double): Double;
SectionExistsnull28     function  SectionExists(Section : String) : Boolean;
LocalOnlynull29     function  LocalOnly(Section : String) : Boolean;
30 
31     procedure WriteString(const Section, Ident, Value: String;ToLocal : Boolean=FALSE);
32     procedure WriteInteger(const Section, Ident: string; Value: Longint;ToLocal : Boolean=FALSE);
33     procedure WriteBool(const Section, Ident: string; Value: Boolean;ToLocal : Boolean=FALSE);
34     procedure WriteFloat(const Section, Ident: string; Value: Double);
35     procedure SaveToDisk;
36     procedure DeleteKey(const Section, Ident: String;ToLocal : Boolean=FALSE);
37     procedure ReadSection(const Section: string; Strings: TStrings;ToLocal : Boolean=FALSE);
38     procedure ReadSectionRaw(const Section: string; Strings: TStrings);
39     procedure LoadLocalSectionsList;
40 end;
41 
42 var
43   cqrini : TMyIni;
44 
45 implementation
46 
47 constructor TMyIni.Create(IniFile,LocalIniFile : String);
48 begin
49   InitCriticalSection(crit);
50   fIniFileName := IniFile;
51   ini  := TMemIniFile.Create(IniFile);
52   lini := TMemIniFile.Create(LocalIniFile);
53   ini.CacheUpdates :=false;    //should be as default, but is it?
54   lini.CacheUpdates :=false;
55 end;
56 
TMyIni.ReadStringnull57 function TMyIni.ReadString(const Section, Ident, Default: string): string;
58 begin
59   EnterCriticalsection(crit);
60   try
61     if LocalOnly(Section) then
62       Result := lini.ReadString(Section, Ident, Default)
63     else
64       Result := ini.ReadString(Section, Ident, Default)
65   finally
66     LeaveCriticalsection(crit)
67   end
68 end;
69 
ReadIntegernull70 function TMyIni.ReadInteger(const Section, Ident: string; Default: Longint;ToLocal : Boolean=FALSE): Longint;
71 begin
72   EnterCriticalsection(crit);
73   try
74     if (LocalOnly(Section) or ToLocal) then
75       Result := lini.ReadInteger(Section, Ident, Default)
76     else
77       Result := ini.ReadInteger(Section, Ident, Default)
78   finally
79     LeaveCriticalsection(crit)
80   end
81 end;
82 
ReadBoolnull83 function TMyIni.ReadBool(const Section, Ident: string; Default: Boolean;ToLocal : Boolean=FALSE): Boolean;
84 begin
85   EnterCriticalsection(crit);
86   try
87     if (LocalOnly(Section) or ToLocal)  then
88       Result := lini.ReadBool(Section, Ident, Default)
89     else
90       Result := ini.ReadBool(Section, Ident, Default)
91   finally
92     LeaveCriticalsection(crit)
93   end
94 end;
95 
ReadFloatnull96 function TMyIni.ReadFloat(const Section, Ident: string; Default: Double): Double;
97 begin
98   EnterCriticalsection(crit);
99   try
100     if LocalOnly(Section) then
101       Result := lini.ReadFloat(Section, Ident, Default)
102     else
103       Result := ini.ReadFloat(Section, Ident, Default)
104   finally
105     LeaveCriticalsection(crit)
106   end
107 end;
108 
109 
110 procedure TMyIni.WriteString(const Section, Ident, Value: String;ToLocal : Boolean=FALSE);
111 begin
112   EnterCriticalsection(crit);
113   try
114     if (LocalOnly(Section) or ToLocal) then
115       lini.WriteString(Section, Ident, Value)
116     else
117       ini.WriteString(Section, Ident, Value)
118   finally
119     LeaveCriticalsection(crit)
120   end
121 end;
122 
123 procedure TMyIni.WriteInteger(const Section, Ident: string; Value: Longint;ToLocal : Boolean=FALSE);
124 begin
125   EnterCriticalsection(crit);
126   try
127     if (LocalOnly(Section) or ToLocal) then
128       lini.WriteInteger(Section, Ident, Value)
129     else
130       ini.WriteInteger(Section, Ident, Value)
131   finally
132     LeaveCriticalsection(crit)
133   end
134 end;
135 
136 procedure TMyIni.WriteBool(const Section, Ident: string; Value: Boolean;ToLocal : Boolean=FALSE);
137 begin
138   EnterCriticalsection(crit);
139   try
140     if (LocalOnly(Section) or ToLocal) then
141       lini.WriteBool(Section, Ident, Value)
142     else
143       ini.WriteBool(Section, Ident, Value)
144   finally
145     LeaveCriticalsection(crit)
146   end
147 end;
148 
149 procedure TMyIni.WriteFloat(const Section, Ident: string; Value: Double);
150 begin
151   EnterCriticalsection(crit);
152   try
153     if LocalOnly(Section) then
154       lini.WriteFloat(Section, Ident, Value)
155     else
156       ini.WriteFloat(Section, Ident, Value)
157   finally
158     LeaveCriticalsection(crit)
159   end
160 end;
161 
162 procedure TMyIni.SaveToDisk;
163 begin
164   EnterCriticalsection(crit);
165   try
166     ini.UpdateFile;
167     lini.UpdateFile
168   finally
169     LeaveCriticalsection(crit)
170   end
171 end;
172 
173 procedure TMyIni.DeleteKey(const Section, Ident: String;ToLocal : Boolean=FALSE);
174 begin
175   EnterCriticalsection(crit);
176   try
177     if (LocalOnly(Section) or ToLocal) then
178       lini.DeleteKey(Section,Ident)
179     else
180       ini.DeleteKey(Section,Ident)
181   finally
182     LeaveCriticalsection(crit)
183   end
184 end;
185 
186 procedure TMyIni.ReadSection(const Section: string; Strings: TStrings;ToLocal : Boolean=FALSE);
187 begin
188   EnterCriticalsection(crit);
189   try
190     if (LocalOnly(Section) or ToLocal) then
191       lini.ReadSection(Section,Strings)
192     else
193       ini.ReadSection(Section,Strings)
194   finally
195     LeaveCriticalsection(crit)
196   end
197 end;
198 
199 procedure TMyIni.ReadSectionRaw(const Section: string; Strings: TStrings);
200 begin
201   EnterCriticalsection(crit);
202   try
203     if LocalOnly(Section) then
204       lini.ReadSectionRaw(Section,Strings)
205     else
206       ini.ReadSectionRaw(Section,Strings)
207   finally
208     LeaveCriticalsection(crit)
209   end
210 end;
211 
212 
SectionExistsnull213 function TMyIni.SectionExists(Section : String) : Boolean;
214 begin
215   EnterCriticalsection(crit);
216   try
217     if LocalOnly(Section) then
218       Result := lini.SectionExists(Section)
219     else
220       Result := ini.SectionExists(Section)
221   finally
222     LeaveCriticalsection(crit)
223   end
224 end;
225 
TMyIni.LocalOnlynull226 function TMyIni.LocalOnly(Section : String) : Boolean;
227 begin
228   Result := Pos(Section+',',LocalSections)>0
229 end;
230 
231 procedure TMyIni.LoadLocalSectionsList;
232 begin
233   LocalSections := cqrini.ReadString('ConfigStorage','Items','')
234 end;
235 
236 destructor TMyIni.Destroy;
237 begin
238   inherited;
239   ini.UpdateFile;
240   FreeAndNil(ini);
241   lini.UpdateFile;
242   FreeAndNil(lini);
243   DoneCriticalsection(crit)
244 end;
245 
246 initialization
247 
248 finalization
249   Writeln('Closing ini file ...')
250 end.
251 
252