-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinifiles.nim
320 lines (268 loc) · 9.93 KB
/
inifiles.nim
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
#
# nimrod-tools
# (c) Copyright 2013 Adrian Veith
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
## This module implements a Object Pascal like TIniFile
## class, unlike the original it uses a fast btree
## and respects comments
import strutils, igncasestr, lazybtree, forloops, os
type
TKeyInfo = tuple
x: int
delim: int
TSecInfo = tuple
name: string
lines: seq[string]
keys: PBTree[istring, TKeyInfo]
TIniFile* = ref object
sections: seq[TSecInfo]
Fkeys: PBTree[istring, int]
space: tuple[indent, before, after: int]
filename* : string
proc Index(it:ref TItem[istring, int]): int {.inline.} = it.value - 1
proc setIndex(it:ref TItem[istring, int], value: int) {.inline.} =
it.value = value + 1
proc clean(o: var TKeyInfo) {.inline.} = discard
proc setFormat* (self: TIniFile, space: tuple[indent, before, after: int]): TIniFile {.discardable.} =
self.space = space
return self
proc keyAdd(self: TIniFile, line: string, xSec: int) {.inline.} =
self.sections[xSec].lines.add(line)
if line[0] == ';' or line[0] == '#' :
return
let p = line.find('=')
if p >= 0 :
self.sections[xSec].keys[line.substr(0, p -1).strip] = (self.sections[xSec].lines.high, p+1)
proc lineAdd(self: TIniFile, line: string, xSec: var int) =
#var line = line.strip
var p: int
var isSection = line[0] == '['
var sName: string
if isSection :
p = line.find(']')
if p > 0 :
sName = line.substr(1, p-1)
else :
isSection = false
template addSection() {.dirty.} =
#sName = sName.toLower
var it = findOrInsert(self.Fkeys, sName)
xSec = it.index
if xSec < 0 :
self.sections.add((name: sName, lines: @[], keys: nil))
xSec = self.sections.high
it.setIndex(xSec)
if self.sections == nil :
if not isSection: sName = ""
self.sections = @[]
addSection()
if isSection : return
if isSection :
addSection()
else :
self.keyAdd(line, xSec)
proc finalCreate(self: TIniFile) {.inline.} =
if isNil(self.sections): self.sections = @[]
proc fromString* (data: string): TIniFile =
new(result)
var x = 0
for s in data.splitLines :
result.lineAdd(s, x)
result.finalCreate()
proc fromFile* (name: string): TIniFile =
new(result)
result.filename = name
if existsFile(name):
var x = 0
for s in name.lines :
result.lineAdd(s, x)
result.finalCreate()
proc readKey* (self: TIniFile, sec, key: string, default: string = ""): string =
let it = self.Fkeys.Find(sec.istring)
if not isNil(it) :
let idx = it.index
let itk = self.sections[idx].keys.Find(key.istring)
if not isNil(itk) : return self.sections[idx].lines[itk.value.x].substr(itk.value.delim).strip
return default
template parseOrDefault(s: string, parse: expr, default: expr) {.immediate.} =
if s != "" :
try :
result = parse(s)
except EInvalidValue :
result = default
else :
result = default
proc readBool* (self: TIniFile, sec, key: string, default: Bool = false): Bool =
parseOrDefault(self.readKey(sec, key), parseBool, default)
proc readInt* (self: TIniFile, sec, key: string, default: Int = 0): Int =
parseOrDefault(self.readKey(sec, key), parseInt, default)
proc readBiggestInt* (self: TIniFile, sec, key: string, default: biggestInt = 0): biggestInt =
parseOrDefault(self.readKey(sec, key), parseBiggestInt, default)
proc readFloat* (self: TIniFile, sec, key: string, default: Float = 0.0): Float =
parseOrDefault(self.readKey(sec, key), parseFloat, default)
proc readEnum* [T: enum] (self: TIniFile, sec, key: string, default: T): T =
parseOrDefault(self.readKey(sec, key), parseEnum[T], default)
proc readSection * (self: TIniFile, sec: string, withComments: bool = false): seq[string] =
result = @[]
let it = self.Fkeys.find(sec.istring)
if not isNil(it) :
for line in self.sections[it.index].lines :
if not isNil(line) and (withComments or not (line[0] in {';', '#'})) : result.add(line)
proc readSectionKeys * (self: TIniFile, sec: string): seq[string] =
var res: seq[string] = @[]
let it = self.Fkeys.find(sec.istring)
if not isNil(it) :
self.sections[it.index].keys.forEachKey proc(k: istring) = res.add(k.string)
return res
proc readSections * (self: TIniFile): seq[string] =
result = @[]
for sec in self.sections :
result.add(sec.name)
proc forceSection(self: TIniFile, sec: string): int {.inline.} =
#let sName = sec.tolower
let it = self.Fkeys.findOrInsert(sec)
result = it.index
if result < 0 :
self.sections.add((name: sec, lines: @[], keys: nil))
result = self.sections.high
it.setIndex(result)
proc writeSection * (self: TIniFile, sec: string, data: openarray[string], preserveComments: Bool = false) =
let idx = self.forceSection(sec)
var finalComments : seq[string] = @[]
if preserveComments :
var isLeading = true
var cntLeading = 0
for line in self.sections[idx].lines:
if line[0] == ';' or line[0] == '#' :
if isLeading :
inc(cntLeading)
else :
finalComments.add(line)
else :
isLeading = false
self.sections[idx].lines.setLen(cntLeading)
else :
self.sections[idx].lines.setLen(0)
self.sections[idx].keys = nil
for line in data :
self.keyAdd(line, idx)
for line in finalComments :
self.sections[idx].lines.add(line)
proc hasSection* (self: TIniFile, sec: string): bool = not IsNil(self.Fkeys.Find(sec.istring))
proc hasKey* (self: TIniFile, sec, key: string): bool =
let it = self.Fkeys.Find(sec.istring)
return not isNil(it) and not isNil(self.sections[it.index].keys.Find(key.istring))
proc eraseKey* (self: TIniFile, sec, key: string): bool {.discardable.} =
let it = self.Fkeys.Find(sec.istring)
if not isNil(it) :
let idx = it.index
let itk = self.sections[idx].keys.Find(key.istring)
if not isNil(itk) :
self.sections[idx].lines[itk.value.x] = nil
self.sections[idx].keys.Delete(itk.key)
return true
return false
proc eraseSection * (self: TIniFile, sec: string): bool {.discardable.} =
let it = self.Fkeys.DeleteSlot(sec)
result = not isNil(it)
if result :
var w = addr(self.sections[it.index])
w[].name = nil
w[].lines = nil
w[].keys = nil
proc writeKey* (self: TIniFile, sec, key, value: string) =
when false :
if value == "" : self.eraseKey(sec, key)
else : nil
let idx = self.forceSection(sec)
#let sKey = key.tolower
let itk = self.sections[idx].keys.findOrInsert(key)
if itk.value.delim == 0 :
let sLine = repeatStr(self.space.indent, " ") & key & repeatStr(self.space.before, " ") & "=" & repeatStr(self.space.after, " ") & value
itk.value.delim = key.len + 1 + self.space.indent + self.space.before
self.sections[idx].lines.add(sline)
itk.value.x = self.sections[idx].lines.high
else :
self.sections[idx].lines[itk.value.x] = self.sections[idx].lines[itk.value.x].substr(0, itk.value.delim - 1) & repeatStr(self.space.after, " ") & value
proc writeBool* (self: TIniFile, sec, key: string, value: Bool) =
self.writeKey(sec, key, $value)
proc writeInt* (self: TIniFile, sec, key: string, value: Int) =
self.writeKey(sec, key, $value)
proc writeBiggestInt* (self: TIniFile, sec, key: string, value: biggestInt) =
self.writeKey(sec, key, $value)
proc writeFloat* (self: TIniFile, sec, key: string, value: Float) =
self.writeKey(sec, key, $value)
proc writeEnum* [T: enum] (self: TIniFile, sec, key: string, value: T) =
self.writeKey(sec, key, $value)
template exportImpl(self: TIniFile, onNewLine: expr): stmt {.immediate, dirty.} =
for s in self.sections :
if isNil(s.name) : continue
onNewLine("[" & s.name & "]")
for line in s.lines :
if line != nil : onNewLine(line)
proc toString* (self: TIniFile, delim: string = "\n"): string =
let lDelim = delim.len
var total = 0
for s in self.sections :
total += s.name.len + 2 + lDelim
for line in s.lines :
if line != nil : total += line.len + lDelim
result = newStringOfCap(total)
template addNewLine(s) {.immediate.} = result &= s & delim
exportImpl(self, addNewLine)
proc updateFile * (self: TIniFile) =
var f: TFile
finally :
if f != stdout : close(f)
if not isNil(self.fileName) and self.fileName != "" :
f = open(self.fileName, fmWrite)
else :
f = stdout
template addNewLine(s) {.immediate.} = writeln( f, s)
exportImpl(self, addNewLine )
iterator allSections* (self: TIniFile): string {.inline.} =
for sec in self.sections : yield sec.name
iterator allKeys* (self: TIniFile, sec: string): string =
let it = self.Fkeys.find(sec.istring)
if not isNil(it) :
forLoop k, self.sections[it.index].keys.keys() :
yield k.string
when isMainModule:
import commandline
var ini = """
nix = is Gut
[Abc]
teSt = 123
flag = on
count = 23
""".fromString.setFormat ((1,1,1))
echo ini.readKey("abC", "test")
echo ini.readKey("", "Nix")
echo ini.readKey("nicht", "da", "stimmt")
echo ini.eraseKey("abc", "test")
echo ini.readKey("abC", "test", "bin mal weg")
ini.writekey("abc", "test", "bin wieder da")
echo ini.readKey("abC", "test", "bin mal weg")
echo ini.toString
echo ini.readBool("abc", "flag", false)
echo ini.readInt("abc", "count", 0)
echo ini.readInt("abc", "x", 212)
echo ini.readInt("abc", "test", 213)
ini.writeInt("abc", "x", 10212)
echo ini.readInt("abc", "x", 212)
echo ini.readSection("abc").repr
ini.writeSection("abc", ["key=test", "another=true"])
echo ini.readInt("abc", "x", 212)
echo ini.readKey("abc", "key", "ok")
echo ini.readSectionKeys("abc").repr
var cl = ParseParams()
if existsFile(cl.Data[0]) :
ini = fromFile(cl.Data[0])
for sec in ini.allSections :
echo "[", sec, "]"
for key in ini.allKeys(sec) :
echo " ", key, " = ", ini.readKey(sec, key)
echo ini.toString