diff options
author | Spartan322 <Megacake1234@gmail.com> | 2023-09-03 23:54:18 +0200 |
---|---|---|
committer | Spartan322 <Megacake1234@gmail.com> | 2023-09-04 18:22:33 +0200 |
commit | a705b0223664f915d8bb00d4f89e05838da3e4bc (patch) | |
tree | 9833fa669651e04dea81b9b2b5875a8d21ea938e /include/openvic-dataloader/csv/LineObject.hpp | |
parent | 027828532837673a3631c8a9a8128f9b89a9db43 (diff) |
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
Diffstat (limited to 'include/openvic-dataloader/csv/LineObject.hpp')
-rw-r--r-- | include/openvic-dataloader/csv/LineObject.hpp | 38 |
1 files changed, 33 insertions, 5 deletions
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 <algorithm> +#include <cctype> #include <cstddef> #include <cstdint> #include <functional> #include <initializer_list> #include <optional> +#include <ostream> +#include <set> #include <string> #include <string_view> -#include <tuple> +#include <utility> #include <vector> #include <openvic-dataloader/detail/VectorConstexpr.hpp> @@ -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<std::tuple<std::uint32_t, std::string>> { + class LineObject final : public std::vector<std::pair<std::uint32_t, std::string>> { public: // Stored position of value using position_type = std::uint32_t; // Value using inner_value_type = std::string; - using container_type = std::vector<std::tuple<position_type, inner_value_type>>; + using container_type = std::vector<std::pair<position_type, inner_value_type>>; 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<const std::reference_wrapper<const std::string>> 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<LineObject>& lines) { + for (const LineObject& line : lines) { + stream << line << '\n'; + } + return stream; + } }
\ No newline at end of file |