using System;

namespace DM2AE
{
	// 
	public class Util
	{
		// 
		public static readonly string g_sIni = "DM2AE.ini";

		// 
		public static string IniPath
		{
			get { return GetNeighborFile(g_sIni); }
		}

		// 
		public Util()
		{

		}
		// 
		public static System.String GetDir(
			System.String X
			)
		{
			int i = X.LastIndexOf('\\');
			if (i != -1)
				return X.Substring(0, i + 1);
			return "";
		}
		// 
		public static System.String GetFileName(
			System.String X
			)
		{
			int i = X.LastIndexOf('\\');
			if (i != 1)
				return X.Substring(i + 1);
			return X;
		}
		// 
		public static System.String JoinPath(
			System.String Dir, System.String File
			)
		{
			if (Dir.LastIndexOf('\\') != Dir.Length - 1)
				Dir += '\\';
			return Dir + File;
		}
		// 
		public static System.String GetNeighborFile(
			System.String File
			)
		{
			return JoinPath(System.AppDomain.CurrentDomain.BaseDirectory, File);
		}
		// 
		public static System.String EncodeToHtmlText(
			System.String Html
			)
		{
			System.Text.StringBuilder s = new System.Text.StringBuilder(Html, Html.Length * 2);
			s.Replace("&", "&amp;");
			s.Replace("<", "&lt;");
			s.Replace(">", "&gt;");
			return s.ToString();
		}
		// 
		public static System.String Bin2Str(
			System.Byte[] X
			)
		{
			System.Text.StringBuilder s = new System.Text.StringBuilder(X.Length * 3 + 16);
			int i = 0;
			foreach (System.Byte x in X)
			{
				if (i != 0)
				{
					s.Append(' ');
				}
				if (x < 16)
				{
					s.Append('0');
					s.Append(x.ToString("X"));
				}
				else
				{
					s.Append(x.ToString("X"));
				}
				i++;
			}
			return s.ToString();
		}
		// 
		public static System.Byte[] Str2Bin(
			System.String s
			)
		{
			int i = 0, n = s.Length;
			int m = 0;
			for (; i < n; i++)
			{
				char c = s[i];
				if (('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F'))
				{
					m++;
				}
			}
			if (m == 0)
			{
				return null;
			}
			System.Byte[] X = new System.Byte[(m + 1) / 2];
			int t = 0, x = 0;
			int o = 0;
			bool Lead = true;
			i = 0;
			for (; i < n; i++)
			{
				char c = s[i];
				switch (c)
				{
					case '0':
					case '1':
					case '2':
					case '3':
					case '4':
					case '5':
					case '6':
					case '7':
					case '8':
					case '9':
					case 'a':
					case 'b':
					case 'c':
					case 'd':
					case 'e':
					case 'f':
					case 'A':
					case 'B':
					case 'C':
					case 'D':
					case 'E':
					case 'F':
					switch (c)
					{
						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
							t = c - '0';
							break;
						case 'a':
						case 'b':
						case 'c':
						case 'd':
						case 'e':
						case 'f':
							t = c - 'a' + 10;
							break;
						case 'A':
						case 'B':
						case 'C':
						case 'D':
						case 'E':
						case 'F':
							t = c - 'A' + 10;
							break;
					}
						if (Lead)
						{
							x = (t&0x0F)<<4;
						}
						else
						{
							x|= (t&0x0F);
							X[o] = (System.Byte)x;
							o++;
							x = 0;
						}
						Lead = !Lead;
						break;
				}
			}
			return X;
		}
		// 
		public static void SaveBDTo(
			string sFileInto,
			byte[] pBinary
			)
		{
			System.IO.TextWriter pFileInto = null;
			try
			{
				pFileInto = new System.IO.StreamWriter(sFileInto, false, System.Text.Encoding.Default);

				int nLen = pBinary.Length;
				int nLines = (nLen + 15) / 16;

				pFileInto.WriteLine(" ADDRESS   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F   0123456789ABCDEF ");
				pFileInto.WriteLine("------------------------------------------------------------------------------");
				char[] p16 = new char[16];
				for (int iLine = 0; iLine < nLines; iLine++)
				{
					int iAddr = 0x10 * iLine;
					pFileInto.Write(" {0:X8}  ", iAddr);
					int x, cx = System.Math.Min(16, nLen - iAddr);
					for (x = 0; x < cx; x++)
					{
						pFileInto.Write("{0:X2} ", pBinary[iAddr + x]);
					}
					for (; x < 16; x++)
					{
						pFileInto.Write("   ");
					}
					pFileInto.Write("  ");
					for (x = 0; x < 16; x++)
						p16[x] = ' ';
					for (x = 0; x < cx; x++)
					{
						p16[x] = '.';
						byte c = pBinary[iAddr + x];
						if (0x20 <= c && c <= 0x7F)
						{
							p16[x] = (char)c;
						}
					}
					pFileInto.Write(p16);
					pFileInto.WriteLine(" ");
				}
			}
			finally
			{
				if (pFileInto != null) pFileInto.Close();
			}
		}
	}
}
