DEADSOFTWARE

changed some backslashes to forward slashes
[d2df-editor.git] / src / shared / CONFIGSIMPLE.pas
1 unit CONFIGSIMPLE;
3 interface
5 function config_open(FileName: string): Boolean;
6 function config_read_int(param: string; def: Integer): Integer;
7 function config_read_str(param: string; def: string): string;
8 function config_read_bool(param: string; def: Boolean): Boolean;
9 procedure config_close();
11 implementation
13 uses windows;
15 var
16 cfg_data: array of ShortString = nil;
18 function tostr(i: Integer): string;
19 begin
20 Str(i, Result);
21 end;
23 function toint(s: string; var i: Integer): Boolean;
24 var
25 code: Integer;
26 begin
27 Val(s, i, code);
29 Result := code = 0;
30 end;
32 function readparam(param: string; var s: string): Boolean;
33 var
34 a, b, len, d_len: Integer;
35 begin
36 Result := False;
38 if cfg_data = nil then Exit;
40 d_len := Length(cfg_data);
42 for a := 0 to d_len do
43 begin
44 len := Length(cfg_data[a]);
45 if len = 0 then Exit;
47 for b := 1 to len do
48 if cfg_data[a][b] = '=' then
49 if Copy(cfg_data[a], 1, b-1) = param then
50 begin
51 s := Copy(cfg_data[a], b+1, len);
52 Result := True;
53 Exit;
54 end;
55 end;
56 end;
58 function config_open(FileName: string): Boolean;
59 var
60 f: TextFile;
61 str: ShortString;
62 len, d_len, line: Integer;
63 begin
64 Result := False;
66 if cfg_data <> nil then config_close();
68 AssignFile(f, findFileCIStr(FileName));
70 {$I-}
71 Reset(f);
72 {$I+}
74 if IOResult <> 0 then Exit;
76 d_len := 32;
77 SetLength(cfg_data, d_len);
78 line := 0;
80 while not EOF(f) do
81 begin
82 Readln(f, str);
84 len := Length(str);
85 if len < 3 then Continue;
86 if str[1] = ';' then Continue;
88 if line >= d_len then
89 begin
90 d_len := d_len+32;
91 SetLength(cfg_data, d_len);
92 end;
94 cfg_data[line] := str;
95 line := line+1;
96 end;
98 CloseFile(f);
100 Result := True;
101 end;
103 function config_read_int(param: string; def: Integer): Integer;
104 var
105 s: string;
106 begin
107 Result := def;
109 if not readparam(param, s) then Exit;
111 if not toint(s, Result) then Result := def;
112 end;
114 function config_read_str(param: string; def: string): string;
115 var
116 s: string;
117 begin
118 Result := def;
120 if not readparam(param, s) then Exit;
122 Result := s;
123 end;
125 function config_read_bool(param: string; def: Boolean): Boolean;
126 var
127 s: string;
128 begin
129 Result := def;
131 if not readparam(param, s) then Exit;
133 Result := s <> '0';
134 end;
136 procedure config_close();
137 begin
138 if cfg_data <> nil then cfg_data := nil;
139 end;
142 end.