view IniSection.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 source

// This file (IniSection.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
{
	public class IniSection
	{
		private string name;
		private Dictionary<string, string> values;
		
		public IniSection(string sectionName)
		{			
			CheckSectionName(sectionName);
			name = sectionName;
			values = new Dictionary<string,string>();
		}
		
		private static void CheckSectionName(string sectionName)
		{
			if (sectionName == null)
			{
				throw new ArgumentException("sectionName cannot be null");
			}
			
			sectionName = sectionName.Trim();
			
			if (sectionName == "")
			{
				throw new ArgumentException("sectionName cannot be an empty string");
			}
		}
		
		public string Name
		{
			get
			{
				return name;
			}
		}
		
		public string[] Keys
		{
			get
			{
				int valCount = values.Count;
				string[] col = new string[valCount];
				
				if (valCount > 0)
				{
					values.Keys.CopyTo(col, 0);
				}
				
				return col;
			}
		}
		
		public void AddKeyValuePair(string key, string pairedValue)
		{
			if (!values.ContainsKey(key))
			{
				values.Add(key, pairedValue);
			}
			else
			{
				throw new ArgumentException("The key '"+key+"' was already defined in ["+Name+"]");
			}
		}
		
		public string GetValueForKey(string key)
		{
			string pairedValue = null;
			values.TryGetValue(key, out pairedValue);
			return pairedValue;
		}
	}
}