view IniLineParser.cs @ 1:f9444f1786cd

Re #6 - INI parsing library * Add ToString methods * Add types of string and parse to objects instead of parsing to key-value pair * Create method to load INI data from string * Add content of AddSection method
author IBBoard <dev@ibboard.co.uk>
date Sun, 11 Jan 2009 15:37:14 +0000
parents fbde5e1920ba
children f6f726c92e56
line wrap: on
line source

// This file (IniLineParser.cs) is a part of [SOME APPLICATION] 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;

namespace IBBoard.Ini
{
	public class IniLineParser
	{		
		/// <summary>
		/// Parses a non-section INI line and returns an <see cref="IIniLine"/> object.
		/// </summary>
		/// <param name="line">
		/// The line to parse
		/// </param>
		/// <returns>
		/// An <see cref="IIniLine"/> containing the data of the line
		/// </returns>
		public static IIniLine ParseIniLine(string line)
		{
			line = line.Trim();
			IIniLine iniLine = null;

			if (line == "")
			{
				iniLine = new IniBlankLine();
			}
			else if (line.StartsWith(";"))
			{
				iniLine = new IniCommentLine(line);
			}
			else
			{
				int idx = line.IndexOf('=');
				
				if (idx > 0)
				{
					iniLine = CreateKeyValuePairLine(line, idx);
				}
				else
				{
					iniLine = new IniCommentLine(line);
				}
			}
			
			return iniLine;
		}
		
		private static IniKeyValuePairLine CreateKeyValuePairLine(string line, int idx)
		{
			string keyString = line.Substring(0, idx).Trim();
			string valueString = line.Substring(idx+1).Trim();
			return new IniKeyValuePairLine(keyString, valueString);
		}
	}
}