-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIniParser.cs
More file actions
375 lines (306 loc) · 9 KB
/
IniParser.cs
File metadata and controls
375 lines (306 loc) · 9 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
namespace Pluton.Core
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
public class IniParser : CountedInstance
{
public readonly string FilePath;
public readonly string Name;
public Dictionary<string, IniSection> Sections = new Dictionary<string, IniSection>();
public string this[string section, string setting] {
get {
return this[section][setting];
}
set {
if (Sections.ContainsKey(section)) {
if (Sections[section].Settings.ContainsKey(setting)) {
Sections[section].Settings[setting].Value = value;
} else {
Sections[section].Settings.Add(setting, new IniSetting(setting, value));
}
} else {
Sections.Add(section, new IniSection(section));
Sections[section].Settings.Add(setting, new IniSetting(setting, value));
}
}
}
public IniSection this[string section] {
get {
if (Sections.ContainsKey(section))
return Sections[section];
return new IniSection("");
}
}
public IniParser(string iniPath)
{
string section = "ROOT";
FilePath = iniPath;
IniSection currentSection = null;
bool inroot = true;
List<string> comments = new List<string>();
Name = Path.GetFileNameWithoutExtension(iniPath);
if (!File.Exists(iniPath))
throw new FileNotFoundException("Unable to locate " + iniPath);
using (TextReader reader = new StreamReader(iniPath)) {
for (string line = reader.ReadLine(); line != null; line = reader.ReadLine()) {
line = line.Trim();
if (line == String.Empty)
continue;
if (line.StartsWith("[") && line.EndsWith("]")) {
section = line.Substring(1, line.Length - 2);
if (!Sections.ContainsKey(section)) {
Sections.Add(section, new IniSection(section));
if (comments.Count != 0) {
Sections[section].Comments.AddRange(comments);
comments = new List<string>();
}
}
currentSection = Sections[section];
inroot = false;
} else {
if (line.StartsWith(";")) {
comments.Add(line);
continue;
}
string[] ConfEqValue = line.Split(new char[] { '=' }, 2);
if (ConfEqValue.Length == 0)
continue;
if (inroot) {
if (!Sections.ContainsKey(section)) {
Sections.Add(section, new IniSection(section));
if (comments.Count != 0) {
Sections[section].Comments.AddRange(comments);
comments = new List<string>();
}
}
currentSection = Sections[section];
inroot = false;
}
if (ConfEqValue.Length == 2)
currentSection.AddSetting(ConfEqValue[0], ConfEqValue[1]);
else if (ConfEqValue.Length == 1)
currentSection.AddSetting(ConfEqValue[0], null);
if (comments.Count != 0) {
currentSection.Settings[ConfEqValue[0]].Comments.AddRange(comments);
comments = new List<string>();
}
}
}
}
}
public void AddSectionComments(string section, params string[] comments)
{
if (Sections.ContainsKey(section))
Sections[section].Comments.AddRange(comments);
else
Logger.LogWarning($"[IniParser] There is no [{section}] section in: {FilePath}");
}
public void AddSettingComments(string section, string setting, params string[] comments)
{
if (Sections.ContainsKey(section) && Sections[section].Settings.ContainsKey(setting))
Sections[section].Settings[setting].Comments.AddRange(comments);
else
Logger.LogWarning($"[IniParser] There is no {setting} setting in [{section}] section in: {FilePath}");
}
public void AddSetting(string section, string setting)
{
this[section, setting] = String.Empty;
}
public void AddSetting(string section, string setting, string value)
{
this[section, setting] = value;
}
public int Count()
{
return Sections.Count;
}
public void DeleteSetting(string section, string setting)
{
this[section]?.Settings.Remove(setting);
}
public void DeleteSection(string section)
{
if (Sections.ContainsKey(section))
Sections.Remove(section);
}
public string[] EnumSection(string section)
{
return Sections.ContainsKey(section) ? Sections[section].Settings.Keys.ToArray() : new string[0];
}
public string GetSetting(string section, string setting)
{
return this[section, setting];
}
public string GetSetting(string section, string setting, string defaultvalue)
{
if (this[section, setting] != null)
return this[section, setting];
this[section, setting] = defaultvalue;
return defaultvalue;
}
public bool GetBoolSetting(string section, string setting)
{
bool result = false;
if (Boolean.TryParse(this[section, setting], out result))
return result;
Logger.LogWarning($"[IniParser] [{section}] -> {setting} -> {this[section, setting]} cant be converted to Boolean. ({FilePath})");
return false;
}
public bool GetBoolSetting(string section, string setting, bool defaultvalue)
{
if (this[section, setting] != null)
return GetBoolSetting(section, setting);
this[section, setting] = defaultvalue.ToString();
return defaultvalue;
}
public int GetIntSetting(string section, string setting)
{
int result = 0;
if (Int32.TryParse(this[section, setting], out result))
return result;
Logger.LogWarning($"[IniParser] [{section}] -> {setting} -> {this[section, setting]} cant be converted to Int32. ({FilePath})");
return 0;
}
public int GetIntSetting(string section, string setting, int defaultvalue)
{
if (this[section, setting] != null)
return GetIntSetting(section, setting);
this[section, setting] = defaultvalue.ToString();
return defaultvalue;
}
public void Save()
{
this.SaveSettings(FilePath);
}
public void SaveSettings(string newFilePath)
{
string result = String.Empty;
foreach (var section in Sections.Values) {
result += section.ToString() + Environment.NewLine;
}
using (TextWriter writer = new StreamWriter(newFilePath))
writer.Write(result);
}
public void SetSetting(string section, string setting, string value)
{
this[section, setting] = value;
}
public bool ContainsSetting(string section, string setting)
{
return Sections.ContainsKey(section) && Sections[section].Settings.ContainsKey(setting);
}
public bool ContainsValue(string value)
{
return Sections.Values.Any(section => {
return section.Settings.Any(setting => {
return setting.Value.Value == value;
});
});
}
public class IniSection
{
public List<string> Comments = new List<string>();
public string this[string index] {
get {
if (Settings.ContainsKey(index))
return Settings[index].Value;
return null;
}
set {
AddSetting(index, value);
}
}
public string SectionName;
public Dictionary<string, IniSetting> Settings = new Dictionary<string, IniSetting>();
public IniSection(string name)
{
SectionName = name;
}
public void AddSetting(string setting, string value)
{
if (!Settings.ContainsKey(setting))
Settings.Add(setting, new IniSetting(setting, value));
else
Settings[setting] = new IniSetting(setting, value);
}
public IniSection(string name, params string[] comments)
: this(name)
{
Comments = comments.ToList();
}
public IniSection(string name, IEnumerable<string> comments)
: this(name)
{
Comments = comments.ToList();
}
public void AddComment(string comment)
{
Comments.Add(comment);
}
public void RemoveComment(string comment)
{
Comments.Remove(comment);
}
public void ClearComments()
{
Comments.Clear();
}
public override string ToString()
{
string result = String.Empty;
string result2 = String.Empty;
if (Comments.Count != 0)
foreach (string comment in Comments)
result += ";" + comment + Environment.NewLine;
foreach (IniSetting setting in Settings.Values)
result2 += setting.ToString();
return result + $"[{SectionName}]" + Environment.NewLine + result2;
}
}
public class IniSetting
{
public List<string> Comments = new List<string>();
public string SettingName;
public string Value;
public IniSetting(string name, string value)
{
SettingName = name;
Value = value;
}
public IniSetting(string name, string value, params string[] comments)
: this(name, value)
{
Comments = comments.ToList();
}
public IniSetting(string name, string value, IEnumerable<string> comments)
: this(name, value)
{
Comments = comments.ToList();
}
public void AddComment(string comment)
{
Comments.Add(comment);
}
public void RemoveComment(string comment)
{
Comments.Remove(comment);
}
public void ClearComments()
{
Comments.Clear();
}
public override string ToString()
{
string result = String.Empty;
if (Comments.Count != 0) {
result += Environment.NewLine;
foreach (string comment in Comments)
result += ";" + comment + Environment.NewLine;
}
return result + $"{SettingName}={Value}" + Environment.NewLine;
}
}
}
}