Last change
on this file since 16666 was
16666,
checked in by hmaislin, 5 years ago
|
#2929: Modified make / folder structure to build original app, added x86 dll
|
File size:
1.1 KB
|
Line | |
---|
1 | package config |
---|
2 | |
---|
3 | import ( |
---|
4 | "bytes" |
---|
5 | "io" |
---|
6 | "log" |
---|
7 | "strings" |
---|
8 | ) |
---|
9 | |
---|
10 | func ParseConfig(contents []byte, mapper func(field, value string, config interface{}) (err error), config interface{}) error { |
---|
11 | data := bytes.NewBuffer(contents) |
---|
12 | |
---|
13 | var field, value string |
---|
14 | var err error |
---|
15 | |
---|
16 | lineNum := 0 |
---|
17 | for { |
---|
18 | l, buferr := data.ReadString('\n') // parse line-by-line |
---|
19 | lineNum++ |
---|
20 | l = strings.TrimSpace(l) |
---|
21 | |
---|
22 | if buferr != nil { |
---|
23 | if buferr != io.EOF { |
---|
24 | return buferr |
---|
25 | } |
---|
26 | |
---|
27 | if len(l) == 0 { |
---|
28 | break |
---|
29 | } |
---|
30 | } |
---|
31 | |
---|
32 | if len(l) == 0 { |
---|
33 | continue |
---|
34 | } // empty line |
---|
35 | |
---|
36 | // strip comments |
---|
37 | if p := strings.Index(l, "#"); p >= 0 { // comment |
---|
38 | l = l[:p] |
---|
39 | } |
---|
40 | // for comments that take a whole line |
---|
41 | l = strings.TrimSpace(l) |
---|
42 | if len(l) == 0 { |
---|
43 | continue |
---|
44 | } |
---|
45 | |
---|
46 | // fill field and value strings |
---|
47 | if p := strings.Index(l, "="); p >= 0 { // comment |
---|
48 | field = strings.TrimSpace(l[:p-1]) |
---|
49 | value = strings.TrimSpace(l[p+1:]) |
---|
50 | } else { |
---|
51 | log.Fatalf("No '=' on line %d of config file", lineNum) |
---|
52 | } |
---|
53 | |
---|
54 | err = mapper(field, value, config) |
---|
55 | |
---|
56 | if err != nil { |
---|
57 | return err |
---|
58 | } |
---|
59 | // Reached end of data |
---|
60 | if buferr == io.EOF { |
---|
61 | break |
---|
62 | } |
---|
63 | } |
---|
64 | return nil |
---|
65 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.