-
Notifications
You must be signed in to change notification settings - Fork 2
/
INIFILES.PAS
359 lines (325 loc) · 7.6 KB
/
INIFILES.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
UNIT
IniFiles;
INTERFACE
USES
Objects;
TYPE
{ Collection of strings like "video=vesa.bgi" }
{ Note, "=" must be directly between key and }
{ value, no blanks allowed }
{ Empty key means insertion of ordinary string }
{ It may be used to implement indexed logfiles }
{ Of course, these lines must not contain
{ strings with "=" or starting with "[". }
PIniSection =^TIniSection;
TIniSection = Object( TCollection )
TheName: PString;
constructor Init( const AName: String );
destructor Done; virtual;
function GetIndexOf( Key: String ): Integer;
function Get( const Key: String ): String;
procedure Put( const Key, Value: String; var Modified: Boolean );
function GetKeyAt( const Index: Integer ): String;
function GetValueAt( const Index: Integer ): String;
function Name: String;
procedure FreeItem( Item: Pointer ); virtual;
procedure Insert( Item: Pointer ); virtual;
constructor Load( var S: TStream );
procedure Store( var S: TStream );
end;
{ Collection of TIniSections }
PIniFile =^TIniFile;
TIniFile = Object( TCollection )
Modified: Boolean;
Name: PString;
constructor Init( FileName: String; var Status: Integer );
destructor Done; virtual;
function Get( const Section, Key: String ): String;
procedure Put( const Section, Key, Value: String );
function GetSection( Section: String ): PIniSection;
end;
IMPLEMENTATION
USES
Advance;
CONST
CrLf: Array [ 0..1 ] of Char = ( #13, #10 );
FUNCTION PStr2Str( P: PString ): String;
begin
If P <> NIL
then PStr2Str := P^
else PStr2Str := ''
end;
FUNCTION ReadString( var Stream: TStream ): String;
var
S: String;
C: Char;
label
Loop, Failure;
begin
S := '';
Loop:
Stream.Read( C, 1 );
If ( C <> #13 ) and ( Stream.Status = stOK ) then begin
S := S + C;
goto Loop;
end;
ReadString := S;
If Stream.Status <> stOK then Failure: begin
Stream.Reset;
Exit;
end;
Stream.Read( C, 1 );
If ( C <> #10 ) or ( Stream.Status <> stOK ) then begin
Stream.Reset;
Stream.Seek( Stream.GetPos - 1 );
end;
end;
{ INI Section
{ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}
CONSTRUCTOR TIniSection.Init;
begin
inherited Init( 5, 5 );
TheName := NewStr( AName );
end;
FUNCTION RemoveLeadSpaces( const S: String ): String;
var I: Integer;
begin
I := 1;
While ( I <= Length( S )) and ( S[ I ] = ' ' ) do Inc( I );
RemoveLeadSpaces := Copy( S, I, 255 );
end;
FUNCTION TruncSpace(S:String): String;
var I: Byte;
begin
I:=Length(S); While S[I]=' ' do DEC(I);
S[0]:=Char(I);
TruncSpace:=S
end;
CONSTRUCTOR TIniSection.Load;
var
Base, L: LongInt;
Str: String;
function Simplify( S: String ): String;
var
I: Integer;
begin
I := Pos( '=', S );
If I <> 0 then begin
Simplify := RemoveLeadSpaces( TruncSpace( Copy( S, 1, I - 1 )))
+ '=' + RemoveLeadSpaces( TruncSpace( Copy( S, I + 1, 255 )));
end else
Simplify := RemoveLeadSpaces( TruncSpace( S ));
end;
label
Loop, Skip;
begin
Base := S.GetPos;
Str := ReadString( S );
If ( Str[ 1 ] = '[' ) and ( Str[ Length( Str ) ] = ']' ) then begin
TheName := NewStr( Copy( Str, 2, Length( Str ) - 2 ));
end else begin
TheName := NIL;
S.Seek( Base );
end;
Items := NIL;
Count := 0;
Limit := 0;
Delta := 5;
Loop:
Str := Simplify( ReadString( S ));
If Str <> '' then begin
Insert( NewStr( Str ));
goto Loop;
end;
Skip:
L := S.GetPos;
Str := ReadString( S );
If Str <> ''
then S.Seek( L )
else
If S.GetPos <> L then goto Skip;
If S.GetPos = Base then begin
inherited Done;
Fail
end;
end;
PROCEDURE TIniSection.FreeItem;
begin
DisposeStr( Item );
end;
PROCEDURE TIniSection.Insert;
begin
If Item <> NIL then inherited Insert( Item );
end;
PROCEDURE TIniSection.Store;
var
Str: String;
procedure DoPutItem( P: PString ); far;
begin
Str := PStr2Str( P ) + #13#10;
S.Write( Str[ 1 ], Length( Str ));
end;
begin
Str := PStr2Str( TheName );
If Str <> '' then begin
Str := '[' + Str + ']' + #13#10;
S.Write( Str[ 1 ], Length( Str ));
end;
ForEach( @DoPutItem );
S.Write( CrLf, SizeOf( CrLf ));
end;
DESTRUCTOR TIniSection.Done;
begin
inherited Done;
DisposeStr( TheName );
end;
FUNCTION TIniSection.Name;
begin
Name := PStr2Str( TheName )
end;
FUNCTION TIniSection.GetIndexOf;
var
P: PString;
S: String;
I: Integer;
function Search( P: PString ): boolean; far;
var
S: String;
I: Integer;
begin
S := UpStrg( PStr2Str( P ));
I := Pos( Key, S );
Search := ( I = 1 ) and ( S[ Length( Key ) + 1 ] = '=' );
end;
begin
Key := UpStrg( Key );
GetIndexOf := IndexOf( FirstThat( @Search ));
end;
FUNCTION TIniSection.Get;
var
I: Integer;
S: String;
begin
I := GetIndexOf( Key );
If I >= 0 then begin
S := PStr2Str( At( I ));
I := Pos( '=', S );
If I > 0 then begin
Get := Copy( S, I + 1, 255 );
Exit
end;
end;
Get := ''
end;
PROCEDURE TIniSection.Put;
var
I: Integer;
S: String;
NewValue: String;
begin
If Key = '' then begin
Insert( NewStr( Value ));
Exit;
end;
I := GetIndexOf( Key );
NewValue := Key + '=' + Value;
If I >= 0 then begin
S := PStr2Str( At( I ));
If S <> NewValue then begin
DisposeStr( At( I ));
AtPut( I, NewStr( NewValue ));
Modified := True;
end;
end else begin
Insert( NewStr( NewValue ));
Modified := True;
end;
end;
FUNCTION TIniSection.GetKeyAt;
var
S: String;
I: Integer;
begin
S := PStr2Str( At( Index ));
I := PosChar( '=', S );
If I = 0
then GetKeyAt := ''
else GetKeyAt := TruncSpace( Copy( S, 1, I - 1 ));
end;
FUNCTION TIniSection.GetValueAt;
var
S: String;
I: Integer;
begin
S := PStr2Str( At( Index ));
I := PosChar( '=', S );
If I = 0
then GetValueAt := ''
else GetValueAt := RemoveLeadSpaces( Copy( S, I + 1, 255 ));
end;
{ INI File
{ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}
CONSTRUCTOR TIniFile.Init;
var
T: TBufStream;
P: PIniSection;
begin
inherited Init( 5, 5 );
Name := NewStr( FileName );
T.Init( FileName, stOpenRead, 512 );
If T.Status = stOK then
repeat
P := New( PIniSection, Load( T ));
If P <> NIL
then Insert( P );
until P = NIL;
Status := T.Status;
T.Done;
end;
DESTRUCTOR TIniFile.Done;
var
T: TBufStream;
procedure DoPutItem( P: PIniSection ); far;
begin
If P <> NIL then P^.Store( T );
end;
begin
If Modified then begin
T.Init( PStr2Str( Name ), stCreate, 512 );
ForEach( @DoPutItem );
T.Done;
end;
DisposeStr( Name );
inherited Done;
end;
FUNCTION TIniFile.GetSection;
function Search( P: PIniSection ): boolean; far;
begin
Search := UpStrg( P^.Name ) = Section
end;
begin
Section := UpStrg( Section );
GetSection := FirstThat( @Search )
end;
FUNCTION TIniFile.Get;
var
P: PIniSection;
begin
P := GetSection( Section );
If P <> NIL
then Get := P^.Get( Key )
else Get := ''
end;
PROCEDURE TIniFile.Put;
var
P: PIniSection;
begin
P := GetSection( Section );
If P = NIL then begin
New( P, Init( Key ));
Insert( P );
Modified := True;
end;
P^.Put( Key, Value, Modified );
end;
END.