예제 #1
0
파일: Comm.cs 프로젝트: fialot/Logger
        private msgFunction isFunction(string msg, int position)
        {
            msgFunction fun = new msgFunction();
            int         p1  = msg.IndexOf('(', position);
            int         p2  = msg.IndexOf(')', position + 1);

            if (p1 >= 0 && p2 >= 0)
            {
                fun.name = msg.Substring(position + 1, p1 - 1 - position);
                if (fun.name.Contains("\\") || fun.name.Contains(" "))
                {
                    fun.name = "";
                    return(fun);
                }

                fun.arguments = msg.Substring(p1 + 1, p2 - (p1 + 1)).Split(new string[] { ";" }, StringSplitOptions.None);
                for (int i = 0; i < fun.arguments.Length; i++)
                {
                    fun.arguments[i] = fun.arguments[i].Trim();
                    if (fun.arguments.Length > 0)
                    {
                        if (fun.arguments[i][0] == '\"' && fun.arguments[i][fun.arguments[i].Length - 1] == '\"')
                        {
                            fun.arguments[i] = fun.arguments[i].Substring(1, fun.arguments[i].Length - 2);
                        }
                    }
                }
                fun.length = p2 - position;
                if (msg.Length > p2 + 1)
                {
                    if (msg[p2 + 1] == ' ')
                    {
                        fun.length += 1;
                    }
                }
            }
            return(fun);
        }
예제 #2
0
파일: Comm.cs 프로젝트: fialot/Logger
        /// <summary>
        /// Format Message
        /// </summary>
        /// <param name="msg">Message string</param>
        /// <returns>Data to send</returns>
        public byte[] FormatMsg(string msg)
        {
            int    pos, lastpos, count = 0;
            string cmd, ins, prefix, prefix1;
            byte   num;
            int    inum;
            ushort snum;
            float  fnum;

            byte[] b;
            byte[] result;
            result = encoding.GetBytes(msg);

            if (msg.Contains(@"\"))
            {
                pos     = msg.IndexOf(@"\", 0);
                lastpos = pos;

                while (pos >= 0)
                {
                    cmd     = FindCmd(msg, pos, ref count);
                    prefix  = "";
                    prefix1 = "";
                    if (cmd.Length >= 2)
                    {
                        prefix = cmd.Substring(0, 2);
                    }
                    if (cmd.Length >= 1)
                    {
                        prefix1 = cmd.Substring(0, 1);
                    }
                    msgFunction fun = isFunction(msg, pos);

                    if (cmd == @"\")// lastpos++;
                    {
                        msg  = msg.Remove(pos, 2);
                        ins  = "";
                        ins += (char)1000;
                        msg  = msg.Insert(pos, ins);
                    }
                    else if (fun.name != null && fun.name != "")
                    {
                        msg = msg.Remove(pos, fun.length + 1);
                        ins = "";
                        if (fun.name == "file")
                        {
                            if (fun.arguments.Length > 0)
                            {
                                ins = Files.LoadFile(fun.arguments[0]);
                            }
                        }
                        else if (fun.name == "marsa")
                        {
                            if (fun.arguments.Length >= 2)
                            {
                                ins     = Protocol.Protocol.MarsA(Conv.HexToUInt(fun.arguments[0]), FormatMsg(fun.arguments[1]));
                                lastpos = -1;
                            }
                        }
                        msg = msg.Insert(pos, ins);
                    }
                    else if (prefix1 == "x")
                    {
                        b   = Conv.HexToBytes(cmd.Remove(0, 1).Trim());
                        ins = encoding.GetString(b);
                        msg = msg.Remove(pos, count + 1);
                        msg = msg.Insert(pos, ins);
                    } // hex
                    else if (prefix1 == "$")
                    {
                        b   = Conv.HexToBytes(cmd.Remove(0, 1).Trim());
                        ins = encoding.GetString(b);
                        msg = msg.Remove(pos, count + 1);
                        msg = msg.Insert(pos, ins);
                    } // hex
                    else if ((prefix1 == "i") && (int.TryParse(cmd.Remove(0, 1).Trim(), out inum)))
                    {
                        b = BitConverter.GetBytes(inum);
                        Array.Reverse(b);
                        ins = encoding.GetString(b);
                        msg = msg.Remove(pos, count + 1);
                        msg = msg.Insert(pos, ins);
                    } // hex
                    else if ((prefix1 == "s") && (ushort.TryParse(cmd.Remove(0, 1).Trim(), out snum)))
                    {
                        b = BitConverter.GetBytes(snum);
                        Array.Reverse(b);
                        ins = encoding.GetString(b);
                        msg = msg.Remove(pos, count + 1);
                        msg = msg.Insert(pos, ins);
                    }
                    else if ((prefix1 == "f") && (float.TryParse(cmd.Remove(0, 1).Trim(), out fnum)))
                    {
                        b = BitConverter.GetBytes(fnum);
                        Array.Reverse(b);
                        ins = encoding.GetString(b);
                        msg = msg.Remove(pos, count + 1);
                        msg = msg.Insert(pos, ins);
                    } // \n -> <10>
                    else if ((prefix1 == "\'") || (prefix1 == "\"") || (prefix1 == "\\") || (prefix1 == "a") || (prefix1 == "b") || (prefix1 == "f") || (prefix1 == "n") || (prefix1 == "r") || (prefix1 == "t") || (prefix1 == "v"))
                    {
                        switch (prefix1)
                        {
                        case "'":
                            ins = "\'";
                            break;

                        case "\"":
                            ins = "\"";
                            break;

                        case "\\":
                            ins = "\\";
                            break;

                        case "a":
                            ins = "\a";
                            break;

                        case "b":
                            ins = "\b";
                            break;

                        case "f":
                            ins = "\f";
                            break;

                        case "n":
                            ins = "\n";
                            break;

                        case "r":
                            ins = "\r";
                            break;

                        case "t":
                            ins = "\t";
                            break;

                        case "v":
                            ins = "\v";
                            break;

                        default:
                            ins = "\n";
                            break;
                        }
                        //ins = "\n";
                        if (cmd.Length >= 2)
                        {
                            if (cmd[1] == ' ')
                            {
                                msg = msg.Remove(pos, 3);
                            }
                            else
                            {
                                msg = msg.Remove(pos, 2);
                            }
                        }
                        else
                        {
                            msg = msg.Remove(pos, 2);
                        }
                        msg = msg.Insert(pos, ins);
                    } // hex
                    else if (byte.TryParse(cmd, out num))
                    {
                        ins = "";
                        byte[] test = new byte[1];
                        test[0] = num;
                        if (num == 0)
                        {
                            ins += (char)2000;
                        }
                        else
                        {
                            ins = encoding.GetString(test);
                        }
                        msg = msg.Remove(pos, count + 1);
                        msg = msg.Insert(pos, ins);
                    }
                    else
                    {
                        msg = msg.Remove(pos, 1);
                    }

                    if (msg.Length > lastpos + 1)
                    {
                        pos = msg.IndexOf(@"\", lastpos + 1);
                    }
                    else
                    {
                        pos = -1;
                    }
                }
                result = new byte[msg.Length];
                for (int i = 0; i < result.Length; i++)
                {
                    if ((Int16)msg[i] == 1000)
                    {
                        result[i] = (byte)'\\';
                    }
                    else if ((Int16)msg[i] == 2000)
                    {
                        result[i] = 0;
                    }
                    else
                    {
                        result[i] = encoding.GetBytes(msg[i] + "")[0];
                    }
                }
            }
            return(result);
        }