-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathIniFile.cs
872 lines (772 loc) · 32 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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
/*
RMLib: Nonvisual support classes used by multiple R&M Software programs
Copyright (C) Rick Parrish, R&M Software
This file is part of RMLib.
RMLib is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
RMLib is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with RMLib. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
namespace RandM.RMLib
{
/// <summary>
/// Provides read/write access to .INI configuration files
/// </summary>
/// <remarks>
/// To prevent disk thrashing changes are buffered until the class is disposed unless:
/// - Custom constructor passes TRUE for AAutoSave parameter
/// - Property AutoSave is changed to TRUE
/// - Save() is called manually
/// </remarks>
public class IniFile : IDisposable
{
private const string INI_FILE_ENCRYPTED_HEADER = "RMLib.IniFile.Encrypted";
private const string INI_FILE_SALT = "IniFile.cs";
private bool _Disposed = false;
private string _FileName = "";
private List<string> _HeaderComment = new List<string>();
private bool _Modified = false;
private Dictionary<string, IniFileSection> _Sections = new Dictionary<string, IniFileSection>(StringComparer.OrdinalIgnoreCase);
private List<string> _SectionNames = new List<string>();
/// <summary>
/// Default constructor to load the given INI into memory
/// </summary>
/// <param name="fileName">The INI to load</param>
public IniFile(string fileName) : this(fileName, new RMSecureString()) { }
/// <summary>
/// Custom constructor to load the given INI into memory, and indicate whether the changes should be buffered or immediately written to disk
/// </summary>
/// <param name="fileName">The INI to load</param>
/// <param name="password">The password used to encrypt/decrypt the contents of the INI file</param>
public IniFile(string fileName, RMSecureString password)
{
AutoSave = false;
Password = new RMSecureString();
_FileName = fileName;
Password = password;
List<string> CurrentComment = new List<string>();
string CurrentSection = "";
if (File.Exists(fileName))
{
// Read in the INI file
string FileText = FileUtils.FileReadAllText(fileName, RMEncoding.Ansi);
// Decrypt the INI file (if necessary)
if (password.Length > 0)
{
try
{
FileText = AES.Decrypt(FileText, password.GetPlainText(), INI_FILE_SALT);
Password = password;
}
catch (Exception)
{
return;
}
}
// Split the INI file into separate lines
string[] Lines = FileText.Replace("\r\n", "\n").Replace("\r", "\n").Split('\n');
// Loop through each line
foreach (string Line in Lines)
{
// Make sure the line isn't empty
if ((!string.IsNullOrEmpty(Line.Trim())))
{
// Check if this is a comment
if (Line.TrimStart().StartsWith(";"))
{
if (string.IsNullOrEmpty(CurrentSection))
{
_HeaderComment.Add(Line.TrimStart().Substring(1));
}
else
{
CurrentComment.Add(Line.TrimStart().Substring(1));
}
}
else
{
// Check if this is a new section
if ((Line.TrimStart().StartsWith("[")) && (Line.TrimEnd().EndsWith("]")))
{
// It is, so add the new section
CurrentSection = Line.Trim().Substring(1, Line.Trim().Length - 2);
_Sections[CurrentSection] = new IniFileSection(CurrentComment);
_SectionNames.Add(CurrentSection);
CurrentComment = new List<string>();
}
else
{
// It isn't so add the key/value to the current section
if (!string.IsNullOrEmpty(CurrentSection))
{
// Make sure this line is in the KEY=VALUE form
int EqualPos = Line.IndexOf('=');
if (EqualPos >= 1)
{
// Get the key
string Key = Line.Substring(0, EqualPos);
// Get the value
string Value = Line.Substring(EqualPos + 1);
// Add the key/value pair to the dictionary
_Sections[CurrentSection].WriteString(CurrentComment, Key, Value);
CurrentComment = new List<string>();
}
}
}
}
}
}
// Ensure the supplied password is valid
if (Password.Length > 0)
{
if ((_HeaderComment.Count == 0) || (_HeaderComment[0] != INI_FILE_ENCRYPTED_HEADER))
{
// Password did not properly decrypt the ini file
_Sections.Clear();
_SectionNames.Clear();
return;
}
}
}
}
/// <summary>
/// Handle saving the INI to disk at disposal (if there were modifications)
/// </summary>
public void Dispose()
{
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}
/// <summary>
/// Handle saving the INI to disk at disposal (if there were modifications)
/// </summary>
private void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if (!_Disposed)
{
// If disposing equals true, dispose all managed
// and unmanaged resources.
if (disposing)
{
// Dispose managed resources.
}
// Call the appropriate methods to clean up
// unmanaged resources here.
// If disposing is false,
// only the following code is executed.
if (_Modified) Save();
// Note disposing has been done.
_Disposed = true;
}
}
/// <summary>
/// Controls whether changes are buffered (=false) or immediately written to disk (=true)
/// </summary>
public bool AutoSave { get; set; }
/// <summary>
/// Remove the given key (and it's value) from the given section
/// </summary>
/// <param name="sectionName">The section containing the key to remove</param>
/// <param name="keyName">The key to remove</param>
public void DeleteKey(string sectionName, string keyName)
{
if (_Sections.ContainsKey(sectionName))
{
_Modified |= _Sections[sectionName].DeleteKey(keyName);
if (_Modified && AutoSave) Save();
}
}
/// <summary>
/// Remove the given section (along with all the keys+values) from the INI
/// </summary>
/// <param name="sectionName">The section to remove</param>
public void EraseSection(string sectionName)
{
if (_Sections.ContainsKey(sectionName))
{
_Sections.Remove(sectionName);
// Need to do this in a loop since section names are case insensitive, but List<string> is case sensitive
for (int i = _SectionNames.Count - 1; i >= 0; i--)
{
if (sectionName.ToUpper() == _SectionNames[i].ToUpper())
{
_SectionNames.RemoveAt(i);
}
}
_Modified = true;
if (_Modified && AutoSave) Save();
}
}
/// <summary>
/// The comment block that was read from / to be written to the top of the INI file
/// </summary>
/// <remarks>
/// If a comment is found at the top of an INI file, it is assumed to be a header comment, and so is not associated with the first section. This may not be correct behaviour, but the alternative is to associate the first comment with the first section, but then that comment will be removed if the first section is removed, which would be bad if the first comment really was a header comment
/// </remarks>
public List<string> HeaderComment
{
get { return _HeaderComment; }
set
{
_HeaderComment = value;
_Modified = true;
if (AutoSave) Save();
}
}
/// <summary>
/// Check whether a given section exists in the INI
/// </summary>
/// <param name="sectionName">The section to look in</param>
/// <param name="keyName">The key to look for</param>
/// <returns>TRUE = section and key exists, FALSE = it does not exist</returns>
public bool KeyExists(string sectionName, string keyName)
{
return _Sections.ContainsKey(sectionName) && _Sections[sectionName].ContainsKey(keyName);
}
public RMSecureString Password { get; set; }
/// <summary>
/// Returns a string array of all the keys in the given section
/// </summary>
/// <param name="sectionName">The section to retrieve keys from</param>
/// <returns>The keys in the given section</returns>
public string[] ReadSection(string sectionName)
{
if (_Sections.ContainsKey(sectionName))
{
string[] Keys = _Sections[sectionName].ReadSection();
for (int i = 0; i < Keys.Length; i++)
{
Keys[i] = Keys[i];
}
return Keys;
}
else
{
return new string[0];
}
}
/// <summary>
/// Returns a string array of all the sections in the INI
/// </summary>
/// <returns>The sections in the INI</returns>
public string[] ReadSections()
{
return _SectionNames.ToArray();
}
/// <summary>
/// Reads a string from the given key in the given section, using the provided default if the key does not exist
/// </summary>
/// <param name="sectionName">The section to look in</param>
/// <param name="keyName">The key to retrieve the value for</param>
/// <param name="defaultValue">The value to use if the key cannot be found</param>
/// <returns></returns>
public string ReadString(string sectionName, string keyName, string defaultValue)
{
if (_Sections.ContainsKey(sectionName))
{
return _Sections[sectionName].ReadString(keyName, defaultValue);
}
else
{
return defaultValue;
}
}
public string[] ReadString(string sectionName, string keyName, IList defaultValue)
{
List<string> Result = new List<string>();
int i = 0;
while (KeyExists(sectionName, keyName + i.ToString()))
{
Result.Add(ReadString(sectionName, keyName + i.ToString(), (i > defaultValue.Count - 1) ? "" : defaultValue[i].ToString()));
i += 1;
}
return Result.ToArray();
}
#region Read* (not string) methods
public Boolean ReadBoolean(string ASection, string AKey, Boolean ADefault)
{
Boolean X;
string Result = ReadString(ASection, AKey, ADefault.ToString());
return (Boolean.TryParse(Result, out X)) ? X : ADefault;
}
public Boolean[] ReadBoolean(string ASection, string AKey, IList ADefault)
{
List<Boolean> Result = new List<Boolean>();
string[] Items = ReadString(ASection, AKey, ADefault);
for (int i = 0; i < Items.Length; i++)
{
Boolean X;
Result.Add((Boolean.TryParse(Items[i], out X)) ? X : (i > ADefault.Count - 1) ? false : (Boolean)ADefault[i]);
}
return Result.ToArray();
}
public Byte ReadByte(string ASection, string AKey, Byte ADefault)
{
Byte X;
string Result = ReadString(ASection, AKey, ADefault.ToString());
return (Byte.TryParse(Result, out X)) ? X : ADefault;
}
public Byte[] ReadByte(string ASection, string AKey, IList ADefault)
{
List<Byte> Result = new List<Byte>();
string[] Items = ReadString(ASection, AKey, ADefault);
for (int i = 0; i < Items.Length; i++)
{
Byte X;
Result.Add((Byte.TryParse(Items[i], out X)) ? X : (i > ADefault.Count - 1) ? (Byte)0 : (Byte)ADefault[i]);
}
return Result.ToArray();
}
public Char ReadChar(string ASection, string AKey, Char ADefault)
{
Char X;
string Result = ReadString(ASection, AKey, ADefault.ToString());
return (Char.TryParse(Result, out X)) ? X : ADefault;
}
public Char[] ReadChar(string ASection, string AKey, IList ADefault)
{
List<Char> Result = new List<Char>();
string[] Items = ReadString(ASection, AKey, ADefault);
for (int i = 0; i < Items.Length; i++)
{
Char X;
Result.Add((Char.TryParse(Items[i], out X)) ? X : (i > ADefault.Count - 1) ? '\0' : (Char)ADefault[i]);
}
return Result.ToArray();
}
public DateTime ReadDateTime(string ASection, string AKey, DateTime ADefault)
{
DateTime X;
string Result = ReadString(ASection, AKey, ADefault.ToString());
return (DateTime.TryParse(Result, out X)) ? X : ADefault;
}
public DateTime[] ReadDateTime(string ASection, string AKey, IList ADefault)
{
List<DateTime> Result = new List<DateTime>();
string[] Items = ReadString(ASection, AKey, ADefault);
for (int i = 0; i < Items.Length; i++)
{
DateTime X;
Result.Add((DateTime.TryParse(Items[i], out X)) ? X : (i > ADefault.Count - 1) ? DateTime.MinValue : (DateTime)ADefault[i]);
}
return Result.ToArray();
}
public Decimal ReadDecimal(string ASection, string AKey, Decimal ADefault)
{
Decimal X;
string Result = ReadString(ASection, AKey, ADefault.ToString());
return (Decimal.TryParse(Result, out X)) ? X : ADefault;
}
public Decimal[] ReadDecimal(string ASection, string AKey, IList ADefault)
{
List<Decimal> Result = new List<Decimal>();
string[] Items = ReadString(ASection, AKey, ADefault);
for (int i = 0; i < Items.Length; i++)
{
Decimal X;
Result.Add((Decimal.TryParse(Items[i], out X)) ? X : (i > ADefault.Count - 1) ? 0 : (Decimal)ADefault[i]);
}
return Result.ToArray();
}
public Double ReadDouble(string ASection, string AKey, Double ADefault)
{
Double X;
string Result = ReadString(ASection, AKey, ADefault.ToString());
return (Double.TryParse(Result, out X)) ? X : ADefault;
}
public Double[] ReadDouble(string ASection, string AKey, IList ADefault)
{
List<Double> Result = new List<Double>();
string[] Items = ReadString(ASection, AKey, ADefault);
for (int i = 0; i < Items.Length; i++)
{
Double X;
Result.Add((Double.TryParse(Items[i], out X)) ? X : (i > ADefault.Count - 1) ? 0 : (Double)ADefault[i]);
}
return Result.ToArray();
}
public Int16 ReadInt16(string ASection, string AKey, Int16 ADefault)
{
Int16 X;
string Result = ReadString(ASection, AKey, ADefault.ToString());
return (Int16.TryParse(Result, out X)) ? X : ADefault;
}
public Int16[] ReadInt16(string ASection, string AKey, IList ADefault)
{
List<Int16> Result = new List<Int16>();
string[] Items = ReadString(ASection, AKey, ADefault);
for (int i = 0; i < Items.Length; i++)
{
Int16 X;
Result.Add((Int16.TryParse(Items[i], out X)) ? X : (i > ADefault.Count - 1) ? (Int16)0 : (Int16)ADefault[i]);
}
return Result.ToArray();
}
public Int32 ReadInt32(string ASection, string AKey, Int32 ADefault)
{
Int32 X;
string Result = ReadString(ASection, AKey, ADefault.ToString());
return (Int32.TryParse(Result, out X)) ? X : ADefault;
}
public Int32[] ReadInt32(string ASection, string AKey, IList ADefault)
{
List<Int32> Result = new List<Int32>();
string[] Items = ReadString(ASection, AKey, ADefault);
for (int i = 0; i < Items.Length; i++)
{
Int32 X;
Result.Add((Int32.TryParse(Items[i], out X)) ? X : (i > ADefault.Count - 1) ? 0 : (Int32)ADefault[i]);
}
return Result.ToArray();
}
public Int64 ReadInt64(string ASection, string AKey, Int64 ADefault)
{
Int64 X;
string Result = ReadString(ASection, AKey, ADefault.ToString());
return (Int64.TryParse(Result, out X)) ? X : ADefault;
}
public Int64[] ReadInt64(string ASection, string AKey, IList ADefault)
{
List<Int64> Result = new List<Int64>();
string[] Items = ReadString(ASection, AKey, ADefault);
for (int i = 0; i < Items.Length; i++)
{
Int64 X;
Result.Add((Int64.TryParse(Items[i], out X)) ? X : (i > ADefault.Count - 1) ? 0 : (Int64)ADefault[i]);
}
return Result.ToArray();
}
public SByte ReadSByte(string ASection, string AKey, SByte ADefault)
{
SByte X;
string Result = ReadString(ASection, AKey, ADefault.ToString());
return (SByte.TryParse(Result, out X)) ? X : ADefault;
}
public SByte[] ReadSByte(string ASection, string AKey, IList ADefault)
{
List<SByte> Result = new List<SByte>();
string[] Items = ReadString(ASection, AKey, ADefault);
for (int i = 0; i < Items.Length; i++)
{
SByte X;
Result.Add((SByte.TryParse(Items[i], out X)) ? X : (i > ADefault.Count - 1) ? (SByte)0 : (SByte)ADefault[i]);
}
return Result.ToArray();
}
public Single ReadSingle(string ASection, string AKey, Single ADefault)
{
Single X;
string Result = ReadString(ASection, AKey, ADefault.ToString());
return (Single.TryParse(Result, out X)) ? X : ADefault;
}
public Single[] ReadSingle(string ASection, string AKey, IList ADefault)
{
List<Single> Result = new List<Single>();
string[] Items = ReadString(ASection, AKey, ADefault);
for (int i = 0; i < Items.Length; i++)
{
Single X;
Result.Add((Single.TryParse(Items[i], out X)) ? X : (i > ADefault.Count - 1) ? 0 : (Single)ADefault[i]);
}
return Result.ToArray();
}
public UInt16 ReadUInt16(string ASection, string AKey, UInt16 ADefault)
{
UInt16 X;
string Result = ReadString(ASection, AKey, ADefault.ToString());
return (UInt16.TryParse(Result, out X)) ? X : ADefault;
}
public UInt16[] ReadUInt16(string ASection, string AKey, IList ADefault)
{
List<UInt16> Result = new List<UInt16>();
string[] Items = ReadString(ASection, AKey, ADefault);
for (int i = 0; i < Items.Length; i++)
{
UInt16 X;
Result.Add((UInt16.TryParse(Items[i], out X)) ? X : (i > ADefault.Count - 1) ? (UInt16)0 : (UInt16)ADefault[i]);
}
return Result.ToArray();
}
public UInt32 ReadUInt32(string ASection, string AKey, UInt32 ADefault)
{
UInt32 X;
string Result = ReadString(ASection, AKey, ADefault.ToString());
return (UInt32.TryParse(Result, out X)) ? X : ADefault;
}
public UInt32[] ReadUInt32(string ASection, string AKey, IList ADefault)
{
List<UInt32> Result = new List<UInt32>();
string[] Items = ReadString(ASection, AKey, ADefault);
for (int i = 0; i < Items.Length; i++)
{
UInt32 X;
Result.Add((UInt32.TryParse(Items[i], out X)) ? X : (i > ADefault.Count - 1) ? 0 : (UInt32)ADefault[i]);
}
return Result.ToArray();
}
public UInt64 ReadUInt64(string ASection, string AKey, UInt64 ADefault)
{
UInt64 X;
string Result = ReadString(ASection, AKey, ADefault.ToString());
return (UInt64.TryParse(Result, out X)) ? X : ADefault;
}
public UInt64[] ReadUInt64(string ASection, string AKey, IList ADefault)
{
List<UInt64> Result = new List<UInt64>();
string[] Items = ReadString(ASection, AKey, ADefault);
for (int i = 0; i < Items.Length; i++)
{
UInt64 X;
Result.Add((UInt64.TryParse(Items[i], out X)) ? X : (i > ADefault.Count - 1) ? 0 : (UInt64)ADefault[i]);
}
return Result.ToArray();
}
#endregion
/// <summary>
/// Check whether a given section exists in the INI
/// </summary>
/// <param name="sectionName">The section to look for</param>
/// <returns>TRUE = section exists, FALSE = it does not exist</returns>
public bool SectionExists(string sectionName)
{
return _Sections.ContainsKey(sectionName);
}
/// <summary>
/// Default method to manually save the INI to disk, using the filename that was used when opening the ini
/// </summary>
public void Save()
{
Save(_FileName);
}
/// <summary>
/// Custom method to manually save the INI to disk, using the provided filename
/// </summary>
/// <remarks>
/// The old INI is left untouched, and does not get deleted
/// </remarks>
/// <param name="AFileName">The new filename to save the INI as</param>
public void Save(string AFileName)
{
_Modified = false;
if (Password.Length > 0)
{
if ((_HeaderComment.Count == 0) || (_HeaderComment[0] != INI_FILE_ENCRYPTED_HEADER))
{
_HeaderComment.Insert(0, INI_FILE_ENCRYPTED_HEADER);
}
}
List<string> Lines = new List<string>();
if (_HeaderComment.Count > 0)
{
for (int i = 0; i < _HeaderComment.Count; i++)
{
Lines.Add(";" + _HeaderComment[i]);
}
Lines.Add(""); // A little white space
}
foreach (string SectionName in _SectionNames)
{
_Sections[SectionName].Save(SectionName, ref Lines);
Lines.Add(""); // A bit of white space
}
string FileText = string.Join("\r\n", Lines.ToArray());
if (Password.Length > 0) FileText = AES.Encrypt(FileText, Password.GetPlainText(), INI_FILE_SALT);
Directory.CreateDirectory(Path.GetDirectoryName(AFileName));
FileUtils.FileWriteAllText(AFileName, FileText);
}
/// <summary>
/// Adds the given value to the given key in the given section
/// </summary>
/// <param name="sectionName">The section to add the value to</param>
/// <param name="keyName">The key to add the value to</param>
/// <param name="defaultValue">The value to be added</param>
public void WriteString(string sectionName, string keyName, string defaultValue)
{
// Check if the dictionary contains the given section
if (!_Sections.ContainsKey(sectionName))
{
// It doesn't, so we need to create it
_Sections[sectionName] = new IniFileSection();
_SectionNames.Add(sectionName);
}
// Update the modified flag
_Modified |= _Sections[sectionName].WriteString(keyName, defaultValue);
// If we want to write all updates immediately, we should do so here
if (_Modified && AutoSave) Save();
}
/// <summary>
/// Adds the given value array to the given key in the given section
/// </summary>
/// <param name="sectionName">The section to add the value to</param>
/// <param name="keyName">The key to add the value to</param>
/// <param name="defaultValue">The value array to be added</param>
public void WriteString(string sectionName, string keyName, IList defaultValue)
{
// First, remove any entry with the keyname + number
string[] Keys = ReadSection(sectionName);
foreach (string Key in Keys)
{
if (Key.IndexOf(keyName, StringComparison.OrdinalIgnoreCase) == 0)
{
string KeyWithoutPrefix = Key.Substring(keyName.Length);
int temp;
if (int.TryParse(KeyWithoutPrefix, out temp))
{
DeleteKey(sectionName, Key);
}
}
}
// Save the old autosave value then disable it, since if we have 100 keys to write we don't want to rewrite the file 100 times!
bool OldAutoSave = AutoSave;
AutoSave = false;
// Then save the new keys
for (int i = 0; i < defaultValue.Count; i++)
{
// All the built-in types can be saved via Ini.WriteString()
WriteString(sectionName, keyName + i.ToString(), defaultValue[i].ToString());
}
// And restore the autosave setting
AutoSave = OldAutoSave;
// If we want to write all updates immediately, we should do so here
if (_Modified && AutoSave) Save();
}
}
class IniFileKey
{
private List<string> _Comment = new List<string>();
private bool _Quoted = false;
private string _Value = "";
public string ReadString()
{
return _Value;
}
public void Save(string keyName, ref List<string> lines)
{
if (_Comment.Count > 0)
{
for (int i = 0; i < _Comment.Count; i++)
{
lines.Add(";" + _Comment[i]);
}
}
if (_Quoted)
{
lines.Add(keyName + "=\"" + _Value + "\"");
}
else
{
lines.Add(keyName + "=" + _Value);
}
}
public bool WriteString(string value)
{
// Persist the existing comment, if one exists
return WriteString(_Comment, value);
}
public bool WriteString(List<string> comment, string value)
{
_Quoted = false;
if (value.StartsWith("\"") && value.EndsWith("\""))
{
value = value.Substring(1, value.Length - 2);
_Quoted = true;
}
bool Changed = (_Value != value);
_Comment = comment;
_Value = value;
return Changed;
}
}
class IniFileSection
{
private List<string> _Comment = new List<string>();
private Dictionary<string, IniFileKey> _Keys;
public IniFileSection() : this(new List<string>()) { }
public IniFileSection(List<string> comment)
{
_Comment = comment;
_Keys = new Dictionary<string, IniFileKey>(StringComparer.OrdinalIgnoreCase);
}
public bool ContainsKey(string keyName)
{
return _Keys.ContainsKey(keyName);
}
public bool DeleteKey(string keyName)
{
if (_Keys.ContainsKey(keyName))
{
_Keys.Remove(keyName);
return true;
}
else
{
return false;
}
}
public string[] ReadSection()
{
string[] Lines = new string[_Keys.Count];
_Keys.Keys.CopyTo(Lines, 0);
return Lines;
}
public string ReadString(string keyName, string defaultValue)
{
if (_Keys.ContainsKey(keyName))
{
return _Keys[keyName].ReadString();
}
else
{
return defaultValue;
}
}
public void Save(string sectionName, ref List<string> lines)
{
if (_Comment.Count > 0)
{
for (int i = 0; i < _Comment.Count; i++)
{
lines.Add(";" + _Comment[i]);
}
}
lines.Add("[" + sectionName + "]");
foreach (KeyValuePair<string, IniFileKey> KV in _Keys)
{
KV.Value.Save(KV.Key, ref lines);
}
}
public bool WriteString(string keyName, string value)
{
if (!_Keys.ContainsKey(keyName))
{
_Keys[keyName] = new IniFileKey();
}
return _Keys[keyName].WriteString(value);
}
public bool WriteString(List<string> comment, string keyName, string value)
{
if (!_Keys.ContainsKey(keyName))
{
_Keys[keyName] = new IniFileKey();
}
return _Keys[keyName].WriteString(comment, value);
}
}
}