-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathini.go
376 lines (291 loc) · 8.07 KB
/
ini.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
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
package mcron
//github.com/c4pt0r/ini
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
)
const (
GLOBAL_SECTION = "_GLOBAL_SECTION_"
)
// steal it from stdandard lib's flag pkg, :)
// -- bool Value
type boolValue bool
func newBoolValue(val bool, p *bool) *boolValue {
*p = val
return (*boolValue)(p)
}
func (b *boolValue) Set(s string) error {
v, err := strconv.ParseBool(s)
*b = boolValue(v)
return err
}
func (b *boolValue) Get() interface{} { return bool(*b) }
func (b *boolValue) String() string { return fmt.Sprintf("%v", *b) }
func (b *boolValue) IsBoolFlag() bool { return true }
// optional interface to indicate boolean flags that can be
// supplied without "=value" text
type boolFlag interface {
Value
IsBoolFlag() bool
}
// -- int Value
type intValue int
func newIntValue(val int, p *int) *intValue {
*p = val
return (*intValue)(p)
}
func (i *intValue) Set(s string) error {
v, err := strconv.ParseInt(s, 0, 64)
*i = intValue(v)
return err
}
func (i *intValue) Get() interface{} { return int(*i) }
func (i *intValue) String() string { return fmt.Sprintf("%v", *i) }
// -- int64 Value
type int64Value int64
func newInt64Value(val int64, p *int64) *int64Value {
*p = val
return (*int64Value)(p)
}
func (i *int64Value) Set(s string) error {
v, err := strconv.ParseInt(s, 0, 64)
*i = int64Value(v)
return err
}
func (i *int64Value) Get() interface{} { return int64(*i) }
func (i *int64Value) String() string { return fmt.Sprintf("%v", *i) }
// -- uint Value
type uintValue uint
func newUintValue(val uint, p *uint) *uintValue {
*p = val
return (*uintValue)(p)
}
func (i *uintValue) Set(s string) error {
v, err := strconv.ParseUint(s, 0, 64)
*i = uintValue(v)
return err
}
func (i *uintValue) Get() interface{} { return uint(*i) }
func (i *uintValue) String() string { return fmt.Sprintf("%v", *i) }
// -- uint64 Value
type uint64Value uint64
func newUint64Value(val uint64, p *uint64) *uint64Value {
*p = val
return (*uint64Value)(p)
}
func (i *uint64Value) Set(s string) error {
v, err := strconv.ParseUint(s, 0, 64)
*i = uint64Value(v)
return err
}
func (i *uint64Value) Get() interface{} { return uint64(*i) }
func (i *uint64Value) String() string { return fmt.Sprintf("%v", *i) }
// -- string Value
type stringValue string
func newStringValue(val string, p *string) *stringValue {
*p = val
return (*stringValue)(p)
}
func (s *stringValue) Set(val string) error {
*s = stringValue(val)
return nil
}
func (s *stringValue) Get() interface{} { return string(*s) }
func (s *stringValue) String() string { return fmt.Sprintf("%s", *s) }
// -- float64 Value
type float64Value float64
func newFloat64Value(val float64, p *float64) *float64Value {
*p = val
return (*float64Value)(p)
}
func (f *float64Value) Set(s string) error {
v, err := strconv.ParseFloat(s, 64)
*f = float64Value(v)
return err
}
func (f *float64Value) Get() interface{} { return float64(*f) }
func (f *float64Value) String() string { return fmt.Sprintf("%v", *f) }
// -- time.Duration Value
type durationValue time.Duration
func newDurationValue(val time.Duration, p *time.Duration) *durationValue {
*p = val
return (*durationValue)(p)
}
func (d *durationValue) Set(s string) error {
v, err := time.ParseDuration(s)
*d = durationValue(v)
return err
}
func (d *durationValue) Get() interface{} { return time.Duration(*d) }
func (d *durationValue) String() string { return (*time.Duration)(d).String() }
// Value is the interface to the dynamic value stored in a flag.
// (The default value is represented as a string.)
//
// If a Value has an IsBoolFlag() bool method returning true,
// the command-line parser makes -name equivalent to -name=true
// rather than using the next command-line argument.
type Value interface {
String() string
Set(string) error
}
// Getter is an interface that allows the contents of a Value to be retrieved.
// It wraps the Value interface, rather than being part of it, because it
// appeared after Go 1 and its compatibility rules. All Value types provided
// by this package satisfy the Getter interface.
type Getter interface {
Value
Get() interface{}
}
type ConfSet struct {
fname string // config filename
parsed bool
sections map[string]*Section
}
type Section struct {
Name string
Vals map[string]*Item
}
type Item struct {
SectionName string
Name string
Val Value
}
func NewConf(fname string) *ConfSet {
return &ConfSet{fname, false, make(map[string]*Section)}
}
func (c *ConfSet) Var(val Value, sectionName, name string) {
item := &Item{sectionName, name, val}
if c.sections == nil {
c.sections = make(map[string]*Section)
}
s, ok := c.sections[sectionName]
if !ok {
s = &Section{sectionName, make(map[string]*Item)}
}
_, exists := s.Vals[name]
if exists {
panic(fmt.Sprintf("item %s already exists", name))
}
if s.Vals == nil {
s.Vals = make(map[string]*Item)
}
s.Vals[name] = item
c.sections[sectionName] = s
}
func (c *ConfSet) BoolVar(p *bool, sectionName, name string, value bool) {
c.Var(newBoolValue(value, p), sectionName, name)
}
func (c *ConfSet) Bool(sectionName, name string, value bool) *bool {
p := new(bool)
c.BoolVar(p, sectionName, name, value)
return p
}
func (c *ConfSet) IntVar(p *int, sectionName, name string, value int) {
c.Var(newIntValue(value, p), sectionName, name)
}
func (c *ConfSet) Int(sectionName, name string, value int) *int {
p := new(int)
c.IntVar(p, sectionName, name, value)
return p
}
func (c *ConfSet) Int64Var(p *int64, sectionName, name string, value int64) {
c.Var(newInt64Value(value, p), sectionName, name)
}
func (c *ConfSet) Int64(sectionName, name string, value int64) *int64 {
p := new(int64)
c.Int64Var(p, sectionName, name, value)
return p
}
func (c *ConfSet) UintVar(p *uint, sectionName, name string, value uint) {
c.Var(newUintValue(value, p), sectionName, name)
}
func (c *ConfSet) Uint(sectionName, name string, value uint) *uint {
p := new(uint)
c.UintVar(p, sectionName, name, value)
return p
}
func (c *ConfSet) Uint64Var(p *uint64, sectionName, name string, value uint64) {
c.Var(newUint64Value(value, p), sectionName, name)
}
func (c *ConfSet) Uint64(sectionName, name string, value uint64) *uint64 {
p := new(uint64)
c.Uint64Var(p, sectionName, name, value)
return p
}
func (c *ConfSet) StringVar(p *string, sectionName, name string, value string) {
c.Var(newStringValue(value, p), sectionName, name)
}
func (c *ConfSet) String(sectionName, name string, value string) *string {
p := new(string)
c.StringVar(p, sectionName, name, value)
return p
}
func (c *ConfSet) Float64Var(p *float64, sectionName, name string, value float64) {
c.Var(newFloat64Value(value, p), sectionName, name)
}
func (c *ConfSet) Float64(sectionName, name string, value float64) *float64 {
p := new(float64)
c.Float64Var(p, sectionName, name, value)
return p
}
func (c *ConfSet) DurationVar(p *time.Duration, sectionName, name string, value time.Duration) {
c.Var(newDurationValue(value, p), sectionName, name)
}
func (c *ConfSet) Duration(sectionName, name string, value time.Duration) *time.Duration {
p := new(time.Duration)
c.DurationVar(p, sectionName, name, value)
return p
}
func (c *ConfSet) parseOne(sectionName string, line string) error {
s, sectionExists := c.sections[sectionName]
parts := strings.SplitN(line, "=", 2)
name, value := parts[0], parts[1]
name = strings.TrimSpace(name)
value = strings.TrimSpace(value)
if sectionExists {
if v, valExists := s.Vals[name]; valExists {
if err := v.Val.Set(value); err != nil {
return err
}
}
}
return nil
}
func (c *ConfSet) Parse() error {
c.parsed = true
currentSection := GLOBAL_SECTION
fp, err := os.Open(c.fname)
if err != nil {
return err
}
reader := bufio.NewReader(fp)
for {
line, _, err := reader.ReadLine()
if err == io.EOF {
break
}
if len(line) == 0 {
continue
}
l := strings.TrimSpace(string(line))
// parse section
if l[0] == '[' {
l := strings.TrimSpace(l)
if l[len(l)-1] == ']' {
currentSection = l[1 : len(l)-1]
continue
}
}
// parse item
err = c.parseOne(currentSection, l)
if err != nil {
return err
}
}
return nil
}