Refactored CSV export.
This commit is contained in:
parent
885148ea97
commit
008a5fe0f2
59
src/csv.cpp
59
src/csv.cpp
|
@ -20,44 +20,47 @@
|
||||||
using std::cerr;
|
using std::cerr;
|
||||||
using std::endl;
|
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\","
|
try
|
||||||
<< "\"Title\",\"Description\",\"Full text\"\r\n";
|
|
||||||
for (const Database::entry &entry : entries)
|
|
||||||
{
|
{
|
||||||
string strtags;
|
_out << "\"URI\",\"Archived URI\",\"Date & time\",\"Tags\","
|
||||||
for (const string &tag : entry.tags)
|
<< "\"Title\",\"Description\",\"Full text\"\r\n";
|
||||||
|
for (const Database::entry &entry : _entries)
|
||||||
{
|
{
|
||||||
strtags += tag;
|
string strtags;
|
||||||
if (tag != *(entry.tags.rbegin()))
|
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) << "\",\""
|
catch (std::exception &e)
|
||||||
<< timepoint_to_string(entry.datetime) << "\",\""
|
{
|
||||||
<< quote_csv(strtags) << "\",\""
|
cerr << "Error in " << __func__ << ": " << e.what() << endl;
|
||||||
<< 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const string quote_csv(string field)
|
const string CSV::quote(string field) const
|
||||||
{
|
|
||||||
size_t pos = 0;
|
|
||||||
while ((pos = field.find('"', pos)) != std::string::npos)
|
|
||||||
{
|
{
|
||||||
field.replace(pos, 1, "\"\"");
|
size_t pos = 0;
|
||||||
|
while ((pos = field.find('"', pos)) != std::string::npos)
|
||||||
|
{
|
||||||
|
field.replace(pos, 1, "\"\"");
|
||||||
|
}
|
||||||
|
return field;
|
||||||
}
|
}
|
||||||
return field;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
{}
|
||||||
|
}
|
|
@ -26,9 +26,30 @@ using std::vector;
|
||||||
using std::ostream;
|
using std::ostream;
|
||||||
using std::cout;
|
using std::cout;
|
||||||
|
|
||||||
void export_csv(const vector<Database::entry> &entries, ostream &out = cout);
|
namespace Export
|
||||||
//! replaces " with "".
|
{
|
||||||
const string quote_csv(string field);
|
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);
|
void export_adoc(const vector<Database::entry> &entries, ostream &out = cout);
|
||||||
//! Replaces characters in tags that asciidoctor doesn't like.
|
//! Replaces characters in tags that asciidoctor doesn't like.
|
||||||
|
|
|
@ -98,12 +98,12 @@ int main(const int argc, const char *argv[])
|
||||||
{
|
{
|
||||||
if (file.is_open())
|
if (file.is_open())
|
||||||
{
|
{
|
||||||
export_csv(entries, file);
|
Export::CSV(entries, file).print();
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
export_csv(entries);
|
Export::CSV(entries).print();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,7 @@ SCENARIO ("The CSV export works correctly")
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::ostringstream output;
|
std::ostringstream output;
|
||||||
export_csv({ entry }, output);
|
Export::CSV({ entry }, output).print();
|
||||||
const string csv = output.str();
|
const string csv = output.str();
|
||||||
|
|
||||||
const regex re
|
const regex re
|
||||||
|
|
Loading…
Reference in New Issue