This repository was archived by the owner on Mar 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIniParser.cs
More file actions
460 lines (424 loc) · 15.5 KB
/
IniParser.cs
File metadata and controls
460 lines (424 loc) · 15.5 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Programmer: Ludvik Jerabek
// Date: 08\23\2010
// Purpose: Allow INI manipulation in .NET
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Diagnostics;
using System;
namespace instance_controller
{
// IniFile class used to read and write ini files by loading the file into memory
public class IniFile
{
// List of IniSection objects keeps track of all the sections in the INI file
private Hashtable m_sections;
// Public constructor
public IniFile()
{
m_sections = new Hashtable(StringComparer.InvariantCultureIgnoreCase);
}
// Loads the Reads the data in the ini file into the IniFile object
public void Load(string sFileName)
{
Load(sFileName, false);
}
// Loads the Reads the data in the ini file into the IniFile object
public void Load(string sFileName, bool bMerge)
{
if (!bMerge)
{
RemoveAllSections();
}
// Clear the object...
IniSection tempsection = null;
StreamReader oReader = new StreamReader(sFileName);
Regex regexcomment = new Regex("^([\\s]*#.*)", (RegexOptions.Singleline | RegexOptions.IgnoreCase));
// ^[\\s]*\\[[\\s]*([^\\[\\s].*[^\\s\\]])[\\s]*\\][\\s]*$
Regex regexsection = new Regex("^[\\s]*\\[[\\s]*([^\\[\\s].*[^\\s\\]])[\\s]*\\][\\s]*$", (RegexOptions.Singleline | RegexOptions.IgnoreCase));
//Regex regexsection = new Regex("\\[[\\s]*([^\\[\\s].*[^\\s\\]])[\\s]*\\]", (RegexOptions.Singleline | RegexOptions.IgnoreCase));
Regex regexkey = new Regex("^\\s*([^=\\s]*)[^=]*=(.*)", (RegexOptions.Singleline | RegexOptions.IgnoreCase));
while (!oReader.EndOfStream)
{
string line = oReader.ReadLine();
if (line != string.Empty)
{
Match m = null;
if (regexcomment.Match(line).Success)
{
m = regexcomment.Match(line);
Trace.WriteLine(string.Format("Skipping Comment: {0}", m.Groups[0].Value));
}
else if (regexsection.Match(line).Success)
{
m = regexsection.Match(line);
Trace.WriteLine(string.Format("Adding section [{0}]", m.Groups[1].Value));
tempsection = AddSection(m.Groups[1].Value);
}
else if (regexkey.Match(line).Success && tempsection != null)
{
m = regexkey.Match(line);
Trace.WriteLine(string.Format("Adding Key [{0}]=[{1}]", m.Groups[1].Value, m.Groups[2].Value));
tempsection.AddKey(m.Groups[1].Value).Value = m.Groups[2].Value;
}
else if (tempsection != null)
{
// Handle Key without value
Trace.WriteLine(string.Format("Adding Key [{0}]", line));
tempsection.AddKey(line);
}
else
{
// This should not occur unless the tempsection is not created yet...
Trace.WriteLine(string.Format("Skipping unknown type of data: {0}", line));
}
}
}
oReader.Close();
}
// Used to save the data back to the file or your choice
public void Save(string sFileName)
{
StreamWriter oWriter = new StreamWriter(sFileName, false);
foreach (IniSection s in Sections)
{
Trace.WriteLine(string.Format("Writing Section: [{0}]", s.Name));
oWriter.WriteLine(string.Format("[{0}]", s.Name));
foreach (IniSection.IniKey k in s.Keys)
{
if (k.Value != string.Empty)
{
Trace.WriteLine(string.Format("Writing Key: {0}={1}", k.Name, k.Value));
oWriter.WriteLine(string.Format("{0}={1}", k.Name, k.Value));
}
else
{
Trace.WriteLine(string.Format("Writing Key: {0}", k.Name));
oWriter.WriteLine(string.Format("{0}", k.Name));
}
}
}
oWriter.Close();
}
// Gets all the sections names
public System.Collections.ICollection Sections
{
get
{
return m_sections.Values;
}
}
// Adds a section to the IniFile object, returns a IniSection object to the new or existing object
public IniSection AddSection(string sSection)
{
IniSection s = null;
sSection = sSection.Trim();
// Trim spaces
if (m_sections.ContainsKey(sSection))
{
s = (IniSection)m_sections[sSection];
}
else
{
s = new IniSection(this, sSection);
m_sections[sSection] = s;
}
return s;
}
// Removes a section by its name sSection, returns trus on success
public bool RemoveSection(string sSection)
{
sSection = sSection.Trim();
return RemoveSection(GetSection(sSection));
}
// Removes section by object, returns trus on success
public bool RemoveSection(IniSection Section)
{
if (Section != null)
{
try
{
m_sections.Remove(Section.Name);
return true;
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
}
return false;
}
// Removes all existing sections, returns trus on success
public bool RemoveAllSections()
{
m_sections.Clear();
return (m_sections.Count == 0);
}
// Returns an IniSection to the section by name, NULL if it was not found
public IniSection GetSection(string sSection)
{
sSection = sSection.Trim();
// Trim spaces
if (m_sections.ContainsKey(sSection))
{
return (IniSection)m_sections[sSection];
}
return null;
}
// Returns a KeyValue in a certain section
public string GetKeyValue(string sSection, string sKey)
{
IniSection s = GetSection(sSection);
if (s != null)
{
IniSection.IniKey k = s.GetKey(sKey);
if (k != null)
{
return k.Value;
}
}
return string.Empty;
}
// Sets a KeyValuePair in a certain section
public bool SetKeyValue(string sSection, string sKey, string sValue)
{
IniSection s = AddSection(sSection);
if (s != null)
{
IniSection.IniKey k = s.AddKey(sKey);
if (k != null)
{
k.Value = sValue;
return true;
}
}
return false;
}
// Renames an existing section returns true on success, false if the section didn't exist or there was another section with the same sNewSection
public bool RenameSection(string sSection, string sNewSection)
{
// Note string trims are done in lower calls.
bool bRval = false;
IniSection s = GetSection(sSection);
if (s != null)
{
bRval = s.SetName(sNewSection);
}
return bRval;
}
// Renames an existing key returns true on success, false if the key didn't exist or there was another section with the same sNewKey
public bool RenameKey(string sSection, string sKey, string sNewKey)
{
// Note string trims are done in lower calls.
IniSection s = GetSection(sSection);
if (s != null)
{
IniSection.IniKey k = s.GetKey(sKey);
if (k != null)
{
return k.SetName(sNewKey);
}
}
return false;
}
// IniSection class
public class IniSection
{
// IniFile IniFile object instance
private IniFile m_pIniFile;
// Name of the section
private string m_sSection;
// List of IniKeys in the section
private Hashtable m_keys;
// Constuctor so objects are internally managed
protected internal IniSection(IniFile parent, string sSection)
{
m_pIniFile = parent;
m_sSection = sSection;
m_keys = new Hashtable(StringComparer.InvariantCultureIgnoreCase);
}
// Returns and hashtable of keys associated with the section
public System.Collections.ICollection Keys
{
get
{
return m_keys.Values;
}
}
// Returns the section name
public string Name
{
get
{
return m_sSection;
}
}
// Adds a key to the IniSection object, returns a IniKey object to the new or existing object
public IniKey AddKey(string sKey)
{
sKey = sKey.Trim();
IniSection.IniKey k = null;
if (sKey.Length != 0)
{
if (m_keys.ContainsKey(sKey))
{
k = (IniKey)m_keys[sKey];
}
else
{
k = new IniSection.IniKey(this, sKey);
m_keys[sKey] = k;
}
}
return k;
}
// Removes a single key by string
public bool RemoveKey(string sKey)
{
return RemoveKey(GetKey(sKey));
}
// Removes a single key by IniKey object
public bool RemoveKey(IniKey Key)
{
if (Key != null)
{
try
{
m_keys.Remove(Key.Name);
return true;
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
}
return false;
}
// Removes all the keys in the section
public bool RemoveAllKeys()
{
m_keys.Clear();
return (m_keys.Count == 0);
}
// Returns a IniKey object to the key by name, NULL if it was not found
public IniKey GetKey(string sKey)
{
sKey = sKey.Trim();
if (m_keys.ContainsKey(sKey))
{
return (IniKey)m_keys[sKey];
}
return null;
}
// Sets the section name, returns true on success, fails if the section
// name sSection already exists
public bool SetName(string sSection)
{
sSection = sSection.Trim();
if (sSection.Length != 0)
{
// Get existing section if it even exists...
IniSection s = m_pIniFile.GetSection(sSection);
if (s != this && s != null) return false;
try
{
// Remove the current section
m_pIniFile.m_sections.Remove(m_sSection);
// Set the new section name to this object
m_pIniFile.m_sections[sSection] = this;
// Set the new section name
m_sSection = sSection;
return true;
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
}
return false;
}
// Returns the section name
public string GetName()
{
return m_sSection;
}
// IniKey class
public class IniKey
{
// Name of the Key
private string m_sKey;
// Value associated
private string m_sValue;
// Pointer to the parent CIniSection
private IniSection m_section;
// Constuctor so objects are internally managed
protected internal IniKey(IniSection parent, string sKey)
{
m_section = parent;
m_sKey = sKey;
}
// Returns the name of the Key
public string Name
{
get
{
return m_sKey;
}
}
// Sets or Gets the value of the key
public string Value
{
get
{
return m_sValue;
}
set
{
m_sValue = value;
}
}
// Sets the value of the key
public void SetValue(string sValue)
{
m_sValue = sValue;
}
// Returns the value of the Key
public string GetValue()
{
return m_sValue;
}
// Sets the key name
// Returns true on success, fails if the section name sKey already exists
public bool SetName(string sKey)
{
sKey = sKey.Trim();
if (sKey.Length != 0)
{
IniKey k = m_section.GetKey(sKey);
if (k != this && k != null) return false;
try
{
// Remove the current key
m_section.m_keys.Remove(m_sKey);
// Set the new key name to this object
m_section.m_keys[sKey] = this;
// Set the new key name
m_sKey = sKey;
return true;
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
}
return false;
}
// Returns the name of the Key
public string GetName()
{
return m_sKey;
}
} // End of IniKey class
} // End of IniSection class
} // End of IniFile class
}