DEADSOFTWARE

put "{$MODE ...}" directive in each source file; removed trailing spaces, and convert...
[d2df-sdl.git] / src / shared / CONFIGSIMPLE.pas
1 {$MODE DELPHI}
2 unit CONFIGSIMPLE;
4 interface
6 function config_open(FileName: string): Boolean;
7 function config_read_int(param: string; def: Integer): Integer;
8 function config_read_str(param: string; def: string): string;
9 function config_read_bool(param: string; def: Boolean): Boolean;
10 procedure config_close();
12 implementation
14 uses windows;
16 var
17 cfg_data: array of ShortString = nil;
19 function tostr(i: Integer): string;
20 begin
21 Str(i, Result);
22 end;
24 function toint(s: string; var i: Integer): Boolean;
25 var
26 code: Integer;
27 begin
28 Val(s, i, code);
30 Result := code = 0;
31 end;
33 function readparam(param: string; var s: string): Boolean;
34 var
35 a, b, len, d_len: Integer;
36 begin
37 Result := False;
39 if cfg_data = nil then Exit;
41 d_len := Length(cfg_data);
43 for a := 0 to d_len do
44 begin
45 len := Length(cfg_data[a]);
46 if len = 0 then Exit;
48 for b := 1 to len do
49 if cfg_data[a][b] = '=' then
50 if Copy(cfg_data[a], 1, b-1) = param then
51 begin
52 s := Copy(cfg_data[a], b+1, len);
53 Result := True;
54 Exit;
55 end;
56 end;
57 end;
59 function config_open(FileName: string): Boolean;
60 var
61 f: TextFile;
62 str: ShortString;
63 len, d_len, line: Integer;
64 begin
65 Result := False;
67 if cfg_data <> nil then config_close();
69 AssignFile(f, FileName);
71 {$I-}
72 Reset(f);
73 {$I+}
75 if IOResult <> 0 then Exit;
77 d_len := 32;
78 SetLength(cfg_data, d_len);
79 line := 0;
81 while not EOF(f) do
82 begin
83 Readln(f, str);
85 len := Length(str);
86 if len < 3 then Continue;
87 if str[1] = ';' then Continue;
89 if line >= d_len then
90 begin
91 d_len := d_len+32;
92 SetLength(cfg_data, d_len);
93 end;
95 cfg_data[line] := str;
96 line := line+1;
97 end;
99 CloseFile(f);
101 Result := True;
102 end;
104 function config_read_int(param: string; def: Integer): Integer;
105 var
106 s: string;
107 begin
108 Result := def;
110 if not readparam(param, s) then Exit;
112 if not toint(s, Result) then Result := def;
113 end;
115 function config_read_str(param: string; def: string): string;
116 var
117 s: string;
118 begin
119 Result := def;
121 if not readparam(param, s) then Exit;
123 Result := s;
124 end;
126 function config_read_bool(param: string; def: Boolean): Boolean;
127 var
128 s: string;
129 begin
130 Result := def;
132 if not readparam(param, s) then Exit;
134 Result := s <> '0';
135 end;
137 procedure config_close();
138 begin
139 if cfg_data <> nil then cfg_data := nil;
140 end;
143 end.