remwharead/src/lib/export/csv.cpp

67 lines
2.1 KiB
C++
Raw Normal View History

2019-05-16 03:56:17 +02:00
/* 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 "time.hpp"
2019-06-06 18:56:49 +02:00
#include "csv.hpp"
2019-05-16 03:56:17 +02:00
2019-07-27 09:59:43 +02:00
namespace remwharead
2019-05-16 03:56:17 +02:00
{
2019-07-27 09:59:43 +02:00
using std::cerr;
using std::endl;
void Export::CSV::print() const
2019-05-16 03:56:17 +02:00
{
2019-06-06 16:06:25 +02:00
try
2019-05-16 03:56:17 +02:00
{
2019-06-06 16:06:25 +02:00
_out << "\"URI\",\"Archived URI\",\"Date & time\",\"Tags\","
<< "\"Title\",\"Description\",\"Full text\"\r\n";
for (const Database::entry &entry : _entries)
2019-05-16 03:56:17 +02:00
{
2019-06-06 16:06:25 +02:00
string strtags;
for (const string &tag : entry.tags)
2019-05-16 03:56:17 +02:00
{
2019-06-06 16:06:25 +02:00
strtags += tag;
if (tag != *(entry.tags.rbegin()))
{
strtags += ",";
}
2019-05-16 03:56:17 +02:00
}
2019-06-06 16:06:25 +02:00
_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";
2019-05-16 03:56:17 +02:00
}
2019-06-06 16:06:25 +02:00
}
catch (std::exception &e)
{
cerr << "Error in " << __func__ << ": " << e.what() << endl;
2019-05-16 03:56:17 +02:00
}
}
2019-07-27 09:59:43 +02:00
const string Export::CSV::quote(string field) const
{
2019-06-06 16:06:25 +02:00
size_t pos = 0;
while ((pos = field.find('"', pos)) != std::string::npos)
{
field.replace(pos, 1, "\"\"");
}
return field;
}
2019-05-16 03:56:17 +02:00
}