-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathini.go
81 lines (73 loc) · 1.72 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
package iniparser
import (
"bufio"
"os"
"regexp"
"strconv"
"strings"
)
type _Section map[string]string
type IniFile struct {
fpath string
sections map[string]_Section
}
func (self *IniFile) GetString(section string, name string) (string, bool) {
s := self.sections[section]
if s == nil {
return "", false
}
v, ok := s[name]
return v, ok
}
func (self *IniFile) GetInt(section string, name string) (int, bool) {
v, err := strconv.Atoi(self.sections[section][name])
if err != nil {
return 0, false
}
return v, true
}
func (self *IniFile) GetFloat(section string, name string) (float64, bool) {
v, err := strconv.ParseFloat(self.sections[section][name], 64)
if err != nil {
return 0, false
}
return v, true
}
func (self *IniFile) parse(fpath string, encoding string) error {
f, err := os.Open(fpath)
if err != nil {
return err
}
defer f.Close()
self.sections = make(map[string]_Section)
scanner := bufio.NewScanner(f)
reSection := regexp.MustCompile(`^\[(.+?)\]$`)
reAssign := regexp.MustCompile(`^([^=]+?)=(.+?)$`)
var section _Section = nil
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 || line[0] == '#' || line[0] == ';' {
continue
}
if g := reSection.FindStringSubmatch(line); g != nil {
name := strings.TrimSpace(g[1])
if name != "" {
section = make(_Section)
self.sections[name] = section
}
} else if g := reAssign.FindStringSubmatch(line); g != nil {
key := strings.TrimSpace(g[1])
value := strings.TrimSpace(g[2])
section[key] = value
}
}
return nil
}
func LoadFile(fpath string, encoding string) (*IniFile, error) {
ini := &IniFile{}
err := ini.parse(fpath, encoding)
if err != nil {
return nil, err
}
return ini, nil
}