WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
IniFile.cs
Go to the documentation of this file.
1 
2 using Microsoft.VisualBasic;
3 using System;
4 using System.Collections;
5 using System.Collections.Generic;
6 using System.Data;
7 using System.Diagnostics;
8 using System.IO;
9 using System.Text.RegularExpressions;
10 
11 namespace DracLabs
12 {
13  public class IniFile
14  {
15  // List of IniSection objects keeps track of all the sections in the INI file
16  private Hashtable m_sections;
17  private ArrayList a_sections;
18  // Public constructor
19  public IniFile()
20  {
21  m_sections = new Hashtable(StringComparer.InvariantCultureIgnoreCase);
22  a_sections = new ArrayList();
23  }
24 
25  // Loads the Reads the data in the ini file into the IniFile object
26  public void Load(string sFileName, bool bMerge = false)
27  {
28  if (!bMerge)
29  {
31  }
32  // Clear the object...
33  IniSection tempsection = null;
34  StreamReader oReader = new StreamReader(sFileName);
35  Regex regexcomment = new Regex("^([\\s]*#.*)", (RegexOptions.Singleline | RegexOptions.IgnoreCase));
36  // Broken but left for history
37  //Dim regexsection As New Regex("\[[\s]*([^\[\s].*[^\s\]])[\s]*\]", (RegexOptions.Singleline Or RegexOptions.IgnoreCase))
38  Regex regexsection = new Regex("^[\\s]*\\[[\\s]*([^\\[\\s].*[^\\s\\]])[\\s]*\\][\\s]*$", (RegexOptions.Singleline | RegexOptions.IgnoreCase));
39  Regex regexkey = new Regex("^\\s*([^=\\s]*)[^=]*=(.*)", (RegexOptions.Singleline | RegexOptions.IgnoreCase));
40  while (!oReader.EndOfStream)
41  {
42  string line = oReader.ReadLine();
43  if (line != string.Empty)
44  {
45  Match m = null;
46  if (regexcomment.Match(line).Success)
47  {
48  m = regexcomment.Match(line);
49  Trace.WriteLine(string.Format("Skipping Comment: {0}", m.Groups[0].Value));
50  }
51  else if (regexsection.Match(line).Success)
52  {
53  m = regexsection.Match(line);
54  Trace.WriteLine(string.Format("Adding section [{0}]", m.Groups[1].Value));
55  tempsection = AddSection(m.Groups[1].Value);
56  }
57  else if (regexkey.Match(line).Success && tempsection != null)
58  {
59  m = regexkey.Match(line);
60  Trace.WriteLine(string.Format("Adding Key [{0}]=[{1}]", m.Groups[1].Value, m.Groups[2].Value));
61  tempsection.AddKey(m.Groups[1].Value).Value = m.Groups[2].Value;
62  }
63  else if (tempsection != null)
64  {
65  // Handle Key without value
66  Trace.WriteLine(string.Format("Adding Key [{0}]", line));
67  tempsection.AddKey(line);
68  }
69  else
70  {
71  // This should not occur unless the tempsection is not created yet...
72  Trace.WriteLine(string.Format("Skipping unknown type of data: {0}", line));
73  }
74  }
75  }
76  oReader.Close();
77  }
78 
79  // Used to save the data back to the file or your choice
80  public void Save(string sFileName)
81  {
82  StreamWriter oWriter = new StreamWriter(sFileName, false);
83  foreach (IniSection s in Sections)
84  {
85  Trace.WriteLine(string.Format("Writing Section: [{0}]", s.Name));
86  oWriter.WriteLine(string.Format("[{0}]", s.Name));
87  foreach (IniSection.IniKey k in s.Keys)
88  {
89  if (k.Value != string.Empty)
90  {
91  Trace.WriteLine(string.Format("Writing Key: {0}={1}", k.Name, k.Value));
92  oWriter.WriteLine(string.Format("{0}={1}", k.Name, k.Value));
93  }
94  else
95  {
96  Trace.WriteLine(string.Format("Writing Key: {0}", k.Name));
97  oWriter.WriteLine(string.Format("{0}", k.Name));
98  }
99  }
100  }
101  oWriter.Close();
102  }
103 
104  // Gets all the sections
105  public System.Collections.ICollection Sections
106  {
107  get { return m_sections.Values; }
108  }
109 
110  // SectionList - Preserves the original order of the sections
111  public ArrayList SectionList
112  {
113  get { return a_sections; }
114  }
115 
116  // Adds a section to the IniFile object, returns a IniSection object to the new or existing object
117  public IniSection AddSection(string sSection)
118  {
119  IniSection s = null;
120  sSection = sSection.Trim();
121  // Trim spaces
122  if (m_sections.ContainsKey(sSection))
123  {
124  s = (IniSection)m_sections[sSection];
125  }
126  else
127  {
128  s = new IniSection(this, sSection);
129  m_sections[sSection] = s;
130  a_sections.Add(sSection);
131  }
132  return s;
133  }
134 
135  // Removes a section by its name sSection, returns trus on success
136  public bool RemoveSection(string sSection)
137  {
138  sSection = sSection.Trim();
139  return RemoveSection(GetSection(sSection));
140  }
141 
142  // Removes section by object, returns trus on success
143  public bool RemoveSection(IniSection Section)
144  {
145  if (Section != null)
146  {
147  try
148  {
149  m_sections.Remove(Section.Name);
150  return true;
151  }
152  catch (Exception ex)
153  {
154  Trace.WriteLine(ex.Message);
155  }
156  }
157  return false;
158  }
159 
160  // Removes all existing sections, returns trus on success
161  public bool RemoveAllSections()
162  {
163  m_sections.Clear();
164  return (m_sections.Count == 0);
165  }
166 
167  // Returns an IniSection to the section by name, NULL if it was not found
168  public IniSection GetSection(string sSection)
169  {
170  sSection = sSection.Trim();
171  // Trim spaces
172  if (m_sections.ContainsKey(sSection))
173  {
174  return (IniSection)m_sections[sSection];
175  }
176  return null;
177  }
178 
179  // Returns a KeyValue in a certain section
180  public string GetKeyValue(string sSection, string sKey)
181  {
182  IniSection s = GetSection(sSection);
183  if (s != null)
184  {
185  IniSection.IniKey k = s.GetKey(sKey);
186  if (k != null)
187  {
188  return k.Value;
189  }
190  }
191  return string.Empty;
192  }
193 
194  // Sets a KeyValuePair in a certain section
195  public bool SetKeyValue(string sSection, string sKey, string sValue)
196  {
197  IniSection s = AddSection(sSection);
198  if (s != null)
199  {
200  IniSection.IniKey k = s.AddKey(sKey);
201  if (k != null)
202  {
203  k.Value = sValue;
204  return true;
205  }
206  }
207  return false;
208  }
209 
210  // Renames an existing section returns true on success, false if the section didn't exist or there was another section with the same sNewSection
211  public bool RenameSection(string sSection, string sNewSection)
212  {
213  // Note string trims are done in lower calls.
214  bool bRval = false;
215  IniSection s = GetSection(sSection);
216  if (s != null)
217  {
218  bRval = s.SetName(sNewSection);
219  }
220  return bRval;
221  }
222 
223  // Renames an existing key returns true on success, false if the key didn't exist or there was another section with the same sNewKey
224  public bool RenameKey(string sSection, string sKey, string sNewKey)
225  {
226  // Note string trims are done in lower calls.
227  IniSection s = GetSection(sSection);
228  if (s != null)
229  {
230  IniSection.IniKey k = s.GetKey(sKey);
231  if (k != null)
232  {
233  return k.SetName(sNewKey);
234  }
235  }
236  return false;
237  }
238 
239  // Remove a key by section name and key name
240  public bool RemoveKey(string sSection, string sKey)
241  {
242  IniSection s = GetSection(sSection);
243  if (s != null)
244  {
245  return s.RemoveKey(sKey);
246  }
247  return false;
248  }
249 
250  // IniSection class
251  public class IniSection
252  {
253  // IniFile IniFile object instance
254  private IniFile m_pIniFile;
255  // Name of the section
256  private string m_sSection;
257  // List of IniKeys in the section
258 
259  private Hashtable m_keys;
260  // Constuctor so objects are internally managed
261  protected internal IniSection(IniFile parent, string sSection)
262  {
263  m_pIniFile = parent;
264  m_sSection = sSection;
265  m_keys = new Hashtable(StringComparer.InvariantCultureIgnoreCase);
266  }
267 
268  // Returns all the keys in a section
269  public System.Collections.ICollection Keys
270  {
271  get { return m_keys.Values; }
272  }
273 
274  // Returns the section name
275  public string Name
276  {
277  get { return m_sSection; }
278  }
279 
280  // Adds a key to the IniSection object, returns a IniKey object to the new or existing object
281  public IniKey AddKey(string sKey)
282  {
283  sKey = sKey.Trim();
284  IniSection.IniKey k = null;
285  if (sKey.Length != 0)
286  {
287  if (m_keys.ContainsKey(sKey))
288  {
289  k = (IniKey)m_keys[sKey];
290  }
291  else
292  {
293  k = new IniSection.IniKey(this, sKey);
294  m_keys[sKey] = k;
295  }
296  }
297  return k;
298  }
299 
300  // Removes a single key by string
301  public bool RemoveKey(string sKey)
302  {
303  return RemoveKey(GetKey(sKey));
304  }
305 
306  // Removes a single key by IniKey object
307  public bool RemoveKey(IniKey Key)
308  {
309  if (Key != null)
310  {
311  try
312  {
313  m_keys.Remove(Key.Name);
314  return true;
315  }
316  catch (Exception ex)
317  {
318  Trace.WriteLine(ex.Message);
319  }
320  }
321  return false;
322  }
323 
324  // Removes all the keys in the section
325  public bool RemoveAllKeys()
326  {
327  m_keys.Clear();
328  return (m_keys.Count == 0);
329  }
330 
331  // Returns a IniKey object to the key by name, NULL if it was not found
332  public IniKey GetKey(string sKey)
333  {
334  sKey = sKey.Trim();
335  if (m_keys.ContainsKey(sKey))
336  {
337  return (IniKey)m_keys[sKey];
338  }
339  return null;
340  }
341 
342  // Sets the section name, returns true on success, fails if the section
343  // name sSection already exists
344  public bool SetName(string sSection)
345  {
346  sSection = sSection.Trim();
347  if (sSection.Length != 0)
348  {
349  // Get existing section if it even exists...
350  IniSection s = m_pIniFile.GetSection(sSection);
351  if (!object.ReferenceEquals(s, this) && s != null)
352  {
353  return false;
354  }
355  try
356  {
357  // Remove the current section
358  m_pIniFile.m_sections.Remove(m_sSection);
359  // Set the new section name to this object
360  m_pIniFile.m_sections[sSection] = this;
361  // Set the new section name
362  m_sSection = sSection;
363  return true;
364  }
365  catch (Exception ex)
366  {
367  Trace.WriteLine(ex.Message);
368  }
369  }
370  return false;
371  }
372 
373  // Returns the section name
374  public string GetName()
375  {
376  return m_sSection;
377  }
378 
379  // IniKey class
380  public class IniKey
381  {
382  // Name of the Key
383  private string m_sKey;
384  // Value associated
385  private string m_sValue;
386  // Pointer to the parent CIniSection
387 
388  private IniSection m_section;
389  // Constuctor so objects are internally managed
390  protected internal IniKey(IniSection parent, string sKey)
391  {
392  m_section = parent;
393  m_sKey = sKey;
394  }
395 
396  // Returns the name of the Key
397  public string Name
398  {
399  get { return m_sKey; }
400  }
401 
402  // Sets or Gets the value of the key
403  public string Value
404  {
405  get { return m_sValue; }
406  set { m_sValue = value; }
407  }
408 
409  // Sets the value of the key
410  public void SetValue(string sValue)
411  {
412  m_sValue = sValue;
413  }
414  // Returns the value of the Key
415  public string GetValue()
416  {
417  return m_sValue;
418  }
419 
420  // Sets the key name
421  // Returns true on success, fails if the section name sKey already exists
422  public bool SetName(string sKey)
423  {
424  sKey = sKey.Trim();
425  if (sKey.Length != 0)
426  {
427  IniKey k = m_section.GetKey(sKey);
428  if (!object.ReferenceEquals(k, this) && k != null)
429  {
430  return false;
431  }
432  try
433  {
434  // Remove the current key
435  m_section.m_keys.Remove(m_sKey);
436  // Set the new key name to this object
437  m_section.m_keys[sKey] = this;
438  // Set the new key name
439  m_sKey = sKey;
440  return true;
441  }
442  catch (Exception ex)
443  {
444  Trace.WriteLine(ex.Message);
445  }
446  }
447  return false;
448  }
449 
450  // Returns the name of the Key
451  public string GetName()
452  {
453  return m_sKey;
454  }
455  }
456  // End of IniKey class
457  }
458  // End of IniSection class
459  }
460 }
461 
ArrayList SectionList
Definition: IniFile.cs:112
IniKey AddKey(string sKey)
Definition: IniFile.cs:281
bool RemoveSection(IniSection Section)
Definition: IniFile.cs:143
System.Collections.ICollection Keys
Definition: IniFile.cs:270
string GetKeyValue(string sSection, string sKey)
Definition: IniFile.cs:180
IniSection GetSection(string sSection)
Definition: IniFile.cs:168
bool RemoveKey(string sSection, string sKey)
Definition: IniFile.cs:240
bool RemoveAllSections()
Definition: IniFile.cs:161
bool RemoveKey(IniKey Key)
Definition: IniFile.cs:307
bool SetKeyValue(string sSection, string sKey, string sValue)
Definition: IniFile.cs:195
bool RemoveKey(string sKey)
Definition: IniFile.cs:301
bool RenameKey(string sSection, string sKey, string sNewKey)
Definition: IniFile.cs:224
bool RenameSection(string sSection, string sNewSection)
Definition: IniFile.cs:211
IniKey GetKey(string sKey)
Definition: IniFile.cs:332
IniSection AddSection(string sSection)
Definition: IniFile.cs:117
bool SetName(string sSection)
Definition: IniFile.cs:344
void SetValue(string sValue)
Definition: IniFile.cs:410
bool RemoveSection(string sSection)
Definition: IniFile.cs:136
void Save(string sFileName)
Definition: IniFile.cs:80
System.Collections.ICollection Sections
Definition: IniFile.cs:106
void Load(string sFileName, bool bMerge=false)
Definition: IniFile.cs:26