inifile.anubis
18.2 KB
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
*Project* The Anubis Project
*Title* ini file parser
*Copyright* Copyright (c) David René 2006.
*Created: 2006 02 22
*Author* David René
*Status* Released
*Overview*
This file contain some helper functions for the management of well know Windows(tm)
INI files.
read tools/streams.anubis
read tools/basis.anubis
read tools/base64.anubis
read system/string.anubis
type IniValType:
bool ,
int32 ,
float ,
string ,
b64 .
*descritpion*
different types of line can be in the inifile. Each alternative of the type
contain more information about the value.
For example section_header(String) contain the name of the section just found
type IniLine:
eof,
blank,
section_header(String name), //section with his name
variable(String name, String value), //variable with his name and value in string format
variable_base64(String name), //variable base64 with his name
comment(String). //comment line
*description*
This type, record the variable contain in section. This type only have the name
of variable, the value in string format and the real type of variable if is
known
type IniVar:
iniVar(String name,
Var(String) value,
IniValType type).
type IniSection:
iniSection(String name,
Var(List(IniVar)) content).
public type IniInfo:
iniInfo( String fileName,
Var(List(IniSection)) content,
Var(IniSection) sectionCache ).
public define IniInfo openIniFile(RStream file).
public define IniInfo openIniFile(String file).
public define Bool readBool (IniInfo i, String section, String variable, Bool default).
public define One writeBool (IniInfo i, String section, String variable, Bool value).
define String bool_to_ini_string (Bool bool) =
if bool is false
then "0"
else "1".
define One putSectionInCache( IniInfo i, IniSection section) =
sectionCache(i) <- section
.
define IniSection getSectionInCache( IniInfo i) =
*sectionCache(i).
*Name* isVariable
/** Description*
check if the given variable var exist in the given sectionName
* @param sectionName section which must contain the variable
* @param var name of searched variable
* @return true if the variable exist in the given section otherwise false
*/
public define Bool isVariable(IniInfo i, String sectionName, String var).
* Permet de verifier si une section existe dans le fichier INI associee l'objet.
* @param sectionName section dont on cherche l'existence dans le fichier.
* @return true si la section demande existe sinon false
define Bool isSection( List(IniSection) sectionList, String sectionName) =
if sectionList is
{
[] then false, //the list is empty, the section doesn't exist
[h . t] then if h is iniSection(name, _) then //print("inspecting " +name +" \n");
if name = sectionName
then true
else isSection(t, sectionName)
}.
public define Bool isSection(Var(List(IniSection)) sectionList, String sectionName) =
isSection(*sectionList, sectionName).
* Allow section name modification
* @param oldSectionName original section Name to be replaced
* @param newSectionName new section Name
public define One setSectionName(IniInfo i, String oldSectionName, String newSectionName).
* Delete a complete section with all the subsections.
public define One deleteSection (IniInfo i, String section).
* Delete a variable in a section
public define One deleteVar(IniInfo i, String section, String var).
define List(IniSection) deleteSection (IniInfo i, List(IniSection) sectionList, String sectionName) =
if sectionList is
{
[] then [],
[h . t ] then
if h.name = sectionName then
t
else
[ h . deleteSection(i, t, sectionName)]
}.
public define One deleteSection (IniInfo i, String sectionName) =
content(i) <- deleteSection(i, *content(i), sectionName).
define Maybe(IniSection) getSection( IniInfo ini, List(IniSection) sectionList, String sectionName) =
if sectionList is
{
[] then failure,
[h . t] then if h is iniSection(name, _) then
if name = sectionName
then putSectionInCache(ini, h); success(h)
else getSection(ini, t, sectionName)
}.
define IniSection addSection (IniInfo ini, String sectionName) =
with newSection = iniSection(sectionName, var([])),
content(ini) <- [newSection . *content(ini)];
putSectionInCache(ini, newSection);
newSection
.
define Maybe(IniSection) isInCache(IniInfo i, String sectionName) =
if name(getSectionInCache(i)) = sectionName then
success(getSectionInCache(i))
else failure
.
public define IniSection getSection(IniInfo i, String sectionName) =
//try to read the section in cache
if isInCache(i , sectionName) is
{
failure then if getSection(i, *content(i), sectionName) is
{
failure then
with newSection = addSection(i, sectionName),
putSectionInCache(i, newSection) ; newSection,
success(section) then putSectionInCache(i, section); section
}
success(section) then section
}.
public define One addVar (IniInfo i, String section, String varName, String val, IniValType type) =
if getSection(i, section) is iniSection(name, varList)
then
varList <- [ iniVar(varName , var(val), type) . *varList]
.
define Maybe(IniVar) getIniVar(List(IniVar) varList, String varName) =
if varList is
{
[] then failure,
[h . t] then
if h is iniVar(name, value, type) then
if name = varName
then success(h)
else getIniVar( t , varName)
}.
define Maybe(IniVar) getVar(List(IniVar) varList, String varName) =
if varList is
{
[] then failure,
[h . t] then
if h is iniVar(name, value, type) then
if insensitive_equal(name, varName)
then success(h)
else getVar( t , varName)
}.
public define Maybe(IniVar) getVar (IniInfo i, String section, String varName) =
if getSection(i, section) is iniSection(name, varList)
then getVar( *varList, varName).
public define IniInfo createIniFile( String fileName) =
iniInfo(fileName, var([]), var(iniSection("N/A",var([])))).
define Maybe(Int) findChar(String string, Word8 wantedChar, Int start) =
if nth(start, string) is
{
failure then failure,
success(c) then
if c = wantedChar then success(start)
else findChar(string, wantedChar, start + 1)
}.
define String parseSectionName (String line) =
if findChar(line, ']', 1) is
{
failure then ""
success(idx) then
if sub_string(line, 1, idx-1) is
{
failure then "",
success(section) then section
}
}
.
define IniLine parseVarLine (String line) =
if findChar(line, '=', 0) is
{
failure then blank
success(idx) then
if sub_string(line, 0, idx) is
{
failure then blank
success(valName) then
if sub_string(line, idx+1, (length(line) - (idx+1))) is
{
failure then blank
success(value) then
if value = "@BEGIN base64 - Don't Edit@" then
variable_base64(valName)
else
variable(valName, value)
}
}
}
.
define IniLine parseIniLine (String line) =
if nth((Int)0,line) is
{
failure then blank,
success(c) then
if c = '#' | c = ';'
then comment(line)
else if c = '['
then section_header(parseSectionName(line))
else parseVarLine(line)
}.
define IniInfo reverseAll(IniInfo ini) =
map_forget( (IniSection s) |-> with v = content(s), v <- reverse(*v), *content(ini));
with v = content(ini), v <- reverse(*v); ini.
define Maybe(String) parseBase64Line(Stream s, String content ) =
if read_line(s) is
{
failure then failure,
success(line) then
if line = "@END base64@" then
success(content)
else
parseBase64Line(s, content +"\n"+line)
}.
define IniInfo parseIniFile( Stream s, IniInfo ini, String sectionName) =
if read_line(s) is
{
failure then reverseAll(ini),
success(line) then
if parseIniLine(line) is
{
eof then ini,
blank then parseIniFile(s , ini, sectionName),
section_header(newSectionName) then forget(addSection(ini, newSectionName)); parseIniFile( s, ini, newSectionName),
variable(varName, value) then
if sectionName = "" then
print("variable " + varName + "is without section\n");
parseIniFile(s , ini, sectionName)
else addVar(ini , sectionName, varName , value, string);
parseIniFile(s , ini, sectionName),
variable_base64(varName) then
if parseBase64Line(s ,"") is
{
failure then parseIniFile(s, ini, sectionName),
success(value) then addVar(ini , sectionName, varName , value, b64);parseIniFile(s, ini, sectionName),
}
comment( _0) then parseIniFile(s , ini, sectionName)
}
}.
public define IniInfo loadIniFile( String fileName) =
if (Maybe(RStream))file(fileName, read) is
{
failure then createIniFile(fileName),
success(f) then parseIniFile(make_stream(f), createIniFile(fileName), "")
}.
public define Bool readBool (IniInfo i, String section, String varName, Bool default)=
if getVar(i, section, varName) is
{
failure then default, //this value doesn't exist, then return the default value
success((IniVar) varValue) then
if *value(varValue) = "0" | //check for 0 value, that mean is false
*value(varValue) = "f" | //check for f value i.e. "false", that mean is false
*value(varValue) = "F" |
*value(varValue) = "n" | //check for n value i.e. "no", that mean is false
*value(varValue) = "N"
then false
else true
}.
public define One writeBool (IniInfo i, String wantedSection, String varName, Bool newValue) =
if getSection(i, wantedSection) is iniSection(String name,Var(List(IniVar)) content) then
if getIniVar( *content, varName) is
{
failure then addVar(i, wantedSection, varName, bool_to_ini_string(newValue), (IniValType)bool),
success( (IniVar) varValue ) then value(varValue) <- bool_to_ini_string(newValue)
}.
public define Word32 readWord32 (IniInfo i, String section, String variable, Word32 default)=
if getVar(i, section, variable) is
{
failure then default, //this value doesn't exist, then return the default value
success((IniVar) varValue) then
if decimal_scan(*value(varValue)) is //try to convert the string into real int value
{
failure then default, //it's not convertible then return the default value
success(intValue) then truncate_to_Word32(intValue) //finally return the real integer
}
}.
public define One writeWord32 (IniInfo i, String wantedSection, String varName, Word32 newValue) =
if getSection(i, wantedSection) is iniSection(String name,Var(List(IniVar)) content) then
if getIniVar( *content, varName) is
{
failure then addVar(i, wantedSection, varName, to_decimal(newValue), (IniValType)int32),
success( (IniVar) varValue ) then value(varValue) <- to_decimal(newValue)
}.
public define Int readInt (IniInfo i, String section, String variable, Int default)=
if getVar(i, section, variable) is
{
failure then default, //this value doesn't exist, then return the default value
success((IniVar) varValue) then
if decimal_scan(*value(varValue)) is //try to convert the string into real int value
{
failure then default, //it's not convertible then return the default value
success(intValue) then intValue //finally return the real integer
}
}.
public define One writeInt (IniInfo i, String wantedSection, String varName, Int newValue) =
if getSection(i, wantedSection) is iniSection(String name,Var(List(IniVar)) content) then
if getIniVar( *content, varName) is
{
failure then addVar(i, wantedSection, varName, to_decimal(newValue), (IniValType)int32),
success( (IniVar) varValue ) then value(varValue) <- to_decimal(newValue)
}.
public define Float readFloat (IniInfo i, String section, String variable, Float default)=
if getVar(i, section, variable) is
{
failure then default, //this value doesn't exist, then return the default value
success((IniVar) varValue) then
if string_to_float(*value(varValue)) is //try to convert the string into real int value
{
failure then default, //it's not convertible then return the default value
success(intValue) then intValue //finally return the real integer
}
}.
public define One writeFloat (IniInfo i, String wantedSection, String varName, Float newValue) =
if getSection(i, wantedSection) is iniSection(String name,Var(List(IniVar)) content) then
if getIniVar( *content, varName) is
{
failure then addVar(i, wantedSection, varName, float_to_string(newValue,10), (IniValType)float),
success( (IniVar) varValue ) then value(varValue) <- float_to_string(newValue, 10)
}.
public define String readString (IniInfo i, String section, String variable, String default)=
if getVar(i, section, variable) is
{
failure then default, //this value doesn't exist, then return the default value
success((IniVar) varValue) then *value(varValue)
}.
public define One writeString (IniInfo i, String wantedSection, String varName, String newValue) =
if getSection(i, wantedSection) is iniSection(String name,Var(List(IniVar)) content) then
if getIniVar( *content, varName) is
{
failure then addVar(i, wantedSection, varName, newValue, (IniValType)string),
success( (IniVar) varValue ) then value(varValue) <- newValue
}.
public define ByteArray readBase64 (IniInfo i, String section, String variable, ByteArray default)=
if getVar(i, section, variable) is
{
failure then default, //this value doesn't exist, then return the default value
success((IniVar) varValue) then
if type(varValue) is
b64 then base64_decode(to_byte_array(*value(varValue)))
else default
}.
public define One writeBase64 (IniInfo i, String wantedSection, String varName, ByteArray newValue) =
if getSection(i, wantedSection) is iniSection(String name,Var(List(IniVar)) content) then
if getIniVar( *content, varName) is
{
failure then addVar(i, wantedSection, varName, to_string(base64_encode(newValue)), (IniValType)b64),
success( (IniVar) varValue ) then value(varValue) <- to_string(base64_encode(newValue))
}.
***************** Writing file ********************************
define Maybe(One) writeOfVarList( RWStream s, Var(List(IniVar)) varList) =
if *varList is
{
[] then success(unique),
[ h . t ] then
if h is iniVar(varName, varValue, type) then
if type is b64 then
if reliable_write(s, to_byte_array(varName + "=@BEGIN base64 - Don't Edit@" + *varValue + "\n@END base64@\n")) is
{
failure then failure,
success(n) then writeOfVarList(s, var(t))
}
else
if reliable_write(s, to_byte_array(varName + "=" + *varValue + "\n")) is
{
failure then failure,
success(n) then writeOfVarList(s, var(t))
}
}.
define Maybe(One) writeOfSectionList( RWStream s, Var(List(IniSection)) section) =
if *section is
{
[] then success(unique),
[ h . t ] then
if h is iniSection(sectionName, varList) then
if reliable_write(s, to_byte_array("\n[" + sectionName + "]\n")) is
{
failure then failure,
success(n) then
if writeOfVarList(s, varList) is
{
failure then failure,
success(m) then
if writeOfSectionList(s, var(t)) is
{
failure then failure
success(l) then success(unique)
}
}
}
}.
public define One writeIniInfo( IniInfo ini, String newFile) =
if ini is iniInfo(fileName, sectionList, _) then
if length(newFile) > 0 then
//print("writing of " + newFile + " Inifile \n\n");
if (Maybe(RWStream))file(newFile, new) is
{
failure then unique, //nothing to write
success(f) then forget(writeOfSectionList( f, sectionList))
}
else
//print("writing of " + fileName + " Inifile \n\n");
if (Maybe(RWStream))file(fileName, new) is
{
failure then unique, //nothing to write
success(f) then forget(writeOfSectionList( f, sectionList))
}.
public define One writeIniInfo( IniInfo ini) =
if ini is iniInfo(fileName, sectionList, _) then
writeIniInfo(ini, fileName).
dumpOfSectionList(sectionList).
*************** Debuging tools ****************
define One dumpOfVarList( Var(List(IniVar)) varList) =
if *varList is
{
[] then unique,
[ h . t ] then
if h is iniVar(varName, varValue, type) then
print( varName + "=" + *varValue + "\n");
dumpOfVarList(var(t))
}.
define One dumpOfSectionList( Var(List(IniSection)) section) =
if *section is
{
[] then unique,
[ h . t ] then
if h is iniSection(sectionName, varList) then
print("section [" + sectionName + "]\n");
dumpOfVarList(varList);
dumpOfSectionList(var(t))
}.
public define One dumpIniInfo( IniInfo ini) =
if ini is iniInfo(fileName, sectionList, _) then
print("Dump of " + fileName + " Inifile \n\n");
dumpOfSectionList(sectionList).