view IniFile.cs @ 10:848e7b151d3c

Re #17 - Make INI section names and keys case insensitive * Add ToLower() when adding sections and lines to dictionaries * Add ToLower() when getting sections and lines from dictionaries
author IBBoard <dev@ibboard.co.uk>
date Thu, 26 Mar 2009 20:12:07 +0000
parents 5b0052b5585f
children
line wrap: on
line source

// 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, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

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 : IEnumerable<IniSection>
	{		
		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>
		/// Adds an <see cref="IniSection"/> to the IniFile.
		/// <p>
		/// Throws a DuplicateIniSectionException if the section already exists in this file
		/// </summary>
		/// <param name="section">
		/// The <see cref="IniSection"/> to add
		/// </param>
		public void AddSection(IniSection section)
		{
			string sectionName = section.Name.ToLower();
			
			if (!HasSection(sectionName))
			{
				sections.Add(sectionName, section);
			}
			else
			{
				throw new DuplicateIniSectionException(section.Name);
			}
		}

		public IEnumerator<IniSection> GetEnumerator()
		{
			return sections.Values.GetEnumerator();
		}

		IEnumerator IEnumerable.GetEnumerator()
        {
            return sections.Values.GetEnumerator();
        }

		//// <value>
		/// 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.
		/// </value>
		public IniSection this[string sectionName]
		{
			get
			{	
				IniSection section = null;
				sections.TryGetValue(sectionName.Trim().ToLower(), out section);
				
				if (section == null)
				{
					section = new NonExistantIniSection();
				}
				
				return section;
			}
		}

		/// <summary>
		/// Checks whether the IniFile contains an <see cref=" IniSection"/> with the specified name
		/// </summary>
		/// <param name="sectionName">
		/// The name of the section to look for
		/// </param>
		/// <returns>
		/// <code>true</code> if the section is in the file, else <code>false</code>
		/// </returns>
		public bool HasSection(string sectionName)
		{
			return sections.ContainsKey(sectionName.Trim().ToLower());
		}

		/// <summary>
		/// Gets an array of the <see cref=" IniSection"/>s in the file
		/// </summary>
		public IniSection[] Sections
		{
			get
			{
				IniSection[] col = new IniSection[sections.Count];
				sections.Values.CopyTo(col, 0);
				return col;
			}
		}

		public override string ToString()
		{
			StringBuilder stringBuilder = new StringBuilder();

			foreach (IniSection section in this)
			{
				stringBuilder.Append(section.ToString());
				stringBuilder.Append("\n");
			}
			
			return stringBuilder.ToString().Trim();
		}
		
		/// <summary>
		/// Gets the value from a given key-value pair in a given section. If either the section does not exist or the key does not exist
		/// in the section then NULL will be returned. If the key exists in the section then its corresponding value will be returned.
		/// </summary>
		/// <param name="sectionName">
		/// The <code>IniSection</code> to look for <code>lineKey</code> in
		/// </param>
		/// <param name="lineKey">
		/// The key for the <code>IniKeyValuePairLine</code> within the <code>sectionName</code> <code>IniSection</code> to get the value for
		/// </param>
		/// <returns>
		/// The value for the key in the section, or NULL if the section or the key do not exist
		/// </returns>
		public string GetSectionLineValue(string sectionName, string lineKey)
		{
			IniSection section = this[sectionName];
			return section.GetLineValue(lineKey);
		}
		
		/// <summary>
		/// Gets the value from a given key-value pair in a given section. If either the section does not exist or the key does not exist
		/// in the section then <code>defaultValue</code> will be returned. If the key exists in the section then its corresponding value will be returned.
		/// </summary>
		/// <param name="sectionName">
		/// The <code>IniSection</code> to look for <code>lineKey</code> in
		/// </param>
		/// <param name="lineKey">
		/// The key for the <code>IniKeyValuePairLine</code> within the <code>sectionName</code> <code>IniSection</code> to get the value for
		/// </param>
		/// <param name="defaultValue">
		/// The default value to return if the section or the key do not exist
		/// </param>
		/// <returns>
		/// The value for the key in the section, or NULL if the section or the key do not exist
		/// </returns>
		public string GetSectionLineValue(string sectionName, string lineKey, string defaultValue)
		{
			IniSection section = this[sectionName];
			return section.GetLineValue(lineKey, defaultValue);
		}
	}
}