Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cgicc query string parsing

Tags:

c++

cgi

I'm new to C++ and cgicc, and I want to know if there is an easy way to parse the get parameters to the cgi script? ?action=yes&function=no into an array or something like that?

like image 893
TheOnly92 Avatar asked Jun 04 '10 07:06

TheOnly92


2 Answers

Here's a simple function using the regular expression library included in C++11.

#include <regex>

// ...

std::map<std::string, std::string> Foo::Parse(const std::string& query)
{
    std::map<std::string, std::string> data;
    std::regex pattern("([\\w+%]+)=([^&]*)");
    auto words_begin = std::sregex_iterator(query.begin(), query.end(), pattern);
    auto words_end = std::sregex_iterator();

    for (std::sregex_iterator i = words_begin; i != words_end; i++)
    {
        std::string key = (*i)[1].str();
        std::string value = (*i)[2].str();
        data[key] = value;
    }

    return data;
}
like image 104
ladenedge Avatar answered Oct 20 '22 11:10

ladenedge


A possible solution

read_cgi_input(&items);
http://eekim.com/software/cgihtml/index.html

http://eekim.com/software/cgihtml/cgihtml-5.html#ss5.1

like image 1
T.T.T. Avatar answered Oct 20 '22 12:10

T.T.T.