forked from tonynelson19/ini
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIni.cfc
More file actions
233 lines (153 loc) · 4.96 KB
/
Ini.cfc
File metadata and controls
233 lines (153 loc) · 4.96 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* @accessors true
*/
component {
public any function init(required string filePath, boolean removeDottedKeys=false) {
variables.extendsKey = ";extends"; // safe because keys starting with a semi-colon (;) are treated as comments
variables.sections = processSections(parseExtends(loadSections(arguments.filePath)), arguments.removeDottedKeys);
return this;
}
private struct function loadSections(required string filePath) {
// parse the INI file like normal
var sections = getProfileSections(arguments.filePath);
var data = {};
var key = "";
for (key in sections) {
// remove all spaces from the id to better support inheritance
var id = replace(key, " ", "", "all");
var section = {};
var keys = listToArray(sections[key]);
var i = "";
for (i = 1; i <= arrayLen(keys); i++) {
section[keys[i]] = getProfileString(arguments.filePath, key, keys[i]);
}
data[id] = section;
}
return data;
}
private struct function parseExtends(required struct sections) {
var data = {};
var key = "";
for (key in arguments.sections) {
// check to see if this section extends a different section [child : parent]
if (find(":", key)) {
var id = listFirst(key, ":");
var extends = listRest(key, ":");
} else {
var id = key;
var extends = "";
}
data[id] = arguments.sections[key];
// if it extends another section, add the key
if (extends != "") {
data[id][variables.extendsKey] = extends;
}
}
return data;
}
private struct function processSections(required struct sections, required boolean removeDottedKeys) {
var data = {};
var key = "";
for (key in arguments.sections) {
data[key] = processJSON(arguments.sections, key);
}
for (key in data) {
data[key] = processSection(data, key);
}
// flatten each section based on inheritance
for (key in data) {
data[key] = flattenSection(data, key, arguments.removeDottedKeys);
}
// remove ;extends
for (key in data) {
structDelete(data[key], variables.extendsKey);
}
return data;
}
private struct function processJSON(required struct sections, required string id) {
var section = arguments.sections[arguments.id];
var key = "";
var data = {};
// replace all complex JSON strings with complex objects
for (key in section) {
var value = section[key];
if (isJSON(value)) {
var deserialized = deserializeJSON(value);
// simple values stay the same (prevents true from becoming YES)
if (isSimpleValue(deserialized)) {
data[key] = trim(value);
} else {
data[key] = deserialized;
}
} else {
data[key] = trim(value);
}
}
return data;
}
private struct function processSection(required struct sections, required string id, struct config) {
if (!structKeyExists(arguments, "config")) {
arguments.config = {};
}
var section = arguments.sections[arguments.id];
var key = "";
for (key in section) {
// leave ;extends alone
if (key != variables.extendsKey) {
arguments.config = processKey(arguments.config, key, section[key]);
}
}
structAppend(arguments.config, section, false);
return arguments.config;
}
private struct function processKey(required struct config, required string key, required any value) {
// look for nested properties
if (find(".", arguments.key)) {
var first = listFirst(arguments.key, ".");
var rest = listRest(arguments.key, ".");
// build the outer container
if (!structKeyExists(arguments.config, first)) {
arguments.config[first] = {};
}
// recursively process the rest of the property
structAppend(arguments.config[first], processKey(arguments.config[first], rest, arguments.value), false);
} else {
arguments.config[arguments.key] = arguments.value;
}
return arguments.config;
}
private struct function flattenSection(required struct config, required string id, required boolean removeDottedKeys) {
if (!structKeyExists(arguments.config, arguments.id)) {
throw("Invalid section: #arguments.id#");
}
var section = arguments.config[arguments.id];
// check to see if this section extends other sections
if (structKeyExists(section, variables.extendsKey)) {
var extends = listToArray(section[variables.extendsKey], ":");
var i = "";
// recursively flatten each parent section
for (i = 1; i <= arrayLen(extends); i++) {
structAppend(section, flattenSection(arguments.config, extends[i], arguments.removeDottedKeys), false);
}
}
if (arguments.removeDottedKeys) {
// remove dotted structure keys
for (key in section) {
if (Find(".", key))
structDelete(section, key);
}
}
return section;
}
public struct function getSections() {
return variables.sections;
}
public struct function getSection(required string key) {
arguments.key = trim(arguments.key);
if (structKeyExists(variables.sections, arguments.key)) {
return variables.sections[arguments.key];
} else {
return {};
}
}
}