Beispiel #1
0
        /// <summary>
        /// This routine replaces all place-holders in the given command line string.
        /// </summary>
        private string ReplacePlaceholders(string CmdLineText)
        {
            string MethodName = "Pipe.ReplacePlaceholders";
            char   Ch;
            int    CharPos             = 0;
            int    BegPosOfPlaceHolder = 0;
            int    PosOfNum            = 0;
            string FilterSwitchID      = string.Empty;
            string PipeSwitchID        = string.Empty;
            int    FirstIndex          = 0;
            int    LastIndex           = 0;
            bool   Done = false;
            string TempStr;

            LogWrite(MethodName + ": Raw Cmdline = \"" + CmdLineText + "\"");
            int  State    = 0;
            bool inString = false;

            ToState(ref State, 10, MethodName);

            do
            {
                Ch = GetNextChar(CmdLineText, ref CharPos, "CmdLine", ref inString);

                switch (State)
                {
                case 10:

                    // Looking for opening bracket "[".

                    if (Ch == '[')
                    {
                        BegPosOfPlaceHolder = CharPos - 1;
                        ToState(ref State, 20, MethodName);
                    }
                    else
                    {
                        if (Ch == '\0')
                        {
                            Done = true;
                        }
                    }

                    break;

                case 20:

                    // Looking either for a positional index's first digit
                    // or a slash (/).

                    if (Char.IsDigit(Ch))
                    {
                        // Must be a positional placeholder.

                        PosOfNum = CharPos - 1;
                        ToState(ref State, 30, MethodName);
                    }
                    else
                    {
                        if (Ch == '/')
                        {
                            // Must be a switch placeholder.

                            ToState(ref State, 60, MethodName);
                        }
                        else
                        {
                            if (Ch == '\0')
                            {
                                Done = true;
                            }
                            else
                            {
                                // Character not expected.  Backtrack a character:

                                CharPos--;
                                ToState(ref State, 10, MethodName);
                            }
                        }
                    }

                    break;

                case 30:

                    // Looking for the index's remaining digits.

                    if (Ch == '\0')
                    {
                        Done = true;
                    }
                    else
                    {
                        if (Char.IsDigit(Ch))
                        {
                            // Do nothing
                        }
                        else
                        {
                            if (Ch == ']')
                            {
                                // Located a simple positional place holder.  Get the index:

                                FirstIndex = Int32.Parse(CmdLineText.Substring(PosOfNum, CharPos - PosOfNum - 1)) - 1;

                                // Get the argument:

                                try
                                {
                                    TempStr = GetArg(FirstIndex, inString);
                                }

                                catch (PipeWrenchCompileException ex)
                                {
                                    ex.Data.Add("CmdLine", CmdLineText);
                                    ex.Data.Add("CharPos", CharPos);
                                    throw ex;
                                }

                                // Delete the place holder:

                                CmdLineText = CmdLineText.Remove(BegPosOfPlaceHolder, CharPos - BegPosOfPlaceHolder);
                                CharPos    -= CharPos - BegPosOfPlaceHolder;

                                // Insert the argument:

                                CmdLineText = CmdLineText.Insert(BegPosOfPlaceHolder, TempStr);
                                CharPos    += TempStr.Length;
                                ToState(ref State, 10, MethodName);
                            }
                            else
                            {
                                if (Ch == '-')
                                {
                                    // Matching a positional "range" placeholder.  Get the first index:

                                    FirstIndex = Int32.Parse(CmdLineText.Substring(PosOfNum, CharPos - PosOfNum - 1)) - 1;
                                    ToState(ref State, 40, MethodName);
                                }
                                else
                                {
                                    // Character not expected.  Backtrack a character:

                                    CharPos--;
                                    ToState(ref State, 10, MethodName);
                                }
                            }
                        }
                    }

                    break;

                case 40:

                    // Looking for last index's first digit.

                    if (Ch == '\0')
                    {
                        Done = true;
                    }
                    else
                    {
                        if (Ch == ']')
                        {
                            // Located an infinite range place holder.  Set
                            // the last index to indicate an infinite range:

                            LastIndex = CmdLine.ArgCount - 1;

                            // Process the place holder:

                            ReplacePlaceHolderRange(FirstIndex, LastIndex, BegPosOfPlaceHolder,
                                                    ref CharPos, ref CmdLineText, inString);
                            ToState(ref State, 10, MethodName);
                        }
                        else
                        {
                            if (Char.IsDigit(Ch))
                            {
                                PosOfNum = CharPos - 1;
                                ToState(ref State, 50, MethodName);
                            }
                            else
                            {
                                // Character not expected.  Backtrack a character:

                                CharPos--;
                                ToState(ref State, 10, MethodName);
                            }
                        }
                    }

                    break;

                case 50:

                    // Looking for last index's remaining digits.

                    if (Ch == '\0')
                    {
                        Done = true;
                    }
                    else
                    {
                        if (Ch == ']')
                        {
                            // Located a closed range place holder.  Get the last index:

                            LastIndex = Int32.Parse(CmdLineText.Substring(PosOfNum, CharPos - PosOfNum - 1)) - 1;

                            // Process the place holder:

                            ReplacePlaceHolderRange(FirstIndex, LastIndex, BegPosOfPlaceHolder,
                                                    ref CharPos, ref CmdLineText, inString);
                            ToState(ref State, 10, MethodName);
                        }
                        else
                        {
                            if (Char.IsDigit(Ch))
                            {
                                // Do nothing
                            }
                            else
                            {
                                // Character not expected.  Backtrack a character:

                                CharPos--;
                                ToState(ref State, 10, MethodName);
                            }
                        }
                    }

                    break;

                case 60:

                    // Looking for the filter's switch identifier.

                    if (Ch == '\0')
                    {
                        Done = true;
                    }
                    else
                    {
                        if (Char.IsLetter(Ch))
                        {
                            // Got the switch ID.

                            FilterSwitchID = Ch.ToString();
                            ToState(ref State, 70, MethodName);
                        }
                        else
                        {
                            // Character not expected.  Backtrack a character:

                            CharPos--;
                            ToState(ref State, 10, MethodName);
                        }
                    }

                    break;

                case 70:

                    // Looking for an equal sign character.

                    if (Ch == '\0')
                    {
                        Done = true;
                    }
                    else
                    {
                        if (Ch == '=')
                        {
                            // Got the equal sign character.

                            ToState(ref State, 80, MethodName);
                        }
                        else
                        {
                            // Character not expected.  Backtrack a character:

                            CharPos--;
                            ToState(ref State, 10, MethodName);
                        }
                    }

                    break;

                case 80:

                    // Looking for either a pipe's switch identifier or the
                    // first digit of a positional parameter index (a number).

                    if (Ch == '\0')
                    {
                        Done = true;
                    }
                    else
                    {
                        if (Char.IsLetter(Ch))
                        {
                            // Got the switch ID.

                            PipeSwitchID = Ch.ToString();
                            ToState(ref State, 90, MethodName);
                        }
                        else
                        {
                            if (Char.IsDigit(Ch))
                            {
                                // Got the first digit of a positional index.

                                PosOfNum = CharPos - 1;
                                ToState(ref State, 100, MethodName);
                            }
                            else
                            {
                                // Character not expected.  Backtrack a character:

                                CharPos--;
                                ToState(ref State, 10, MethodName);
                            }
                        }
                    }

                    break;

                case 90:

                    // Looking for closing bracket character.

                    if (Ch == '\0')
                    {
                        Done = true;
                    }
                    else
                    {
                        if (Ch == ']')
                        {
                            // Located a switch placeholder mapped to a switch parameter.
                            // Delete the place holder:

                            CmdLineText = CmdLineText.Remove(BegPosOfPlaceHolder, CharPos - BegPosOfPlaceHolder);
                            CharPos    -= CharPos - BegPosOfPlaceHolder;

                            int i = CmdLine.GetSwitchIndex("/" + PipeSwitchID);

                            if (i > -1)
                            {
                                // The switch was passed to the pipe.

                                TempStr = string.Empty;

                                if (CmdLine.GetSwitch(i).Value != null)
                                {
                                    // Is numeric or string switch.  Insert the
                                    // switch's value into the command line:

                                    TempStr = CmdLine.GetSwitch(i).Value.ToString();

                                    if (CmdLine.GetSwitch(i).Value is string)
                                    {
                                        // Quote the string:

                                        TempStr = "'" + TempStr + "'";
                                    }
                                }

                                // Insert the replacement text:

                                CmdLineText = CmdLineText.Insert(BegPosOfPlaceHolder, "/" + FilterSwitchID + TempStr);
                                CharPos    += TempStr.Length + 2;
                            }

                            ToState(ref State, 10, MethodName);
                        }
                        else
                        {
                            // Character not expected.  Backtrack a character:

                            CharPos--;
                            ToState(ref State, 10, MethodName);
                        }
                    }

                    break;

                case 100:

                    // Looking for the remaining digits of a positional index.

                    if (Ch == '\0')
                    {
                        Done = true;
                    }
                    else
                    {
                        if (Char.IsDigit(Ch))
                        {
                            // Do nothing
                        }
                        else
                        {
                            if (Ch == ']')
                            {
                                // Located a switch placeholder mapped to
                                // a positional parameter.  Get the index:

                                FirstIndex = Int32.Parse(CmdLineText.Substring(PosOfNum, CharPos - PosOfNum - 1)) - 1;

                                // Get the argument:

                                try
                                {
                                    TempStr = GetArg(FirstIndex, inString);
                                }

                                catch (PipeWrenchCompileException ex)
                                {
                                    ex.Data.Add("CmdLine", CmdLineText);
                                    ex.Data.Add("CharPos", CharPos);
                                    throw ex;
                                }

                                // Delete the place holder:

                                CmdLineText = CmdLineText.Remove(BegPosOfPlaceHolder, CharPos - BegPosOfPlaceHolder);
                                CharPos    -= CharPos - BegPosOfPlaceHolder;

                                // Insert the argument:

                                CmdLineText = CmdLineText.Insert(BegPosOfPlaceHolder, "/" + FilterSwitchID + TempStr);
                                CharPos    += TempStr.Length;
                                ToState(ref State, 10, MethodName);
                            }
                            else
                            {
                                // Character not expected.  Backtrack a character:

                                CharPos--;
                                ToState(ref State, 10, MethodName);
                            }
                        }
                    }

                    break;
                }
            }while (!Done);

            LogWrite(MethodName + ": Translated Cmdline = \"" + CmdLineText + "\"");
            return(CmdLineText);
        }