From a705b0223664f915d8bb00d4f89e05838da3e4bc Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Sun, 3 Sep 2023 17:54:18 -0400 Subject: Add Windows-1252 CSV Parser Fix CsvGrammar counting behavior Add csv parse argument to headless/main.cpp Change LineObject contained value type to pair Add ostream print to LineObject Add vscode tasks.json Add vscode launch.json Move csv::Parser template class specialization to end of Parser.cpp Add detail/ClassExport.hpp to assist compiler export --- include/openvic-dataloader/csv/LineObject.hpp | 38 +++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 5 deletions(-) (limited to 'include/openvic-dataloader/csv/LineObject.hpp') diff --git a/include/openvic-dataloader/csv/LineObject.hpp b/include/openvic-dataloader/csv/LineObject.hpp index 8a9c2ec..c6d8e2a 100644 --- a/include/openvic-dataloader/csv/LineObject.hpp +++ b/include/openvic-dataloader/csv/LineObject.hpp @@ -1,13 +1,17 @@ #pragma once +#include +#include #include #include #include #include #include +#include +#include #include #include -#include +#include #include #include @@ -24,13 +28,13 @@ namespace ovdl::csv { /// ;a;b;c -> 0,4+ == "" /// /// If this is incorrect, please report an issue. - class LineObject final : public std::vector> { + class LineObject final : public std::vector> { public: // Stored position of value using position_type = std::uint32_t; // Value using inner_value_type = std::string; - using container_type = std::vector>; + using container_type = std::vector>; OVDL_VECTOR_CONSTEXPR LineObject() = default; OVDL_VECTOR_CONSTEXPR LineObject(LineObject&) = default; @@ -54,7 +58,7 @@ namespace ovdl::csv { /// Special Functionality /// Retrieves value, produces "" for empty values constexpr std::string_view get_value_for(std::size_t position) const { - if (position <= _prefix_end || position >= _suffix_end) return ""; + if (position < _prefix_end || position >= _suffix_end) return ""; for (const auto& [pos, val] : *this) { if (pos == position) return val; } @@ -62,7 +66,7 @@ namespace ovdl::csv { } /// Tries to retrieve reference, produces nullopt for empty values constexpr std::optional> try_get_string_at(std::size_t position) const { - if (position <= _prefix_end || position > _suffix_end) return std::nullopt; + if (position < _prefix_end || position >= _suffix_end) return std::nullopt; for (const auto& [pos, val] : *this) { if (pos == position) return std::cref(val); } @@ -83,4 +87,28 @@ namespace ovdl::csv { // Should be position after last value or position after last seperator position_type _suffix_end = 0; }; + + inline std::ostream& operator<<(std::ostream& stream, const LineObject& line) { + static const char SEP = ';'; + LineObject::position_type sep_index = 0; + for (const auto& [pos, val] : line) { + while (sep_index < pos) { + stream << SEP; + sep_index++; + } + if (std::any_of(val.begin(), val.end(), [](char c) { return c == SEP || std::isspace(c); })) { + stream << '"' << val << '"'; + } else { + stream << val; + } + } + return stream; + } + + inline std::ostream& operator<<(std::ostream& stream, const std::vector& lines) { + for (const LineObject& line : lines) { + stream << line << '\n'; + } + return stream; + } } \ No newline at end of file -- cgit v1.2.3-56-ga3b1