-
Notifications
You must be signed in to change notification settings - Fork 0
/
IniSection.cs
269 lines (239 loc) · 11.6 KB
/
IniSection.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
// Rampastring's INI parser
// http://www.moddb.com/members/rampastring
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
namespace Rampastring.Tools
{
/// <summary>
/// Represents a [section] in an INI file.
/// </summary>
public class IniSection : IIniSection
{
public IniSection() { }
public IniSection(string sectionName)
{
SectionName = sectionName;
}
public string SectionName { get; set; }
public List<KeyValuePair<string, string>> Keys = new List<KeyValuePair<string, string>>();
/// <summary>
/// Adds a key to the INI section.
/// Throws a <see cref="InvalidOperationException"/> if the key already exists.
/// Use <see cref="AddOrReplaceKey(string, string)"/> if you want to replace
/// an existing key instead.
/// </summary>
/// <param name="keyName">The name of the INI key.</param>
/// <param name="value">The value of the INI key.</param>
public void AddKey(string keyName, string value)
{
if (keyName == null || value == null)
throw new ArgumentException("INI keys cannot have null key names or values.");
if (Keys.FindIndex(kvp => kvp.Key == keyName) > -1)
throw new InvalidOperationException("The given key already exists in the section!");
Keys.Add(new KeyValuePair<string, string>(keyName, value));
}
/// <summary>
/// Adds a key to the INI section, or replaces the key's value if the key
/// already exists.
/// </summary>
/// <param name="keyName">The name of the INI key.</param>
/// <param name="value">The value of the INI key.</param>
public void AddOrReplaceKey(string keyName, string value)
{
if (keyName == null || value == null)
throw new ArgumentException("INI keys cannot have null key names or values.");
int index = Keys.FindIndex(k => k.Key == keyName);
if (index > -1)
Keys[index] = new KeyValuePair<string, string>(keyName, value);
else
Keys.Add(new KeyValuePair<string, string>(keyName, value));
}
/// <summary>
/// Removes a key from the INI section.
/// Does not throw an exception if the key does not exist.
/// </summary>
/// <param name="keyName">The name of the INI key to remove.</param>
public void RemoveKey(string keyName)
{
int index = Keys.FindIndex(k => k.Key == keyName);
if (index > -1)
Keys.RemoveAt(index);
}
/// <summary>
/// Returns a string value from the INI section.
/// </summary>
/// <param name="key">The name of the INI key.</param>
/// <param name="defaultValue">The value to return if the section or key wasn't found.</param>
/// <returns>The given key's value if the section and key was found. Otherwise the given defaultValue.</returns>
public string GetStringValue(string key, string defaultValue)
{
var kvp = Keys.Find(k => k.Key == key);
if (kvp.Value == null)
return defaultValue;
return kvp.Value;
}
/// <summary>
/// Returns an integer value from the INI section.
/// </summary>
/// <param name="key">The name of the INI key.</param>
/// <param name="defaultValue">The value to return if the section or key wasn't found,
/// or converting the key's value to an integer failed.</param>
/// <returns>The given key's value if the section and key was found and
/// the value is a valid integer. Otherwise the given defaultValue.</returns>
public int GetIntValue(string key, int defaultValue)
{
return Conversions.IntFromString(GetStringValue(key, string.Empty), defaultValue);
}
/// <summary>
/// Returns a double-precision floating point value from the INI section.
/// </summary>
/// <param name="key">The name of the INI key.</param>
/// <param name="defaultValue">The value to return if the section or key wasn't found,
/// or converting the key's value to a double failed.</param>
/// <returns>The given key's value if the section and key was found and
/// the value is a valid double. Otherwise the given defaultValue.</returns>
public double GetDoubleValue(string key, double defaultValue)
{
return Conversions.DoubleFromString(GetStringValue(key, string.Empty), defaultValue);
}
/// <summary>
/// Returns a single-precision floating point value from the INI section.
/// </summary>
/// <param name="key">The name of the INI key.</param>
/// <param name="defaultValue">The value to return if the section or key wasn't found,
/// or converting the key's value to a float failed.</param>
/// <returns>The given key's value if the section and key was found and
/// the value is a valid float. Otherwise the given defaultValue.</returns>
public float GetSingleValue(string key, float defaultValue)
{
return Conversions.FloatFromString(GetStringValue(key, String.Empty), defaultValue);
}
/// <summary>
/// Sets the string value of a key in the INI section.
/// If the key doesn't exist, it is created.
/// </summary>
/// <param name="key">The name of the INI key.</param>
/// <param name="value">The value of the INI key.</param>
public void SetStringValue(string key, string value)
{
AddOrReplaceKey(key, value);
}
/// <summary>
/// Sets the integer value of a key in the INI section.
/// If the key doesn't exist, it is created.
/// </summary>
/// <param name="key">The name of the INI key.</param>
/// <param name="value">The value of the INI key.</param>
public void SetIntValue(string key, int value)
{
AddOrReplaceKey(key, value.ToString());
}
/// <summary>
/// Sets the double-precision floating point value of a key in the INI section.
/// If the key doesn't exist, it is created.
/// </summary>
/// <param name="key">The name of the INI key.</param>
/// <param name="value">The value of the INI key.</param>
public void SetDoubleValue(string key, double value)
{
AddOrReplaceKey(key, value.ToString(CultureInfo.InvariantCulture));
}
/// <summary>
/// Sets the single-precision floating point value of a key in the INI section.
/// If the key doesn't exist, it is created.
/// </summary>
/// <param name="key">The name of the INI key.</param>
/// <param name="value">The value of the INI key.</param>
public void SetFloatValue(string key, float value)
{
AddOrReplaceKey(key, value.ToString(CultureInfo.InvariantCulture));
}
/// <summary>
/// Sets the boolean value of a key in the INI section.
/// If the key doesn't exist, it is created.
/// Uses the <see cref="BooleanStringStyle.TRUEFALSE"/> boolean string style.
/// </summary>
/// <param name="key">The name of the INI key.</param>
/// <param name="value">The value of the INI key.</param>
public void SetBooleanValue(string key, bool value)
{
SetBooleanValue(key, value, BooleanStringStyle.TRUEFALSE);
}
/// <summary>
/// Sets the boolean value of a key in the INI section.
/// If the key doesn't exist, it is created.
/// </summary>
/// <param name="key">The name of the INI key.</param>
/// <param name="value">The value of the INI key.</param>
/// <param name="booleanStringStyle">The boolean string style.</param>
public void SetBooleanValue(string key, bool value, BooleanStringStyle booleanStringStyle)
{
string strValue = Conversions.BooleanToString(value, booleanStringStyle);
AddOrReplaceKey(key, strValue);
}
/// <summary>
/// Returns a boolean value from the INI section.
/// </summary>
/// <param name="key">The name of the INI key.</param>
/// <param name="defaultValue">The value to return if the section or key wasn't found,
/// or converting the key's value to a boolean failed.</param>
/// <returns>The given key's value if the section and key was found and
/// the value is a valid boolean. Otherwise the given defaultValue.</returns>
public bool GetBooleanValue(string key, bool defaultValue)
{
return Conversions.BooleanFromString(GetStringValue(key, String.Empty), defaultValue);
}
/// <summary>
/// Sets the list value of a key in the INI section.
/// The list elements are converted to strings using the list element's
/// ToString method and the given separator is applied between the elements.
/// </summary>
/// <typeparam name="T">The type of the list elements.</typeparam>
/// <param name="key">The INI key.</param>
/// <param name="list">The list.</param>
/// <param name="separator">The separator between list elements.</param>
public void SetListValue<T>(string key, List<T> list, char separator)
{
AddOrReplaceKey(key, string.Join(separator.ToString(), list));
}
/// <summary>
/// Parses and returns a list value of a key in the INI section.
/// </summary>
/// <typeparam name="T">The type of the list elements.</typeparam>
/// <param name="key">The INI key.</param>
/// <param name="separator">The separator between the list elements.</param>
/// <param name="converter">The function that converts the list elements from strings to the given type.</param>
/// <returns>A list that contains the parsed elements.</returns>
public List<T> GetListValue<T>(string key, char separator, Func<string, T> converter)
{
List<T> list = new List<T>();
string value = GetStringValue(key, string.Empty);
string[] parts = value.Split(new char[] { separator }, StringSplitOptions.RemoveEmptyEntries);
foreach (string part in parts)
{
list.Add(converter(part));
}
return list;
}
/// <summary>
/// Parses and returns a path string from the INI section.
/// The path string has all of its directory separators ( / \ )
/// replaced with an environment-specific one.
/// </summary>
public string GetPathStringValue(string key, string defaultValue)
{
return GetStringValue(key, defaultValue).Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar);
}
/// <summary>
/// Checks if the specified INI key exists in this section.
/// </summary>
/// <param name="key">The INI key.</param>
/// <returns>True if the key exists in this section, otherwise false.</returns>
public bool KeyExists(string key)
{
return Keys.FindIndex(k => k.Key == key) > -1;
}
}
}