-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIni.go
184 lines (158 loc) · 4.41 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
package wingo
import (
"strings"
"github.com/rogeecn/wingo/co"
)
// High-level abstraction to a .ini file.
//
// Contains a slice of sections, which can be freely modified.
//
// Created with IniLoad().
type Ini struct {
Sections []IniSection // All sections of this .ini file.
sourcePath string
}
// Loads the sections and keys of an INI file.
func IniLoad(filePath string) (*Ini, error) {
me := &Ini{}
lines, err := me.loadLines(filePath)
if err != nil {
return nil, err
}
me.Sections = make([]IniSection, 0, 4) // arbitrary
var curSection IniSection
for _, line := range lines {
if len(line) == 0 {
continue // skip blank lines
}
if line[0] == '[' && line[len(line)-1] == ']' { // [section] ?
if curSection.Name != "" {
me.Sections = append(me.Sections, curSection)
}
curSection = IniSection{ // create a new section with the given name
Name: strings.TrimSpace(line[1 : len(line)-1]),
Values: make([]IniKey, 0, 4), // arbitrary
}
} else if curSection.Name != "" {
keyVal := strings.SplitN(line, "=", 2)
curSection.Values = append(curSection.Values, IniKey{
Name: strings.TrimSpace(keyVal[0]),
Value: strings.TrimSpace(keyVal[1]),
})
}
}
if curSection.Name != "" { // for the last section
me.Sections = append(me.Sections, curSection)
}
me.sourcePath = filePath // keep
return me, nil
}
func (me *Ini) loadLines(filePath string) ([]string, error) {
fin, err := FileMappedOpen(filePath, co.FILE_OPEN_READ_EXISTING)
if err != nil {
return nil, err
}
defer fin.Close()
return fin.ReadLines(), nil
}
// Returns the IniSection with the given name, if any.
func (me *Ini) Section(name string) (*IniSection, bool) {
for i := range me.Sections {
section := &me.Sections[i]
if section.Name == name {
return section, true
}
}
return nil, false
}
// Saves the contents to a .ini file.
func (me *Ini) SaveToFile(filePath string) error {
var serialized strings.Builder
for s := range me.Sections {
section := &me.Sections[s]
serialized.WriteString("[" + section.Name + "]\r\n")
for v := range section.Values {
value := §ion.Values[v]
serialized.WriteString(value.Name + "=" + value.Value + "\r\n")
}
isLast := s == len(me.Sections)-1
if !isLast {
serialized.WriteString("\r\n")
}
}
fout, err := FileOpen(filePath, co.FILE_OPEN_RW_OPEN_OR_CREATE)
if err != nil {
return err
}
defer fout.Close()
blob := []byte(serialized.String())
fout.Resize(len(blob))
_, err = fout.Write(blob)
me.sourcePath = filePath // update
return err
}
// Returns the latest source path of the .ini file.
//
// When IniLoad() is called, this path is stored. When Ini.SaveToFile() is
// called, this new path is stored.
//
// This is useful when you load an .ini file, and you need to update it later.
// When you first open, you pass the file path; on subsequent saves, you just
// call Ini.SourcePath() to retrieve the path, instead of manually saving it
// somewhere.
//
// Example:
//
// ini, _ := win.IniLoad("C:\\Temp\\foo.ini")
//
// // modify ini...
//
// ini.SaveToFile(ini.SourcePath())
func (me *Ini) SourcePath() string {
return me.sourcePath
}
// Returns the specific value, if existing.
//
// Note that a pointer to the string is returned, so that the value can be
// directly modified.
//
// Example:
//
// ini, _ := win.IniLoad("C:\\Temp\\foo.ini")
//
// if val, ok := ini.Value("my section", "my value"); ok {
// fmt.Printf("Old value: %s\n", *val)
// *val = "new value"
// }
func (me *Ini) Value(sectionName, valueName string) (*string, bool) {
if section, ok := me.Section(sectionName); ok {
if value, ok := section.Value(valueName); ok {
return &value.Value, true
}
}
return nil, false
}
//------------------------------------------------------------------------------
// A single section of an Ini.
//
// Contains a slice of keys, which can be freely modified.
type IniSection struct {
Name string // The name of this section.
Values []IniKey // All values of this section.
}
// Returns the IniKey with the given name, if any.
func (me *IniSection) Value(name string) (*IniKey, bool) {
for i := range me.Values {
value := &me.Values[i]
if value.Name == name {
return value, true
}
}
return nil, false
}
//------------------------------------------------------------------------------
// A single key of an IniSection.
type IniKey struct {
Name string // The name of this key.
Value string // The value of this key.
}