aboutsummaryrefslogtreecommitdiff
path: root/libs/coding/csv_reader.hpp
blob: f8ed4c2f1186e27f6096db61352aa270a5a86d12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#pragma once

#include "coding/reader.hpp"

#include <fstream>
#include <functional>
#include <optional>
#include <string>
#include <vector>

namespace coding
{
class CSVReader
{
public:
  using Row = std::vector<std::string>;
  using Rows = std::vector<Row>;

  explicit CSVReader(std::string const & filename, bool hasHeader = false, char delimiter = ',');
  explicit CSVReader(std::istream & stream, bool hasHeader = false, char delimiter = ',');
  explicit CSVReader(Reader const & reader, bool hasHeader = false, char delimiter = ',');

  bool HasHeader() const;
  char GetDelimiter() const;

  Row const & GetHeader() const;
  std::optional<Row> ReadRow();
  Rows ReadAll();

  template <typename Fn>
  void ForEachRow(Fn && fn)
  {
    while (auto const optRow = ReadRow())
      fn(*optRow);
  }

  // The total number of lines read including the header. Count starts at 0.
  size_t GetCurrentLineNumber() const;

private:
  class ReaderInterface
  {
  public:
    virtual ~ReaderInterface() = default;

    virtual std::optional<std::string> ReadLine() = 0;
  };

  class IstreamWrapper : public ReaderInterface
  {
  public:
    explicit IstreamWrapper(std::istream & stream);

    // ReaderInterface overrides:
    std::optional<std::string> ReadLine() override;

  private:
    std::istream & m_stream;
  };

  class ReaderWrapper : public ReaderInterface
  {
  public:
    explicit ReaderWrapper(Reader const & reader);

    // ReaderInterface overrides:
    std::optional<std::string> ReadLine() override;

  private:
    size_t m_pos = 0;
    Reader const & m_reader;
  };

  class DefaultReader : public ReaderInterface
  {
  public:
    explicit DefaultReader(std::string const & filename);

    // ReaderInterface overrides:
    std::optional<std::string> ReadLine() override;

  private:
    std::ifstream m_stream;
  };

  explicit CSVReader(std::unique_ptr<ReaderInterface> reader, bool hasHeader, char delimiter);

  std::unique_ptr<ReaderInterface> m_reader;
  size_t m_currentLine = 0;
  bool m_hasHeader = false;
  char m_delimiter = ',';
  Row m_header;
};

class CSVRunner
{
public:
  explicit CSVRunner(CSVReader && reader);

  class Iterator
  {
  public:
    using iterator_category = std::input_iterator_tag;
    using value_type = CSVReader::Row;

    explicit Iterator(CSVReader & reader, bool isEnd = false);
    Iterator(Iterator const & other);
    Iterator & operator++();
    Iterator operator++(int);
    // Checks whether both this and other are equal. Two CSVReader iterators are equal if both of
    // them are end-of-file iterators or not and both of them refer to the same CSVReader.
    bool operator==(Iterator const & other) const;
    bool operator!=(Iterator const & other) const;
    CSVReader::Row & operator*();

  private:
    CSVReader & m_reader;
    std::optional<CSVReader::Row> m_current;
  };

  // Warning: It reads first line.
  Iterator begin();
  Iterator end();

private:
  CSVReader m_reader;
};
}  // namespace coding