From b0c3ba3f91926b0c95625bdbf4aab69269130b13 Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Thu, 9 May 2024 10:06:02 -0400 Subject: Add runtime encoding detection and conversion Win-1251/1252 detection is a reduced C++ version of https://github.com/hsivonen/chardetng Add manually-specified encoding fallback Add default system encoding fallback Add error recovery to v2script Add unknown encoding detection warning Remove csv::Parser templating Fix lua files dropping data Update lexy to foonathan/lexy@1e5d99fa3826b1c3c8628d3a11117fb4fb4cc0d0 Remove exclusive reliance on lexy::default_encoding for v2script Move internal concepts to src/openvic-detail/InternalConcepts.hpp Move contents of DetectUtf8.hpp to src/detail/Detect.hpp Move openvic-dataloader/AbstractSyntaxTree.hpp to src Move DiagnosticLogger.hpp to src Move File.hpp to src Move openvic-dataloader/detail/utlity files to openvic-dataloader/detail Add ovdl::utility::type_concat Add ovdl::utility::type_prepend Add ovdl::utility::is_instance_of Overhaul parse error messages --- src/openvic-dataloader/File.hpp | 139 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 src/openvic-dataloader/File.hpp (limited to 'src/openvic-dataloader/File.hpp') diff --git a/src/openvic-dataloader/File.hpp b/src/openvic-dataloader/File.hpp new file mode 100644 index 0000000..90fcb11 --- /dev/null +++ b/src/openvic-dataloader/File.hpp @@ -0,0 +1,139 @@ +#pragma once + +#include +#include // IWYU pragma: keep +#include +#include + +#include +#include + +#include +#include + +#include + +namespace ovdl { + struct File { + using buffer_ids = detail::TypeRegister< + lexy::buffer, + lexy::buffer, + lexy::buffer, + lexy::buffer, + lexy::buffer, + lexy::buffer>; + + explicit File(const char* path); + + const char* path() const noexcept; + + bool is_valid() const noexcept; + + template + constexpr bool is_buffer() const { + return buffer_ids::type_id>() + 1 == _buffer.index(); + } + + template + lexy::buffer* try_get_buffer_as() { + return std::get_if>(&_buffer); + } + + template + const lexy::buffer* try_get_buffer_as() const { + return std::get_if>(&_buffer); + } + + template + lexy::buffer& get_buffer_as() { + assert((is_buffer())); + return *std::get_if>(&_buffer); + } + + template + const lexy::buffer& get_buffer_as() const { + assert((is_buffer())); + return *std::get_if>(&_buffer); + } + +#define SWITCH_LIST \ + X(1) \ + X(2) \ + X(3) \ + X(4) \ + X(5) \ + X(6) + +#define X(NUM) \ + case NUM: \ + return visitor(std::get(_buffer)); + + template + decltype(auto) visit_buffer(Visitor&& visitor) { + switch (_buffer.index()) { + SWITCH_LIST + default: ovdl::detail::unreachable(); + } + } + + template + Return visit_buffer(Visitor&& visitor) { + switch (_buffer.index()) { + SWITCH_LIST + default: ovdl::detail::unreachable(); + } + } + + template + decltype(auto) visit_buffer(Visitor&& visitor) const { + switch (_buffer.index()) { + SWITCH_LIST + default: ovdl::detail::unreachable(); + } + } + + template + Return visit_buffer(Visitor&& visitor) const { + switch (_buffer.index()) { + SWITCH_LIST + default: ovdl::detail::unreachable(); + } + } +#undef X +#undef SWITCH_LIST + + protected: + const char* _path; + detail::type_prepend_t _buffer; + }; + + template + struct BasicFile : File { + using node_type = NodeT; + + template + explicit BasicFile(const char* path, lexy::buffer&& buffer) + : File(path) { + _buffer = static_cast&&>(buffer); + } + + template + explicit BasicFile(lexy::buffer&& buffer) + : File("") { + _buffer = static_cast&&>(buffer); + } + + void set_location(const node_type* n, NodeLocation loc) { + _map.insert(n, loc); + } + + NodeLocation location_of(const node_type* n) const { + auto result = _map.lookup(n); + DRYAD_ASSERT(result != nullptr, "every Node should have a NodeLocation"); + return *result; + } + + protected: + dryad::node_map _map; + }; +} \ No newline at end of file -- cgit v1.2.3-56-ga3b1