From 7bba7176194c4d1d9c7066f8eda7a94491d924e6 Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Mon, 30 Nov 2026 21:27:55 +0100 Subject: Cleanups and updates --- server/src/http/ext/gpx.cpp | 139 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 server/src/http/ext/gpx.cpp (limited to 'server/src/http/ext/gpx.cpp') diff --git a/server/src/http/ext/gpx.cpp b/server/src/http/ext/gpx.cpp new file mode 100644 index 0000000..c1f565f --- /dev/null +++ b/server/src/http/ext/gpx.cpp @@ -0,0 +1,139 @@ +module; + +#include + +module routemon:http.ext.gpx$impl; + +import :http.ext.gpx; + +namespace chrono = std::chrono; + +namespace routemon::http::ext::gpx { + +auto parse_error_category_impl::name() const noexcept -> char const* +{ + return "gpx_parse"; +} + +auto parse_error_category_impl::message(int condition) const noexcept + -> std::string +{ + std::ignore = condition; + return "failed to parse GPX file"; +} + +auto parse_error_category() noexcept -> parse_error_category_impl const& +{ + static auto const inst = parse_error_category_impl{}; + return inst; +} + +auto parse_error() noexcept -> std::error_code +{ + return std::error_code{1, parse_error_category()}; +} + +auto parse_result::set_exception(std::exception_ptr ex) noexcept -> void +{ + res_ = ex; +} + +auto parse_result::set_gpx_file(routemon::gpx::file&& f) noexcept -> void +{ + res_ = std::move(f); +} + +auto parse_result::init_parse_duration( + chrono::steady_clock::duration dur) noexcept -> void +{ + init_parse_dur_ = dur; +} + +[[nodiscard]] auto parse_result::init_parse_duration() const noexcept + -> chrono::steady_clock::duration +{ + return init_parse_dur_; +} + +auto parse_result::put_parse_duration( + chrono::steady_clock::duration dur) noexcept -> void +{ + put_parse_dur_ = dur; +} + +[[nodiscard]] auto parse_result::put_parse_duration() const noexcept + -> chrono::steady_clock::duration +{ + return put_parse_dur_; +} + +auto parse_result::finish_parse_duration( + chrono::steady_clock::duration dur) noexcept -> void +{ + finish_parse_dur_ = dur; +} + +[[nodiscard]] auto parse_result::finish_parse_duration() const noexcept + -> chrono::steady_clock::duration +{ + return finish_parse_dur_; +} + +auto parse_result::unwrap() -> routemon::gpx::file&& +{ + return std::visit( + util::overloaded{ + [](std::exception_ptr ex) -> routemon::gpx::file&& + { + if (ex) + std::rethrow_exception(ex); + else + throw std::runtime_error{"no GPX file parse result available"}; + }, + [](routemon::gpx::file&& f) -> routemon::gpx::file&& + { return std::move(f); }, + }, + std::move(res_)); +} + +auto body::reader::init( + boost::optional /* n */, beast::error_code& ec) noexcept + -> void +{ + auto const init_start = chrono::steady_clock::now(); + try + { + r_.init(); + ec = {}; + } + catch (std::exception& ex) + { + res_->set_exception(std::current_exception()); + ec = parse_error(); + } + res_->init_parse_duration(chrono::steady_clock::now() - init_start); +} + +auto body::reader::finish(beast::error_code& ec) noexcept -> void +{ + auto const finish_start = chrono::steady_clock::now(); + try + { + if (buf_.size() > 0) + { + r_.put(std::string_view{buf_}); + buf_.clear(); + } + + res_->set_gpx_file(r_.finish()); + ec = {}; + } + catch (std::exception& ex) + { + res_->set_exception(std::current_exception()); + ec = parse_error(); + } + res_->finish_parse_duration(chrono::steady_clock::now() - finish_start); +} + +} // namespace routemon::http::ext::gpx -- cgit v1.3.1