Last active
August 19, 2016 18:11
-
-
Save Tosainu/a72ce610b9c162291fc0 to your computer and use it in GitHub Desktop.
oauth/access_token response parser
This file contains hidden or 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
#include <iostream> | |
#include <unordered_map> | |
#include <boost/fusion/include/std_pair.hpp> | |
#include <boost/spirit/include/qi.hpp> | |
#include <boost/spirit/include/qi_grammar.hpp> | |
namespace qi = boost::spirit::qi; | |
template <class String, class Iterator = typename String::const_iterator> | |
struct query_string : qi::grammar<Iterator, std::unordered_map<String, String>()> { | |
query_string() : query_string::base_type(map_) { | |
map_ = pair_ % qi::lit('&'); | |
pair_ = string_ >> qi::lit('=') >> string_; | |
string_ = +qi::char_("0-9a-zA-Z_"); | |
} | |
qi::rule<Iterator, std::unordered_map<String, String>()> map_; | |
qi::rule<Iterator, std::pair<String, String>()> pair_; | |
qi::rule<Iterator, String()> string_; | |
}; | |
auto main() -> int { | |
{ | |
std::cout << "- std::string --------------------\n"; | |
const std::string str( | |
"oauth_token=hogehogehogehogehogehogehogehogehogehoge&" | |
"oauth_token_secret=fugafugafugafugafugafugafugafugafugafuga&" | |
"user_id=12345&" | |
"screen_name=nyan"); | |
std::unordered_map<std::string, std::string> res{}; | |
if (qi::parse(str.begin(), str.end(), query_string<std::string>(), res)) { | |
for (auto&& v : res) { | |
std::cout << v.first << " => " << v.second << '\n'; | |
} | |
} | |
} | |
{ | |
std::wcout << L"- std::wstring -------------------\n"; | |
const std::wstring str( | |
L"oauth_token=hogehogehogehogehogehogehogehogehogehoge&" | |
"oauth_token_secret=fugafugafugafugafugafugafugafugafugafuga&" | |
"user_id=12345&" | |
"screen_name=nyan"); | |
std::unordered_map<std::wstring, std::wstring> res{}; | |
if (qi::parse(str.begin(), str.end(), query_string<std::wstring>(), res)) { | |
for (auto&& v : res) { | |
std::wcout << v.first << L" => " << v.second << L'\n'; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment