-
Notifications
You must be signed in to change notification settings - Fork 2
/
ini.go
265 lines (234 loc) · 7.36 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
package ini
import (
"bytes"
"fmt"
"io"
"os"
)
// manage all the sections and their key values defined in the .ini file
//
type Ini struct {
defaultSectionName string
sections map[string]*Section
}
func NewIni() *Ini {
return &Ini{defaultSectionName: "default",
sections: make(map[string]*Section)}
}
func (ini *Ini) GetDefaultSectionName() string {
return ini.defaultSectionName
}
func (ini *Ini) SetDefaultSectionName(defSectionName string) {
ini.defaultSectionName = defSectionName
}
// create a new section if the section with name does not exist
// or return the exist one if the section with name already exists
//
func (ini *Ini) NewSection(name string) *Section {
if section, ok := ini.sections[name]; ok {
return section
}
section := NewSection(name)
ini.sections[name] = section
return section
}
// add a section to the .ini file and overwrite the exist section
// with same name
func (ini *Ini) AddSection(section *Section) {
ini.sections[section.Name] = section
}
// Get all the section name in the ini
//
// return all the section names
func (ini *Ini) Sections() []*Section {
r := make([]*Section, 0)
for _, section := range ini.sections {
r = append(r, section)
}
return r
}
// check if a key exists or not in the Ini
//
// return true if the key in section exists
func (ini *Ini) HasKey(sectionName, key string) bool {
if section, ok := ini.sections[sectionName]; ok {
return section.HasKey(key)
}
return false
}
// get section by section name
//
// return: section or nil
func (ini *Ini) GetSection(name string) (*Section, error) {
if section, ok := ini.sections[name]; ok {
return section, nil
}
return nil, noSuchSection(name)
}
// return true if the section with name exists
// return false if the section with name does not exist
func (ini *Ini) HasSection(name string) bool {
_, err := ini.GetSection(name)
return err == nil
}
// get the value of key in section
func (ini *Ini) GetValue(sectionName, key string) (string, error) {
if section, ok := ini.sections[sectionName]; ok {
return section.GetValue(key)
}
return "", noSuchSection(sectionName)
}
// get the value of the key in section
// if the key does not exist, return the defValue
func (ini *Ini) GetValueWithDefault(sectionName, key string, defValue string) string {
if section, ok := ini.sections[sectionName]; ok {
return section.GetValueWithDefault(key, defValue)
}
return defValue
}
// get the value of key in section as bool.
// return true if the value of the key is one of following(case insensitive):
// - true
// - yes
// - t
// - y
// - 1
// return false for all other values
func (ini *Ini) GetBool(sectionName, key string) (bool, error) {
if section, ok := ini.sections[sectionName]; ok {
return section.GetBool(key)
}
return false, noSuchSection(sectionName)
}
// get the value of key as bool and return the default value if the section in the .ini file
// or key in the section does not exist
func (ini *Ini) GetBoolWithDefault(sectionName, key string, defValue bool) bool {
if section, ok := ini.sections[sectionName]; ok {
return section.GetBoolWithDefault(key, defValue)
}
return defValue
}
// get the value of key in the section as int
func (ini *Ini) GetInt(sectionName, key string) (int, error) {
if section, ok := ini.sections[sectionName]; ok {
return section.GetInt(key)
}
return 0, noSuchSection(sectionName)
}
// get the value of key in the section as int and return defValue if the section in the .ini file
// or key in the section does not exist
func (ini *Ini) GetIntWithDefault(sectionName, key string, defValue int) int {
if section, ok := ini.sections[sectionName]; ok {
return section.GetIntWithDefault(key, defValue)
}
return defValue
}
// get the value of key in the section as uint
func (ini *Ini) GetUint(sectionName, key string) (uint, error) {
if section, ok := ini.sections[sectionName]; ok {
return section.GetUint(key)
}
return 0, noSuchSection(sectionName)
}
// get the value of key in the section as int and return defValue if the section in the .ini file
// or key in the section does not exist
func (ini *Ini) GetUintWithDefault(sectionName, key string, defValue uint) uint {
if section, ok := ini.sections[sectionName]; ok {
return section.GetUintWithDefault(key, defValue)
}
return defValue
}
// get the value of key in the section as int64
func (ini *Ini) GetInt64(sectionName, key string) (int64, error) {
if section, ok := ini.sections[sectionName]; ok {
return section.GetInt64(key)
}
return 0, noSuchSection(sectionName)
}
// get the value of key in the section as int64 and return defValue if the section in the .ini file
// or key in the section does not exist
func (ini *Ini) GetInt64WithDefault(sectionName, key string, defValue int64) int64 {
if section, ok := ini.sections[sectionName]; ok {
return section.GetInt64WithDefault(key, defValue)
}
return defValue
}
// get the value of key in the section as uint64
func (ini *Ini) GetUint64(sectionName, key string) (uint64, error) {
if section, ok := ini.sections[sectionName]; ok {
return section.GetUint64(key)
}
return 0, noSuchSection(sectionName)
}
// get the value of key in the section as uint64 and return defValue if the section in the .ini file
// or key in the section does not exist
func (ini *Ini) GetUint64WithDefault(sectionName, key string, defValue uint64) uint64 {
if section, ok := ini.sections[sectionName]; ok {
return section.GetUint64WithDefault(key, defValue)
}
return defValue
}
// get the value of key in the section as float32
func (ini *Ini) GetFloat32(sectionName, key string) (float32, error) {
if section, ok := ini.sections[sectionName]; ok {
return section.GetFloat32(key)
}
return 0, noSuchSection(sectionName)
}
// get the value of key in the section as float32 and return defValue if the section in the .ini file
// or key in the section does not exist
func (ini *Ini) GetFloat32WithDefault(sectionName, key string, defValue float32) float32 {
if section, ok := ini.sections[sectionName]; ok {
return section.GetFloat32WithDefault(key, defValue)
}
return defValue
}
// get the value of key in the section as float64
func (ini *Ini) GetFloat64(sectionName, key string) (float64, error) {
if section, ok := ini.sections[sectionName]; ok {
return section.GetFloat64(key)
}
return 0, noSuchSection(sectionName)
}
// get the value of key in the section as float64 and return defValue if the section in the .ini file
// or key in the section does not exist
func (ini *Ini) GetFloat64WithDefault(sectionName, key string, defValue float64) float64 {
if section, ok := ini.sections[sectionName]; ok {
return section.GetFloat64WithDefault(key, defValue)
}
return defValue
}
func noSuchSection(sectionName string) error {
return fmt.Errorf("no such section:%s", sectionName)
}
func (ini *Ini) String() string {
buf := bytes.NewBuffer(make([]byte, 0))
ini.Write(buf)
return buf.String()
}
// write the content of the .ini in the .ini file format, e.g. in following format:
//
// [section1]
// key1 = value1
// key2 = value2
// [section2]
// key3 = value3
// key4 = value4
func (ini *Ini) Write(writer io.Writer) error {
for _, section := range ini.sections {
err := section.Write(writer)
if err != nil {
return err
}
}
return nil
}
// Write the conents of ini to a file
func (ini *Ini) WriteToFile(fileName string) error {
file, err := os.Create(fileName)
if err == nil {
defer file.Close()
return ini.Write(file)
}
return err
}