diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IniFile.cs	Thu Jan 08 20:33:56 2009 +0000
@@ -0,0 +1,81 @@
+// This file (IniFile.cs) is a part of the IBBoard.Ini library and is copyright 2009 IBBoard.
+//
+// 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.
+
+using System;
+using System.Collections.Generic;
+
+namespace IBBoard.Ini
+{		
+	/// <summary>
+	/// 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.
+	/// </summary>
+	public class IniFile
+	{		
+		private Dictionary<string, IniSection> sections;
+		
+		/// <summary>
+		/// Default constructor that provides an empty INI file with no sections
+		/// </summary>
+		public IniFile()
+		{
+			sections = new Dictionary<string,IniSection>();
+		}
+		
+		/// <summary>
+		/// 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.
+		/// </summary>
+		/// <param name="sectionName">
+		/// The name of the section to get
+		/// </param>
+		/// <returns>
+		/// Either the requested <see cref="IniSection"/> or a <see cref=" NonExistantIniSection"/> if it doesn't exist
+		/// </returns>
+		public IniSection GetSection(string sectionName)
+		{
+			IniSection section = null;
+			sections.TryGetValue(sectionName.Trim(), out section);
+			
+			if (section == null)
+			{
+				section = new NonExistantIniSection();
+			}
+			
+			return section;
+		}
+		
+		/// <summary>
+		/// Adds an <see cref="IniSection"/> to the IniFile.
+		/// <p>
+		/// Throws an ArgumentException if the section already exists in this file
+		/// </summary>
+		/// <param name="section">
+		/// The <see cref="IniSection"/> to add
+		/// </param>
+		public void AddSection(IniSection section)
+		{
+			if (!HasSection(section.Name))
+			{
+			}
+			else
+			{
+				throw new ArgumentException("Ini section already exists");
+			}
+		}
+		
+		public bool HasSection(string sectionName)
+		{
+			return sections.ContainsKey(sectionName.Trim());
+		}
+		
+		public IniSection[] Sections
+		{
+			get
+			{
+				IniSection[] col = new IniSection[sections.Count];
+				sections.Values.CopyTo(col, 0);
+				return col;
+			}
+		}
+	}
+}