diff --git a/src/mastodon-cpp.cpp b/src/mastodon-cpp.cpp index 4351efb..334a195 100644 --- a/src/mastodon-cpp.cpp +++ b/src/mastodon-cpp.cpp @@ -27,46 +27,6 @@ using namespace Mastodon; -return_base::operator bool() -{ - if (error_code == 0) - { - return true; - } - else - { - return false; - } -} - -return_base::operator uint8_t() -{ - return error_code; -} - -return_call::operator const string() const -{ - return answer; -} - -std::ostream &Mastodon::operator <<(std::ostream &out, const return_call &ret) -{ - out << ret.answer; - return out; -} - -return_call::return_call() -{} - -return_call::return_call(const uint8_t ec, const string &em, - const uint16_t hec, const string &a) -: http_error_code(hec) -, answer(a) -{ - error_code = ec; - error_message = em; -} - API::API(const string &instance, const string &access_token) : _instance(instance) , _access_token(access_token) @@ -310,7 +270,7 @@ const string API::unescape_html(const string &html) // Matches numbered entities between 1 and 8 digits, decimal or hexadecimal std::regex re_entity("&#(x)?(\\d{1,8});"); std::smatch match; - + while (std::regex_search(buffer, match, re_entity)) { char32_t codepoint = 0; diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index cecbfd9..e5d7b8f 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -19,7 +19,6 @@ #include #include -#include #include #include #include @@ -28,8 +27,13 @@ #include #include -using std::uint8_t; -using std::uint16_t; +// If we are compiling mastodon-cpp, use another include path +#ifdef MASTODON_CPP + #include "return_types.hpp" +#else + #include +#endif + using std::string; /*! @@ -38,98 +42,6 @@ using std::string; namespace Mastodon { - /*! - * @brief Basis for return types. - * - * @since 0.100.0 - */ - typedef struct return_base - { - /*! - * @brief @ref error "Error code". - * - * @since 0.100.0 - */ - uint8_t error_code = 0; - - /*! - * @brief The error message, or "". - * - * @since 0.100.0 - */ - string error_message; - - /*! - * @brief true if return_base::error_code is 0, otherwise false. - * - * @since 0.100.0 - */ - operator bool(); - - /*! - * @brief Same as return_base::error_code. - * - * @since 0.100.0 - */ - operator uint8_t(); - } return_base; - - /*! - * @brief Return type for API calls. - * - * Example: - * @code - * Mastodon::return_call ret = masto.get(Mastodon::API::v1::instance); - * if (!ret) // Or ret.error_code != 0 - * { - * cout << "Error " << std::to_string(ret.error_code); - * cout << " (HTTP " << std::to_string(ret.http_error_code) << "): "; - * cout << ret.error_message << endl - * } - * else - * { - * cout << ret << endl; // Or ret.answer - * } - * @endcode - * - * @since 0.100.0 - */ - typedef struct return_call : return_base - { - /*! - * @brief HTTP error code. - * - * @since 0.100.0 - */ - uint16_t http_error_code = 0; - - /*! - * @brief The response from the server. - * - * @since 0.100.0 - */ - string answer; - - return_call(); - return_call(const uint8_t ec, const string &em, - const uint16_t hec, const string &a); - - /*! - * @brief Same es return_call::answer. - * - * @since 0.100.0 - */ - operator const string() const; - - /*! - * @brief Same es return_call::answer. - * - * @since 0.100.0 - */ - friend std::ostream &operator <<(std::ostream &out, - const return_call &ret); - } return_call; - /*! * @brief Interface to the Mastodon API. * diff --git a/src/return_types.cpp b/src/return_types.cpp new file mode 100644 index 0000000..69e4bc4 --- /dev/null +++ b/src/return_types.cpp @@ -0,0 +1,59 @@ +/* This file is part of mastodon-cpp. + * Copyright © 2019 tastytea + * + * 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 . + */ + +#include "return_types.hpp" + +using namespace Mastodon; + +return_base::operator bool() +{ + if (error_code == 0) + { + return true; + } + else + { + return false; + } +} + +return_base::operator uint8_t() +{ + return error_code; +} + +return_call::operator const string() const +{ + return answer; +} + +std::ostream &Mastodon::operator <<(std::ostream &out, const return_call &ret) +{ + out << ret.answer; + return out; +} + +return_call::return_call() +{} + +return_call::return_call(const uint8_t ec, const string &em, + const uint16_t hec, const string &a) +: http_error_code(hec) +, answer(a) +{ + error_code = ec; + error_message = em; +} diff --git a/src/return_types.hpp b/src/return_types.hpp new file mode 100644 index 0000000..1bc6e03 --- /dev/null +++ b/src/return_types.hpp @@ -0,0 +1,122 @@ +/* This file is part of mastodon-cpp. + * Copyright © 2019 tastytea + * + * 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 . + */ + +#ifndef MASTODON_CPP_RETURN_TYPES_HPP +#define MASTODON_CPP_RETURN_TYPES_HPP + +#include +#include + +using std::uint8_t; +using std::uint16_t; +using std::string; + +namespace Mastodon +{ + /*! + * @brief Basis for return types. + * + * @since 0.100.0 + */ + typedef struct return_base + { + /*! + * @brief @ref error "Error code". + * + * @since 0.100.0 + */ + uint8_t error_code = 0; + + /*! + * @brief The error message, or "". + * + * @since 0.100.0 + */ + string error_message; + + /*! + * @brief true if return_base::error_code is 0, otherwise false. + * + * @since 0.100.0 + */ + operator bool(); + + /*! + * @brief Same as return_base::error_code. + * + * @since 0.100.0 + */ + operator uint8_t(); + } return_base; + + /*! + * @brief Return type for API calls. + * + * Example: + * @code + * Mastodon::return_call ret = masto.get(Mastodon::API::v1::instance); + * if (!ret) // Or ret.error_code != 0 + * { + * cout << "Error " << std::to_string(ret.error_code); + * cout << " (HTTP " << std::to_string(ret.http_error_code) << "): "; + * cout << ret.error_message << endl + * } + * else + * { + * cout << ret << endl; // Or ret.answer + * } + * @endcode + * + * @since 0.100.0 + */ + typedef struct return_call : return_base + { + /*! + * @brief HTTP error code. + * + * @since 0.100.0 + */ + uint16_t http_error_code = 0; + + /*! + * @brief The response from the server. + * + * @since 0.100.0 + */ + string answer; + + return_call(); + return_call(const uint8_t ec, const string &em, + const uint16_t hec, const string &a); + + /*! + * @brief Same es return_call::answer. + * + * @since 0.100.0 + */ + operator const string() const; + + /*! + * @brief Same es return_call::answer. + * + * @since 0.100.0 + */ + friend std::ostream &operator <<(std::ostream &out, + const return_call &ret); + } return_call; +} + +#endif // MASTODON_CPP_RETURN_TYPES_HPP