diff --git a/CMakeLists.txt b/CMakeLists.txt index f53f97e..d92ee51 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,8 @@ list(APPEND FLAGS -Werror -Wall -Wextra -Wshadow -Wpedantic -Wno-gnu-anonymous-s add_link_options(${FLAGS}) -add_executable(${PROJECT_NAME} main.cpp misc.cpp config.cpp servehelper.cpp pixivclient.cpp blankie/serializer.cpp blankie/escape.cpp blankie/murl.cpp +add_executable(${PROJECT_NAME} main.cpp misc.cpp config.cpp servehelper.cpp numberhelper.cpp pixivclient.cpp + blankie/serializer.cpp blankie/escape.cpp blankie/murl.cpp routes/home.cpp routes/css.cpp routes/users/common.cpp routes/users/users.cpp routes/users/illustrations.cpp) set_target_properties(${PROJECT_NAME} PROPERTIES diff --git a/blankie/murl.cpp b/blankie/murl.cpp index 0e41a11..c44ab4f 100644 --- a/blankie/murl.cpp +++ b/blankie/murl.cpp @@ -3,6 +3,7 @@ #include #include "murl.h" +#include "../numberhelper.h" #define UNRESERVED "[\\w\\d\\-._~]" #define PCT_ENCODED "%[\\da-f]{2}" @@ -30,7 +31,6 @@ "(?:#" FRAGMENT ")?" static std::regex url_regex(HTTP_HTTPS_URL, std::regex::icase); -static inline int to_int(const std::string& str); static void handle_segment(std::vector& segments, const std::string& str, size_t offset, size_t length); namespace blankie { @@ -122,21 +122,6 @@ std::string normalize_path(const std::string& str) { }; // namespace murl }; // namespace blankie -static inline int to_int(const std::string& str) { - char* endptr; - - long res = strtol(str.c_str(), &endptr, 10); - if (res > INT_MAX) { - throw std::overflow_error(str + " is too big"); - } else if (res < INT_MIN) { - throw std::underflow_error(str + " is too small"); - } else if (endptr[0] != '\0') { - throw std::invalid_argument(str + " has trailing text"); - } - - return static_cast(res); -} - static void handle_segment(std::vector& segments, const std::string& str, size_t offset, size_t length) { if (length == 2 && str[offset] == '.' && str[offset + 1] == '.') { if (segments.empty()) { diff --git a/numberhelper.cpp b/numberhelper.cpp new file mode 100644 index 0000000..0e10b26 --- /dev/null +++ b/numberhelper.cpp @@ -0,0 +1,31 @@ +#include +#include + +#include "numberhelper.h" + +unsigned long long to_ull(const std::string& str) { + char* endptr; + + errno = 0; + unsigned long long ret = strtoull(str.c_str(), &endptr, 10); + if (ret > ULLONG_MAX && errno == ERANGE) { + throw std::overflow_error(str + " is too big"); + } else if (endptr[0] != '\0') { + throw std::invalid_argument(str + " has trailing text"); + } + return ret; +} + +int to_int(const std::string& str) { + char* endptr; + + long ret = strtol(str.c_str(), &endptr, 10); + if (ret > INT_MAX) { + throw std::overflow_error(str + " is too big"); + } else if (ret < INT_MIN) { + throw std::underflow_error(str + " is too small"); + } else if (endptr[0] != '\0') { + throw std::invalid_argument(str + " has trailing text"); + } + return static_cast(ret); +} diff --git a/numberhelper.h b/numberhelper.h new file mode 100644 index 0000000..9c153a1 --- /dev/null +++ b/numberhelper.h @@ -0,0 +1,7 @@ +#pragma once + +#include +#include + +unsigned long long to_ull(const std::string& str); +int to_int(const std::string& str); diff --git a/pixivclient.cpp b/pixivclient.cpp index 8eb5160..33b4c4b 100644 --- a/pixivclient.cpp +++ b/pixivclient.cpp @@ -1,12 +1,12 @@ #include +#include "numberhelper.h" #include "blankie/murl.h" #include "pixivclient.h" static inline std::optional get_1920x960_cover_image(blankie::murl::Url url); static inline std::optional get_original_cover_image(blankie::murl::Url url); static inline std::optional get_original_profile_picture(blankie::murl::Url url); -static inline uint64_t to_ull(const std::string& str); PixivClient::PixivClient() { this->_www_pixiv_net_client.set_keep_alive(true); @@ -130,17 +130,3 @@ static inline std::optional get_original_profile_picture(blankie::m url.path = sm.str(1) + sm.str(2); return url.to_string(); } - -static inline uint64_t to_ull(const std::string& str) { - char* endptr; - - errno = 0; - unsigned long long res = strtoull(str.c_str(), &endptr, 10); - if (endptr[0] != '\0') { - throw std::invalid_argument(str + " contains trailing data"); - } else if (res == ULLONG_MAX && errno == ERANGE) { - throw std::overflow_error(str + " overflows uint64_t"); - } - - return res; -} diff --git a/routes/users/illustrations.cpp b/routes/users/illustrations.cpp index 991da1e..7d4ace1 100644 --- a/routes/users/illustrations.cpp +++ b/routes/users/illustrations.cpp @@ -1,11 +1,11 @@ #include #include "../routes.h" +#include "../../numberhelper.h" #include "../../servehelper.h" #include "../../pixivclient.h" #include "common.h" -static inline uint64_t to_ull(const std::string& str); static Element generate_pager(size_t page, size_t items, size_t items_per_page); static inline Element generate_content(const std::vector& illust_ids, size_t page, size_t items_per_page); @@ -48,19 +48,6 @@ void user_illustrations_route(const httplib::Request& req, httplib::Response& re serve(req, res, config, user.display_name + " illustrations", std::move(body)); } -static inline uint64_t to_ull(const std::string& str) { - char* endptr; - - errno = 0; - uint64_t res = strtoull(str.c_str(), &endptr, 10); - if (res == ULLONG_MAX && errno == ERANGE) { - throw std::overflow_error(str + " is too big"); - } else if (endptr[0] != '\0') { - throw std::invalid_argument(str + " contains trailing text or is not an integer"); - } - return res; -} - static Element generate_pager(size_t page, size_t items, size_t items_per_page) { using namespace std::string_literals; diff --git a/routes/users/users.cpp b/routes/users/users.cpp index ae8d376..d7c013f 100644 --- a/routes/users/users.cpp +++ b/routes/users/users.cpp @@ -1,10 +1,9 @@ #include "../routes.h" +#include "../../numberhelper.h" #include "../../servehelper.h" #include "../../pixivclient.h" #include "common.h" -static inline uint64_t to_ull(const std::string& str); - void users_route(const httplib::Request& req, httplib::Response& res, const Config& config, PixivClient& pixiv_client) { uint64_t user_id = to_ull(req.matches[1].str()); User user; @@ -26,16 +25,3 @@ void users_route(const httplib::Request& req, httplib::Response& res, const Conf } serve(req, res, config, user.display_name + " (@" + user.username + ')', generate_user_header(user, config)); } - -static inline uint64_t to_ull(const std::string& str) { - char* endptr; - - errno = 0; - uint64_t res = strtoull(str.c_str(), &endptr, 10); - if (res == ULLONG_MAX && errno == ERANGE) { - throw std::overflow_error(str + " is too big"); - } else if (endptr[0] != '\0') { - throw std::invalid_argument(str + " contains trailing text or is not an integer"); - } - return res; -}