-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIniFile.cs
346 lines (300 loc) · 8.22 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
// **************************
// *** 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 object mLock = new object();
// *** File name ***
private string mFileName = null;
internal string FileName
{
get
{
return mFileName;
}
}
// *** Lazy loading flag ***
private bool mLazy = false;
// *** Local cache ***
private Dictionary<string, Dictionary<string, string>> mSections = new Dictionary<string,Dictionary<string, string>>();
// *** Local cache modified flag ***
private bool mCacheModified = 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)
{
mFileName = fileName;
mLazy = lazy;
if (!mLazy) Refresh();
}
// *** Read file contents into local cache ***
internal void Refresh()
{
lock (mLock)
{
StreamReader sr = null;
try
{
// *** Clear local cache ***
mSections.Clear();
// *** Open the INI file ***
try
{
sr = new StreamReader(mFileName);
}
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 (mSections.ContainsKey(sectionName))
{
currentSection = null;
}
else
{
currentSection = new Dictionary<string, string>();
mSections.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();
sr = null;
}
}
}
// *** Flush local cache content ***
internal void Flush()
{
lock(mLock)
{
// *** If local cache was not modified, exit ***
if (!mCacheModified) return;
mCacheModified=false;
// *** Open the file ***
using (StreamWriter sw = new StreamWriter(mFileName))
{
// *** Cycle on all sections ***
bool first = false;
foreach (KeyValuePair<string, Dictionary<string, string>> sectionPair in mSections)
{
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 (mLazy)
{
mLazy = false;
Refresh();
}
lock (mLock)
{
// *** Check if the section exists ***
Dictionary<string, string> section;
if (!mSections.TryGetValue(sectionName, out section)) return defaultValue;
// *** Check if the key exists ***
string value;
if (!section.TryGetValue(key, out 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 (mLazy)
{
mLazy = false;
Refresh();
}
lock (mLock)
{
// *** Flag local cache modification ***
mCacheModified = true;
// *** Check if the section exists ***
Dictionary<string, string> section;
if (!mSections.TryGetValue(sectionName, out section))
{
// *** If it doesn't, add it ***
section = new Dictionary<string, string>();
mSections.Add(sectionName,section);
}
// *** Modify the value ***
if (section.ContainsKey(key)) section.Remove(key);
section.Add(key, value);
}
}
// *** 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(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)
{
SetValue(sectionName, key, value.ToString());
}
#endregion
}
}