MySources/csvfile.hpp

143 lines
4.0 KiB
C++
Raw Normal View History

#ifndef CSVFILE_HPP
#define CSVFILE_HPP
2021-05-24 13:43:32 +02:00
#include <boost/lexical_cast.hpp>
#include <boost/tokenizer.hpp>
2021-04-08 20:03:23 +02:00
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
2021-05-24 13:43:32 +02:00
#include <vector>
2021-03-01 14:44:35 +01:00
class csvFile;
2021-03-01 14:44:35 +01:00
inline static csvFile &endrow(csvFile &file);
inline static csvFile &flush(csvFile &file);
2021-03-01 14:44:35 +01:00
class csvFile {
std::ofstream fs_;
bool is_first_;
const std::string separator_;
const std::string escape_seq_;
const std::string special_chars_;
public:
2021-03-01 14:44:35 +01:00
csvFile(const std::string filename, const bool &overwrite,
const std::string separator = ",")
: fs_(), is_first_(true), separator_(separator), escape_seq_("\""),
special_chars_("\"") {
fs_.exceptions(std::ios::failbit | std::ios::badbit);
2021-03-01 14:44:35 +01:00
if (filename.empty()) {
fs_.copyfmt(std::cout);
fs_.clear(std::cout.rdstate());
fs_.basic_ios<char>::rdbuf(std::cout.rdbuf());
} else {
if (!std::filesystem::exists(
std::filesystem::path("../OptimizationResults")
.append("statistics.csv"))) {
std::ofstream outfile(std::filesystem::path("../OptimizationResults")
2021-03-01 14:44:35 +01:00
.append("statistics.csv")
.string());
outfile.close();
2021-03-01 14:44:35 +01:00
}
overwrite ? fs_.open(filename, std::ios::trunc)
: fs_.open(filename, std::ios::app);
}
}
2021-03-01 14:44:35 +01:00
~csvFile() {
flush();
fs_.close();
}
void flush() { fs_.flush(); }
void endrow() {
fs_ << std::endl;
is_first_ = true;
}
2021-03-01 14:44:35 +01:00
csvFile &operator<<(csvFile &(*val)(csvFile &)) { return val(*this); }
2021-03-01 14:44:35 +01:00
csvFile &operator<<(const char *val) { return write(escape(val)); }
2021-03-01 14:44:35 +01:00
csvFile &operator<<(const std::string &val) { return write(escape(val)); }
2021-05-24 13:43:32 +02:00
template<typename T>
csvFile &operator<<(const T &val)
{
return write(val);
}
template<typename T>
static std::vector<std::vector<T>> parse(const std::filesystem::path &csvFilepath)
{
std::vector<std::vector<T>> resultCSV;
if (!std::filesystem::exists(csvFilepath)) {
std::cerr << "The file does not exist:" << csvFilepath.string() << std::endl;
return resultCSV;
}
std::ifstream inputfile(csvFilepath.string().c_str());
if (!inputfile.is_open()) {
std::cerr << "Can't open file:" << csvFilepath.string() << std::endl;
return resultCSV;
}
std::vector<T> row;
std::string line;
using Tokenizer = boost::tokenizer<boost::escaped_list_separator<char>>;
while (std::getline(inputfile, line)) {
Tokenizer tokenizer(line);
row.resize(std::distance(tokenizer.begin(), tokenizer.end()));
std::transform(tokenizer.begin(), tokenizer.end(), row.begin(), [](const std::string &el) {
return boost::lexical_cast<T>(el);
});
// std::cout << std::endl;
// row.assign(tokenizer.begin(), tokenizer.end());
// for (const auto &el : row) {
// std::cout << el << " ";
// }
// std::cout << std::endl;
resultCSV.push_back(row);
}
return resultCSV;
}
private:
2021-03-01 14:44:35 +01:00
template <typename T> csvFile &write(const T &val) {
if (!is_first_) {
fs_ << separator_;
} else {
is_first_ = false;
}
fs_ << val;
return *this;
}
std::string escape(const std::string &val) {
std::ostringstream result;
result << '"';
std::string::size_type to, from = 0u, len = val.length();
while (from < len && std::string::npos !=
(to = val.find_first_of(special_chars_, from))) {
result << val.substr(from, to - from) << escape_seq_ << val[to];
from = to + 1;
}
result << val.substr(from) << '"';
return result.str();
}
};
2021-03-01 14:44:35 +01:00
inline static csvFile &endrow(csvFile &file) {
file.endrow();
return file;
}
2021-03-01 14:44:35 +01:00
inline static csvFile &flush(csvFile &file) {
file.flush();
return file;
}
#endif // CSVFILE_HPP