private bool LexFileArguments()

in src/FabActUtil/CommandLineParser/CommandLineArgumentParser.cs [312:430]


        private bool LexFileArguments(string fileName, out string[] argumentsStrings)
        {
            string args;

            try
            {
                using (var file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    args = (new StreamReader(file)).ReadToEnd();
                }
            }
            catch (Exception e)
            {
                this.reporter(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        SR.Error_CannotOpenCommandLineArgumentFile,
                        fileName,
                        e.Message));
                argumentsStrings = null;
                return false;
            }

            var hadError = false;
            var argArray = new ArrayList();
            var currentArg = new StringBuilder();
            var inQuotes = false;
            var index = 0;

            // while (index < args.Length)
            try
            {
                while (true)
                {
                    // skip whitespace
                    while (char.IsWhiteSpace(args[index]))
                    {
                        index += 1;
                    }

                    // # - comment to end of line
                    if (args[index] == '#')
                    {
                        index += 1;
                        while (args[index] != '\n')
                        {
                            index += 1;
                        }

                        continue;
                    }

                    // do one argument
                    do
                    {
                        if (args[index] == '\\')
                        {
                            var slashes = 1;
                            index += 1;
                            while (index == args.Length && args[index] == '\\')
                            {
                                slashes += 1;
                            }

                            if (index == args.Length || args[index] != '"')
                            {
                                currentArg.Append('\\', slashes);
                            }
                            else
                            {
                                currentArg.Append('\\', slashes >> 1);
                                if ((slashes & 1) != 0)
                                {
                                    currentArg.Append('"');
                                }
                                else
                                {
                                    inQuotes = !inQuotes;
                                }
                            }
                        }
                        else if (args[index] == '"')
                        {
                            inQuotes = !inQuotes;
                            index += 1;
                        }
                        else
                        {
                            currentArg.Append(args[index]);
                            index += 1;
                        }
                    }
                    while (!char.IsWhiteSpace(args[index]) || inQuotes);

                    argArray.Add(currentArg.ToString());
                    currentArg.Length = 0;
                }
            }
            catch (IndexOutOfRangeException)
            {
                // got EOF
                if (inQuotes)
                {
                    this.reporter(
                        string.Format(
                            SR.Error_UnbalancedSlashes,
                            fileName));
                    hadError = true;
                }
                else if (currentArg.Length > 0)
                {
                    // valid argument can be terminated by EOF
                    argArray.Add(currentArg.ToString());
                }
            }

            argumentsStrings = (string[])argArray.ToArray(typeof(string));
            return hadError;
        }