From 9d862c6a6268d379ffbc18c1437414665c9229ff Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 11 Mar 2022 23:57:27 +0100 Subject: [PATCH] kraken: add 'csv' library A very basic, naive CSV parser. --- src/CMakeLists.txt | 6 ++++++ src/csv/CMakeLists.txt | 5 +++++ src/csv/read-csv.cc | 44 ++++++++++++++++++++++++++++++++++++++++++ src/csv/read-csv.hh | 29 ++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 src/csv/CMakeLists.txt create mode 100644 src/csv/read-csv.cc create mode 100644 src/csv/read-csv.hh diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 368dfd6..ab55c25 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,10 @@ add_executable(kraken kraken.cc) target_link_libraries(kraken PRIVATE common_options) +add_subdirectory(csv) + +target_link_libraries(kraken PRIVATE + csv +) + install(TARGETS kraken) diff --git a/src/csv/CMakeLists.txt b/src/csv/CMakeLists.txt new file mode 100644 index 0000000..c682f51 --- /dev/null +++ b/src/csv/CMakeLists.txt @@ -0,0 +1,5 @@ +add_library(csv STATIC + read-csv.cc + read-csv.hh +) +target_link_libraries(csv PRIVATE common_options) diff --git a/src/csv/read-csv.cc b/src/csv/read-csv.cc new file mode 100644 index 0000000..5274d12 --- /dev/null +++ b/src/csv/read-csv.cc @@ -0,0 +1,44 @@ +#include "read-csv.hh" + +#include + +namespace kraken::csv { + +namespace { + +// for convenience, use a stringstream which does not accept string_view inputs +csv_line_type parse_line(std::string const& line) { + auto parsed = csv_line_type{}; + + auto input = std::istringstream(line); + for (std::string atom; std::getline(input, atom, ',');) { + parsed.emplace_back(std::move(atom)); + } + + return parsed; +} + +} // namespace + +csv_type read_csv(std::istream& input, CsvHeader header) { + auto parsed = std::vector{}; + + bool first = true; + for (std::string line; std::getline(input, line);) { + if (first && header == CsvHeader::SKIP) { + first = false; + continue; + } + + parsed.emplace_back(parse_line(std::move(line))); + } + + return parsed; +} + +csv_type read_csv(std::string const& input, CsvHeader header) { + auto input_stream = std::istringstream(input); + return read_csv(input_stream, header); +} + +} // namespace kraken::csv diff --git a/src/csv/read-csv.hh b/src/csv/read-csv.hh new file mode 100644 index 0000000..a1ceab8 --- /dev/null +++ b/src/csv/read-csv.hh @@ -0,0 +1,29 @@ +#pragma once + +#include +#include +#include + +namespace kraken::csv { + +/// Should the first line of the CSV file be kept or ignored. +enum class CsvHeader { + /// Ignored. + SKIP, + /// Kepts. + KEEP, +}; + +/// Represent a raw CSV line as a vector of strings. +using csv_line_type = std::vector; + +/// Represent a raw CSV file as a vector of raw CSV lines +using csv_type = std::vector; + +/// Parse a CSV file from an input-stream, return a vector of parsed lines. +csv_type read_csv(std::istream& input, CsvHeader header = CsvHeader::SKIP); + +/// Convenience function which reads CSV from a string. +csv_type read_csv(std::string const& input, CsvHeader header = CsvHeader::SKIP); + +} // namespace kraken::csv