static void parse_command_line()

in cppwinrt/cmd_reader.h [577:664]


        static void parse_command_line(Character* cmdstart, std::vector<std::string>& argv, size_t* argument_count)
        {

            std::string arg;
            bool copy_character;
            unsigned backslash_count;
            bool in_quotes;
            bool first_arg;

            Character* p = cmdstart;
            in_quotes = false;
            first_arg = true;
            *argument_count = 0;

            for (;;)
            {
                if (*p)
                {
                    while (*p == ' ' || *p == '\t')
                        ++p;
                }

                if (!first_arg)
                {
                    argv.emplace_back(arg);
                    arg.clear();
                    ++*argument_count;
                }

                if (*p == '\0')
                    break;

                for (;;)
                {
                    copy_character = true;

                    // Rules:
                    // 2N     backslashes   + " ==> N backslashes and begin/end quote
                    // 2N + 1 backslashes   + " ==> N backslashes + literal "
                    // N      backslashes       ==> N backslashes
                    backslash_count = 0;

                    while (*p == '\\')
                    {
                        ++p;
                        ++backslash_count;
                    }

                    if (*p == '"')
                    {
                        // if 2N backslashes before, start/end quote, otherwise
                        // copy literally:
                        if (backslash_count % 2 == 0)
                        {
                            if (in_quotes && p[1] == '"')
                            {
                                p++; // Double quote inside quoted string
                            }
                            else
                            {
                                // Skip first quote char and copy second:
                                copy_character = false;
                                in_quotes = !in_quotes;
                            }
                        }

                        backslash_count /= 2;
                    }

                    while (backslash_count--)
                    {
                        arg.push_back('\\');
                    }

                    if (*p == '\0' || (!in_quotes && (*p == ' ' || *p == '\t')))
                        break;

                    if (copy_character)
                    {
                        arg.push_back(*p);
                    }

                    ++p;
                }

                first_arg = false;
            }
        }