summaryrefslogtreecommitdiff
path: root/src/csv.hh
diff options
context:
space:
mode:
Diffstat (limited to 'src/csv.hh')
-rw-r--r--src/csv.hh44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/csv.hh b/src/csv.hh
new file mode 100644
index 0000000..8c47ceb
--- /dev/null
+++ b/src/csv.hh
@@ -0,0 +1,44 @@
+#ifndef CSV_HH
+#define CSV_HH
+
+#include "io.hh" // IWYU pragma: export
+#include "line.hh"
+
+#include <expected>
+#include <memory>
+#include <span>
+#include <string_view>
+
+namespace csv {
+
+// Note that this reader is very simple, no quotes or escapes.
+// Empty lines are ignored.
+class Reader {
+ public:
+ virtual ~Reader() = default;
+
+ // Returned span is only valid until next call to read.
+ // Returns empty span at end-of-file and only then.
+ [[nodiscard]]
+ virtual std::expected<std::span<std::string_view>, io::ReadError> read() = 0;
+
+ // Starts at zero. Returns next line.
+ // So, before first read it is zero, after first read it is one.
+ [[nodiscard]] virtual uint64_t number() const = 0;
+
+ protected:
+ Reader() = default;
+
+ Reader(Reader const&) = delete;
+ Reader& operator=(Reader const&) = delete;
+};
+
+[[nodiscard]] std::unique_ptr<Reader> open(std::unique_ptr<line::Reader> reader,
+ char separator = ',');
+
+[[nodiscard]] std::unique_ptr<Reader> open(std::unique_ptr<io::Reader> reader,
+ char separator = ',');
+
+} // namespace csv
+
+#endif // CSV_HH