comparison IniFile.cs @ 0:fbde5e1920ba

Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
author IBBoard <dev@ibboard.co.uk>
date Thu, 08 Jan 2009 20:33:56 +0000
parents
children f9444f1786cd
comparison
equal deleted inserted replaced
-1:000000000000 0:fbde5e1920ba
1 // This file (IniFile.cs) is a part of the IBBoard.Ini library and is copyright 2009 IBBoard.
2 //
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.
4
5 using System;
6 using System.Collections.Generic;
7
8 namespace IBBoard.Ini
9 {
10 /// <summary>
11 /// 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.
12 /// </summary>
13 public class IniFile
14 {
15 private Dictionary<string, IniSection> sections;
16
17 /// <summary>
18 /// Default constructor that provides an empty INI file with no sections
19 /// </summary>
20 public IniFile()
21 {
22 sections = new Dictionary<string,IniSection>();
23 }
24
25 /// <summary>
26 /// 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.
27 /// </summary>
28 /// <param name="sectionName">
29 /// The name of the section to get
30 /// </param>
31 /// <returns>
32 /// Either the requested <see cref="IniSection"/> or a <see cref=" NonExistantIniSection"/> if it doesn't exist
33 /// </returns>
34 public IniSection GetSection(string sectionName)
35 {
36 IniSection section = null;
37 sections.TryGetValue(sectionName.Trim(), out section);
38
39 if (section == null)
40 {
41 section = new NonExistantIniSection();
42 }
43
44 return section;
45 }
46
47 /// <summary>
48 /// Adds an <see cref="IniSection"/> to the IniFile.
49 /// <p>
50 /// Throws an ArgumentException if the section already exists in this file
51 /// </summary>
52 /// <param name="section">
53 /// The <see cref="IniSection"/> to add
54 /// </param>
55 public void AddSection(IniSection section)
56 {
57 if (!HasSection(section.Name))
58 {
59 }
60 else
61 {
62 throw new ArgumentException("Ini section already exists");
63 }
64 }
65
66 public bool HasSection(string sectionName)
67 {
68 return sections.ContainsKey(sectionName.Trim());
69 }
70
71 public IniSection[] Sections
72 {
73 get
74 {
75 IniSection[] col = new IniSection[sections.Count];
76 sections.Values.CopyTo(col, 0);
77 return col;
78 }
79 }
80 }
81 }