-
Notifications
You must be signed in to change notification settings - Fork 0
/
IniFile.cs
298 lines (257 loc) · 8.97 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace IniFile
{
/// <summary>
/// Represents a property in an INI file.
/// </summary>
public class IniProperty
{
/// <summary>
/// Property name (key).
/// </summary>
public string Name { get; set; }
/// <summary>
/// Property value.
/// </summary>
public string Value { get; set; }
/// <summary>
/// Set the comment to display above this property.
/// </summary>
public string Comment { get; set; }
}
/// <summary>
/// Represents a section in an INI file.
/// </summary>
public class IniSection
{
private readonly IDictionary<string, IniProperty> _properties;
/// <summary>
/// Section name.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Set the comment to display above this section.
/// </summary>
public string Comment { get; set; }
/// <summary>
/// Get the properties in this section.
/// </summary>
public IniProperty[] Properties => _properties.Values.ToArray();
/// <summary>
/// Create a new IniSection.
/// </summary>
/// <param name="name"></param>
public IniSection(string name)
{
Name = name;
_properties = new Dictionary<string, IniProperty>();
}
/// <summary>
/// Get a property value.
/// </summary>
/// <param name="name">Name of the property.</param>
/// <returns>Value of the property or null if it doesn't exist.</returns>
public string Get(string name)
{
if (_properties.ContainsKey(name))
return _properties[name].Value;
return null;
}
/// <summary>
/// Get a property value, coercing the type of the value
/// into the type given by the generic parameter.
/// </summary>
/// <param name="name">Name of the property.</param>
/// <typeparam name="T">The type to coerce the value into.</typeparam>
/// <returns></returns>
public T Get<T>(string name)
{
if (_properties.ContainsKey(name))
return (T)Convert.ChangeType(_properties[name].Value, typeof(T));
return default(T);
}
/// <summary>
/// Set a property value.
/// </summary>
/// <param name="name">Name of the property.</param>
/// <param name="value">Value of the property.</param>
/// <param name="comment">A comment to display above the property.</param>
public void Set(string name, string value, string comment = null)
{
if (string.IsNullOrWhiteSpace(value))
{
RemoveProperty(name);
return;
}
if (!_properties.ContainsKey(name))
_properties.Add(name, new IniProperty { Name = name, Value = value, Comment = comment });
else
{
_properties[name].Value = value;
if (comment != null)
_properties[name].Comment = comment;
}
}
/// <summary>
/// Remove a property from this section.
/// </summary>
/// <param name="propertyName">The property name to remove.</param>
public void RemoveProperty(string propertyName)
{
if (_properties.ContainsKey(propertyName))
_properties.Remove(propertyName);
}
}
/// <summary>
/// Represenst an INI file that can be read from or written to.
/// </summary>
public class IniFile
{
private readonly IDictionary<string, IniSection> _sections;
/// <summary>
/// If True, writes extra spacing between the property name and the property value.
/// (foo=bar) vs (foo = bar)
/// </summary>
public bool WriteSpacingBetweenNameAndValue { get; set; }
/// <summary>
/// The character a comment line will begin with. Default '#'.
/// </summary>
public char CommentChar { get; set; }
/// <summary>
/// Get the sections in this IniFile.
/// </summary>
public IniSection[] Sections => _sections.Values.ToArray();
/// <summary>
/// Create a new IniFile instance.
/// </summary>
public IniFile()
{
_sections = new Dictionary<string, IniSection>();
CommentChar = '#';
}
/// <summary>
/// Load an INI file from the file system.
/// </summary>
/// <param name="path">Path to the INI file.</param>
public IniFile(string path) : this()
{
Load(path);
}
/// <summary>
/// Load an INI file.
/// </summary>
/// <param name="reader">A TextReader instance.</param>
public IniFile(TextReader reader) : this()
{
Load(reader);
}
private void Load(string path)
{
using (var file = new StreamReader(path))
Load(file);
}
private void Load(TextReader reader)
{
IniSection section = null;
string line;
while ((line = reader.ReadLine()) != null)
{
line = line.Trim();
// skip empty lines
if (line == string.Empty)
continue;
// skip comments
if (line.StartsWith(";") || line.StartsWith("#"))
continue;
if (line.StartsWith("[") && line.EndsWith("]"))
{
var sectionName = line.Substring(1, line.Length - 2);
if (!_sections.ContainsKey(sectionName))
{
section = new IniSection(sectionName);
_sections.Add(sectionName, section);
}
continue;
}
if (section != null)
{
var keyValue = line.Split(new[] { "=" }, 2, StringSplitOptions.RemoveEmptyEntries);
if (keyValue.Length != 2)
continue;
section.Set(keyValue[0].Trim(), keyValue[1].Trim());
}
}
}
/// <summary>
/// Get a section by name. If the section doesn't exist, it is created.
/// </summary>
/// <param name="sectionName">The name of the section.</param>
/// <returns>A section. If the section doesn't exist, it is created.</returns>
public IniSection Section(string sectionName)
{
IniSection section;
if (!_sections.TryGetValue(sectionName, out section))
{
section = new IniSection(sectionName);
_sections.Add(sectionName, section);
}
return section;
}
/// <summary>
/// Remove a section.
/// </summary>
/// <param name="sectionName">Name of the section to remove.</param>
public void RemoveSection(string sectionName)
{
if (_sections.ContainsKey(sectionName))
_sections.Remove(sectionName);
}
/// <summary>
/// Create a new INI file.
/// </summary>
/// <param name="path">Path to the INI file to create.</param>
public void Save(string path)
{
using (var file = new StreamWriter(path))
Save(file);
}
/// <summary>
/// Create a new INI file.
/// </summary>
/// <param name="writer">A TextWriter instance.</param>
public void Save(TextWriter writer)
{
foreach (var section in _sections.Values)
{
if (section.Properties.Length == 0)
continue;
if (section.Comment != null)
writer.WriteLine($"{CommentChar} {section.Comment}");
writer.WriteLine($"[{section.Name}]");
foreach (var property in section.Properties)
{
if (property.Comment != null)
writer.WriteLine($"{CommentChar} {property.Comment}");
var format = WriteSpacingBetweenNameAndValue ? "{0} = {1}" : "{0}={1}";
writer.WriteLine(format, property.Name, property.Value);
}
writer.WriteLine();
}
}
/// <summary>
/// Returns the content of this INI file as a string.
/// </summary>
/// <returns>The text content of this INI file.</returns>
public override string ToString()
{
using (var sw = new StringWriter())
{
Save(sw);
return sw.ToString();
}
}
}
}