From 008a5fe0f25b3ffed8c42b10a91c5da7ade2bc63 Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 6 Jun 2019 16:06:25 +0200 Subject: [PATCH] Refactored CSV export. --- src/csv.cpp | 59 ++++++++++++++++++++++++---------------------- src/export.cpp | 25 ++++++++++++++++++++ src/export.hpp | 27 ++++++++++++++++++--- src/main.cpp | 4 ++-- tests/test_csv.cpp | 2 +- 5 files changed, 83 insertions(+), 34 deletions(-) create mode 100644 src/export.cpp diff --git a/src/csv.cpp b/src/csv.cpp index 4cb7a79..5ebd940 100644 --- a/src/csv.cpp +++ b/src/csv.cpp @@ -20,44 +20,47 @@ using std::cerr; using std::endl; -void export_csv(const vector &entries, ostream &out) +namespace Export { - try + void CSV::print() const { - out << "\"URI\",\"Archived URI\",\"Date & time\",\"Tags\"," - << "\"Title\",\"Description\",\"Full text\"\r\n"; - for (const Database::entry &entry : entries) + try { - string strtags; - for (const string &tag : entry.tags) + _out << "\"URI\",\"Archived URI\",\"Date & time\",\"Tags\"," + << "\"Title\",\"Description\",\"Full text\"\r\n"; + for (const Database::entry &entry : _entries) { - strtags += tag; - if (tag != *(entry.tags.rbegin())) + string strtags; + for (const string &tag : entry.tags) { - strtags += ","; + strtags += tag; + if (tag != *(entry.tags.rbegin())) + { + strtags += ","; + } } + _out << '"' << quote(entry.uri) << "\",\"" + << quote(entry.archive_uri) << "\",\"" + << timepoint_to_string(entry.datetime) << "\",\"" + << quote(strtags) << "\",\"" + << quote(entry.title) << "\",\"" + << quote(entry.description) << "\",\"" + << quote(entry.fulltext_oneline()) << '"'<< "\r\n"; } - out << '"' << quote_csv(entry.uri) << "\",\"" - << quote_csv(entry.archive_uri) << "\",\"" - << timepoint_to_string(entry.datetime) << "\",\"" - << quote_csv(strtags) << "\",\"" - << quote_csv(entry.title) << "\",\"" - << quote_csv(entry.description) << "\",\"" - << quote_csv(entry.fulltext_oneline()) << '"'<< "\r\n"; + } + catch (std::exception &e) + { + cerr << "Error in " << __func__ << ": " << e.what() << endl; } } - catch (std::exception &e) - { - cerr << "Error in " << __func__ << ": " << e.what() << endl; - } -} -const string quote_csv(string field) -{ - size_t pos = 0; - while ((pos = field.find('"', pos)) != std::string::npos) + const string CSV::quote(string field) const { - field.replace(pos, 1, "\"\""); + size_t pos = 0; + while ((pos = field.find('"', pos)) != std::string::npos) + { + field.replace(pos, 1, "\"\""); + } + return field; } - return field; } diff --git a/src/export.cpp b/src/export.cpp new file mode 100644 index 0000000..406daa1 --- /dev/null +++ b/src/export.cpp @@ -0,0 +1,25 @@ +/* This file is part of remwharead. + * Copyright © 2019 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "export.hpp" + +namespace Export +{ + ExportBase::ExportBase(const vector &entries, ostream &out) + : _entries(entries) + , _out(out) + {} +} diff --git a/src/export.hpp b/src/export.hpp index 2d91f6d..d50a947 100644 --- a/src/export.hpp +++ b/src/export.hpp @@ -26,9 +26,30 @@ using std::vector; using std::ostream; using std::cout; -void export_csv(const vector &entries, ostream &out = cout); -//! replaces " with "". -const string quote_csv(string field); +namespace Export +{ + class ExportBase + { + public: + explicit ExportBase(const vector &entries, + ostream &out = cout); + + protected: + const vector &_entries; + ostream &_out; + }; + + class CSV : protected ExportBase + { + public: + using ExportBase::ExportBase; + + void print() const; + //! replaces " with "". + const string quote(string field) const; + }; +} + void export_adoc(const vector &entries, ostream &out = cout); //! Replaces characters in tags that asciidoctor doesn't like. diff --git a/src/main.cpp b/src/main.cpp index 02cbf77..23fee28 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -98,12 +98,12 @@ int main(const int argc, const char *argv[]) { if (file.is_open()) { - export_csv(entries, file); + Export::CSV(entries, file).print(); file.close(); } else { - export_csv(entries); + Export::CSV(entries).print(); } break; } diff --git a/tests/test_csv.cpp b/tests/test_csv.cpp index 2ac99da..27db289 100644 --- a/tests/test_csv.cpp +++ b/tests/test_csv.cpp @@ -47,7 +47,7 @@ SCENARIO ("The CSV export works correctly") try { std::ostringstream output; - export_csv({ entry }, output); + Export::CSV({ entry }, output).print(); const string csv = output.str(); const regex re