using System;

namespace DM2AE
{
	// 
	public class WinIni
	{
		// 
		class Pair
		{
			// 
			private System.String m_pKey;
			// 
			private System.String m_pVal;
			// 
			public Pair(
				System.String pKey,
				System.String pVal
				)
			{
				m_pKey = pKey;
				m_pVal = pVal;
			}
			// 
			public System.String Key
			{
				get { return m_pKey; }
			}
			// 
			public System.String Value
			{
				get { return m_pVal; }
			}
		};
		// 
		class Section
		{
			// 
			private System.String m_pKey;
			// 
			private System.Collections.SortedList m_pPairs;
			// 
			public Section(
				System.String pKey
				)
			{
				m_pKey = pKey;
				m_pPairs = new System.Collections.SortedList();
			}
			// 
			public System.String Key
			{
				get { return m_pKey; }
			}
			// 
			public System.Collections.SortedList Pairs
			{
				get { return m_pPairs; }
			}
		};

		// 
		private System.Collections.SortedList m_pSections;
		// 
		private System.Text.Encoding m_pEncoding;

		// 
		public WinIni()
		{
			m_pSections = new System.Collections.SortedList();
			m_pEncoding = System.Text.Encoding.Default;
		}
		// 
		public WinIni(
			System.Text.Encoding pEncoding
			)
		{
			m_pSections = new System.Collections.SortedList();
			m_pEncoding = pEncoding;
		}
		// 
		public System.String GetProfileString(
			System.String sApp,
			System.String sKey,
			System.String sDef
			)
		{
			Section pSection = (Section)m_pSections[sApp.ToLower()];
			if (pSection != null)
			{
				Pair pPair = (Pair)pSection.Pairs[sKey.ToLower()];
				if (pPair != null)
				{
					return pPair.Value;
				}
			}
			return sDef;
		}
		// 
		public System.String[] GetProfileString(
			System.String sApp
			)
		{
			Section pSection = (Section)m_pSections[sApp.ToLower()];
			if (pSection != null)
			{
				int i = 0, n = pSection.Pairs.Count;
				System.String[] pNames = new System.String[n];
				foreach (Pair pPair in pSection.Pairs.Values)
				{
					pNames[i] = pPair.Key;
					i++;
				}
				return pNames;
			}
			return null;
		}
		// 
		public void WriteProfileString(
			System.String sApp,
			System.String sKey,
			System.String sVal
			)
		{
			Section pSection = (Section)m_pSections[sApp.ToLower()];
			if (pSection == null)
			{
				m_pSections[sApp.ToLower()] = (pSection = new Section(sApp));
			}
			Pair pPair = new Pair(sKey, sVal);
			pSection.Pairs[sKey.ToLower()] = pPair;
		}
		// 
		public bool LoadFrom(System.String FileName)
		{
			try
			{
				m_pSections.Clear();

				System.IO.StreamReader ReadIn = new System.IO.StreamReader(FileName, m_pEncoding);
				System.String sLine = null;

				while (true)
				{
					if (sLine == null)
						sLine = ReadIn.ReadLine();
					if (sLine == null)
						break;
					if (IsCommentLine(sLine))
					{
						sLine = null;
						continue;
					}
					if (IsSectionBracketExist(sLine))
					{
						System.String sApp = sLine.Substring(1, sLine.Length - 2);
						while (true)
						{
							sLine = ReadIn.ReadLine();
							if (sLine == null)
								break;
							if (IsCommentLine(sLine))
							{
								sLine = null;
								continue;
							}
							if (IsSectionBracketExist(sLine))
								break;
							int i = sLine.IndexOf('=');
							if (i < 0)
								continue;
							System.String sKey = sLine.Substring(0, i);
							System.String sVal = sLine.Substring(i + 1);
							WriteProfileString(sApp, sKey, sVal);
						}
					}
				}

				ReadIn.Close();
				return true;
			}
			catch(System.Exception err)
			{
				System.Diagnostics.Debug.WriteLine(err.Message);

				m_pSections.Clear();
				return false;
			}
		}
		// 
		public bool SaveTo(System.String FileName)
		{
			try
			{
				System.IO.StreamWriter WriteInto = new System.IO.StreamWriter(FileName, false, m_pEncoding);

				System.Text.StringBuilder s = new System.Text.StringBuilder(256);

				foreach (Section pSection in m_pSections.Values)
				{
					s.Length = 0;
					s.Append('[');
					s.Append(pSection.Key);
					s.Append(']');
					WriteInto.WriteLine(s.ToString());

					foreach (Pair pPair in pSection.Pairs.Values)
					{
						s.Length = 0;
						s.Append(pPair.Key);
						s.Append('=');
						s.Append(pPair.Value);
						WriteInto.WriteLine(s.ToString());
					}
				}

				WriteInto.Close();
				return true;
			}
			catch(System.Exception err)
			{
				System.Diagnostics.Debug.WriteLine(err.Message);
				return false;
			}
		}

		// 
		static private bool IsCommentLine(System.String s)
		{
			s = s.Trim();
			if (s.Length < 2 || s[0] == ';')
				return true;
			return false;
		}
		// 
		static private bool IsSectionBracketExist(System.String s)
		{
			if (s.Length < 3)
				return false;
			if (s[0] == '[' && s[s.Length - 1] == ']')
				return true;
			return false;
		}
	}
}
