forked from benbaker76/ZXNTCount
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IniFile.cs
681 lines (512 loc) · 15.2 KB
/
IniFile.cs
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
// * ----------------------------------------------------------------------------
// * Author: Ben Baker
// * Website: headsoft.com.au
// * E-Mail: [email protected]
// * Copyright (C) 2015 Headsoft. All Rights Reserved.
// * ----------------------------------------------------------------------------
#define WRITE_DEFAULT
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections.Generic;
using System.Drawing;
using System.ComponentModel;
using System.Globalization;
namespace ZXNTCount
{
public class IniFile : IDisposable
{
private string m_fileName;
private bool m_fileChanged = false;
private Dictionary<string, IniSectionNode> m_sectionDictionary = null;
private CultureInfo m_enUSCultureInfo = null;
private bool m_disposed = false;
~IniFile()
{
Dispose(false);
}
public IniFile(string fileName)
{
m_fileName = fileName;
m_sectionDictionary = new Dictionary<string, IniSectionNode>();
m_enUSCultureInfo = CultureInfo.CreateSpecificCulture("en-US");
TryReadIniFile(fileName, m_sectionDictionary);
}
public IniFile(string[] lineArray)
{
m_sectionDictionary = new Dictionary<string, IniSectionNode>();
m_enUSCultureInfo = CultureInfo.CreateSpecificCulture("en-US");
ReadIni(lineArray, m_sectionDictionary);
}
public IniFile(Dictionary<string, IniSectionNode> sectionDictionary)
{
m_sectionDictionary = new Dictionary<string, IniSectionNode>();
m_enUSCultureInfo = CultureInfo.CreateSpecificCulture("en-US");
MergeSectionDictionary(sectionDictionary);
}
public bool TryAppendIniFile(string fileName)
{
Dictionary<string, IniSectionNode> sectionDictionary = new Dictionary<string, IniSectionNode>();
if (!TryReadIniFile(fileName, sectionDictionary))
return false;
MergeSectionDictionary(sectionDictionary);
return true;
}
private void MergeSectionDictionary(Dictionary<string, IniSectionNode> sectionDictionary)
{
foreach (KeyValuePair<string, IniSectionNode> section in sectionDictionary)
{
IniSectionNode sectionNode = null;
if (m_sectionDictionary.TryGetValue(section.Key.ToLower(), out sectionNode))
{
foreach (KeyValuePair<string, IniKeyNode> key in section.Value.KeyDictionary)
{
if (sectionNode.KeyDictionary.ContainsKey(key.Key.ToLower()))
sectionNode.KeyDictionary[key.Key] = key.Value;
else
sectionNode.KeyDictionary.Add(key.Key.ToLower(), key.Value);
}
}
else
m_sectionDictionary.Add(section.Key.ToLower(), section.Value);
}
}
private bool TryReadIniFile(string fileName, Dictionary<string, IniSectionNode> sectionDictionary)
{
if (!File.Exists(fileName))
return false;
string[] lineArray = File.ReadAllLines(fileName);
ReadIni(lineArray, sectionDictionary);
return true;
}
private void ReadIni(string[] lineArray, Dictionary<string, IniSectionNode> sectionDictionary)
{
IniSectionNode sectionNode = null;
char[] charArray = new char[] { '=' };
List<string> textList = new List<string>();
foreach (string line in lineArray)
{
string strLine = line.Trim();
if (String.IsNullOrEmpty(line) || strLine.StartsWith(";"))
{
textList.Add(line);
continue;
}
if (strLine.StartsWith("[") && strLine.EndsWith("]"))
{
string strSection = strLine.Substring(1, strLine.Length - 2);
if (!sectionDictionary.TryGetValue(strSection.ToLower(), out sectionNode))
{
sectionNode = new IniSectionNode(textList.ToArray(), strSection);
sectionDictionary.Add(strSection.ToLower(), sectionNode);
textList.Clear();
}
continue;
}
if (strLine.Contains("="))
{
if (sectionNode != null)
{
string[] splitArray = strLine.Split(charArray, 2);
if (splitArray.Length >= 2)
{
string key = splitArray[0].Trim();
string value = String.Join("=", splitArray, 1, splitArray.Length - 1);
if (!sectionNode.KeyDictionary.ContainsKey(key.ToLower()))
{
IniKeyNode keyNode = new IniKeyNode(textList.ToArray(), key, value);
sectionNode.KeyDictionary.Add(key.ToLower(), keyNode);
textList.Clear();
}
}
}
continue;
}
textList.Add(line);
}
}
public bool TryConvertToString<T>(T value, out string valueString)
{
valueString = null;
try
{
TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(T));
valueString = typeConverter.ConvertToString(null, m_enUSCultureInfo, value);
}
catch
{
return false;
}
return true;
}
public bool TryConvertFromString<T>(string valueString, out T value)
{
value = default(T);
if (String.IsNullOrEmpty(valueString))
return false;
try
{
TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(T));
value = (T)typeConverter.ConvertFromString(null, m_enUSCultureInfo, valueString);
}
catch
{
return false;
}
return true;
}
public bool WriteList<T>(string section, string key, List<T> valueList)
{
string[] stringArray = new string[valueList.Count];
for (int i = 0; i < valueList.Count; i++)
TryConvertToString<T>(valueList[i], out stringArray[i]);
string valueString = String.Join("~", stringArray);
return Write(section, key, valueString);
}
public bool WriteArray<T>(string section, string key, T[] valueArray)
{
string[] stringArray = new string[valueArray.Length];
for (int i = 0; i < valueArray.Length; i++)
TryConvertToString<T>(valueArray[i], out stringArray[i]);
string valueString = String.Join("~", stringArray);
return Write(section, key, valueString);
}
public bool Write<T>(string section, string key, T value)
{
string valueString = null;
TryConvertToString<T>(value, out valueString);
return Write(section, key, valueString);
}
public bool Write(string section, string key, string value)
{
if (key == null && value == null)
return TryDeleteSection(section);
if (value == null)
return TryDeleteKey(section, key);
IniSectionNode sectionNode = null;
if (m_sectionDictionary.TryGetValue(section.ToLower(), out sectionNode))
{
IniKeyNode valueNode = null;
if (sectionNode.KeyDictionary.TryGetValue(key.ToLower(), out valueNode))
{
valueNode.Value = value;
}
else
{
valueNode = new IniKeyNode(key, value);
sectionNode.KeyDictionary.Add(key.ToLower(), valueNode);
}
}
else
{
sectionNode = new IniSectionNode(section);
sectionNode.KeyDictionary.Add(key.ToLower(), new IniKeyNode(key, value));
m_sectionDictionary.Add(section.ToLower(), sectionNode);
}
m_fileChanged = true;
return true;
}
public string[] GetStringArray()
{
string strSection = null;
List<string> strList = new List<string>();
foreach (KeyValuePair<string, IniSectionNode> section in m_sectionDictionary)
{
if (strSection != section.Key)
{
strSection = section.Key;
if (section.Value.Text != null)
strList.AddRange(section.Value.Text);
strList.Add(String.Format("[{0}]", section.Value.Name));
}
foreach (KeyValuePair<string, IniKeyNode> key in section.Value.KeyDictionary)
{
if (key.Value.Text != null)
strList.AddRange(key.Value.Text);
strList.Add(String.Format("{0}={1}", key.Value.Key, key.Value.Value));
}
}
return strList.ToArray();
}
public void Flush()
{
if (!m_fileChanged)
return;
try
{
if (m_fileName != null)
File.WriteAllLines(m_fileName, GetStringArray(), Encoding.Unicode);
m_fileChanged = false;
}
catch
{
}
}
public void Reload()
{
Flush();
m_sectionDictionary = new Dictionary<string, IniSectionNode>();
TryReadIniFile(m_fileName, m_sectionDictionary);
}
public string Read(string section, string key)
{
string value = null;
TryRead(section, key, out value);
return value;
}
public string Read(string section, string key, string defaultValue)
{
string value = null;
TryRead(section, key, defaultValue, out value);
return value;
}
public bool TryRead(string section, string key, out string value)
{
return TryRead(section, key, null, out value);
}
public bool TryRead(string section, string key, string defaultValue, out string value)
{
value = defaultValue;
IniSectionNode iniSectionNode = null;
if (!m_sectionDictionary.TryGetValue(section.ToLower(), out iniSectionNode))
{
#if WRITE_DEFAULT
Write(section, key, defaultValue);
#endif
return false;
}
IniKeyNode iniKeyNode = null;
if (!iniSectionNode.KeyDictionary.TryGetValue(key.ToLower(), out iniKeyNode))
{
#if WRITE_DEFAULT
Write(section, key, defaultValue);
#endif
return false;
}
value = iniKeyNode.Value;
return true;
}
public List<T> ReadList<T>(string section, string key)
{
return ReadList<T>(section, key, new List<T>());
}
public List<T> ReadList<T>(string section, string key, List<T> defaultValue)
{
string dataString = null;
if (!TryRead(section, key, out dataString))
return defaultValue;
string[] stringArray = dataString.Split(new char[] { '~' });
List<T> valueList = new List<T>();
for (int i = 0; i < valueList.Count; i++)
{
T value = default(T);
if (TryConvertFromString<T>(stringArray[i], out value))
{
valueList.Add(value);
}
else
{
if (i < defaultValue.Count)
valueList.Add(defaultValue[i]);
else
valueList.Add(value);
}
}
return valueList;
}
public T[] ReadArray<T>(string section, string key)
{
return ReadArray<T>(section, key, new T[] { });
}
public T[] ReadArray<T>(string section, string key, T[] defaultValue)
{
string valueString = null;
if (!TryRead(section, key, out valueString))
return defaultValue;
string[] stringArray = valueString.Split(new char[] { '~' });
T[] valueArray = new T[stringArray.Length];
for (int i = 0; i < valueArray.Length; i++)
{
if (!TryConvertFromString<T>(stringArray[i], out valueArray[i]))
{
if (i < defaultValue.Length)
valueArray[i] = defaultValue[i];
}
}
return valueArray;
}
public T Read<T>(string section, string key)
{
T value;
string valueString = null;
TryRead(section, key, out valueString);
TryConvertFromString<T>(valueString, out value);
return value;
}
public T Read<T>(string section, string key, T defaultValue)
{
T value;
string defaultValueString = null;
TryConvertToString<T>(defaultValue, out defaultValueString);
string valueString = Read(section, key, defaultValueString);
if (!TryConvertFromString<T>(valueString, out value))
return defaultValue;
return value;
}
public bool ContainsKey(string section, string key)
{
IniSectionNode iniSectionNode = null;
if (!m_sectionDictionary.TryGetValue(section.ToLower(), out iniSectionNode))
return false;
if (!iniSectionNode.KeyDictionary.ContainsKey(key.ToLower()))
return false;
return true;
}
public bool TryDeleteKey(string section, string key)
{
IniSectionNode iniSectionNode = null;
if (!m_sectionDictionary.TryGetValue(section.ToLower(), out iniSectionNode))
return false;
iniSectionNode.KeyDictionary.Remove(key.ToLower());
m_fileChanged = true;
return true;
}
public bool TryDeleteSection(string section)
{
if (!m_sectionDictionary.ContainsKey(section.ToLower()))
return false;
m_sectionDictionary.Remove(section.ToLower());
m_fileChanged = true;
return true;
}
public bool TryRenameSection(string oldSection, string newSection)
{
IniSectionNode iniSectionNode = null;
if (!m_sectionDictionary.TryGetValue(oldSection.ToLower(), out iniSectionNode))
return false;
foreach (KeyValuePair<string, IniKeyNode> key in iniSectionNode.KeyDictionary)
Write(newSection, key.Value.Key, key.Value.Value);
m_sectionDictionary.Remove(oldSection.ToLower());
m_fileChanged = true;
return true;
}
public List<string> GetSectionList()
{
List<string> sectionList = new List<string>();
foreach (KeyValuePair<string, IniSectionNode> section in m_sectionDictionary)
sectionList.Add(section.Value.Name);
return sectionList;
}
public bool TryGetKeyList(string section, out List<string> keyList)
{
keyList = new List<string>();
IniSectionNode iniSectionNode = null;
if (!m_sectionDictionary.TryGetValue(section.ToLower(), out iniSectionNode))
return false;
foreach (KeyValuePair<string, IniKeyNode> key in iniSectionNode.KeyDictionary)
keyList.Add(key.Value.Key);
return true;
}
public bool TryGetKeyList(string section, out List<IniKeyNode> keyList)
{
keyList = new List<IniKeyNode>();
IniSectionNode iniSectionNode = null;
if (!m_sectionDictionary.TryGetValue(section.ToLower(), out iniSectionNode))
return false;
foreach (KeyValuePair<string, IniKeyNode> key in iniSectionNode.KeyDictionary)
keyList.Add(key.Value);
return true;
}
public bool TryGetKeyDictionary(string section, out Dictionary<string, IniKeyNode> keyDictionary)
{
keyDictionary = null;
IniSectionNode iniSectionNode = null;
if (!m_sectionDictionary.TryGetValue(section.ToLower(), out iniSectionNode))
return false;
keyDictionary = iniSectionNode.KeyDictionary;
return true;
}
public override string ToString()
{
List<string> textList = new List<string>();
foreach (KeyValuePair<string, IniSectionNode> section in m_sectionDictionary)
textList.Add(section.Value.ToString());
return String.Join(Environment.NewLine, textList.ToArray());
}
public Dictionary<string, IniSectionNode> SectionDictionary
{
get { return m_sectionDictionary; }
}
#region IDisposable Members
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
// Protected implementation of Dispose pattern.
protected virtual void Dispose(bool disposing)
{
if (m_disposed)
return;
if (disposing)
{
}
Flush();
m_disposed = true;
}
#endregion
}
public class IniKeyNode
{
public string[] Text = null;
public string Key = null;
public string Value = null;
public IniKeyNode(string key, string value)
{
Key = key;
Value = value;
}
public IniKeyNode(string[] text, string key, string value)
{
Text = text;
Key = key;
Value = value;
}
public override string ToString()
{
List<string> textList = new List<string>();
textList.AddRange(Text);
textList.Add(String.Format("{0}={1}", Key, Value));
return String.Join(Environment.NewLine, textList.ToArray());
}
}
public class IniSectionNode
{
public string[] Text = null;
public string Name = null;
public Dictionary<string, IniKeyNode> KeyDictionary = null;
public IniSectionNode(string name)
{
Name = name;
KeyDictionary = new Dictionary<string, IniKeyNode>();
}
public IniSectionNode(string[] text, string name)
{
Text = text;
Name = name;
KeyDictionary = new Dictionary<string, IniKeyNode>();
}
public override string ToString()
{
List<string> textList = new List<string>();
textList.AddRange(Text);
textList.Add(String.Format("[{0}]", Name));
foreach (KeyValuePair<string, IniKeyNode> key in KeyDictionary)
{
textList.Add(key.Value.ToString());
}
return String.Join(Environment.NewLine, textList.ToArray());
}
}
}