Refactored CSV export.

This commit is contained in:
tastytea 2019-06-06 16:06:25 +02:00
parent 885148ea97
commit 008a5fe0f2
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
5 changed files with 83 additions and 34 deletions

View File

@ -20,44 +20,47 @@
using std::cerr;
using std::endl;
void export_csv(const vector<Database::entry> &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;
}

25
src/export.cpp Normal file
View File

@ -0,0 +1,25 @@
/* This file is part of remwharead.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "export.hpp"
namespace Export
{
ExportBase::ExportBase(const vector<Database::entry> &entries, ostream &out)
: _entries(entries)
, _out(out)
{}
}

View File

@ -26,9 +26,30 @@ using std::vector;
using std::ostream;
using std::cout;
void export_csv(const vector<Database::entry> &entries, ostream &out = cout);
//! replaces " with "".
const string quote_csv(string field);
namespace Export
{
class ExportBase
{
public:
explicit ExportBase(const vector<Database::entry> &entries,
ostream &out = cout);
protected:
const vector<Database::entry> &_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<Database::entry> &entries, ostream &out = cout);
//! Replaces characters in tags that asciidoctor doesn't like.

View File

@ -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;
}

View File

@ -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