fixed headers, refactoring

This commit is contained in:
tastytea 2018-04-01 03:36:11 +02:00
parent 4baef545db
commit 445567744d
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
21 changed files with 258 additions and 239 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7)
project (mastodon-cpp
VERSION 0.7.19
VERSION 0.7.20
LANGUAGES CXX
)

View File

@ -24,10 +24,10 @@
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy.hpp"
#include "easy/easy.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#endif
using std::string;

View File

@ -19,7 +19,7 @@
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "easy.hpp"
#include "easy/easy.hpp"
#include "easy/account.hpp"
#include "easy/application.hpp"
#include "easy/attachment.hpp"
@ -36,7 +36,7 @@
#include "easy/status.hpp"
#include "easy/tag.hpp"
#else
#include <mastodon-cpp/easy.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#include <mastodon-cpp/easy/account.hpp>
#include <mastodon-cpp/easy/application.hpp>
#include <mastodon-cpp/easy/attachment.hpp>

View File

@ -22,10 +22,10 @@
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy.hpp"
#include "easy/easy.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#endif
using std::string;

View File

@ -53,10 +53,7 @@ const std::chrono::duration<double> Attachment::duration() const
{
const double sec = get_double("meta.original.duration");
if (sec > 0.0)
{
return std::chrono::duration<double>(sec);
}
return std::chrono::duration<double>(sec);
}
const std::array<double, 2> Attachment::focus() const

View File

@ -25,10 +25,10 @@
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy.hpp"
#include "easy/easy.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#endif
using std::string;

View File

@ -23,10 +23,10 @@
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy.hpp"
#include "easy/easy.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#endif
using std::string;

View File

@ -23,12 +23,12 @@
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy.hpp"
#include "status.hpp"
#include "easy/easy.hpp"
#include "easy/status.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy.hpp>
#include <mastodon-cpp/status.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#include <mastodon-cpp/easy/status.hpp>
#endif
using std::string;

View File

@ -23,8 +23,6 @@
using namespace Mastodon;
using std::string;
using std::uint_fast64_t;
using std::chrono::system_clock;
Easy::Easy(const string &instance, const string &access_token)
: API(instance, access_token)
@ -51,41 +49,6 @@ const std::vector<string> Easy::json_array_to_vector(const string &json)
return {};
}
Easy::Entity::Entity(const string &json)
: _tree(Json::nullValue)
, _valid(false)
{
from_string(json);
}
const void Easy::Entity::from_string(const string &json)
{
std::stringstream ss(json);
ss >> _tree;
// If the JSON is a single object encapsulated in an array,
// transform it into an object. If the JSON string is [], transform to null
if (_tree.type() == Json::ValueType::arrayValue && _tree.size() <= 1)
{
_tree = _tree[0];
}
if (_tree.isNull())
{
ttdebug << "ERROR: JSON string holds no object\n";
ttdebug << "String was: " << json << '\n';
}
else if (!_tree["error"].isNull())
{
ttdebug << "ERROR: Server returned an error\n";
ttdebug << "String was: " << json << '\n';
}
else
{
_valid = true;
}
}
const std::vector<Easy::stream_event>
Easy::parse_stream(const std::string &streamdata)
{
@ -113,144 +76,3 @@ const std::vector<Easy::stream_event>
return vec;
}
const Json::Value Easy::Entity::to_object() const
{
return _tree;
}
Easy::Entity::Entity()
: _valid(false)
{}
const bool Easy::Entity::valid() const
{
return _valid;
}
const string Easy::Entity::error() const
{
return get_string("error");
}
const Json::Value Easy::Entity::get(const string &key) const
{
const Json::Value *node;
if (key.find('.') == std::string::npos)
{
node = &_tree[key];
}
else
{
// If dots in key, we have to walk through the tree
std::size_t pos = 0;
string current_key = key;
node = &_tree;
while ((pos = current_key.find('.')) != std::string::npos)
{
try
{
node = &(*node)[current_key.substr(0, pos)];
current_key = current_key.substr(pos + 1);
}
catch (const Json::LogicError &e)
{
ttdebug << e.what() << '\n';
goto error;
}
}
node = &(*node)[current_key];
}
if (!node->isNull())
{
return *node;
}
error:
ttdebug << "Could not get data: " << key << '\n';
return Json::Value();
}
const string Easy::Entity::get_string(const string &key) const
{
const Json::Value node = get(key);
if (node.isString())
{
return node.asString();
}
return "";
}
const uint_fast64_t Easy::Entity::get_uint64(const string &key) const
{
const Json::Value node = get(key);
if (node.isUInt64())
{
return node.asUInt64();
}
return 0;
}
const double Easy::Entity::get_double(const string &key) const
{
const Json::Value node = get(key);
if (node.isDouble())
{
return node.asDouble();
}
return 0.0;
}
const bool Easy::Entity::get_bool(const string &key) const
{
const Json::Value node = get(key);
if (node.isBool())
{
return node.asBool();
}
return false;
}
const system_clock::time_point
Easy::Entity::get_time_point(const string &key) const
{
const Json::Value node = get(key);
if (node.isString())
{
std::stringstream sstime(node.asString());
struct std::tm tm = {0};
sstime >> std::get_time(&tm, "%Y-%m-%dT%T");
std::time_t time = timegm(&tm);
return system_clock::from_time_t(time);
}
// Return clocks epoch
return system_clock::time_point();
}
const std::vector<string> Easy::Entity::get_vector(const string &key) const
{
const Json::Value node = get(key);
if (node.isArray())
{
std::vector<string> vec;
for (const Json::Value &value : node)
{
vec.push_back(value.asString());
}
return vec;
}
return {};
}

