-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigParser.cs
More file actions
317 lines (268 loc) · 6.56 KB
/
ConfigParser.cs
File metadata and controls
317 lines (268 loc) · 6.56 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using System.IO;
using System.Collections.Generic;
public class ConfigParser
{
public struct ConfigEntry
{
public string section;
public string key;
public string value;
/**
* ConfigEntry
*
* @param string section
* @param string key
* @param string value
*/
public ConfigEntry(string key, string value = "", string section = "")
{
this.section = section.ToLower().Trim();
this.key = key.ToLower().Trim();
this.value = value;
}
/**
* Set Value
*
* @param string value
*/
public void SetValue(string value)
{
this.value = value;
}
}
/**
* Changed Value Event System
*/
public delegate void ChangedValue(string section, string key, string newValue, string oldValue);
public event ChangedValue OnChangedValue;
public delegate void ClearValues();
public event ClearValues OnClearValues;
/**
* Default Section
*/
public const string DEFAULT_SECTION = "default";
/**
* List Of Created Sections
*/
protected List<string> sections = new List<string>();
/**
* List Of Entries
*/
protected List<ConfigEntry> entries = new List<ConfigEntry>();
/**
* Load File
*
* @param string filename
* @param boll clear
* @return bool
*/
public bool LoadFile(string filename, bool clear = true)
{
if (!File.Exists(filename))
{
return false;
}
string content = System.IO.File.ReadAllText(filename);
return LoadFromString(content, clear);
}
/**
* Clear Config
*/
public void Clear()
{
if (OnClearValues != null)
{
OnClearValues();
}
entries.Clear();
}
/**
* Parse String
*
* @param string content
* @param bool clear - clears current config
* @return bool
*/
public bool LoadFromString(string content, bool clear = true)
{
if (clear)
{
Clear();
}
string section = DEFAULT_SECTION;
string[] lines = content.Split('\n');
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i].Trim();
if (line.Length > 0)
{
switch (line[0])
{
case ';':
case '#':
// These lines are comments
// nop
break;
case '[':
// Set section
section = line.Substring(1, line.Length - 2);
break;
default:
int eqChar = line.IndexOf("=");
if (eqChar >= 0)
{
string key = line.Substring(0, eqChar);
string val = line.Substring(eqChar + 1);
Set(key, val, section);
}
break;
}
}
}
return true;
}
/**
* Get Section
*
* @param string section
* @return List<ConfigEntry>
*/
public List<ConfigEntry> Get(string section)
{
List<ConfigEntry> result = new List<ConfigEntry>();
for (int i = 0; i < entries.Count; i++)
{
if (entries[i].section == section)
{
result.Add(entries[i]);
}
}
return result;
}
/**
* Check Section Is Exists
*
* @return bool
*/
public bool IsSectionExists(string section)
{
return sections.IndexOf(section.Trim()) >= 0;
}
/**
* Get Index
*
* Return with -1 if entry not exists otherwise it will give back the entry's index
*
* @param string key
* @param string section = "default"
* @return int
*/
public int GetIndex(string key, string section = DEFAULT_SECTION)
{
for (int i = 0; i < entries.Count; i++)
{
if (entries[i].section == section.ToLower().Trim() && entries[i].key == key.ToLower().Trim())
{
return i;
}
}
return -1;
}
/**
* Get
*
* If entry not exists returns with defaultVal
* If entry exists returns with it's value
*
* @param string key
* @param string section = "default"
* @param string defaultVal = ""
* @return string
*/
public string Get(string key, string section = DEFAULT_SECTION, string defaultVal = "")
{
int index = GetIndex(key, section);
if (index >= 0)
{
return entries[index].value;
}
return defaultVal;
}
/**
* Get By Index (use only if you don't modify any value)
*
* @param int index
* @return string
*/
public string GetByIndex(int index, string defaultVal = "")
{
if (index >= 0)
{
return entries[index].value;
}
return defaultVal;
}
/**
* Set Entry
*
* Create Or Change an entry
*
* @param string key
* @param string section = "default"
*/
public void Set(string key, string value, string section = DEFAULT_SECTION)
{
int index = GetIndex(key, section);
// Send Change Event if it is enabled
if (OnChangedValue != null)
{
OnChangedValue(section, key, value, (index < 0 ? "" : entries[index].value));
}
// Entry Exists
if (index >= 0)
{
entries.RemoveAt(index);
}
else
{
if (!IsSectionExists(section))
{
sections.Add(section.Trim());
}
}
// Entry Not Exists
ConfigEntry e = new ConfigEntry(section, key, value);
entries.Add(e);
}
/**
* Get Created Sections
*/
public string[] GetSections()
{
return sections.ToArray();
}
/**
* To Array
*/
public ConfigEntry[] ToArray()
{
return entries.ToArray();
}
/**
* Generate Config String
*/
public override string ToString()
{
string result = "";
for (int i = 0; i < sections.Count; i++)
{
List<ConfigEntry> e = Get(sections[i]);
result += "[" + sections[i] + "]\n";
for (int j = 0; j < e.Count; j++)
{
result += e[j].key + "=" + e[j].value + "\n";
}
}
return result;
}
}