-
Notifications
You must be signed in to change notification settings - Fork 5
/
Ini.cs
206 lines (179 loc) · 5.76 KB
/
Ini.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
public class Ini
{
Dictionary<string, Dictionary<string, string>> ini = new Dictionary<string, Dictionary<string, string>>(StringComparer.InvariantCultureIgnoreCase);
string file;
/// <summary>
/// Initialize an INI file
/// Load it if it exists
/// </summary>
/// <param name="file">Full path where the INI file has to be read from or written to</param>
public Ini(string file)
{
this.file = file;
if (!File.Exists(file))
return;
Load();
}
/// <summary>
/// Load the INI file content
/// </summary>
public void Load()
{
var txt = File.ReadAllText(file);
Dictionary<string, string> currentSection = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
ini[""] = currentSection;
foreach (var l in txt.Split(new[] { "\n" }, StringSplitOptions.RemoveEmptyEntries)
.Select((t, i) => new
{
idx = i,
text = t.Trim()
}))
// .Where(t => !string.IsNullOrWhiteSpace(t) && !t.StartsWith(";")))
{
var line = l.text;
if (line.StartsWith(";") || string.IsNullOrWhiteSpace(line))
{
currentSection.Add(";" + l.idx.ToString(), line);
continue;
}
if (line.StartsWith("[") && line.EndsWith("]"))
{
currentSection = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
ini[line.Substring(1, line.Length - 2)] = currentSection;
continue;
}
var idx = line.IndexOf("=");
if (idx == -1)
currentSection[line] = "";
else
currentSection[line.Substring(0, idx)] = line.Substring(idx + 1);
}
}
/// <summary>
/// Get a parameter value at the root level
/// </summary>
/// <param name="key">parameter key</param>
/// <returns></returns>
public string GetValue(string key)
{
return GetValue(key, "", "");
}
/// <summary>
/// Get a parameter value in the section
/// </summary>
/// <param name="key">parameter key</param>
/// <param name="section">section</param>
/// <returns></returns>
public string GetValue(string key, string section)
{
return GetValue(key, section, "");
}
/// <summary>
/// Returns a parameter value in the section, with a default value if not found
/// </summary>
/// <param name="key">parameter key</param>
/// <param name="section">section</param>
/// <param name="default">default value</param>
/// <returns></returns>
public string GetValue(string key, string section, string @default)
{
if (!ini.ContainsKey(section))
return @default;
if (!ini[section].ContainsKey(key))
return @default;
return ini[section][key];
}
/// <summary>
/// Save the INI file
/// </summary>
public void Save()
{
var sb = new StringBuilder();
foreach (var section in ini)
{
if (section.Key != "")
{
sb.AppendFormat("[{0}]", section.Key);
sb.AppendLine();
}
foreach (var keyValue in section.Value)
{
if (keyValue.Key.StartsWith(";"))
{
sb.Append(keyValue.Value);
sb.AppendLine();
}
else
{
sb.AppendFormat("{0}={1}", keyValue.Key, keyValue.Value);
sb.AppendLine();
}
}
if (!endWithCRLF(sb))
sb.AppendLine();
}
File.WriteAllText(file, sb.ToString());
}
bool endWithCRLF(StringBuilder sb)
{
if (sb.Length < 4)
return sb[sb.Length - 2] == '\r' &&
sb[sb.Length - 1] == '\n';
else
return sb[sb.Length - 4] == '\r' &&
sb[sb.Length - 3] == '\n' &&
sb[sb.Length - 2] == '\r' &&
sb[sb.Length - 1] == '\n';
}
/// <summary>
/// Write a parameter value at the root level
/// </summary>
/// <param name="key">parameter key</param>
/// <param name="value">parameter value</param>
public void WriteValue(string key, string value)
{
WriteValue(key, "", value);
}
/// <summary>
/// Write a parameter value in a section
/// </summary>
/// <param name="key">parameter key</param>
/// <param name="section">section</param>
/// <param name="value">parameter value</param>
public void WriteValue(string key, string section, string value)
{
Dictionary<string, string> currentSection;
if (!ini.ContainsKey(section))
{
currentSection = new Dictionary<string, string>();
ini.Add(section, currentSection);
}
else
currentSection = ini[section];
currentSection[key] = value;
}
/// <summary>
/// Get all the keys names in a section
/// </summary>
/// <param name="section">section</param>
/// <returns></returns>
public string[] GetKeys(string section)
{
if (!ini.ContainsKey(section))
return new string[0];
return ini[section].Keys.ToArray();
}
/// <summary>
/// Get all the section names of the INI file
/// </summary>
/// <returns></returns>
public string[] GetSections()
{
return ini.Keys.Where(t => t != "").ToArray();
}
}