diff options
author | Oleg Morozenkov <reo7sp@users.noreply.github.com> | 2017-01-14 12:54:58 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-14 12:54:58 +0300 |
commit | f986328344e1e5a1c7fad3a529f27bb85aed0c83 (patch) | |
tree | eae827328055dde732bd8fff66500c1bf0051aac /src | |
parent | 429dd74b583e0ac52d50142be493cba532ca017d (diff) | |
parent | 19a83123e908e8d80b404d47ec843fa6f0d4a5a6 (diff) |
Merge pull request #32 from kukin-konstantin/master
ReplyKeyboardMarkup and UTF-8
Diffstat (limited to 'src')
-rw-r--r-- | src/TgTypeParser.cpp | 29 | ||||
-rw-r--r-- | src/tools/StringTools.cpp | 20 |
2 files changed, 24 insertions, 25 deletions
diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index 45124e2..f22a868 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -442,16 +442,12 @@ string TgTypeParser::parseFile(const File::Ptr& object) const { ReplyKeyboardMarkup::Ptr TgTypeParser::parseJsonAndGetReplyKeyboardMarkup(const boost::property_tree::ptree& data) const { ReplyKeyboardMarkup::Ptr result(new ReplyKeyboardMarkup); - for (const pair<const string, ptree>& item : data.find("keyboard")->second) { - vector<string> array; - for (const pair<const string, ptree>& innerItem : item.second) { - array.push_back(innerItem.second.data()); - } - result->keyboard.push_back(array); + for (const boost::property_tree::ptree::value_type& item : data.find("keyboard")->second){ + result->keyboard.push_back(parseJsonAndGetArray<KeyboardButton>(&TgTypeParser::parseJsonAndGetKeyboardButton, item.second)); } - result->resizeKeyboard = data.get<bool>("resize_keyboard"); - result->oneTimeKeyboard = data.get<bool>("one_time_keyboard"); - result->selective = data.get<bool>("selective"); + result->resizeKeyboard = data.get<bool>("resize_keyboard", false); + result->oneTimeKeyboard = data.get<bool>("one_time_keyboard", false); + result->selective = data.get<bool>("selective", false); return result; } @@ -462,17 +458,17 @@ std::string TgTypeParser::parseReplyKeyboardMarkup(const ReplyKeyboardMarkup::Pt string result; result += '{'; result += "\"keyboard\":["; - for (vector<string>& item : object->keyboard) { + for (vector<KeyboardButton::Ptr>& item : object->keyboard) { result += '['; - for (string& innerItem : item) { - result += '"'; - result += innerItem; - result += "\","; + for (KeyboardButton::Ptr& innerItem : item) { + result += parseKeyboardButton(innerItem); + result += ','; } result.erase(result.length() - 1); result += "],"; } - result.erase(result.length() - 1); + if (!object->keyboard.empty()) + result.erase(result.length() - 1); result += "],"; appendToJson(result, "resize_keyboard", object->resizeKeyboard); appendToJson(result, "one_time_keyboard", object->oneTimeKeyboard); @@ -1334,7 +1330,8 @@ std::string TgTypeParser::parseInlineKeyboardMarkup(const InlineKeyboardMarkup:: result.erase(result.length() - 1); result += "],"; } - result.erase(result.length() - 1); + if (!object->inlineKeyboard.empty()) + result.erase(result.length() - 1); result += "]}"; return result; } diff --git a/src/tools/StringTools.cpp b/src/tools/StringTools.cpp index c682b35..d5270db 100644 --- a/src/tools/StringTools.cpp +++ b/src/tools/StringTools.cpp @@ -84,20 +84,22 @@ string generateRandomString(size_t length) { return result; } + string urlEncode(const string& value, const std::string& additionalLegitChars) { - static const string legitPunctuation = "-_.~"; - ostringstream result; - result.fill('0'); - result << hex; - for (const char& c : value) { - if (isalnum(c) || legitPunctuation.find(c) != legitPunctuation.npos || additionalLegitChars.find(c) != additionalLegitChars.npos) { - result << c; + static const string legitPunctuation = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-~:"; + std::stringstream ss; + std::string t; + for (auto const &c : value) { + if ((legitPunctuation.find(c) == std::string::npos) + && (additionalLegitChars.find(c)==std::string::npos)) { + ss << '%' << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (unsigned int)(unsigned char)c; + t = ss.str(); } else { - result << '%' << setw(2) << int((unsigned char) c); + ss << c; } } - return result.str(); + return ss.str(); } string urlDecode(const string& value) { |