-
Notifications
You must be signed in to change notification settings - Fork 4
/
conf.go
210 lines (182 loc) · 3.93 KB
/
conf.go
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
package config
import (
"bytes"
"fmt"
"regexp"
"strconv"
"strings"
)
var splitLines = regexp.MustCompile(`\r?\n[\s\t]*`)
type ConfError struct {
line int
index int
text string
}
func (e *ConfError) Error() string {
return fmt.Sprintf("unexpected %s at line %d:%d", e.text, e.line, e.index)
}
type conf_t map[string]map[string]interface{}
func (c *conf_t) getSection(section string) map[string]interface{} {
if sec, ok := (*c)[section]; ok {
return sec
} else {
return (*c)["default"] // return a dummy so Get* functions won't panic
}
}
func (c *conf_t) Iterate(section string, callback func(key string)) {
for k, _ := range c.getSection(section) {
callback(k)
}
}
func (c *conf_t) GetString(section, key string, defaultvalue string) string {
if s, ok := c.getSection(section)[key].(string); ok {
return s
} else {
return defaultvalue
}
}
func (c *conf_t) GetInt(section, key string, defaultvalue int64) int64 {
if s, ok := c.getSection(section)[key].(float64); ok {
return int64(s)
} else {
return defaultvalue
}
}
func (c *conf_t) GetFloat(section, key string, defaultvalue float64) float64 {
if s, ok := c.getSection(section)[key].(float64); ok {
return s
} else {
return defaultvalue
}
}
func (c *conf_t) GetBool(section, key string, defaultvalue bool) bool {
if s, ok := c.getSection(section)[key].(bool); ok {
return s
} else {
return defaultvalue
}
}
func (c *conf_t) GetArray(section, key string) []interface{} {
if s, ok := c.getSection(section)[key].([]interface{}); ok {
return s
} else {
return nil
}
}
func ParseConf(str string) (*conf_t, error) {
key, value, value2 := &bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}
config := make(conf_t)
curSection := make(map[string]interface{})
config["default"] = curSection
for ln, line := range splitLines.Split(str, -1) {
key.Reset()
value.Reset()
idx, p, quote := 0, key, byte(0)
L:
for idx < len(line) {
c := line[idx]
switch c {
case '[':
if quote == 0 {
if e := strings.Index(line, "]"); e > 0 {
if curSection == nil {
curSection = make(map[string]interface{})
}
config[line[1:e]] = curSection
break L
} else {
return nil, &ConfError{ln, idx, string(c)}
}
} else {
p.WriteByte(c)
}
case ' ', '\t':
if quote != 0 {
p.WriteByte(c)
}
case '\'', '"':
if idx > 0 && line[idx-1] == '\\' {
p.WriteByte(c)
} else if quote == 0 {
quote = c
} else if quote == c {
quote = 0
} else {
return nil, &ConfError{ln, idx, string(c)}
}
case '#':
if quote == 0 {
break L
} else {
p.WriteByte(c)
}
case '=':
if quote != 0 {
p.WriteByte(c)
} else if p != value {
p = value
} else {
return nil, &ConfError{ln, idx, "="}
}
default:
p.WriteByte(c)
}
idx++
}
if quote != 0 {
return nil, &ConfError{ln, idx, string(quote)}
}
k := key.String()
if curSection == nil || k == "" {
continue
}
value2.Reset()
v, idx := value.Bytes(), 0
for idx < len(v) {
if v[idx] == '\\' {
if idx == len(v)-1 {
return nil, &ConfError{ln, idx, value.String()}
}
switch v[idx+1] {
case 'n':
value2.WriteByte('\n')
case 'r':
value2.WriteByte('\r')
case 't':
value2.WriteByte('\t')
default:
value2.WriteByte(v[idx+1])
}
idx += 2
} else {
value2.WriteByte(v[idx])
idx++
}
}
v2 := value2.String()
_append := func(v interface{}) {
if ov, existed := curSection[k]; existed {
if arr, ok := ov.([]interface{}); ok {
arr = append(arr, v)
} else {
curSection[k] = []interface{}{ov, v}
}
} else {
curSection[k] = v
}
}
switch v2 {
case "on", "yes", "true":
_append(true)
case "off", "no", "false":
_append(false)
default:
if num, err := strconv.ParseFloat(v2, 64); err == nil {
_append(num)
} else {
_append(v2)
}
}
}
return &config, nil
}