DEADSOFTWARE

changed license to GPLv3 only; sorry, no trust to FSF anymore
[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, version 3 of the License ONLY.
6 *
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.
11 *
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/>.
14 *)
15 {$INCLUDE a_modes.inc}
16 unit CONFIGSIMPLE;
18 interface
20 function config_open(FileName: string): Boolean;
21 function config_read_int(param: string; def: Integer): Integer;
22 function config_read_str(param: string; def: string): string;
23 function config_read_bool(param: string; def: Boolean): Boolean;
24 procedure config_close();
26 implementation
28 uses windows;
30 var
31 cfg_data: array of ShortString = nil;
33 function tostr(i: Integer): string;
34 begin
35 Str(i, Result);
36 end;
38 function toint(s: string; var i: Integer): Boolean;
39 var
40 code: Integer;
41 begin
42 Val(s, i, code);
44 Result := code = 0;
45 end;
47 function readparam(param: string; var s: string): Boolean;
48 var
49 a, b, len, d_len: Integer;
50 begin
51 Result := False;
53 if cfg_data = nil then Exit;
55 d_len := Length(cfg_data);
57 for a := 0 to d_len do
58 begin
59 len := Length(cfg_data[a]);
60 if len = 0 then Exit;
62 for b := 1 to len do
63 if cfg_data[a][b] = '=' then
64 if Copy(cfg_data[a], 1, b-1) = param then
65 begin
66 s := Copy(cfg_data[a], b+1, len);
67 Result := True;
68 Exit;
69 end;
70 end;
71 end;
73 function config_open(FileName: string): Boolean;
74 var
75 f: TextFile;
76 str: ShortString;
77 len, d_len, line: Integer;
78 begin
79 Result := False;
81 if cfg_data <> nil then config_close();
83 AssignFile(f, FileName);
85 {$I-}
86 Reset(f);
87 {$I+}
89 if IOResult <> 0 then Exit;
91 d_len := 32;
92 SetLength(cfg_data, d_len);
93 line := 0;
95 while not EOF(f) do
96 begin
97 Readln(f, str);
99 len := Length(str);
100 if len < 3 then Continue;
101 if str[1] = ';' then Continue;
103 if line >= d_len then
104 begin
105 d_len := d_len+32;
106 SetLength(cfg_data, d_len);
107 end;
109 cfg_data[line] := str;
110 line := line+1;
111 end;
113 CloseFile(f);
115 Result := True;
116 end;
118 function config_read_int(param: string; def: Integer): Integer;
119 var
120 s: string;
121 begin
122 Result := def;
124 if not readparam(param, s) then Exit;
126 if not toint(s, Result) then Result := def;
127 end;
129 function config_read_str(param: string; def: string): string;
130 var
131 s: string;
132 begin
133 Result := def;
135 if not readparam(param, s) then Exit;
137 Result := s;
138 end;
140 function config_read_bool(param: string; def: Boolean): Boolean;
141 var
142 s: string;
143 begin
144 Result := def;
146 if not readparam(param, s) then Exit;
148 Result := s <> '0';
149 end;
151 procedure config_close();
152 begin
153 if cfg_data <> nil then cfg_data := nil;
154 end;
157 end.