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
|
// Reader and writer for a variant of the `.ini` file format with `key = value` entries in `[sections]`.
package encoding_ini
import "base:runtime"
import "base:intrinsics"
import "core:strings"
import "core:strconv"
import "core:io"
import "core:os"
import "core:fmt"
_ :: fmt
Options :: struct {
comment: string,
key_lower_case: bool,
}
DEFAULT_OPTIONS :: Options {
comment = ";",
key_lower_case = false,
}
Iterator :: struct {
section: string,
_src: string,
options: Options,
}
iterator_from_string :: proc(src: string, options := DEFAULT_OPTIONS) -> Iterator {
return {
section = "",
options = options,
_src = src,
}
}
// Returns the raw `key` and `value`. `ok` will be false if no more key=value pairs cannot be found.
// They key and value may be quoted, which may require the use of `strconv.unquote_string`.
iterate :: proc(it: ^Iterator) -> (key, value: string, ok: bool) {
for line_ in strings.split_lines_iterator(&it._src) {
line := strings.trim_space(line_)
if len(line) == 0 {
continue
}
if line[0] == '[' {
end_idx := strings.index_byte(line, ']')
if end_idx < 0 {
end_idx = len(line)
}
it.section = line[1:end_idx]
continue
}
if it.options.comment != "" && strings.has_prefix(line, it.options.comment) {
continue
}
equal := strings.index(line, " =") // check for things keys that `ctrl+= = zoom_in`
quote := strings.index_byte(line, '"')
if equal < 0 || quote > 0 && quote < equal {
equal = strings.index_byte(line, '=')
if equal < 0 {
continue
}
} else {
equal += 1
}
key = strings.trim_space(line[:equal])
value = strings.trim_space(line[equal+1:])
ok = true
return
}
it.section = ""
return
}
Map :: distinct map[string]map[string]string
load_map_from_string :: proc(src: string, allocator: runtime.Allocator, options := DEFAULT_OPTIONS) -> (m: Map, err: runtime.Allocator_Error) {
unquote :: proc(val: string) -> (string, runtime.Allocator_Error) {
if len(val) > 0 && (val[0] == '"' || val[0] == '\'') {
v, allocated, ok := strconv.unquote_string(val)
if !ok {
return strings.clone(val)
}
if allocated {
return v, nil
}
return strings.clone(v), nil
}
return strings.clone(val)
}
context.allocator = allocator
it := iterator_from_string(src, options)
for key, value in iterate(&it) {
section := it.section
if section not_in m {
section = strings.clone(section) or_return
m[section] = {}
}
// store key-value pair
pairs := &m[section]
new_key := unquote(key) or_return
if options.key_lower_case {
old_key := new_key
new_key = strings.to_lower(key) or_return
delete(old_key) or_return
}
pairs[new_key] = unquote(value) or_return
}
return
}
load_map_from_path :: proc(path: string, allocator: runtime.Allocator, options := DEFAULT_OPTIONS) -> (m: Map, err: runtime.Allocator_Error, ok: bool) {
data := os.read_entire_file(path, allocator) or_return
defer delete(data, allocator)
m, err = load_map_from_string(string(data), allocator, options)
ok = err == nil
defer if !ok {
delete_map(m)
}
return
}
save_map_to_string :: proc(m: Map, allocator: runtime.Allocator) -> (data: string) {
b := strings.builder_make(allocator)
_, _ = write_map(strings.to_writer(&b), m)
return strings.to_string(b)
}
delete_map :: proc(m: Map) {
allocator := m.allocator
for section, pairs in m {
for key, value in pairs {
delete(key, allocator)
delete(value, allocator)
}
delete(section)
delete(pairs)
}
delete(m)
}
write_section :: proc(w: io.Writer, name: string, n_written: ^int = nil) -> (n: int, err: io.Error) {
defer if n_written != nil { n_written^ += n }
io.write_byte (w, '[', &n) or_return
io.write_string(w, name, &n) or_return
io.write_byte (w, ']', &n) or_return
io.write_byte (w, '\n', &n) or_return
return
}
write_pair :: proc(w: io.Writer, key: string, value: $T, n_written: ^int = nil) -> (n: int, err: io.Error) {
defer if n_written != nil { n_written^ += n }
io.write_string(w, key, &n) or_return
io.write_string(w, " = ", &n) or_return
when intrinsics.type_is_string(T) {
val := string(value)
if len(val) > 0 && (val[0] == ' ' || val[len(val)-1] == ' ') {
io.write_quoted_string(w, val, n_written=&n) or_return
} else {
io.write_string(w, val, &n) or_return
}
} else {
n += fmt.wprint(w, value)
}
io.write_byte(w, '\n', &n) or_return
return
}
write_map :: proc(w: io.Writer, m: Map) -> (n: int, err: io.Error) {
section_index := 0
for section, pairs in m {
if section_index == 0 && section == "" {
// ignore section
} else {
write_section(w, section, &n) or_return
}
for key, value in pairs {
write_pair(w, key, value, &n) or_return
}
section_index += 1
}
return
}
|