Mercurial > repos > IBBoard.Ini
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 |
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 | 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 | 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 | 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 | 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 | 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 | 46 |
47 public IEnumerator<IniSection> GetEnumerator() | |
48 { | |
49 return sections.Values.GetEnumerator(); | |
50 } | |
51 | |
52 IEnumerator IEnumerable.GetEnumerator() | |
53 { | |
54 return sections.Values.GetEnumerator(); | |
55 } | |
56 | |
57 //// <value> | |
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. | |
59 /// </value> | |
60 public IniSection this[string sectionName] | |
61 { | |
62 get | |
63 { | |
64 IniSection section = null; | |
65 sections.TryGetValue(sectionName.Trim(), out section); | |
66 | |
67 if (section == null) | |
68 { | |
69 section = new NonExistantIniSection(); | |
70 } | |
71 | |
72 return section; | |
73 } | |
74 } | |
75 | |
2 | 76 /// <summary> |
77 /// Checks whether the IniFile contains an <see cref=" IniSection"/> with the specified name | |
78 /// </summary> | |
79 /// <param name="sectionName"> | |
80 /// The name of the section to look for | |
81 /// </param> | |
82 /// <returns> | |
83 /// <code>true</code> if the section is in the file, else <code>false</code> | |
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 | 89 |
90 /// <summary> | |
91 /// Gets an array of the <see cref=" IniSection"/>s in the file | |
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 | 102 |
103 public override string ToString() | |
104 { | |
105 StringBuilder stringBuilder = new StringBuilder(); | |
106 | |
107 foreach (IniSection section in this) | |
108 { | |
109 stringBuilder.Append(section.ToString()); | |
110 stringBuilder.Append("\n"); | |
111 } | |
112 | |
113 return stringBuilder.ToString().Trim(); | |
114 } | |
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 } |