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, version 3 of the License ONLY.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 {$INCLUDE a_modes.inc}
32 TConfig
= class{$IFDEF USE_MEMPOOL}(TPoolObject
){$ENDIF}
34 FParams
: array of TParam
;
35 FSections
: array of ShortString;
36 FCurrentSection
: Word;
37 function ReadParam(Section
, Param
, Default
: string): string;
38 procedure WriteParam(Section
, Param
, Value
: string);
39 procedure ProcessStr(Str
: string);
42 constructor CreateFile(FileName
: string);
43 constructor CreateMem(pData
: Pointer; _Length
: LongWord);
44 destructor Destroy(); override;
45 procedure FreeConfig();
46 procedure SaveFile(FileName
: string);
47 function ReadInt(Section
, Param
: string; Default
: Integer): Integer;
48 function ReadStr(Section
, Param
: string; Default
: String): string;
49 function ReadBool(Section
, Param
: string; Default
: Boolean): Boolean;
50 function SectionExists(Section
: string): Boolean;
51 procedure WriteInt(Section
, Param
: string; Value
: Integer);
52 procedure WriteStr(Section
, Param
, Value
: string);
53 procedure WriteBool(Section
, Param
: string; Value
: Boolean);
63 constructor TConfig
.Create();
70 constructor TConfig
.CreateFile(FileName
: string);
77 if not FileExists(FileName
) then Exit
;
79 AssignFile(f
, FileName
);
89 constructor TConfig
.CreateMem(pData
: Pointer; _Length
: LongWord);
96 if _Length
= 0 then Exit
;
97 if pData
= nil then Exit
;
99 SetLength(str
, _Length
);
101 CopyMemory(@str
[1], pData
, _Length
);
105 for a
:= 2 to Length(Str
) do
106 if (Str
[a
-1]+Str
[a
] = #13#10) or (a
= Length(Str
)) then
108 if a
<> Length(Str
) then ProcessStr(Copy(Str
, 1, a
-2)) else ProcessStr(Str
);
116 destructor TConfig
.Destroy();
124 procedure TConfig
.FreeConfig();
129 SetLength(FSections
, 1);
131 FCurrentSection
:= 0;
134 procedure TConfig
.ProcessStr(Str
: string);
140 if (Str
<> '') and (Length(Str
) > 2) and (Str
[1] <> ';') then
144 if Pos('=', Str
) > 0 then
146 SetLength(FParams
, Length(FParams
)+1);
148 with FParams
[High(FParams
)] do
151 Param
:= Trim(Copy(Str
, 1, a
-1));
152 Value
:= Trim(Copy(Str
, a
+1, l
));
153 Section
:= FCurrentSection
;
156 else if (Str
[1] = '[') and (Str
[l
] = ']') then
158 SetLength(FSections
, Length(FSections
)+1);
159 FCurrentSection
:= High(FSections
);
160 FSections
[FCurrentSection
] := Trim(Copy(Str
, 2, l
-2));
165 function TConfig
.ReadBool(Section
, Param
: string; Default
: Boolean): Boolean;
169 if Default
then a
:= 1 else a
:= 0;
171 Result
:= StrToIntDef(ReadParam(Section
, Param
, IntToStr(a
)), a
) <> 0;
174 function TConfig
.ReadInt(Section
, Param
: string; Default
: Integer): Integer;
176 Result
:= StrToIntDef(ReadParam(Section
, Param
, IntToStr(Default
)), Default
);
179 function TConfig
.ReadParam(Section
, Param
, Default
: string): string;
188 if FParams
= nil then Exit
;
189 if FSections
= nil then Exit
;
193 for a
:= 0 to High(FSections
) do
194 if LowerCase(FSections
[a
]) = LowerCase(Section
) then
202 p
:= LowerCase(Param
);
204 for a
:= 0 to High(FParams
) do
205 if (FParams
[a
].Section
= s
) and (LowerCase(FParams
[a
].Param
) = p
) then
207 Result
:= FParams
[a
].Value
;
212 function TConfig
.ReadStr(Section
, Param
, Default
: string): string;
214 Result
:= ReadParam(Section
, Param
, Default
);
217 procedure TConfig
.SaveFile(FileName
: string);
223 AssignFile(f
, FileName
);
225 if (FSections
<> nil) or (FParams
<> nil) then
227 if FSections
<> nil then
228 for a
:= 0 to High(FSections
) do
230 if FSections
[a
] <> '' then Writeln(f
, '['+FSections
[a
]+']');
232 if FParams
<> nil then
233 for b
:= 0 to High(FParams
) do
234 if FParams
[b
].Section
= a
then WriteLn(f
, FParams
[b
].Param
+'='+FParams
[b
].Value
);
236 if (a
<> High(FSections
)) and (FSections
[a
] <> '') then WriteLn(f
, '');
242 function TConfig
.SectionExists(Section
: string): Boolean;
248 if FSections
= nil then Exit
;
250 Section
:= LowerCase(Section
);
252 for a
:= 0 to High(FSections
) do
253 if Section
= LowerCase(FSections
[a
]) then
260 procedure TConfig
.WriteBool(Section
, Param
: string; Value
: Boolean);
262 WriteParam(Section
, Param
, BoolToStr(Value
));
265 procedure TConfig
.WriteInt(Section
, Param
: string; Value
: Integer);
267 WriteParam(Section
, Param
, IntToStr(Value
));
270 procedure TConfig
.WriteParam(Section
, Param
, Value
: string);
280 if FSections
<> nil then
281 for a
:= 0 to High(FSections
) do
282 if FSections
[a
] = Section
then
290 SetLength(FSections
, Length(FSections
)+1);
291 a
:= High(FSections
);
292 FSections
[a
] := Section
;
296 if FParams
<> nil then
297 for b
:= 0 to High(FParams
) do
298 if (LowerCase(FParams
[b
].Param
) = LowerCase(Param
)) and (FParams
[b
].Section
= a
) then
304 if ok
then FParams
[b
].Value
:= Value
307 SetLength(FParams
, Length(FParams
)+1);
308 FParams
[High(FParams
)].Param
:= Param
;
309 FParams
[High(FParams
)].Value
:= Value
;
310 FParams
[High(FParams
)].Section
:= a
;
314 procedure TConfig
.WriteStr(Section
, Param
, Value
: string);
316 WriteParam(Section
, Param
, Value
);