cthulhu-bot/commands.cpp
/*
 * This method parses incoming string into vector containing command name and
 * its arguments.
 * Returns true if parsed string is proper command; false otherwise.
 */
This method parses incoming string into vector containing command name and
its arguments.
Returns true if parsed string is proper command; false otherwise.
This method parses incoming string into vector containing command name and its arguments . Returns true if parsed string is proper command ; false otherwise .
bool ChtonianBot::parseCommand(const std::string &str,
    vector<string> &result)
{
    wstring cmd = deUTF8(str);
    const wregex command_name(L"^(![^\\s]+).*");
    const wregex simple_arg(L"^([^\\s]+).*");
    const wregex quoted_arg(L"^\"(.*?(?<!\\\\)(\\\\\\\\)*)\".*");
    result.clear();
    wsmatch arg_match;
    if(regex_match(cmd, arg_match, command_name))
    {
        wstring cmd_name = arg_match[1].str();
        cmd.erase(0, cmd_name.length());
        result.push_back(UTF8(cmd_name.c_str()));
        while(!cmd.empty())
        {
            const wregex trimmed(L"^(\\s*).*");
            wsmatch trimmed_str;
            if(regex_match(cmd, trimmed_str, trimmed))
            {
                cmd.erase(0, trimmed_str[1].length());
            }
            wstring arg;
            int arg_size = 0; 
            if(regex_match(cmd, arg_match, quoted_arg))
            {
                arg = arg_match[1].str();
                arg_size = arg.length() + 2; 
                const wregex escape(L"\\\\\"");
                arg = regex_replace(arg, escape, L"\"");
            }
            else if(regex_match(cmd, arg_match, simple_arg))
            {
                arg = arg_match[1].str();
                arg_size = arg.length();
            }
            cmd.erase(0, arg_size);
            result.push_back(UTF8(arg.c_str()));
        }
        return true;
    }
    return false;
}
bool ChtonianBot :: parseCommand ( const std :: string & str , vector < string > & result ) { wstring cmd = deUTF8 ( str ) ; const wregex command_name ( L" \\ " ) ; const wregex simple_arg ( L" \\ " ) ; const wregex quoted_arg ( L" \" \\ \\ \\ \\ \\ \\ \" " ) ; result . clear ( ) ; wsmatch arg_match ; if ( regex_match ( cmd , arg_match , command_name ) ) { wstring cmd_name = arg_match [ 1 ] . str ( ) ; cmd . erase ( 0 , cmd_name . length ( ) ) ; result . push_back ( UTF8 ( cmd_name . c_str ( ) ) ) ; while ( ! cmd . empty ( ) ) { const wregex trimmed ( L" \\ " ) ; wsmatch trimmed_str ; if ( regex_match ( cmd , trimmed_str , trimmed ) ) { cmd . erase ( 0 , trimmed_str [ 1 ] . length ( ) ) ; } wstring arg ; int arg_size = 0 ; if ( regex_match ( cmd , arg_match , quoted_arg ) ) { arg = arg_match [ 1 ] . str ( ) ; arg_size = arg . length ( ) + 2 ; const wregex escape ( L" \\ \\ \" " ) ; arg = regex_replace ( arg , escape , L" \" " ) ; } else if ( regex_match ( cmd , arg_match , simple_arg ) ) { arg = arg_match [ 1 ] . str ( ) ; arg_size = arg . length ( ) ; } cmd . erase ( 0 , arg_size ) ; result . push_back ( UTF8 ( arg . c_str ( ) ) ) ; } return true ; } return false ; }
This method parses incoming string into vector containing command name and
 its arguments.
This method parses incoming string into vector containing command name and its arguments .
// Trim whitespace from beginning of string
 // amount of characters that need to be removed
 // because we also need to remove
 // 2 '"' characters
 /*const wregex escape(L"\\\\(.)");
                arg = regex_replace(arg, escape, L"(?1)");
                TODO: Do PROPER escaping here!
                (Now we only replace \" -> ")*/