DEADSOFTWARE

4af11dec609e83d14efa55d57f7343d46828ac99
[d2df-sdl.git] / src / shared / CONFIGSIMPLE.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
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.
7 *
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.
12 *
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/>.
15 *)
16 {$INCLUDE a_modes.inc}
17 unit CONFIGSIMPLE;
19 interface
21 function config_open(FileName: string): Boolean;
22 function config_read_int(param: string; def: Integer): Integer;
23 function config_read_str(param: string; def: string): string;
24 function config_read_bool(param: string; def: Boolean): Boolean;
25 procedure config_close();
27 implementation
29 uses windows;
31 var
32 cfg_data: array of ShortString = nil;
34 function tostr(i: Integer): string;
35 begin
36 Str(i, Result);
37 end;
39 function toint(s: string; var i: Integer): Boolean;
40 var
41 code: Integer;
42 begin
43 Val(s, i, code);
45 Result := code = 0;
46 end;
48 function readparam(param: string; var s: string): Boolean;
49 var
50 a, b, len, d_len: Integer;
51 begin
52 Result := False;
54 if cfg_data = nil then Exit;
56 d_len := Length(cfg_data);
58 for a := 0 to d_len do
59 begin
60 len := Length(cfg_data[a]);
61 if len = 0 then Exit;
63 for b := 1 to len do
64 if cfg_data[a][b] = '=' then
65 if Copy(cfg_data[a], 1, b-1) = param then
66 begin
67 s := Copy(cfg_data[a], b+1, len);
68 Result := True;
69 Exit;
70 end;
71 end;
72 end;
74 function config_open(FileName: string): Boolean;
75 var
76 f: TextFile;
77 str: ShortString;
78 len, d_len, line: Integer;
79 begin
80 Result := False;
82 if cfg_data <> nil then config_close();
84 AssignFile(f, FileName);
86 {$I-}
87 Reset(f);
88 {$I+}
90 if IOResult <> 0 then Exit;
92 d_len := 32;
93 SetLength(cfg_data, d_len);
94 line := 0;
96 while not EOF(f) do
97 begin
98 Readln(f, str);
100 len := Length(str);
101 if len < 3 then Continue;
102 if str[1] = ';' then Continue;
104 if line >= d_len then
105 begin
106 d_len := d_len+32;
107 SetLength(cfg_data, d_len);
108 end;
110 cfg_data[line] := str;
111 line := line+1;
112 end;
114 CloseFile(f);
116 Result := True;
117 end;
119 function config_read_int(param: string; def: Integer): Integer;
120 var
121 s: string;
122 begin
123 Result := def;
125 if not readparam(param, s) then Exit;
127 if not toint(s, Result) then Result := def;
128 end;
130 function config_read_str(param: string; def: string): string;
131 var
132 s: string;
133 begin
134 Result := def;
136 if not readparam(param, s) then Exit;
138 Result := s;
139 end;
141 function config_read_bool(param: string; def: Boolean): Boolean;
142 var
143 s: string;
144 begin
145 Result := def;
147 if not readparam(param, s) then Exit;
149 Result := s <> '0';
150 end;
152 procedure config_close();
153 begin
154 if cfg_data <> nil then cfg_data := nil;
155 end;
158 end.