bcabf1745acc5760a0375285f42ebfcb70ec1ac2
3 {$INCLUDE ../shared/a_modes.inc}
6 -----------------------------------
7 CONFIG.PAS ВЕРСИЯ ОТ 24.09.06
8 -----------------------------------
20 TConfig
= class(TObject
)
22 FParams
: array of TParam
;
23 FSections
: array of ShortString;
24 FCurrentSection
: Word;
25 function ReadParam(Section
, Param
, Default
: string): string;
26 procedure WriteParam(Section
, Param
, Value
: string);
27 procedure ProcessStr(Str
: string);
30 constructor CreateFile(FileName
: string);
31 constructor CreateMem(pData
: Pointer; _Length
: LongWord);
32 destructor Destroy(); override;
33 procedure FreeConfig();
34 procedure SaveFile(FileName
: string);
35 function ReadInt(Section
, Param
: string; Default
: Integer): Integer;
36 function ReadStr(Section
, Param
: string; Default
: String): string;
37 function ReadBool(Section
, Param
: string; Default
: Boolean): Boolean;
38 function SectionExists(Section
: string): Boolean;
39 procedure WriteInt(Section
, Param
: string; Value
: Integer);
40 procedure WriteStr(Section
, Param
, Value
: string);
41 procedure WriteBool(Section
, Param
: string; Value
: Boolean);
51 constructor TConfig
.Create();
58 constructor TConfig
.CreateFile(FileName
: string);
65 if not FileExists(FileName
) then Exit
;
67 AssignFile(f
, FileName
);
77 constructor TConfig
.CreateMem(pData
: Pointer; _Length
: LongWord);
84 if _Length
= 0 then Exit
;
85 if pData
= nil then Exit
;
87 SetLength(str
, _Length
);
89 CopyMemory(@str
[1], pData
, _Length
);
93 for a
:= 2 to Length(Str
) do
94 if (Str
[a
-1]+Str
[a
] = #13#10) or (a
= Length(Str
)) then
96 if a
<> Length(Str
) then ProcessStr(Copy(Str
, 1, a
-2)) else ProcessStr(Str
);
104 destructor TConfig
.Destroy();
112 procedure TConfig
.FreeConfig();
117 SetLength(FSections
, 1);
119 FCurrentSection
:= 0;
122 procedure TConfig
.ProcessStr(Str
: string);
128 if (Str
<> '') and (Length(Str
) > 2) and (Str
[1] <> ';') then
132 if Pos('=', Str
) > 0 then
134 SetLength(FParams
, Length(FParams
)+1);
136 with FParams
[High(FParams
)] do
139 Param
:= Trim(Copy(Str
, 1, a
-1));
140 Value
:= Trim(Copy(Str
, a
+1, l
));
141 Section
:= FCurrentSection
;
144 else if (Str
[1] = '[') and (Str
[l
] = ']') then
146 SetLength(FSections
, Length(FSections
)+1);
147 FCurrentSection
:= High(FSections
);
148 FSections
[FCurrentSection
] := Trim(Copy(Str
, 2, l
-2));
153 function TConfig
.ReadBool(Section
, Param
: string; Default
: Boolean): Boolean;
157 if Default
then a
:= 1 else a
:= 0;
159 Result
:= StrToIntDef(ReadParam(Section
, Param
, IntToStr(a
)), a
) <> 0;
162 function TConfig
.ReadInt(Section
, Param
: string; Default
: Integer): Integer;
164 Result
:= StrToIntDef(ReadParam(Section
, Param
, IntToStr(Default
)), Default
);
167 function TConfig
.ReadParam(Section
, Param
, Default
: string): string;
176 if FParams
= nil then Exit
;
177 if FSections
= nil then Exit
;
181 for a
:= 0 to High(FSections
) do
182 if LowerCase(FSections
[a
]) = LowerCase(Section
) then
190 p
:= LowerCase(Param
);
192 for a
:= 0 to High(FParams
) do
193 if (FParams
[a
].Section
= s
) and (LowerCase(FParams
[a
].Param
) = p
) then
195 Result
:= FParams
[a
].Value
;
200 function TConfig
.ReadStr(Section
, Param
, Default
: string): string;
202 Result
:= ReadParam(Section
, Param
, Default
);
205 procedure TConfig
.SaveFile(FileName
: string);
211 AssignFile(f
, FileName
);
213 if (FSections
<> nil) or (FParams
<> nil) then
215 if FSections
<> nil then
216 for a
:= 0 to High(FSections
) do
218 if FSections
[a
] <> '' then Writeln(f
, '['+FSections
[a
]+']');
220 if FParams
<> nil then
221 for b
:= 0 to High(FParams
) do
222 if FParams
[b
].Section
= a
then WriteLn(f
, FParams
[b
].Param
+'='+FParams
[b
].Value
);
224 if (a
<> High(FSections
)) and (FSections
[a
] <> '') then WriteLn(f
, '');
230 function TConfig
.SectionExists(Section
: string): Boolean;
236 if FSections
= nil then Exit
;
238 Section
:= LowerCase(Section
);
240 for a
:= 0 to High(FSections
) do
241 if Section
= LowerCase(FSections
[a
]) then
248 procedure TConfig
.WriteBool(Section
, Param
: string; Value
: Boolean);
250 WriteParam(Section
, Param
, BoolToStr(Value
));
253 procedure TConfig
.WriteInt(Section
, Param
: string; Value
: Integer);
255 WriteParam(Section
, Param
, IntToStr(Value
));
258 procedure TConfig
.WriteParam(Section
, Param
, Value
: string);
268 if FSections
<> nil then
269 for a
:= 0 to High(FSections
) do
270 if FSections
[a
] = Section
then
278 SetLength(FSections
, Length(FSections
)+1);
279 a
:= High(FSections
);
280 FSections
[a
] := Section
;
284 if FParams
<> nil then
285 for b
:= 0 to High(FParams
) do
286 if (LowerCase(FParams
[b
].Param
) = LowerCase(Param
)) and (FParams
[b
].Section
= a
) then
292 if ok
then FParams
[b
].Value
:= Value
295 SetLength(FParams
, Length(FParams
)+1);
296 FParams
[High(FParams
)].Param
:= Param
;
297 FParams
[High(FParams
)].Value
:= Value
;
298 FParams
[High(FParams
)].Section
:= a
;
302 procedure TConfig
.WriteStr(Section
, Param
, Value
: string);
304 WriteParam(Section
, Param
, Value
);