-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiniParser.cs
More file actions
250 lines (208 loc) · 7.14 KB
/
iniParser.cs
File metadata and controls
250 lines (208 loc) · 7.14 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
using System;
using System.IO;
using System.Collections;
public class iniParser
{
private Hashtable keyPairs = new Hashtable();
private String _parameters;
private struct SectionPair
{
public String Section;
public String Key;
}
/// <summary>
/// Constructor
/// </summary>
public iniParser(String parameters)
{
_parameters = parameters;
String strLine = null;
String currentRoot = null;
String[] keyPair = null;
try
{
// iniFile = new StreamReader(parameters);
StringReader reader = new StringReader(parameters);
while ((strLine = reader.ReadLine()) != null)
{
strLine = strLine.Trim();
if (strLine != "")
{
if (strLine.StartsWith("[") && strLine.EndsWith("]"))
currentRoot = strLine.Substring(1, strLine.Length - 2);
else if (strLine.Contains("=")) //tylko linijki gdzie jest =
{
keyPair = strLine.Split(new char[] { '=' }, 2);
SectionPair sectionPair;
String value = null;
if (currentRoot == null)
currentRoot = "ROOT";
sectionPair.Section = currentRoot;
sectionPair.Key = keyPair[0];
if (keyPair.Length > 1)
value = keyPair[1];
keyPairs.Add(sectionPair, value);
}
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
// }
// else
// throw new FileNotFoundException("Unable to locate " + iniPath);
}
/// <summary>
/// Pobierz cala sekcje podaje sie sama nazwe sekcji np [SQL]
/// </summary>
/// <param name="sectionName">Bez nawiasow kwadratowych nazwa sekcji np SQL</param>
public string getSectionString(String sectionName)
{
StringReader reader = new StringReader(_parameters);
String strLine = null;
String currentRoot = null;
string retval = "";
while ((strLine = reader.ReadLine()) != null)
{
strLine = strLine.Trim();
if (strLine != "")
{
if (strLine.StartsWith("[") && strLine.EndsWith("]")) //znaleziono zapis sekcji
currentRoot = strLine.Substring(1, strLine.Length - 2); //bez nawiasow kwadratowych
if (currentRoot == sectionName && string.Format("[{0}]", currentRoot) != strLine) //sekcja = sekcji podanej pobierz wartosci ale samej sekcji nei bierz
retval += strLine + "\n";
}
}
return retval;
}
/// <summary>
/// Returns the value for the given section, key pair.
/// </summary>
/// <param name="sectionName">Section name. np SQL (bez nawiasow kwadratowych)</param>
/// <param name="settingName">Key name. np dupa</param>
public String GetSetting(String sectionName, String settingName)
{
SectionPair sectionPair;
sectionPair.Section = sectionName;
sectionPair.Key = settingName;
return (String)keyPairs[sectionPair];
}
/// <summary>
/// Enumerates all lines for given section.
/// </summary>
/// <param name="sectionName">Section to enum.</param>
public String[] EnumSection(String sectionName)
{
ArrayList tmpArray = new ArrayList();
foreach (SectionPair pair in keyPairs.Keys)
{
if (pair.Section == sectionName.ToUpper())
tmpArray.Add(pair.Key);
}
return (String[])tmpArray.ToArray(typeof(String));
}
/// <summary>
/// Iteracja parametrów @
/// </summary>
/// <param name="sectionName"></param>
/// <returns></returns>
public ArrayList EnumParameters()
{
ArrayList tmpArray = new ArrayList();
foreach (SectionPair pair in keyPairs.Keys)
{
if (pair.Section.StartsWith("@"))
tmpArray.Add(pair.Section);
}
return tmpArray;
}
/// <summary>
/// Adds or replaces a setting to the table to be saved.
/// </summary>
/// <param name="sectionName">Section to add under. np [SQL]</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.ToUpper();
sectionPair.Key = settingName.ToUpper();
if (keyPairs.ContainsKey(sectionPair))
keyPairs.Remove(sectionPair);
keyPairs.Add(sectionPair, settingValue);
}
/// <summary>
/// Adds or replaces a setting to the table to be saved with a null value.
/// </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.ToUpper();
sectionPair.Key = settingName.ToUpper();
if (keyPairs.ContainsKey(sectionPair))
keyPairs.Remove(sectionPair);
}
/// <summary>
/// Nieobsluzone Save settings to new file.
/// </summary>
/// <param name="newFilePath">New file path.</param>
public void SaveSettings(String newFilePath)
{
ArrayList sections = new ArrayList();
String tmpValue = "";
String strToSave = "";
foreach (SectionPair sectionPair in keyPairs.Keys)
{
if (!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)
{
tmpValue = (String)keyPairs[sectionPair];
if (tmpValue != null)
tmpValue = "=" + tmpValue;
strToSave += (sectionPair.Key + tmpValue + "\r\n");
}
}
strToSave += "\r\n";
}
try
{
TextWriter tw = new StreamWriter(newFilePath);
tw.Write(strToSave);
tw.Close();
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Nieobsluzone Save settings back to ini file.
/// </summary>
public void SaveSettings()
{
// SaveSettings(iniFilePath);
}
}