-
Notifications
You must be signed in to change notification settings - Fork 0
/
IniFile.cs
379 lines (328 loc) · 9.06 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
// **************************
// *** IniFile class V1.0 ***
// **************************
// *** (C)2009 S.T.A. snc ***
// **************************
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
namespace CumulusMX
{
internal class IniFile
{
#region "Declarations"
// *** Lock for thread-safe access to file and local cache ***
private readonly object m_Lock = new object();
// *** File name ***
private string m_FileName = null;
internal string FileName
{
get
{
return m_FileName;
}
}
// *** Lazy loading flag ***
private bool m_Lazy = false;
// *** Local cache ***
private readonly Dictionary<string, Dictionary<string, string>> m_Sections = new Dictionary<string,Dictionary<string, string>>();
// *** Local cache modified flag ***
private bool m_CacheModified = false;
#endregion
#region "Methods"
// *** Constructor ***
public IniFile(string FileName)
{
Initialize(FileName, false);
}
public IniFile(string FileName, bool Lazy)
{
Initialize(FileName, Lazy);
}
// *** Initialization ***
private void Initialize (string FileName, bool Lazy)
{
m_FileName = FileName;
m_Lazy = Lazy;
if (!m_Lazy) Refresh();
}
// *** Read file contents into local cache ***
internal void Refresh()
{
lock (m_Lock)
{
FileStream fs = null;
StreamReader sr = null;
try
{
// *** Clear local cache ***
m_Sections.Clear();
// *** Open the INI file ***
try
{
fs = new FileStream(m_FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
sr = new StreamReader(fs);
}
catch (FileNotFoundException)
{
return;
}
// *** Read up the file content ***
Dictionary<string, string> CurrentSection = null;
string s;
while ((s = sr.ReadLine()) != null)
{
s = s.Trim();
// *** Check for section names ***
if (s.StartsWith("[") && s.EndsWith("]"))
{
if (s.Length > 2)
{
string SectionName = s.Substring(1,s.Length-2);
// *** Only first occurrence of a section is loaded ***
if (m_Sections.ContainsKey(SectionName))
{
CurrentSection = null;
}
else
{
CurrentSection = new Dictionary<string, string>();
m_Sections.Add(SectionName,CurrentSection);
}
}
}
else if (CurrentSection != null)
{
// *** Check for key+value pair ***
int i;
if ((i=s.IndexOf('=')) > 0)
{
int j = s.Length - i - 1;
string Key = s.Substring(0,i).Trim();
if (Key.Length > 0)
{
// *** Only first occurrence of a key is loaded ***
if (!CurrentSection.ContainsKey(Key))
{
string Value = (j > 0) ? (s.Substring(i+1,j).Trim()) : ("");
CurrentSection.Add(Key,Value);
}
}
}
}
}
}
finally
{
// *** Cleanup: close file ***
if (sr != null) sr.Close();
if (fs != null) fs.Close();
sr = null;
fs = null;
}
}
}
// *** Flush local cache content ***
internal void Flush()
{
lock(m_Lock)
{
// *** If local cache was not modified, exit ***
if (!m_CacheModified) return;
m_CacheModified=false;
// *** Open the file ***
using (StreamWriter sw = new StreamWriter(m_FileName))
{
// *** Cycle on all sections ***
bool First = false;
foreach (KeyValuePair<string, Dictionary<string, string>> SectionPair in m_Sections)
{
Dictionary<string, string> Section = SectionPair.Value;
if (First) sw.WriteLine();
First = true;
// *** Write the section name ***
sw.Write('[');
sw.Write(SectionPair.Key);
sw.WriteLine(']');
// *** Cycle on all key+value pairs in the section ***
foreach (KeyValuePair<string, string> ValuePair in Section)
{
// *** Write the key+value pair ***
sw.Write(ValuePair.Key);
sw.Write('=');
sw.WriteLine(ValuePair.Value);
}
}
}
}
}
// *** Read a value from local cache ***
internal string GetValue(string SectionName, string Key, string DefaultValue)
{
// *** Lazy loading ***
if (m_Lazy)
{
m_Lazy = false;
Refresh();
}
lock (m_Lock)
{
// *** Check if the section exists ***
Dictionary<string, string> Section;
if (!m_Sections.TryGetValue(SectionName, out Section)) return DefaultValue;
// *** Check if the key exists ***
string Value;
if (!Section.TryGetValue(Key, out Value)) return DefaultValue;
// *** Check if the value is blank ***
if (string.IsNullOrWhiteSpace(Value)) return DefaultValue;
// *** Return the found value ***
return Value;
}
}
// *** Insert or modify a value in local cache ***
internal void SetValue(string SectionName, string Key, string Value)
{
// *** Lazy loading ***
if (m_Lazy)
{
m_Lazy = false;
Refresh();
}
lock (m_Lock)
{
// *** Flag local cache modification ***
m_CacheModified = true;
// *** Check if the section exists ***
Dictionary<string, string> Section;
if (!m_Sections.TryGetValue(SectionName, out Section))
{
// *** If it doesn't, add it ***
Section = new Dictionary<string, string>();
m_Sections.Add(SectionName,Section);
}
// *** Modify the value ***
if (Section.ContainsKey(Key)) Section.Remove(Key);
Section.Add(Key, Value);
}
}
internal bool ValueExists(string SectionName, string Key)
{
// *** Lazy loading ***
if (m_Lazy)
{
m_Lazy = false;
Refresh();
}
lock (m_Lock)
{
// *** Check if the section exists ***
Dictionary<string, string> Section;
if (!m_Sections.TryGetValue(SectionName, out Section)) return false;
// *** Check if the key exists ***
string Value;
if (Section.TryGetValue(Key, out Value))
return true;
else
return false;
}
}
// *** Encode byte array ***
private string EncodeByteArray(byte[] Value)
{
if (Value == null) return null;
StringBuilder sb = new StringBuilder();
foreach (byte b in Value)
{
string hex = Convert.ToString(b,16);
int l = hex.Length;
if (l > 2)
{
sb.Append(hex.Substring(l-2,2));
}
else
{
if (l < 2) sb.Append("0");
sb.Append(hex);
}
}
return sb.ToString();
}
// *** Decode byte array ***
private byte[] DecodeByteArray(string Value)
{
if (Value == null) return null;
int l = Value.Length;
if (l < 2) return new byte[] { };
l /= 2;
byte[] Result = new byte[l];
for (int i=0; i<l; i++) Result[i] = Convert.ToByte(Value.Substring(i*2,2),16);
return Result;
}
// *** Getters for various types ***
internal bool GetValue(string SectionName, string Key, bool DefaultValue)
{
string StringValue=GetValue(SectionName, Key, DefaultValue.ToString(System.Globalization.CultureInfo.InvariantCulture));
int Value;
if (int.TryParse(StringValue, out Value)) return (Value != 0);
return DefaultValue;
}
internal int GetValue(string SectionName, string Key, int DefaultValue)
{
string StringValue=GetValue(SectionName, Key, DefaultValue.ToString(CultureInfo.InvariantCulture));
int Value;
if (int.TryParse(StringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out Value)) return Value;
return DefaultValue;
}
internal double GetValue(string SectionName, string Key, double DefaultValue)
{
string StringValue=GetValue(SectionName, Key, DefaultValue.ToString(CultureInfo.InvariantCulture));
double Value;
if (double.TryParse(StringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out Value)) return Value;
return DefaultValue;
}
internal byte[] GetValue(string SectionName, string Key, byte[] DefaultValue)
{
string StringValue = GetValue(SectionName, Key, EncodeByteArray(DefaultValue));
try
{
return DecodeByteArray(StringValue);
}
catch (FormatException)
{
return DefaultValue;
}
}
internal DateTime GetValue(string SectionName, string Key, DateTime DefaultValue)
{
string StringValue = GetValue(SectionName, Key, DefaultValue.ToString(CultureInfo.InvariantCulture));
DateTime Value;
if (DateTime.TryParse(StringValue, out Value)) return Value;
return DefaultValue;
}
// *** Setters for various types ***
internal void SetValue(string SectionName, string Key, bool Value)
{
SetValue(SectionName, Key, (Value) ? ("1") : ("0"));
}
internal void SetValue(string SectionName, string Key, int Value)
{
SetValue(SectionName, Key, Value.ToString(CultureInfo.InvariantCulture));
}
internal void SetValue(string SectionName, string Key, double Value)
{
SetValue(SectionName, Key, Value.ToString("G17", CultureInfo.InvariantCulture));
}
internal void SetValue(string SectionName, string Key, byte[] Value)
{
SetValue(SectionName, Key, EncodeByteArray(Value));
}
internal void SetValue(string SectionName, string Key, DateTime Value)
{
// write datetimes in ISO 8601 ("sortable")
SetValue(SectionName, Key, Value.ToString("s"));
}
#endregion
}
}