Created
November 2, 2015 01:11
-
-
Save AntonNik0laev/12d68c40adca9de12075 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef std::map<std::string, std::string> TQuery; | |
struct Uri | |
{ | |
std::string scheme; | |
std::string host; | |
std::string path; | |
TQuery query; | |
}; | |
BOOST_FUSION_ADAPT_STRUCT( | |
Uri, | |
(std::string, scheme) | |
(std::string, host) | |
(std::string, path) | |
(TQuery, query) | |
) | |
template<class Iterator> | |
struct UriGrammar : public boost::spirit::qi::grammar<Iterator, Uri()> { | |
UriGrammar() :UriGrammar::base_type(uri) | |
{ | |
namespace qi = boost::spirit::qi; | |
typedef qi::rule<Iterator, std::string()> StringArgRule; | |
typedef qi::rule<Iterator, std::map<std::string, std::string>()> MapArgRule; | |
typedef qi::rule<Iterator, std::pair<std::string, std::string>()> PairArgRule; | |
StringArgRule scheme = *qi::alnum >> "://"; | |
StringArgRule host = qi::alnum >> *(qi::alnum | qi::char_(".")); | |
StringArgRule path = qi::char_('/') >> *(qi::alnum | qi::char_("/.")); | |
StringArgRule queryParamName = *qi::alnum; | |
StringArgRule queryParamValue = *(qi::alnum |qi::char_('%') ); | |
PairArgRule queryPair = queryParamName >> '=' >> queryParamValue; | |
MapArgRule query = qi::lit('?') >> (queryPair % '&'); | |
uri = scheme >> host >> path >> query; | |
} | |
boost::spirit::qi::rule<Iterator, Uri()> uri; | |
}; | |
int main() | |
{ | |
Uri uriStruct; | |
bool parsed = qi::parse(uri.begin(), uri.end(), UriGrammar<std::string::iterator>(), uriStruct); | |
std::cout << uriStruct.host << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment