view IniLineParser.cs @ 13:ee9f11c67399 default tip

* Correct version number pattern no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Mon, 28 Feb 2011 20:22:41 +0000
parents f6f726c92e56
children
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, 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;

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);
		}
	}
}