-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
132 lines (100 loc) · 2.67 KB
/
parser.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
package initgo
import (
"bufio"
"os"
"regexp"
"strings"
)
const (
PARSE_PROCESS_INIT = 0
PARSE_PROCESS_SECTION = 1
PARSE_PROCESS_KEY_VALUE = 2
)
// Load INI config
func LoadConfig(filename string) (config *Config, err error) {
config = new(Config)
config.filename = filename
config.config = make(map[string]ConfigData)
config.keys = make(map[string][]string)
c := ConfigData{
inheritSection: "",
}
config.config[DEFAULT_SECTION] = c
config.parseIni()
return config, nil
}
// Reload config file
func (config *Config) ReloadConfig() (err error) {
c, err := LoadConfig(config.filename)
c.parseIni()
config = c
return err
}
// Create config data
func createConfigInitData(inherit string) ConfigData {
resInherit := DEFAULT_SECTION
if inherit != "" {
resInherit = inherit
}
configData := ConfigData{
inheritSection: resInherit,
data: make(map[string]string),
}
return configData
}
// Get section and inherit section
func getSection(source string) (section, inheritSection string) {
sectionReg, _ := regexp.Compile(REGEX_SECTION)
if ok := sectionReg.MatchString(source); ok {
section = strings.TrimSpace(sectionReg.FindStringSubmatch(source)[1])
inheritSectionReg, _ := regexp.Compile(REGEX_INHERITANCE_SECTION)
inheritSection = DEFAULT_SECTION
if ok := inheritSectionReg.MatchString(section); ok {
inheritSection = strings.TrimSpace(inheritSectionReg.FindStringSubmatch(section)[2])
section = strings.TrimSpace(inheritSectionReg.FindStringSubmatch(section)[1])
}
}
return
}
// Get key-value in section
func getKeyValue(source string) (key, value string) {
reg, _ := regexp.Compile(REGEX_KEY_VALUE)
if ok := reg.MatchString(source); ok {
key = strings.TrimSpace(reg.FindStringSubmatch(source)[1])
value = strings.TrimSpace(reg.FindStringSubmatch(source)[2])
}
return
}
// Parse INI file and save values
func (config *Config) parseIni() (err error) {
f, err := os.Open(config.filename)
defer f.Close()
config.mu.Lock()
defer config.mu.Unlock()
reader := bufio.NewReader(f)
scanner := bufio.NewScanner(reader)
var processing = PARSE_PROCESS_SECTION
var currentSection string
for scanner.Scan() {
if scanner.Err() != nil {
break
}
line := scanner.Text()
// Section
section, inheritSection := getSection(line)
if len(section) > 0 && len(inheritSection) > 0 {
processing = PARSE_PROCESS_KEY_VALUE
currentSection = section
c := createConfigInitData(inheritSection)
config.config[currentSection] = c
}
// Key-Value
if processing == PARSE_PROCESS_KEY_VALUE {
key, value := getKeyValue(line)
if len(key) > 0 {
config.config[currentSection].data[key] = value
}
}
}
return err
}