From 2a64a203c7015403546ba770967db93c287c652a Mon Sep 17 00:00:00 2001 From: Pranav Srinivas Kumar Date: Mon, 27 Jul 2020 10:04:10 -0500 Subject: [PATCH 1/5] Added basic csv2::Writer class --- include/csv2/parameters.hpp | 49 +++++++++++++++++++++++++++++++++++++ include/csv2/reader.hpp | 45 +--------------------------------- include/csv2/writer.hpp | 40 ++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 44 deletions(-) create mode 100644 include/csv2/parameters.hpp create mode 100644 include/csv2/writer.hpp diff --git a/include/csv2/parameters.hpp b/include/csv2/parameters.hpp new file mode 100644 index 0000000..d9f72b3 --- /dev/null +++ b/include/csv2/parameters.hpp @@ -0,0 +1,49 @@ +#pragma once +#include + +namespace csv2 { + +namespace trim_policy { +struct no_trimming { +public: + static std::pair trim(const char *buffer, size_t start, size_t end) { + (void)(buffer); // to silence unused parameter warning + return {start, end}; + } +}; + +template struct trim_characters { +private: + constexpr static bool is_trim_char(char) { return false; } + + template constexpr static bool is_trim_char(char c, char head, Tail... tail) { + return c == head || is_trim_char(c, tail...); + } + +public: + static std::pair trim(const char *buffer, size_t start, size_t end) { + size_t new_start = start, new_end = end; + while (new_start != new_end && is_trim_char(buffer[new_start], character_list...)) + ++new_start; + while (new_start != new_end && is_trim_char(buffer[new_end - 1], character_list...)) + --new_end; + return {new_start, new_end}; + } +}; + +using trim_whitespace = trim_characters<' ', '\t'>; +} // namespace trim_policy + +template struct delimiter { + constexpr static char value = character; +}; + +template struct quote_character { + constexpr static char value = character; +}; + +template struct first_row_is_header { + constexpr static bool value = flag; +}; + +} \ No newline at end of file diff --git a/include/csv2/reader.hpp b/include/csv2/reader.hpp index e470bcd..ba823fb 100644 --- a/include/csv2/reader.hpp +++ b/include/csv2/reader.hpp @@ -1,55 +1,12 @@ #pragma once #include #include +#include #include #include -#include namespace csv2 { -namespace trim_policy { -struct no_trimming { -public: - static std::pair trim(const char *buffer, size_t start, size_t end) { - (void)(buffer); // to silence unused parameter warning - return {start, end}; - } -}; - -template struct trim_characters { -private: - constexpr static bool is_trim_char(char) { return false; } - - template constexpr static bool is_trim_char(char c, char head, Tail... tail) { - return c == head || is_trim_char(c, tail...); - } - -public: - static std::pair trim(const char *buffer, size_t start, size_t end) { - size_t new_start = start, new_end = end; - while (new_start != new_end && is_trim_char(buffer[new_start], character_list...)) - ++new_start; - while (new_start != new_end && is_trim_char(buffer[new_end - 1], character_list...)) - --new_end; - return {new_start, new_end}; - } -}; - -using trim_whitespace = trim_characters<' ', '\t'>; -} // namespace trim_policy - -template struct delimiter { - constexpr static char value = character; -}; - -template struct quote_character { - constexpr static char value = character; -}; - -template struct first_row_is_header { - constexpr static bool value = flag; -}; - template , class quote_character = quote_character<'"'>, class first_row_is_header = first_row_is_header, class trim_policy = trim_policy::trim_whitespace> diff --git a/include/csv2/writer.hpp b/include/csv2/writer.hpp new file mode 100644 index 0000000..f53047b --- /dev/null +++ b/include/csv2/writer.hpp @@ -0,0 +1,40 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +namespace csv2 { + +template > +class Writer { + std::ofstream& stream_; // output stream for the writer +public: + template + Writer(Stream&& stream) : stream_(std::forward(stream)) {} + + ~Writer() { + stream_.close(); + } + + template + void write_row(Container&& row) { + const auto& strings = std::forward(row); + const auto delimiter_string = std::string(1, delimiter::value); + std::copy(strings.begin(), strings.end() - 1, + std::ostream_iterator(stream_, delimiter_string.c_str())); + stream_ << strings.back() << "\n"; + } + + template + void write_rows(Container&& rows) { + const auto& container_of_rows = std::forward(rows); + for (const auto& row : container_of_rows) { + write_row(row); + } + } +}; + +} \ No newline at end of file From bc052e13c0048793ef0f3056c0b88fa0e7ba2f52 Mon Sep 17 00:00:00 2001 From: Pranav Srinivas Kumar Date: Mon, 27 Jul 2020 10:08:39 -0500 Subject: [PATCH 2/5] Updated README showing CSV Writer --- README.md | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f25d77c..da15eff 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@