using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Text.RegularExpressions; using System.Collections; using System.Diagnostics; using System.Data; using System.IO; using Microsoft.Win32; namespace IControls_FireManager { public class Util { /**************************************************************** * String --> Int, °ø¹éÀ̳ª NULL Àΰæ¿ì. ±âº»°ªÀ¸·Î ¸®ÅÏÇÑ´Ù. ****************************************************************/ public static int StrToInt(object txt, int val) { int ret_val = val; try { if (txt == null) return val; if (txt.ToString().Trim() == "") return val; ret_val = int.Parse(txt.ToString()); } catch (Exception ex) { Util.UErrorMessage(ex, 0, 0); } finally { } return ret_val; } /**************************************************************** * °ø¹éÀ̳ª NULL Àΰæ¿ì. ±âº»°ªÀ¸·Î ¸®ÅÏÇÑ´Ù. ****************************************************************/ public static string NullToStr(object txt) { string ret_val = ""; try { if (txt == null) return ret_val; if (txt.ToString().Trim() == "") return ret_val; ret_val = txt.ToString().Trim(); } catch { //Util.UErrorMessage(ex, 0, 0); } finally { } return ret_val; } /******************************************* * control ¼ýÀÚÇü ÀԷ°ª °Ë»ç. *******************************************/ public static Boolean ChkOnlyLiteralTextBox(TextBox txt, String txtboxName) { try { String input_value = txt.Text; if (!onlyLiteral(input_value)) { txt.Text = ""; txt.Focus(); throw new Exception(String.Format("[{0}] ÀÔ·ÂÇü½ÄÀÌ ¿Ã¹Ù¸£Áö ¾Ê½À´Ï´Ù. ¼ýÀÚ¸¦ ÀÔ·ÂÇÏ¿© Áֽʽÿä.", txtboxName)); } } catch (Exception ex) { Util.UErrorMessage(ex, 0, 0); } return true; } /******************************************* * TextBox Çʼö ÀÔ·Â °Ë»ç. *******************************************/ public static Boolean ChkTxtBox(TextBox txt, String txtboxName) { try { if (txt.Text.Trim() == "") { txt.Focus(); throw new Exception(String.Format("[{0}] ÀԷ°ªÀº ÇʼöÀÔ´Ï´Ù.", txtboxName)); } return true; } catch (Exception ex) { Util.UErrorMessage(ex, 0, 0); throw ex; } } /******************************************* * MaskedTextBox Çʼö ÀÔ·Â °Ë»ç. *******************************************/ public static Boolean ChkTxtBox(MaskedTextBox txt, String txtboxName) { try { if (txt.Text.Trim() == "") { txt.Focus(); throw new Exception(String.Format("[{0}] ÀԷ°ªÀº ÇʼöÀÔ´Ï´Ù.", txtboxName)); } return true; } catch (Exception ex) { Util.UErrorMessage(ex, 0, 0); throw ex; } } /******************************************* * ComboBox Çʼö ÀÔ·Â °Ë»ç. *******************************************/ public static Boolean ChkComboBox(ComboBox cbo, String cboName) { try { if (cbo.SelectedValue == null) { throw new Exception(String.Format("[{0}] °ªÀ» ¼±ÅÃÇÏ¿© Áֽʽÿä.", cboName)); } return true; } catch (Exception ex) { Util.UErrorMessage(ex, 0, 0); throw ex; } } /******************************************* * ¿ìÆí¹øÈ£Á¶ÇÕ °Ë»ç. *******************************************/ public static Boolean chkZipCode(String intxt) { String ZipRegex = "[0-9]{3}-[0-9]{3}"; if (Regex.IsMatch(intxt, ZipRegex)) { return true; } else { return false; } } /******************************************* * ¼ýÀÚÁ¶ÇÕ °Ë»ç. *******************************************/ public static Boolean onlyLiteral(String intxt) { String patttern = "^[0-9]+$"; //Á¤¼ö:0-9¼ýÀÚ ½Ç¼ö:^[+-]?\d+(\.\d+)?$ ¶Ç´Â ^[+-]?\d*\.?\d*$ if (Regex.IsMatch(intxt, patttern)) { return true; } else { return false; } } /******************************************* * ¼ýÀÚ¿µ¹®Á¶ÇÕ °Ë»ç. *******************************************/ public static Boolean onlyLiteralnAlpa(String intxt) { String patttern = "\\w+"; //[a-zA-Z0-9_] ¿µ¹®¼ýÀÚ_ if (Regex.IsMatch(intxt, patttern)) { return true; } else { return false; } } /******************************************* * chars -> StringÀ¸·Î º¯È¯ *******************************************/ public static String CharToStr(char[] chars) { String s = ""; foreach (Char c in chars) { s += string.Format("{0}", c); } return s; } /******************************************* * bytes -> StringÀ¸·Î º¯È¯ *******************************************/ public static String ByteToStr(byte[] bytes, int start_idx, int length_idx) { ASCIIEncoding ascii = new ASCIIEncoding(); //ȸ·Î I/OŸÀÔ int charCount = ascii.GetCharCount(bytes, start_idx, length_idx); Char[] iochar = new Char[charCount]; int charsDecodedCount = ascii.GetChars(bytes, start_idx, length_idx, iochar, 0); return CharToStr(iochar); } /******************************************* * byte -> hex ½ºÆ®¸µÀ¸·Î º¯È¯ *******************************************/ public static String ToHex(byte[] bin_data) { string result = ""; foreach (byte ch in bin_data) { result += string.Format("{0:x2} ", ch); // 2ÀÚ¸®ÀÇ 16Áø¼ö·Î Ãâ·Â, [Âü°í] ¸µÅ© Àо °Í } return result; } /******************************************* * ½ºÆ®¸µÀ» byte ¹è¿­·Î º¯È¯ *******************************************/ public static byte[] ToBytes(String arg) { string arg2 = NullToStr(arg); System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); return encoding.GetBytes(arg2); } /******************************************* * byte + byte ¹è¿­·Î º¯È¯ *******************************************/ public static byte[] ByteArrayCopyAdd(byte[] srcByte, byte[] addByte, int addLen) { int totalLen = srcByte.Length + addLen; byte[] totalByte = new byte[totalLen]; try { Array.Copy(srcByte, 0, totalByte, 0, srcByte.Length); Array.Copy(addByte, 0, totalByte, srcByte.Length, addLen); } catch (Exception ex) { Util.UErrorMessage(ex, 0, 0); throw ex; //Debug.WriteLine(ex.Message); } return totalByte; } /******************************************* * Y,N String --> Bool·Î º¯È¯ *******************************************/ public static bool StringToBool(String flg) { return flg == "Y" ? true : false; } /******************************************* * 2010-1-1 ÀÌÈÄ ÇöÀç±îÁö °£°Ý *******************************************/ public static double milisec() { DateTime stdDate = new DateTime(2010, 1, 1, 0, 0, 0); TimeSpan ts = DateTime.Now - stdDate; double intRtn = ts.TotalMilliseconds; return intRtn; } /******************************************* * ÇÏÀ§,»óÀ§ ¼ýÀÚ·Î float º¼Æ®¸®ÅÏ *******************************************/ public static float ToValtage(int vol_dn, int vol_up) { float voltage24 = 0; //24V (»óÀ§*256 + ÇÏÀ§) / 100 voltage24 = (vol_up * 256) + vol_dn; voltage24 = voltage24 / 100; return voltage24; } /******************************************* * TextBox ÀÔ·ÂÀ» ´ë¹®ÀÚ·Î º¯°æÇØÁÜ *******************************************/ public static void ToUpper(TextBox obj) { try { int caretPosition; caretPosition = obj.SelectionStart; obj.Text = obj.Text.ToUpper(); obj.Select(caretPosition, 0); } catch (Exception ex) { Util.UErrorMessage(ex, 0, 0); } } /* public static string FirebirdPath() { String filePath = ""; try { //·¹Áö½ºÆ®¸®¿¡¼­ DBÁ¢¼ÓÁ¤º¸ Àоî¿À±â RegistryKey rk = Registry.LocalMachine.OpenSubKey("SOFTWARE\\I_FireManager", false); if (rk != null) { String DATABASE_NAME = rk.GetValue("DATABASE_NAME").ToString(); int pos = DATABASE_NAME.IndexOf(":"); if (pos != -1) { filePath = DATABASE_NAME.Substring(pos + 1); } } } catch(Exception ex) { Util.UErrorMessage(ex,0,0); } return filePath; } */ static int prt_dbg_level = -1;// dbg message level public static void UDebugMessage(string msg, int dbg_category, int dbg_level) {// dbg message if (dbg_level <= prt_dbg_level) { Debug.WriteLine("[Debug]: " + msg); } } public static void UDebugMessage(string msg, int dbg_category, int dbg_level, StackTrace st) {// error message if (dbg_level <= prt_dbg_level) { Debug.WriteLine(string.Format("[Debug][{0}]: {1} //+ {2} -//", dbg_category, msg, st.ToString())); } } public static void UErrorMessage(Exception ex, int dbg_category, int dbg_level) { // error message with stack if (dbg_level <= prt_dbg_level) { Debug.WriteLine(string.Format("[Debug]:[ERR]: {0}, //+ {1} -// ", ex.Message, ex.StackTrace)); } } } }