forked from barrenechea/AutoDice
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIniParser.cs
201 lines (167 loc) · 6.44 KB
/
IniParser.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
using System.Collections;
using System.IO;
using System.Linq;
namespace AutoDice
{
public class IniParser
{
private readonly string _iniFilePath;
private readonly Hashtable _keyPairs = new Hashtable();
/// <summary>
/// Opens the INI file at the given path ;and enumerates the values in the IniParser.
/// </summary>
/// <param name="iniPath">Full path to INI file.<</param>
public IniParser(string iniPath)
{
TextReader iniFile = null;
string currentRoot = null;
_iniFilePath = iniPath;
if (File.Exists(iniPath))
{
try
{
iniFile = new StreamReader(iniPath);
var strLine = iniFile.ReadLine();
while (strLine != null)
{
strLine = strLine.Trim();
if (strLine != "")
{
if (strLine.StartsWith("[") && strLine.EndsWith("]"))
{
currentRoot = strLine.Substring(1, strLine.Length - 2).Trim();
}
else
{
var keyPair = strLine.Split(new[] {'='}, 2);
SectionPair sectionPair;
string value = null;
if (currentRoot == null)
currentRoot = "ROOT";
sectionPair.Section = currentRoot;
sectionPair.Key = keyPair[0].Trim();
if (keyPair.Length > 1)
value = keyPair[1].Trim();
_keyPairs.Add(sectionPair, value);
}
}
strLine = iniFile.ReadLine();
}
}
finally
{
if (iniFile != null)
iniFile.Close();
}
}
else
throw new FileNotFoundException("Unable to locate " + iniPath);
}
/// <summary>
/// Returns the ;value for the given section, key pair.
/// </summary>
/// <param name="sectionName">Section name.</param>
/// <param name="settingName">Key name.</param>
public string GetSetting(string sectionName, string settingName)
{
SectionPair sectionPair;
sectionPair.Section = sectionName;
sectionPair.Key = settingName;
return (string) _keyPairs[sectionPair];
}
/// <summary>
/// Enumerates alllines for given section.
/// </summary>
/// <param name="sectionName">Section to enum.</param>
/// ;
public string[] EnumSection(string sectionName)
{
var tmpArray = new ArrayList();
foreach (var pair in _keyPairs.Keys.Cast<SectionPair>().Where(pair => pair.Section == sectionName))
{
tmpArray.Add(pair.Key);
}
return (string[]) tmpArray.ToArray(typeof (string));
}
/// <summary>
/// Adds or replaces a setting to the tableto be saved.
/// </summary>
/// <param name="sectionName">Section to add under.</param>
/// <param name="settingName">Key name to add.</param>
/// <param name="settingValue">Value of key.</param>
public void AddSetting(string sectionName, string settingName, string settingValue)
{
SectionPair sectionPair;
sectionPair.Section = sectionName;
sectionPair.Key = settingName;
if (_keyPairs.ContainsKey(sectionPair))
_keyPairs.Remove(sectionPair);
_keyPairs.Add(sectionPair, settingValue);
}
/// <summary>
/// Adds or replaces a setting to the tableto be saved with a nullvalue.
/// </summary>
/// <param name="sectionName">Section to add under.</param>
/// <param name="settingName">Key name to add.</param>
public void AddSetting(string sectionName, string settingName)
{
AddSetting(sectionName, settingName, null);
}
/// <summary>
/// Remove a setting.
/// </summary>
/// <param name="sectionName">Section to add under.</param>
/// <param name="settingName">Key name to add.</param>
public void DeleteSetting(string sectionName, string settingName)
{
SectionPair sectionPair;
sectionPair.Section = sectionName;
sectionPair.Key = settingName;
if (_keyPairs.ContainsKey(sectionPair))
_keyPairs.Remove(sectionPair);
}
/// <summary>
/// Save settingsto new file.
/// </summary>
/// <param name="newFilePath">New file path.</param>
public void SaveSettings(string newFilePath)
{
var sections = new ArrayList();
var strToSave = "";
foreach (
var sectionPair in
_keyPairs.Keys.Cast<SectionPair>().Where(sectionPair => !sections.Contains(sectionPair.Section)))
{
sections.Add(sectionPair.Section);
}
foreach (string section in sections)
{
strToSave += ("[" + section + "]\r\n");
foreach (SectionPair sectionPair in _keyPairs.Keys)
{
if (sectionPair.Section != section) continue;
var tmpValue = (string) _keyPairs[sectionPair];
if (tmpValue != null)
tmpValue = "=" + tmpValue;
strToSave += (sectionPair.Key + tmpValue + "\r\n");
}
strToSave += "\r\n";
}
TextWriter tw = new StreamWriter(newFilePath);
tw.Write(strToSave);
tw.Close();
}
/// <summary>
/// Save settingsback to ini file.
/// </summary>
public void SaveSettings()
{
SaveSettings(_iniFilePath);
}
private struct SectionPair
{
public string Key;
public string Section;
}
}
}