From 657e72b4c5b24f8b4f86d25031a526eb623745c7 Mon Sep 17 00:00:00 2001 From: frank Date: Tue, 12 Jun 2018 10:23:43 +0200 Subject: [PATCH] added csv parsing --- data/csv.h | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ misc/Debug.h | 1 + 2 files changed, 89 insertions(+) create mode 100644 data/csv.h diff --git a/data/csv.h b/data/csv.h new file mode 100644 index 0000000..2366b9a --- /dev/null +++ b/data/csv.h @@ -0,0 +1,88 @@ +#ifndef CSV_H +#define CSV_H + +#include +#include +#include +#include "../misc/Debug.h" + +class CSV { + + static constexpr const char* NAME = "CSV"; + +public: + + /** one column within the CSV */ + struct Col { + std::string data; + Col(const std::string& data) : data(data) {;} + const std::string& asString() const {return data;} + int asInt() const {return std::stoi(data);} + long asLong() const {return std::stol(data);} + double asDouble() const {return std::stod(data);} + }; + + /** one row within the CSV */ + struct Row : std::vector { + + }; + + /** one csv document */ + struct Doc : std::vector { + + }; + + + class Reader { + + private: + + const char sep; + + public: + + /** ctor */ + Reader(const char sep) : sep(sep) { + + } + + /** read the given csv file */ + Doc read(const std::string& file) { + + Log::add(NAME, "reading file: " + file); + + Doc doc; + + std::ifstream inp(file.c_str()); + int rowCnt = 0; + std::string line; + + // read all lines within the CSV + while(std::getline(inp, line)) { + + ++rowCnt; + + // split + Row row; + std::stringstream ss(line); + std::string tmp; + while(getline(ss, tmp, sep)){ + row.push_back(Col(tmp)); + } + + // apend row + doc.push_back(row); + + } + + Log::add(NAME, "got " + std::to_string(rowCnt) + " rows"); + + return doc; + + } + + }; + +}; + +#endif // CSV_H diff --git a/misc/Debug.h b/misc/Debug.h index 4f1f1a3..5668a0f 100644 --- a/misc/Debug.h +++ b/misc/Debug.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "Time.h" #include "log/LoggerCOUT.h"