forked from glacjay/goini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathini.go
183 lines (162 loc) · 3.77 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
package ini
import (
"bufio"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"unicode"
)
type Dict map[string]map[string]string
type Error string
var (
regDoubleQuote = regexp.MustCompile("^([^= \t]+)[ \t]*=[ \t]*\"([^\"]*)\"$")
regSingleQuote = regexp.MustCompile("^([^= \t]+)[ \t]*=[ \t]*'([^']*)'$")
regNoQuote = regexp.MustCompile("^([^= \t]+)[ \t]*=[ \t]*([^#;]+)")
regNoValue = regexp.MustCompile("^([^= \t]+)[ \t]*=[ \t]*([#;].*)?")
)
func Load(filename string) (dict Dict, err error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
dict = make(map[string]map[string]string)
reader := bufio.NewReader(file)
lineno := 0
section := ""
dict[section] = make(map[string]string)
for err == nil {
l, _, err := reader.ReadLine()
if err != nil {
break
}
lineno++
if len(l) == 0 {
continue
}
line := strings.TrimFunc(string(l), unicode.IsSpace)
for line[len(line)-1] == '\\' {
line = line[:len(line)-1]
l, _, err := reader.ReadLine()
if err != nil {
return nil, err
}
line += strings.TrimFunc(string(l), unicode.IsSpace)
}
section, err = dict.parseLine(section, line)
if err != nil {
return nil, newError(
err.Error() + fmt.Sprintf("'%s:%d'.", filename, lineno))
}
}
return
}
func (e Error) Error() string {
return string(e)
}
func (dict Dict) parseLine(section, line string) (string, error) {
// commets
if line[0] == '#' || line[0] == ';' {
return section, nil
}
// section name
if line[0] == '[' && line[len(line)-1] == ']' {
section := strings.TrimFunc(line[1:len(line)-1], unicode.IsSpace)
section = strings.ToLower(section)
dict[section] = make(map[string]string)
return section, nil
}
// key = value
if m := regDoubleQuote.FindAllStringSubmatch(line, 1); m != nil {
dict.add(section, m[0][1], m[0][2])
return section, nil
} else if m = regSingleQuote.FindAllStringSubmatch(line, 1); m != nil {
dict.add(section, m[0][1], m[0][2])
return section, nil
} else if m = regNoQuote.FindAllStringSubmatch(line, 1); m != nil {
dict.add(section, m[0][1], strings.TrimFunc(m[0][2], unicode.IsSpace))
return section, nil
} else if m = regNoValue.FindAllStringSubmatch(line, 1); m != nil {
dict.add(section, m[0][1], "")
return section, nil
}
return section, newError("iniparser: syntax error at ")
}
func (dict Dict) add(section, key, value string) {
key = strings.ToLower(key)
dict[section][key] = value
}
func (dict Dict) GetBool(section, key string) (bool, bool) {
sec, ok := dict[section]
if !ok {
return false, false
}
value, ok := sec[key]
if !ok {
return false, false
}
v := value[0]
if v == 'y' || v == 'Y' || v == '1' || v == 't' || v == 'T' {
return true, true
}
if v == 'n' || v == 'N' || v == '0' || v == 'f' || v == 'F' {
return false, true
}
return false, false
}
func (dict Dict) GetString(section, key string) (string, bool) {
sec, ok := dict[section]
if !ok {
return "", false
}
value, ok := sec[key]
if !ok {
return "", false
}
return value, true
}
func (dict Dict) GetInt(section, key string) (int, bool) {
sec, ok := dict[section]
if !ok {
return 0, false
}
value, ok := sec[key]
if !ok {
return 0, false
}
i, err := strconv.Atoi(value)
if err != nil {
return 0, false
}
return i, true
}
func (dict Dict) GetDouble(section, key string) (float64, bool) {
sec, ok := dict[section]
if !ok {
return 0, false
}
value, ok := sec[key]
if !ok {
return 0, false
}
d, err := strconv.ParseFloat(value, 64)
if err != nil {
return 0, false
}
return d, true
}
func (dict Dict) GetSections() []string {
size := len(dict)
sections := make([]string, size)
i := 0
for section, _ := range dict {
sections[i] = section
i++
}
return sections
}
func newError(message string) (e error) {
return Error(message)
}