1 (* Copyright (C) DooM 2D:Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE a_modes.inc}
20 -----------------------------------
21 CONFIG.PAS ÂÅÐÑÈß ÎÒ 24.09.06
22 -----------------------------------
34 TConfig
= class(TObject
)
36 FParams
: array of TParam
;
37 FSections
: array of ShortString;
38 FCurrentSection
: Word;
39 function ReadParam(Section
, Param
, Default
: string): string;
40 procedure WriteParam(Section
, Param
, Value
: string);
41 procedure ProcessStr(Str
: string);
44 constructor CreateFile(FileName
: string);
45 constructor CreateMem(pData
: Pointer; _Length
: LongWord);
46 destructor Destroy(); override;
47 procedure FreeConfig();
48 procedure SaveFile(FileName
: string);
49 function ReadInt(Section
, Param
: string; Default
: Integer): Integer;
50 function ReadStr(Section
, Param
: string; Default
: String): string;
51 function ReadBool(Section
, Param
: string; Default
: Boolean): Boolean;
52 function SectionExists(Section
: string): Boolean;
53 procedure WriteInt(Section
, Param
: string; Value
: Integer);
54 procedure WriteStr(Section
, Param
, Value
: string);
55 procedure WriteBool(Section
, Param
: string; Value
: Boolean);
65 constructor TConfig
.Create();
72 constructor TConfig
.CreateFile(FileName
: string);
79 if not FileExists(FileName
) then Exit
;
81 AssignFile(f
, FileName
);
91 constructor TConfig
.CreateMem(pData
: Pointer; _Length
: LongWord);
98 if _Length
= 0 then Exit
;
99 if pData
= nil then Exit
;
101 SetLength(str
, _Length
);
103 CopyMemory(@str
[1], pData
, _Length
);
107 for a
:= 2 to Length(Str
) do
108 if (Str
[a
-1]+Str
[a
] = #13#10) or (a
= Length(Str
)) then
110 if a
<> Length(Str
) then ProcessStr(Copy(Str
, 1, a
-2)) else ProcessStr(Str
);
118 destructor TConfig
.Destroy();
126 procedure TConfig
.FreeConfig();
131 SetLength(FSections
, 1);
133 FCurrentSection
:= 0;
136 procedure TConfig
.ProcessStr(Str
: string);
142 if (Str
<> '') and (Length(Str
) > 2) and (Str
[1] <> ';') then
146 if Pos('=', Str
) > 0 then
148 SetLength(FParams
, Length(FParams
)+1);
150 with FParams
[High(FParams
)] do
153 Param
:= Trim(Copy(Str
, 1, a
-1));
154 Value
:= Trim(Copy(Str
, a
+1, l
));
155 Section
:= FCurrentSection
;
158 else if (Str
[1] = '[') and (Str
[l
] = ']') then
160 SetLength(FSections
, Length(FSections
)+1);
161 FCurrentSection
:= High(FSections
);
162 FSections
[FCurrentSection
] := Trim(Copy(Str
, 2, l
-2));
167 function TConfig
.ReadBool(Section
, Param
: string; Default
: Boolean): Boolean;
171 if Default
then a
:= 1 else a
:= 0;
173 Result
:= StrToIntDef(ReadParam(Section
, Param
, IntToStr(a
)), a
) <> 0;
176 function TConfig
.ReadInt(Section
, Param
: string; Default
: Integer): Integer;
178 Result
:= StrToIntDef(ReadParam(Section
, Param
, IntToStr(Default
)), Default
);
181 function TConfig
.ReadParam(Section
, Param
, Default
: string): string;
190 if FParams
= nil then Exit
;
191 if FSections
= nil then Exit
;
195 for a
:= 0 to High(FSections
) do
196 if LowerCase(FSections
[a
]) = LowerCase(Section
) then
204 p
:= LowerCase(Param
);
206 for a
:= 0 to High(FParams
) do
207 if (FParams
[a
].Section
= s
) and (LowerCase(FParams
[a
].Param
) = p
) then
209 Result
:= FParams
[a
].Value
;
214 function TConfig
.ReadStr(Section
, Param
, Default
: string): string;
216 Result
:= ReadParam(Section
, Param
, Default
);
219 procedure TConfig
.SaveFile(FileName
: string);
225 AssignFile(f
, FileName
);
227 if (FSections
<> nil) or (FParams
<> nil) then
229 if FSections
<> nil then
230 for a
:= 0 to High(FSections
) do
232 if FSections
[a
] <> '' then Writeln(f
, '['+FSections
[a
]+']');
234 if FParams
<> nil then
235 for b
:= 0 to High(FParams
) do
236 if FParams
[b
].Section
= a
then WriteLn(f
, FParams
[b
].Param
+'='+FParams
[b
].Value
);
238 if (a
<> High(FSections
)) and (FSections
[a
] <> '') then WriteLn(f
, '');
244 function TConfig
.SectionExists(Section
: string): Boolean;
250 if FSections
= nil then Exit
;
252 Section
:= LowerCase(Section
);
254 for a
:= 0 to High(FSections
) do
255 if Section
= LowerCase(FSections
[a
]) then
262 procedure TConfig
.WriteBool(Section
, Param
: string; Value
: Boolean);
264 WriteParam(Section
, Param
, BoolToStr(Value
));
267 procedure TConfig
.WriteInt(Section
, Param
: string; Value
: Integer);
269 WriteParam(Section
, Param
, IntToStr(Value
));
272 procedure TConfig
.WriteParam(Section
, Param
, Value
: string);
282 if FSections
<> nil then
283 for a
:= 0 to High(FSections
) do
284 if FSections
[a
] = Section
then
292 SetLength(FSections
, Length(FSections
)+1);
293 a
:= High(FSections
);
294 FSections
[a
] := Section
;
298 if FParams
<> nil then
299 for b
:= 0 to High(FParams
) do
300 if (LowerCase(FParams
[b
].Param
) = LowerCase(Param
)) and (FParams
[b
].Section
= a
) then
306 if ok
then FParams
[b
].Value
:= Value
309 SetLength(FParams
, Length(FParams
)+1);
310 FParams
[High(FParams
)].Param
:= Param
;
311 FParams
[High(FParams
)].Value
:= Value
;
312 FParams
[High(FParams
)].Section
:= a
;
316 procedure TConfig
.WriteStr(Section
, Param
, Value
: string);
318 WriteParam(Section
, Param
, Value
);