-
Notifications
You must be signed in to change notification settings - Fork 2
/
IniHandler.pas
468 lines (395 loc) · 12.9 KB
/
IniHandler.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
{*
IniHandler.pas - ini configuration file management
--------------
Begin: 2005/03/01
Last revision: $Date: 2011-10-25 04:40:49 $ $Author: areeves $
Version number: $Revision: 1.16 $
Code Documentation Tags: Begin 2009-08-13, Last Revision: 2009-08-13
Project: APHI General Purpose Delphi Library
Website: http://www.naadsm.org/opensource/delphi
Author: Aaron Reeves <Aaron.Reeves@colostate.edu>
Author: Aaron Reeves <aaron@aaronreeves.com>
--------------------------------------------------
Copyright (C) 2005 - 2011 Animal Population Health Institute, Colorado State University
Modifications Copyright (C) 2011 Gryphon Scientific
-
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
}
(*
Documentation generation tags begin with {* or ///
Replacing these with (* or // foils the documentation generator
*)
unit IniHandler;
interface
uses
Variants,
CStringList,
QStringMaps
;
type
/// For maintainting Windows initialization (ini) files
TIniHandler = class
protected
sections: TQStringObjectMap; /// Holds additional instances of TQStringVariantMap, one for each section of the INI file
iniSettings: TQStringVariantMap; /// holds name-value pairs from input ini file
fileName: string; /// holds the ini path and filename
function isComment( const str: string ): boolean;
function isSection( const str: string ): boolean;
function extractSectionName( const str: string ): string;
procedure makeIniSettings( iniLines: TCStringList );
public
constructor create( const iniFileName: string );
destructor destroy(); override;
function section( const name: string ): TQStringVariantMap;
function val( const key: string; const section: string = '' ): variant;
function str( const key: string; const section: string = '' ): string;
function hasKey( const key: string ): boolean;
procedure update( const key: string; const val: variant );
procedure writeFile();
procedure debug();
end
;
implementation
uses
SysUtils,
StrUtils,
MyStrUtils,
DebugWindow
;
CONST DBSHOWMSG = false; /// Set to true to enable debugging messages for this unit
//-----------------------------------------------------------------------------
// Construction/initialization/destruction
//-----------------------------------------------------------------------------
{*
Creates ini file management object, filled with parameters from iniFileName
@param iniFileName Path and ini filename
@comment iniFileName must exist
}
constructor TIniHandler.create( const iniFileName: string );
var
iniFile: TextFile;
tmp: string;
iniLines: TCStringList;
begin
sections := TQStringObjectMap.create();
iniSettings := TQStringVariantMap.create();
fileName := iniFileName;
if( not( fileExists( fileName ) ) )then
begin
exit;
end
else
begin
iniLines := TCStringList.create();
try
try
assignFile( iniFile, iniFileName );
reset( iniFile );
while not( eof( iniFile ) ) do
begin
readln( iniFile, tmp );
iniLines.append( tmp );
end
;
closeFile( iniFile );
makeIniSettings( iniLines );
except
// fail silently
end;
finally
freeAndNil( iniLines );
end;
end
;
end
;
/// Frees memory of the object
destructor TIniHandler.destroy();
var
i: integer;
map: TQStringVariantMap;
begin
for i := 0 to sections.count - 1 do
begin
map := sections.itemAtIndex( i ) as TQStringVariantMap;
map.Free();
end
;
freeAndNil( sections );
freeAndNil( iniSettings );
end
;
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Read functions
//-----------------------------------------------------------------------------
function TIniHandler.section( const name: string ): TQStringVariantMap;
begin
result := sections[name] as TQStringVariantMap;
end
;
{*
Returns the value for key
@param key Name of field
@return Value for key, if not found then null is returned
@comment The key and value are also sent to dbcout
@comment Note that a variant is being returned, caller beware!
}
function TIniHandler.val( const key: string; const section: string = '' ): variant;
var
map: TQStringVariantMap;
begin
if( strIsEmpty( section ) ) then
map := iniSettings
else
map := sections[section] as TQStringVariantMap
;
if( nil = map ) then
result := null
else
begin
if( map.contains( key ) ) then
result := map[key]
else
result := null
;
end
;
dbcout( key, DBSHOWMSG );
dbcout( varToStr( result ), DBSHOWMSG );
end
;
{*
Returns the value for key as a string
@param key Name of field
@return Text value for key, if not found then null is returned
}
function TIniHandler.str( const key: string; const section: string = '' ): string;
var
v: variant;
begin
v := val( key, section );
if( null = v ) then
result := ''
else
result := string( v )
;
end
;
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Write functions
//-----------------------------------------------------------------------------
{*
Updates setting key with val
@param key Field name
@param val Value to be set
@comment Currently no checking that key exists
}
procedure TIniHandler.update( const key: string; const val: variant );
begin
//if( iniSettings.HasKey( key ) ) then iniSettings.Delete( key );
iniSettings[key] := val;
//showMessage( iniSettings[key] );
end
;
/// Writes the ini file from the iniSettings list to disk
procedure TIniHandler.writeFile();
var
iniFile: textfile;
i, j: integer;
s: string;
v: variant;
map: TQStringVariantMap;
sectionName: string;
begin
try
dbcout( 'Writing ini file ' + fileName, DBSHOWMSG );
assignFile( iniFile, fileName );
rewrite( iniFile );
for i := 0 to (iniSettings.Count-1) do
begin
s := iniSettings.keyAtIndex( i );
v := iniSettings.itemAtIndex( i );
if( ( null <> v ) and ( not( strIsEmpty( string( v ) ) ) ) ) then
s := s + ' = ' + string( iniSettings.itemAtIndex( i ) )
;
writeln( iniFile, s );
end
;
for j := 0 to sections.count - 1 do
begin
writeln( iniFile, endl );
sectionName := sections.keyAtIndex( j );
map := sections.itemAtIndex( j ) as TQStringVariantMap;
writeln( iniFile, '[' + sectionName + ']' );
for i := 0 to map.count - 1 do
begin
s := map.keyAtIndex( i );
v := map.itemAtIndex( i );
if( null <> v ) and ( not( strIsEmpty( string( v ) ) ) ) then
s := s + ' = ' + string( map.itemAtIndex( i ) )
;
writeln( iniFile, s );
end
;
end
;
closeFile( iniFile );
except
// fail silently. writing an INI file isn't worth it.
end;
end
;
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Reading functions
//-----------------------------------------------------------------------------
{*
Searches the ini file for fieldname key
@param key Configuration setting (field) name
@return True if key is located, else false
}
function TIniHandler.hasKey( const key: string ): boolean;
begin
if( not( iniSettings.contains( key ) ) ) then
result := false
else
begin
if( iniSettings[key] = null ) then
result := false
else
result := true
;
end
;
end
;
function TIniHandler.isComment( const str: string ): boolean;
begin
// Assumes format "# This is a comment"
result := ( '#' = charAt( str, 0 ) );
end
;
function TIniHandler.isSection( const str: string ): boolean;
begin
// Assumes format "[sectionName]"
result := ( '[' = charAt( str, 0 ) );
end
;
function TIniHandler.extractSectionName( const str: string ): string;
begin
// Assumes format "[sectionName]"
result := leftStr( str, length( str ) - 1 );
result := rightStr( result, length( result ) - 1 );
end
;
{*
Worker method for TIniHandler constructor to fill member iniSettings from
and input ini file. iniLines contains all the lines of the ini file. This
method skips the section names and epmty lines, creating name-value pairs
for iniSettings list. Also outputs (to dbcout) the contents of iniSettings
as it is populated.
@param iniLines Contents of an ini file
}
procedure TIniHandler.makeIniSettings( iniLines: TCStringList );
var
p: PChar;
str: string;
line: TCStringList;
sectionSettings: TQStringVariantMap;
sectionName: string;
begin
line := TCStringList.create();
p := iniLines.first();
// Until the first section heading is encountered, everything goes into iniSettings
// (This is the "default" or "main" section.)
sectionSettings := iniSettings;
while( nil <> p ) do
begin
str := trim( p );
dbcout( str, DBSHOWMSG );
if( ( not( strIsEmpty( str ) ) ) and ( not( isComment( str ) ) ) ) then
begin
if( isSection( str ) ) then
begin
sectionName := extractSectionName( str );
sectionSettings := TQStringVariantMap.create();
sections.insert( sectionName, sectionSettings );
end
else
begin
// Assumes format "key" or "key = value".
// If "key" only, a null variant is inserted into the map with that key.
// Otherwise, a key/value pair is inserted into the map.
if( 0 = ansiPos( '=', str ) ) then
sectionSettings[trim(str)] := null
else
begin
line.clear();
dbcout( 'STR: ' + str, DBSHOWMSG );
line.explode( str, '=' );
dbcout( 'LINE:', DBSHOWMSG );
if( DBSHOWMSG ) then
line.debug()
;
sectionSettings[trim(line.first())] := trim( line.last() );
end
;
end
;
end
;
p := iniLines.next();
end
;
line.Clear();
line.Free();
end
;
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Debugging
//-----------------------------------------------------------------------------
procedure TIniHandler.debug();
var
j: integer;
section: string;
m: TQStringVariantMap;
procedure mapDebug( map: TQStringVariantMap );
var
i: integer;
key, val: string;
begin
for i := 0 to map.count - 1 do
begin
key := map.keyAtIndex( i );
val := string( map[ key ] );
if( strIsEmpty( val ) ) then
val := '(null value)'
;
dbcout( 'Key: ' + key + ', value: ' + val, true );
end
;
end
;
begin
dbcout( endl + '=== begin INI debug', true );
dbcout( 'Main section:', true );
mapDebug( iniSettings );
for j := 0 to sections.count - 1 do
begin
dbcout( endl, true );
section := sections.keyAtIndex( j );
dbcout( 'Section "' + section + '"', true );
m := sections[section] as TQStringVariantMap;
mapDebug( m );
end
;
dbcout( '=== end INI debug', true );
end
;
//-----------------------------------------------------------------------------
end.