Ejemplo n.º 1
0
        public static void TokenizeString(Char[] text, Boolean macroExpand)
        {
            String com_token;

            cmd_argc = 0;
            cmd_args = "";
            var len = Lib.Strlen(text);

            if (macroExpand)
            {
                text = MacroExpandString(text, len);
            }
            if (text == null)
            {
                return;
            }
            len = Lib.Strlen(text);
            Com.ParseHelp ph = new ParseHelp(text);
            while (true)
            {
                var c = ph.Skipwhitestoeol();
                if (c == '\\')
                {
                    c = ph.Nextchar();
                    break;
                }

                if (c == 0)
                {
                    return;
                }
                if (cmd_argc == 1)
                {
                    cmd_args = new String(text, ph.index, len - ph.index);
                    cmd_args.Trim();
                }

                com_token = Com.Parse(ph);
                if (ph.data == null)
                {
                    return;
                }
                if (cmd_argc < Defines.MAX_STRING_TOKENS)
                {
                    cmd_argv[cmd_argc] = com_token;
                    cmd_argc++;
                }
            }
        }
Ejemplo n.º 2
0
        public static void Main(string[] args)
        {
            string test = "testrene = \\\"ein mal eins\\\"; a=3 ";

            Com.ParseHelp ph = new ParseHelp(test);
            while (!ph.IsEof())
            {
                System.Diagnostics.Debug.WriteLine("[" + Com.Parse(ph) + "]");
            }
            System.Diagnostics.Debug.WriteLine("OK!");
            test = " testrene = \\\"ein mal eins\\\"; a=3";
            ph   = new ParseHelp(test);
            while (!ph.IsEof())
            {
                System.Diagnostics.Debug.WriteLine("[" + Com.Parse(ph) + "]");
            }
            System.Diagnostics.Debug.WriteLine("OK!");
        }
Ejemplo n.º 3
0
        public static String Parse(ParseHelp hlp)
        {
            Int32 c;
            var   len = 0;

            if (hlp.data == null)
            {
                return("");
            }

            while (true)
            {
                hlp.Skipwhites();
                if (hlp.IsEof())
                {
                    hlp.data = null;
                    return("");
                }

                if (hlp.Getchar() == '/')
                {
                    if (hlp.Nextchar() == '/')
                    {
                        hlp.Skiptoeol();
                        continue;
                    }
                    else
                    {
                        hlp.Prevchar();
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            if (hlp.Getchar() == '\\')
            {
                hlp.Nextchar();
                while (true)
                {
                    c = hlp.Getchar();
                    hlp.Nextchar();
                    if (c == '\\' || c == 0)
                    {
                        return(new String(com_token, 0, len));
                    }

                    if (len < Defines.MAX_TOKEN_CHARS)
                    {
                        com_token[len] = ( Char )c;
                        len++;
                    }
                }
            }

            c = hlp.Getchar();
            do
            {
                if (len < Defines.MAX_TOKEN_CHARS)
                {
                    com_token[len] = ( Char )c;
                    len++;
                }

                c = hlp.Nextchar();
            }while (c > 32);
            if (len == Defines.MAX_TOKEN_CHARS)
            {
                Com.Printf("Token exceeded " + Defines.MAX_TOKEN_CHARS + " chars, discarded.\\n");
                len = 0;
            }

            return(new String(com_token, 0, len));
        }
Ejemplo n.º 4
0
        public static Char[] MacroExpandString(Char[] text, Int32 len)
        {
            Int32   i, j, count;
            Boolean inquote;

            Char[] scan;
            String token;

            inquote = false;
            scan    = text;
            if (len >= Defines.MAX_STRING_CHARS)
            {
                Com.Printf("Line exceeded " + Defines.MAX_STRING_CHARS + " chars, discarded.\\n");
                return(null);
            }

            count = 0;
            for (i = 0; i < len; i++)
            {
                if (scan[i] == '"')
                {
                    inquote = !inquote;
                }
                if (inquote)
                {
                    continue;
                }
                if (scan[i] != '$')
                {
                    continue;
                }
                Com.ParseHelp ph = new ParseHelp(text, i + 1);
                token = Com.Parse(ph);
                if (ph.data == null)
                {
                    continue;
                }
                token = Cvar.VariableString(token);
                j     = token.Length;
                len  += j;
                if (len >= Defines.MAX_STRING_CHARS)
                {
                    Com.Printf("Expanded line exceeded " + Defines.MAX_STRING_CHARS + " chars, discarded.\\n");
                    return(null);
                }

                System.Array.Copy(scan, 0, temporary, 0, i);
                System.Array.Copy(token.ToCharArray(), 0, temporary, i, token.Length);
                System.Array.Copy(ph.data, ph.index, temporary, i + j, len - ph.index - j);
                System.Array.Copy(temporary, 0, expanded, 0, 0);
                scan = expanded;
                i--;
                if (++count == 100)
                {
                    Com.Printf("Macro expansion loop, discarded.\\n");
                    return(null);
                }
            }

            if (inquote)
            {
                Com.Printf("Line has unmatched quote, discarded.\\n");
                return(null);
            }

            return(scan);
        }
Ejemplo n.º 5
0
        // See GameSpanw.ED_ParseEdict() to see how to use it now.
        public static string Parse(ParseHelp hlp)
        {
            int c;
            var len = 0;

            if (hlp.data == null)
            {
                return("");
            }

            while (true)
            {
                //	   skip whitespace
                hlp.skipwhites();

                if (hlp.isEof())
                {
                    hlp.data = null;

                    return("");
                }

                //	   skip // comments
                if (hlp.getchar() == '/')
                {
                    if (hlp.nextchar() == '/')
                    {
                        hlp.skiptoeol();

                        // goto skip whitespace
                        continue;
                    }

                    hlp.prevchar();

                    break;
                }

                break;
            }

            //	   handle quoted strings specially
            if (hlp.getchar() == '\"')
            {
                hlp.nextchar();

                while (true)
                {
                    c = hlp.getchar();
                    hlp.nextchar();

                    if (c == '\"' || c == 0)
                    {
                        return(new(Com.com_token, 0, len));
                    }

                    if (len < Defines.MAX_TOKEN_CHARS)
                    {
                        Com.com_token[len] = (char)c;
                        len++;
                    }
                }
            }

            //	   parse a regular word
            c = hlp.getchar();

            do
            {
                if (len < Defines.MAX_TOKEN_CHARS)
                {
                    Com.com_token[len] = (char)c;
                    len++;
                }

                c = hlp.nextchar();
            }while (c > 32);

            if (len == Defines.MAX_TOKEN_CHARS)
            {
                Com.Printf("Token exceeded " + Defines.MAX_TOKEN_CHARS + " chars, discarded.\n");
                len = 0;
            }

            return(new(Com.com_token, 0, len));
        }