View File

@ -136,9 +136,6 @@ public:
static const std::vector<stream_event>
parse_stream(const std::string &streamdata);
/*!
* @brief Base class for entities.
*/
class Entity
{
public:

View File

@ -22,10 +22,10 @@
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy.hpp"
#include "easy/easy.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#endif
using std::string;

203
src/easy/entity.cpp Normal file
View File

@ -0,0 +1,203 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <ctime>
#include <iomanip> // get_time
#include <sstream>
#include <chrono>
#include <regex>
#include "easy.hpp"
#include "macros.hpp"
using namespace Mastodon;
using std::string;
using std::chrono::system_clock;
Easy::Entity::Entity(const string &json)
: _tree(Json::nullValue)
, _valid(false)
{
from_string(json);
}
const void Easy::Entity::from_string(const string &json)
{
std::stringstream ss(json);
ss >> _tree;
// If the JSON is a single object encapsulated in an array,
// transform it into an object. If the JSON string is [], transform to null
if (_tree.type() == Json::ValueType::arrayValue && _tree.size() <= 1)
{
_tree = _tree[0];
}
if (_tree.isNull())
{
ttdebug << "ERROR: JSON string holds no object\n";
ttdebug << "String was: " << json << '\n';
}
else if (!_tree["error"].isNull())
{
ttdebug << "ERROR: Server returned an error\n";
ttdebug << "String was: " << json << '\n';
}
else
{
_valid = true;
}
}
const Json::Value Easy::Entity::to_object() const
{
return _tree;
}
Easy::Entity::Entity()
: _valid(false)
{}
const bool Easy::Entity::valid() const
{
return _valid;
}
const string Easy::Entity::error() const
{
return get_string("error");
}
const Json::Value Easy::Entity::get(const string &key) const
{
const Json::Value *node;
if (key.find('.') == std::string::npos)
{
node = &_tree[key];
}
else
{
// If dots in key, we have to walk through the tree
std::size_t pos = 0;
string current_key = key;
node = &_tree;
while ((pos = current_key.find('.')) != std::string::npos)
{
try
{
node = &(*node)[current_key.substr(0, pos)];
current_key = current_key.substr(pos + 1);
}
catch (const Json::LogicError &e)
{
ttdebug << e.what() << '\n';
goto error;
}
}
node = &(*node)[current_key];
}
if (!node->isNull())
{
return *node;
}
error:
ttdebug << "Could not get data: " << key << '\n';
return Json::Value();
}
const string Easy::Entity::get_string(const string &key) const
{
const Json::Value node = get(key);
if (node.isString())
{
return node.asString();
}
return "";
}
const uint_fast64_t Easy::Entity::get_uint64(const string &key) const
{
const Json::Value node = get(key);
if (node.isUInt64())
{
return node.asUInt64();
}
return 0;
}
const double Easy::Entity::get_double(const string &key) const
{
const Json::Value node = get(key);
if (node.isDouble())
{
return node.asDouble();
}
return 0.0;
}
const bool Easy::Entity::get_bool(const string &key) const
{
const Json::Value node = get(key);
if (node.isBool())
{
return node.asBool();
}
return false;
}
const system_clock::time_point
Easy::Entity::get_time_point(const string &key) const
{
const Json::Value node = get(key);
if (node.isString())
{
std::stringstream sstime(node.asString());
struct std::tm tm = {0};
sstime >> std::get_time(&tm, "%Y-%m-%dT%T");
std::time_t time = timegm(&tm);
return system_clock::from_time_t(time);
}
// Return clocks epoch
return system_clock::time_point();
}
const std::vector<string> Easy::Entity::get_vector(const string &key) const
{
const Json::Value node = get(key);
if (node.isArray())
{
std::vector<string> vec;
for (const Json::Value &value : node)
{
vec.push_back(value.asString());
}
return vec;
}
return {};
}

View File

@ -23,11 +23,11 @@
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy.hpp"
#include "account.hpp"
#include "easy/easy.hpp"
#include "easy/account.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#include <mastodon-cpp/easy/account.hpp>
#endif

View File

@ -24,10 +24,10 @@
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy.hpp"
#include "easy/easy.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#endif
using std::string;

View File

@ -23,10 +23,10 @@
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy.hpp"
#include "easy/easy.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#endif
using std::string;

View File

@ -24,12 +24,12 @@
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy.hpp"
#include "account.hpp"
#include "status.hpp"
#include "easy/easy.hpp"
#include "easy/account.hpp"
#include "easy/status.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#include <mastodon-cpp/easy/account.hpp>
#include <mastodon-cpp/easy/status.hpp>
#endif

View File

@ -23,10 +23,10 @@
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy.hpp"
#include "easy/easy.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#endif
using std::string;

View File

@ -23,10 +23,10 @@
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy.hpp"
#include "easy/easy.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#endif
using std::string;

View File

@ -23,14 +23,14 @@
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy.hpp"
#include "account.hpp"
#include "status.hpp"
#include "easy/easy.hpp"
#include "easy/account.hpp"
#include "easy/status.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy.hpp>
#include <mastodon-cpp/account.hpp>
#include <mastodon-cpp/status.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#include <mastodon-cpp/easy/account.hpp>
#include <mastodon-cpp/easy/status.hpp>
#endif
using std::string;

View File

@ -25,22 +25,22 @@
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy.hpp"
#include "account.hpp"
#include "emoji.hpp"
#include "attachment.hpp"
#include "mention.hpp"
#include "tag.hpp"
#include "application.hpp"
#include "easy/easy.hpp"
#include "easy/account.hpp"
#include "easy/emoji.hpp"
#include "easy/attachment.hpp"
#include "easy/mention.hpp"
#include "easy/tag.hpp"
#include "easy/application.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy.hpp>
#include <mastodon-cpp/account.hpp>
#include <mastodon-cpp/emoji.hpp>
#include <mastodon-cpp/attachment.hpp>
#include <mastodon-cpp/mention.hpp>
#include <mastodon-cpp/tag.hpp>
#include <mastodon-cpp/application.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#include <mastodon-cpp/easy/account.hpp>
#include <mastodon-cpp/easy/emoji.hpp>
#include <mastodon-cpp/easy/attachment.hpp>
#include <mastodon-cpp/easy/mention.hpp>
#include <mastodon-cpp/easy/tag.hpp>
#include <mastodon-cpp/easy/application.hpp>
#endif
using std::string;

View File

@ -22,10 +22,10 @@
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#include "easy.hpp"
#include "easy/easy.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy.hpp>
#include <mastodon-cpp/easy/easy.hpp>
#endif
using std::string;