annotate IniFile.cs @ 4:01fc09ab63e2

Re #7 - Add tests for INI parsing * Add DuplicateIniSectionException to make exception cause more obvious
author IBBoard <dev@ibboard.co.uk>
date Sun, 18 Jan 2009 14:29:52 +0000
parents 2dde4c1d19d9
children 7e06c0699257
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 // This file (IniFile.cs) is a part of the IBBoard.Ini library and is copyright 2009 IBBoard.
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 // The file and the library/program it is in are licensed under the GNU LGPL license. Please see COPYING.LGPL for more information and the full license.
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 using System;
1
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
6 using System.Collections;
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 using System.Collections.Generic;
1
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
8 using System.Text;
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 namespace IBBoard.Ini
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 {
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 /// <summary>
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 /// The <code>IniFile</code> provides access to INI formatted data. INI files are no longer used as much as they were, but many older applications including parts of Windows still use them to store data and configuration values in.
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 /// </summary>
1
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
15 public class IniFile : IEnumerable<IniSection>
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 {
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 private Dictionary<string, IniSection> sections;
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 /// <summary>
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 /// Default constructor that provides an empty INI file with no sections
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 /// </summary>
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 public IniFile()
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 {
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 sections = new Dictionary<string,IniSection>();
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 }
1
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
26
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 /// <summary>
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 /// Adds an <see cref="IniSection"/> to the IniFile.
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 /// <p>
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 /// Throws an ArgumentException if the section already exists in this file
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 /// </summary>
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 /// <param name="section">
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 /// The <see cref="IniSection"/> to add
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 /// </param>
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 public void AddSection(IniSection section)
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 {
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 if (!HasSection(section.Name))
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 {
1
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
39 sections.Add(section.Name, section);
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 }
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 else
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 {
4
01fc09ab63e2 Re #7 - Add tests for INI parsing
IBBoard <dev@ibboard.co.uk>
parents: 2
diff changeset
43 throw new DuplicateIniSectionException(section.Name);
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 }
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 }
1
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
46
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
47 public IEnumerator<IniSection> GetEnumerator()
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
48 {
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
49 return sections.Values.GetEnumerator();
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
50 }
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
51
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
52 IEnumerator IEnumerable.GetEnumerator()
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
53 {
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
54 return sections.Values.GetEnumerator();
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
55 }
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
56
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
57 //// <value>
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
58 /// Gets a single section by name. If no section exists with that name then a <see cref=" NonExistantIniSection"/> is returned to remove the need for null checks. To check for the existance of a section use the <code>HasSection</code> method.
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
59 /// </value>
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
60 public IniSection this[string sectionName]
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
61 {
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
62 get
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
63 {
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
64 IniSection section = null;
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
65 sections.TryGetValue(sectionName.Trim(), out section);
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
66
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
67 if (section == null)
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
68 {
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
69 section = new NonExistantIniSection();
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
70 }
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
71
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
72 return section;
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
73 }
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
74 }
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
75
2
2dde4c1d19d9 Closes #6 - Create INI parser
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
76 /// <summary>
2dde4c1d19d9 Closes #6 - Create INI parser
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
77 /// Checks whether the IniFile contains an <see cref=" IniSection"/> with the specified name
2dde4c1d19d9 Closes #6 - Create INI parser
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
78 /// </summary>
2dde4c1d19d9 Closes #6 - Create INI parser
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
79 /// <param name="sectionName">
2dde4c1d19d9 Closes #6 - Create INI parser
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
80 /// The name of the section to look for
2dde4c1d19d9 Closes #6 - Create INI parser
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
81 /// </param>
2dde4c1d19d9 Closes #6 - Create INI parser
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
82 /// <returns>
2dde4c1d19d9 Closes #6 - Create INI parser
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
83 /// <code>true</code> if the section is in the file, else <code>false</code>
2dde4c1d19d9 Closes #6 - Create INI parser
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
84 /// </returns>
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
85 public bool HasSection(string sectionName)
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
86 {
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
87 return sections.ContainsKey(sectionName.Trim());
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
88 }
2
2dde4c1d19d9 Closes #6 - Create INI parser
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
89
2dde4c1d19d9 Closes #6 - Create INI parser
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
90 /// <summary>
2dde4c1d19d9 Closes #6 - Create INI parser
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
91 /// Gets an array of the <see cref=" IniSection"/>s in the file
2dde4c1d19d9 Closes #6 - Create INI parser
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
92 /// </summary>
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
93 public IniSection[] Sections
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
94 {
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
95 get
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
96 {
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
97 IniSection[] col = new IniSection[sections.Count];
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
98 sections.Values.CopyTo(col, 0);
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
99 return col;
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
100 }
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
101 }
1
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
102
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
103 public override string ToString()
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
104 {
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
105 StringBuilder stringBuilder = new StringBuilder();
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
106
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
107 foreach (IniSection section in this)
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
108 {
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
109 stringBuilder.Append(section.ToString());
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
110 stringBuilder.Append("\n");
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
111 }
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
112
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
113 return stringBuilder.ToString().Trim();
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
114 }
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
115
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
116 }
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
117 }