-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIniFileClass.cs
More file actions
390 lines (358 loc) · 14.3 KB
/
Copy pathIniFileClass.cs
File metadata and controls
390 lines (358 loc) · 14.3 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace IniFileUtils
{
/// <summary>
/// Represents a single INI file value with optional comment.
/// </summary>
public class IniValue
{
/// <summary>
/// Gets or sets the value string.
/// </summary>
public string Value { get; set; }
/// <summary>
/// Gets or sets the optional comment for this value.
/// </summary>
public string Comment { get; set; }
/// <summary>
/// Initializes a new instance of the IniValue class.
/// </summary>
/// <param name="value">The value string.</param>
/// <param name="comment">The optional comment.</param>
public IniValue(string value, string comment = null)
{
Value = value;
Comment = comment;
}
}
/// <summary>
/// A thread-safe INI file reader/writer with support for comments and Shift-JIS encoding.
///
/// Features:
/// - Thread-safe access (process-level) via file-based locking
/// - Preserves comments from original INI file
/// - Shift-JIS encoding by default (suitable for Japanese environments)
/// - Support for quoted values containing spaces
/// - Compatible with both .NET Framework 4.8+ and .NET Core 3.1+
///
/// Thread Safety: Thread-safe within a single process. Multiple threads can safely read/write
/// the same INI file. Not suitable for multi-process scenarios (use file-locking mechanisms instead).
///
/// Encoding: Uses Shift-JIS (Code Page 932) by default. In .NET Core, the CodePagesEncodingProvider
/// is automatically registered if available. In .NET Framework, standard code pages are always available.
/// </summary>
public class IniFileClass
{
private static Encoding _defaultEncoding;
private static Dictionary<string, object> _fileLocks = new Dictionary<string, object>();
private static readonly Regex CommentRegex = new Regex("^[;#\\/].*");
static IniFileClass()
{
try
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
catch
{
// CodePagesEncodingProvider registration is not required in .NET Framework.
// Silently ignore if already registered or unavailable.
}
_defaultEncoding = Encoding.GetEncoding(932);
}
private Dictionary<string, Dictionary<string, IniValue>> _iniData = new Dictionary<string, Dictionary<string, IniValue>>();
private Dictionary<string, List<string>> _comments = new Dictionary<string, List<string>>();
private string _filePath;
private Encoding _encoding;
/// <summary>
/// Initializes a new instance of the IniFileClass.
/// </summary>
/// <param name="filePath">The path to the INI file. If null, an empty INI structure is created.</param>
/// <param name="encoding">The encoding to use. Defaults to Shift-JIS (Japanese). Pass Encoding.UTF8 for UTF-8.</param>
public IniFileClass(string filePath = null, Encoding encoding = null)
{
_filePath = filePath;
_encoding = encoding ?? _defaultEncoding;
if (!string.IsNullOrEmpty(_filePath))
{
EnsureLockObject();
LoadIniFile();
}
else
{
_iniData = new Dictionary<string, Dictionary<string, IniValue>>();
_comments = new Dictionary<string, List<string>>();
}
}
private void EnsureLockObject()
{
lock (_fileLocks)
{
if (!_fileLocks.ContainsKey(_filePath))
{
_fileLocks[_filePath] = new object();
}
}
}
private void LoadIniFile()
{
lock (_fileLocks[_filePath])
{
if (!File.Exists(_filePath))
{
_iniData = new Dictionary<string, Dictionary<string, IniValue>>();
return;
}
var lines = File.ReadAllLines(_filePath, _encoding);
ParseLines(lines);
}
}
private void ParseLines(string[] lines)
{
string currentSection = "";
List<string> currentComments = new List<string>();
foreach (var line in lines)
{
string trimmedLine = line.Trim();
if (CommentRegex.IsMatch(trimmedLine))
{
currentComments.Add(trimmedLine);
continue;
}
if (string.IsNullOrEmpty(trimmedLine))
{
continue;
}
if (trimmedLine.StartsWith("[") && trimmedLine.EndsWith("]"))
{
if (currentSection != "")
{
_comments[currentSection] = new List<string>(currentComments);
currentComments.Clear();
}
currentSection = trimmedLine.Substring(1, trimmedLine.Length - 2);
if (!_iniData.ContainsKey(currentSection))
{
_iniData[currentSection] = new Dictionary<string, IniValue>();
}
}
else
{
var keyValue = trimmedLine.Split(new char[] { '=' }, 2);
if (keyValue.Length == 2)
{
string key = keyValue[0].Trim();
string value = keyValue[1].Trim();
// Remove quotes if value is quoted
if (value.StartsWith("\"") && value.EndsWith("\"") && value.Length >= 2)
{
value = value.Substring(1, value.Length - 2);
}
_iniData[currentSection][key] = new IniValue(value);
}
}
}
if (currentSection != "")
{
_comments[currentSection] = new List<string>(currentComments);
}
}
/// <summary>
/// Gets a value from the INI file.
/// </summary>
/// <param name="section">The section name.</param>
/// <param name="key">The key name.</param>
/// <returns>The value string, or null if section/key does not exist.</returns>
public string GetValue(string section, string key)
{
if (_iniData.ContainsKey(section) && _iniData[section].ContainsKey(key))
return _iniData[section][key].Value;
return null;
}
/// <summary>
/// Sets a value in the INI file.
/// </summary>
/// <param name="section">The section name. Created if it does not exist.</param>
/// <param name="key">The key name. Overwritten if it already exists.</param>
/// <param name="value">The value string.</param>
/// <param name="useQuotes">If true, the value will be surrounded by quotes (useful for values containing spaces).</param>
public void SetValue(string section, string key, string value, bool useQuotes = false)
{
if (!_iniData.ContainsKey(section))
_iniData[section] = new Dictionary<string, IniValue>();
string formattedValue = useQuotes ? $"\"{value}\"" : value;
_iniData[section][key] = new IniValue(formattedValue);
}
/// <summary>
/// Removes a key from the INI file.
/// </summary>
/// <param name="section">The section name.</param>
/// <param name="key">The key name.</param>
/// <param name="removeEmptySection">If true, removes the section if it becomes empty.</param>
/// <returns>True if the key was removed, false if not found.</returns>
public bool RemoveKey(string section, string key, bool removeEmptySection = false)
{
if (_iniData.ContainsKey(section) && _iniData[section].ContainsKey(key))
{
_iniData[section].Remove(key);
if (removeEmptySection && _iniData[section].Count == 0)
{
_iniData.Remove(section);
if (_comments.ContainsKey(section))
{
_comments.Remove(section);
}
}
return true;
}
return false;
}
/// <summary>
/// Removes a section from the INI file.
/// </summary>
/// <param name="section">The section name.</param>
/// <returns>True if the section was removed, false if not found.</returns>
public bool RemoveSection(string section)
{
if (_iniData.ContainsKey(section))
{
_iniData.Remove(section);
if (_comments.ContainsKey(section))
{
_comments.Remove(section);
}
return true;
}
return false;
}
/// <summary>
/// Saves the INI data to the file with the specified encoding.
/// Creates the directory if it does not exist.
/// Thread-safe: Uses process-level file locking.
/// </summary>
/// <param name="filePath">Optional: Save to a different file path. If provided, subsequent operations use this path.</param>
/// <exception cref="InvalidOperationException">Thrown when no file path is specified.</exception>
/// <exception cref="IOException">Thrown when the file cannot be written.</exception>
public void Save(string filePath = null)
{
if (!string.IsNullOrEmpty(filePath))
{
_filePath = filePath;
EnsureLockObject();
}
if (string.IsNullOrEmpty(_filePath))
{
throw new InvalidOperationException("Cannot save data: INI file path is not specified.");
}
string directoryPath = Path.GetDirectoryName(_filePath);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
try
{
lock (_fileLocks[_filePath])
{
using (var sw = new StreamWriter(_filePath, false, _encoding))
{
foreach (var section in _iniData)
{
if (_comments.ContainsKey(section.Key))
{
foreach (var comment in _comments[section.Key])
{
sw.WriteLine(comment);
}
}
sw.WriteLine($"[{section.Key}]");
foreach (var kvp in section.Value)
{
if (!string.IsNullOrEmpty(kvp.Value.Comment))
{
sw.WriteLine($"; {kvp.Value.Comment}");
}
sw.WriteLine($"{kvp.Key}={kvp.Value.Value}");
}
sw.WriteLine();
}
}
}
}
catch (Exception ex)
{
throw new IOException($"Failed to save data to file '{_filePath}': {ex.Message}");
}
}
/// <summary>
/// Adds a comment to a specific key.
/// </summary>
/// <param name="section">The section name.</param>
/// <param name="key">The key name.</param>
/// <param name="comment">The comment text (without the semicolon).</param>
/// <exception cref="ArgumentException">Thrown when the section/key does not exist.</exception>
public void AddKeyComment(string section, string key, string comment)
{
if (_iniData.ContainsKey(section) && _iniData[section].ContainsKey(key))
{
_iniData[section][key].Comment = comment;
}
else
{
throw new ArgumentException($"The specified section '{section}' and key '{key}' does not exist.");
}
}
/// <summary>
/// Adds a comment to a section (appears before the section header).
/// </summary>
/// <param name="section">The section name.</param>
/// <param name="comment">The comment text (without prefix).</param>
/// <param name="prefix">The comment prefix character. Default is `;`. Use `#` or `//` as alternatives.</param>
public void SetComment(string section, string comment, string prefix = ";")
{
if (!_comments.ContainsKey(section))
{
_comments[section] = new List<string>();
}
_comments[section].Add(prefix + comment);
}
/// <summary>
/// Removes a section comment.
/// </summary>
/// <param name="section">The section name.</param>
/// <param name="comment">The comment text to remove (without the semicolon).</param>
public void RemoveComment(string section, string comment)
{
if (_comments.ContainsKey(section))
{
_comments[section].Remove(";" + comment);
}
}
/// <summary>
/// Clears all comments from the INI file.
/// </summary>
public void ClearAllComments()
{
foreach (var section in _comments.Keys.ToList())
{
_comments[section].Clear();
}
}
/// <summary>
/// Destructor to clean up the file lock.
/// </summary>
~IniFileClass()
{
if (!string.IsNullOrEmpty(_filePath))
{
lock (_fileLocks)
{
_fileLocks.Remove(_filePath);
}
}
}
}
}