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
|
discard """
output: '''Section: common
Param: Floats1
Section: local
Param: Str
Param: Bool
Param: Floats2
destroy Foo
destroy Foo
'''
cmd: '''nim c --gc:arc $file'''
"""
# bug #15325
import tables
import strutils
const defaultSection = "***"
type
Config* = ref object
table: OrderedTableRef[string, OrderedTable[string, string]]
# ----------------------------------------------------------------------------------------------------------------------
proc newConfig*(): Config =
result = new(Config)
result.table = newOrderedTable[string, OrderedTable[string, string]]()
# ----------------------------------------------------------------------------------------------------------------------
proc add*(self: Config, param, value, section: string) {.nosinks.} =
let s = if section == "": defaultSection else: section
if not self.table.contains(s):
self.table[s] = initOrderedTable[string, string]()
self.table[s][param] = value
# ----------------------------------------------------------------------------------------------------------------------
proc sections*(self: Config): seq[string] =
for i in self.table.keys:
let s = if i == defaultSection: "" else: i
result.add(s)
# ----------------------------------------------------------------------------------------------------------------------
proc params*(self: Config, section: string): seq[string] =
let s = if section == "": defaultSection else: section
if self.table.contains(s):
for i in self.table[s].keys:
result.add(i)
# ----------------------------------------------------------------------------------------------------------------------
proc extract*(str, start, finish: string): string =
let startPos = str.find(start)
if startPos < 0:
return ""
let endPos = str.find(finish, startPos)
if endPos < 0:
return ""
return str[startPos + start.len() ..< endPos]
# ----------------------------------------------------------------------------------------------------------------------
proc loadString*(self: Config, text: string): tuple[valid: bool, errorInLine: int] {.discardable.} =
self.table.clear()
var data = ""
data = text
var
actualSection = ""
lineCount = 0
for i in splitLines(data):
lineCount += 1
var line = strip(i)
if line.len() == 0:
continue
if line[0] == '#' or line[0] == ';':
continue
if line[0] == '[':
let section = strip(extract(line, "[", "]"))
if section.len() != 0:
actualSection = section
else:
self.table.clear()
return (false, lineCount)
else:
let equal = find(line, '=')
if equal <= 0:
self.table.clear()
return (false, lineCount)
else:
let
param = strip(line[0 .. equal - 1])
value = strip(line[equal + 1 .. ^1])
if param.len() == 0:
self.table.clear()
return (false, lineCount)
else:
self.add(param, value, actualSection)
return (true, 0)
# ----------------------------------------------------------------------------------------------------------------------
when isMainModule:
var cfg = newConfig()
cfg.loadString("[common]\nFloats1 = 1,2,3\n[local]\nStr = \"String...\"\nBool = true\nFloats2 = 4, 5, 6\n")
for s in cfg.sections():
echo "Section: " & s
for p in cfg.params(s):
echo " Param: " & p
# bug #16437
type
Foo = object
FooRef = ref Foo
Bar = ref object
f: FooRef
proc `=destroy`(o: var Foo) =
echo "destroy Foo"
proc testMe(x: Bar) =
var c = (if x != nil: x.f else: nil)
assert c != nil
proc main =
var b = Bar(f: FooRef())
testMe(b)
main()
proc testMe2(x: Bar) =
var c: FooRef
c = (if x != nil: x.f else: nil)
assert c != nil
proc main2 =
var b = Bar(f: FooRef())
testMe2(b)
main2()
|