This repository has been archived by the owner on Apr 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ini.pas
280 lines (239 loc) · 6.24 KB
/
ini.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
unit Ini;
interface
type
PValue = ^TValue;
TValue =
record
key : String[20];
value : String[80];
next : PValue;
end;
PSection = ^TSection;
TSection =
record
name : String[20];
settings : PValue;
next : PSection;
end;
function Equal( s1, s2 : String ) : Boolean;
function LoadIniFile( name : String; var settings : PSection ) : Boolean;
function FindIniString( settings : PSection; section, key, default : String ) : String;
function FindIniNumber( settings : PSection; section, key : String; default : LongInt ) : LongInt;
function FindIniBoolean( settings : PSection; section, key : String; default : Boolean ) : Boolean;
procedure SetIniString( var settings : PSection; section, key, value : String );
procedure SetIniNumber( var settings : PSection; section, key : String; value : LongInt );
procedure SetIniBoolean( var settings : PSection; section, key : String; value : Boolean );
function GetIniValues( settings : PSection; section : String ) : PValue;
procedure WriteIniFile( name : String; settings : PSection );
procedure DisposeSettings( var settings : PSection );
implementation
uses SPX_Fnc;
function Equal( s1, s2 : String ) : Boolean;
begin
s1 := Ups( s1);
s2 := Ups( s2);
Equal := s1 = s2;
end;
procedure AddSetting( var settings : PSection; section, key, value : String );
var h : ^PSection;
v : ^PValue;
begin
h := @settings;
while (h^ <> nil) and not Equal( h^^.name, section) do
h := @h^^.next;
if h^ = nil then
begin
New( h^);
h^^.next := nil;
h^^.name := section;
h^^.settings := nil;
end;
v := @h^^.settings;
while v^ <> nil do
v := @v^^.next;
New( v^);
v^^.key := key;
v^^.value := value;
v^^.next := nil;
end;
procedure SetSetting( var settings : PSection; section, key, value : String );
var h : ^PSection;
v : ^PValue;
begin
h := @settings;
while (h^ <> nil) and not Equal( h^^.name, section) do
h := @h^^.next;
if h^ = nil then
begin
New( h^);
h^^.next := nil;
h^^.name := section;
h^^.settings := nil;
end;
v := @h^^.settings;
while (v^ <> nil) and not Equal( v^^.key, key) do
v := @v^^.next;
if v^ = nil then
begin
New( v^);
v^^.next := nil;
end;
v^^.key := key;
v^^.value := value;
end;
procedure DisposeSettings( var settings : PSection );
var s : PSection;
v : PValue;
begin
while settings <> nil do
begin
s := settings;
settings := s^.next;
while s^.settings <> nil do
begin
v := s^.settings;
s^.settings := v^.next;
Dispose( v);
end;
Dispose( s);
end;
end;
function GetIniValues( settings : PSection; section : String ) : PValue;
begin
while settings <> nil do
begin
if Equal( settings^.name, section) then
begin
GetIniValues := settings^.settings;
Exit;
end;
settings := settings^.next;
end;
GetIniValues := nil;
end;
function FindIniString( settings : PSection; section, key, default : String ) : String;
var v : PValue;
begin
while settings <> nil do
begin
if Equal( settings^.name, section) then
begin
v := settings^.settings;
while v <> nil do
begin
if Equal( v^.key, key) then
begin
FindIniString := v^.value;
Exit;
end;
v := v^.next;
end;
end;
settings := settings^.next;
end;
FindIniString := default;
end;
function FindIniNumber( settings : PSection; section, key : String; default : LongInt ) : LongInt;
var s : String;
x : LongInt;
ignore : Integer;
begin
s := FindIniString( settings, section, key, '');
if s = '' then
FindIniNumber := default
else begin
Val( s, x, ignore);
FindIniNumber := x;
Exit;
end;
end;
function FindIniBoolean( settings : PSection; section, key : String; default : Boolean ) : Boolean;
var s : String;
begin
s := FindIniString( settings, section, key, '');
s := Ups( s);
if s = 'TRUE' then
FindIniBoolean := True
else if s = 'FALSE' then
FindIniBoolean := False
else
FindIniBoolean := default;
end;
procedure SetIniString( var settings : PSection; section, key, value : String );
begin
SetSetting( settings, section, key, value);
end;
procedure SetIniNumber( var settings : PSection; section, key : String; value : LongInt );
begin
SetSetting( settings, section, key, St( value));
end;
procedure SetIniBoolean( var settings : PSection; section, key : String; value : Boolean );
begin
if value then
SetSetting( settings, section, key, 'True')
else
SetSetting( settings, section, key, 'False');
end;
function LoadIniFile( name : String; var settings : PSection ) : Boolean;
var s : String;
section : String;
p : Byte;
f : Text;
begin
LoadIniFile := False;
settings := nil;
Assign( f, name + '.INI');
Reset( f);
if IOResult <> 0 then
Exit;
section := '';
while not Eof( f) do
begin
ReadLn( f, s);
if s[1] = '[' then
begin
p := Pos( ']', s);
if p > 0 then
section := Copy( s, 2, p-2)
else
section := Copy( s, 2, Length( s)-1);
end
else if (s <> '') and (s[1] <> ';') and (Pos( '=', s) > 0) then
begin
while Pos( ' ', s) > 0 do
Delete( s, Pos( ' ', s), 1);
p := Pos( '=', s);
AddSetting( settings, section, Copy( s, 1, p-1), Copy( s, p+1, Length( s)-p));
end
else
AddSetting( settings, section, '', s);
end;
Close( f);
LoadIniFile := settings <> nil;
end;
procedure WriteIniFile( name : String; settings : PSection );
var f : Text;
v : PValue;
begin
Assign( f, name + '.INI');
Rewrite( f);
if IOResult <> 0 then
Exit;
while settings <> nil do
begin
if settings^.name <> '' then
WriteLn( f, '[', settings^.name, ']');
v := settings^.settings;
while v <> nil do
begin
if v^.key = '' then
WriteLn( f, v^.value)
else
WriteLn( f, v^.key, '=', v^.value);
v := v^.next;
end;
settings := settings^.next;
end;
Close( f);
end;
end.