-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathini.go
More file actions
61 lines (46 loc) · 1.27 KB
/
Copy pathini.go
File metadata and controls
61 lines (46 loc) · 1.27 KB
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
package ini
import (
"fmt"
"strings"
)
type SectionKey string
type Section map[string]string
func Loads(config string) map[SectionKey]Section {
data := make(map[SectionKey]Section)
section := SectionKey("")
for _, line := range strings.Split(config, "\n") {
if strings.HasPrefix(line, ";") {
continue
} else if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
line = strings.Replace(line, "[", "", 1)
line = strings.Replace(line, "]", "", 1)
section = SectionKey(strings.TrimSpace(line))
if _, ok := data[section]; !ok {
data[section] = make(Section)
}
} else {
if strings.Contains(line, "=") {
key, value := strings.Split(line, "=")[0], strings.Join(strings.Split(line, "=")[1:], "=")
key = strings.TrimSpace(key)
key = strings.Trim(key, "\"")
key = strings.Trim(key, "'")
value = strings.TrimSpace(value)
value = strings.Trim(value, "\"")
value = strings.Trim(value, "'")
data[section][key] = value
}
}
}
return data
}
func Dumps(json map[SectionKey]Section) string {
text := ""
for section := range json {
text += fmt.Sprintf("[%s]\n", section)
for key, value := range json[section] {
text += fmt.Sprintf("%s = %s\n", key, value)
}
text += "\n"
}
return strings.TrimSpace(text)
}