diff options
Diffstat (limited to 'include/openvic-dataloader/csv/LineObject.hpp')
-rw-r--r-- | include/openvic-dataloader/csv/LineObject.hpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/include/openvic-dataloader/csv/LineObject.hpp b/include/openvic-dataloader/csv/LineObject.hpp index c839be2..5de55ba 100644 --- a/include/openvic-dataloader/csv/LineObject.hpp +++ b/include/openvic-dataloader/csv/LineObject.hpp @@ -80,6 +80,15 @@ namespace ovdl::csv { constexpr std::size_t value_count() const { return _suffix_end; } + struct SepTransformer { + const LineObject& line_object; + std::string_view separator; + }; + + constexpr SepTransformer use_sep(std::string_view seperator) const { + return { *this, seperator }; + } + private: // Should be position of first valid value on line position_type _prefix_end = 0; @@ -87,6 +96,15 @@ namespace ovdl::csv { position_type _suffix_end = 0; }; + struct VectorSepTransformer { + const std::vector<LineObject>& vector; + std::string_view separator; + }; + + constexpr VectorSepTransformer use_sep(const std::vector<LineObject>& vector, std::string_view separator) { + return { vector, separator }; + } + inline std::ostream& operator<<(std::ostream& stream, const LineObject& line) { static constexpr char SEP = ';'; LineObject::position_type sep_index = 0; @@ -110,4 +128,36 @@ namespace ovdl::csv { } return stream; } + + inline std::ostream& operator<<(std::ostream& stream, const LineObject::SepTransformer& transformer) { + auto quote_check = [&transformer, is_one = transformer.separator.size() == 1](const std::string_view str) { + if (is_one) { + char SEP = transformer.separator[0]; + return std::any_of(str.begin(), str.end(), [SEP](char c) { return c == SEP || std::isspace(c); }); + } + return std::any_of(str.begin(), str.end(), [](char c) { return std::isspace(c); }) || + str.find(transformer.separator) != std::string::npos; + }; + + LineObject::position_type sep_index = 0; + for (const auto& [pos, val] : transformer.line_object) { + while (sep_index < pos) { + stream << transformer.separator; + sep_index++; + } + if (quote_check(val)) { + stream << '"' << val << '"'; + } else { + stream << val; + } + } + return stream; + } + + inline std::ostream& operator<<(std::ostream& stream, const VectorSepTransformer& transformer) { + for (const LineObject& line : transformer.vector) { + stream << line.use_sep(transformer.separator) << '\n'; + } + return stream; + } }
\ No newline at end of file |