Refactored CSV export.
This commit is contained in:
parent
885148ea97
commit
008a5fe0f2
27
src/csv.cpp
27
src/csv.cpp
|
@ -20,13 +20,15 @@
|
|||
using std::cerr;
|
||||
using std::endl;
|
||||
|
||||
void export_csv(const vector<Database::entry> &entries, ostream &out)
|
||||
namespace Export
|
||||
{
|
||||
void CSV::print() const
|
||||
{
|
||||
try
|
||||
{
|
||||
out << "\"URI\",\"Archived URI\",\"Date & time\",\"Tags\","
|
||||
_out << "\"URI\",\"Archived URI\",\"Date & time\",\"Tags\","
|
||||
<< "\"Title\",\"Description\",\"Full text\"\r\n";
|
||||
for (const Database::entry &entry : entries)
|
||||
for (const Database::entry &entry : _entries)
|
||||
{
|
||||
string strtags;
|
||||
for (const string &tag : entry.tags)
|
||||
|
@ -37,27 +39,28 @@ void export_csv(const vector<Database::entry> &entries, ostream &out)
|
|||
strtags += ",";
|
||||
}
|
||||
}
|
||||
out << '"' << quote_csv(entry.uri) << "\",\""
|
||||
<< quote_csv(entry.archive_uri) << "\",\""
|
||||
_out << '"' << quote(entry.uri) << "\",\""
|
||||
<< quote(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";
|
||||
<< quote(strtags) << "\",\""
|
||||
<< quote(entry.title) << "\",\""
|
||||
<< quote(entry.description) << "\",\""
|
||||
<< quote(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, "\"\"");
|
||||
}
|
||||
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::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.
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue