From 6717e588e71d0586613053521b2dc591405410ba Mon Sep 17 00:00:00 2001 From: Oleg Morozenkov Date: Sun, 5 Jul 2015 01:17:15 +0300 Subject: First version --- src/tgbot/Parser.cpp | 485 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 485 insertions(+) create mode 100644 src/tgbot/Parser.cpp (limited to 'src/tgbot/Parser.cpp') diff --git a/src/tgbot/Parser.cpp b/src/tgbot/Parser.cpp new file mode 100644 index 0000000..3730607 --- /dev/null +++ b/src/tgbot/Parser.cpp @@ -0,0 +1,485 @@ +/* + * Copyright (c) 2015 Oleg Morozenkov + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software", "to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "Parser.h" + +using namespace std; +using namespace boost::property_tree; + +namespace TgBot { + +User::Ptr Parser::parseUser(const ptree& data) const { + User::Ptr result(new User); + result->id = data.get("id"); + result->firstName = data.get("first_name"); + result->lastName = data.get("last_name", ""); + result->username = data.get("username", ""); + return result; +} + +string Parser::parseUser(const User::Ptr& object) const { + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "id", object->id); + appendToJson(result, "first_name", object->firstName); + appendToJson(result, "last_name", object->lastName); + appendToJson(result, "username", object->username); + result.erase(result.length() - 1); + result += '}'; + return result; +} + +GroupChat::Ptr Parser::parseGroupChat(const ptree& data) const { + GroupChat::Ptr result(new GroupChat); + result->id = data.get("id"); + result->title = data.get("title"); + return result; +} + +string Parser::parseGroupChat(const GroupChat::Ptr& object) const { + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "id", object->id); + appendToJson(result, "title", object->title); + result.erase(result.length() - 1); + result += '}'; + return result; +} + +Message::Ptr Parser::parseMessage(const ptree& data) const { + Message::Ptr result(new Message); + result->messageId = data.get("message_id"); + result->from = parseUser(data.find("from")->second); + result->date = data.get("date"); + result->chat = parseGenericChat(data.find("chat")->second); + result->forwardFrom = tryParse(parseUser, data, "forward_from"); + result->forwardDate = data.get("forward_date", 0); + result->replyToMessage = tryParse(parseMessage, data, "reply_to_message"); + result->text = data.get("text", ""); + result->audio = tryParse