aboutsummaryrefslogtreecommitdiff
path: root/libs/coding/csv_reader.cpp
blob: b3a1c2e18a696ee5408b74cd5e78cbb36fcbf1fe (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "coding/csv_reader.hpp"

#include "base/logging.hpp"
#include "base/string_utils.hpp"

namespace coding
{
CSVReader::CSVReader(std::unique_ptr<ReaderInterface> reader, bool hasHeader, char delimiter)
  : m_reader(std::move(reader))
  , m_hasHeader(hasHeader)
  , m_delimiter(delimiter)
{
  if (!HasHeader())
    return;

  auto const row = ReadRow();
  if (row)
    m_header = *row;
}

CSVReader::CSVReader(std::string const & filename, bool hasHeader, char delimiter)
  : CSVReader(std::make_unique<DefaultReader>(filename), hasHeader, delimiter)
{}

CSVReader::CSVReader(std::istream & stream, bool hasHeader, char delimiter)
  : CSVReader(std::make_unique<IstreamWrapper>(stream), hasHeader, delimiter)
{}

CSVReader::CSVReader(Reader const & reader, bool hasHeader, char delimiter)
  : CSVReader(std::make_unique<ReaderWrapper>(reader), hasHeader, delimiter)
{}

bool CSVReader::HasHeader() const
{
  return m_hasHeader;
}

char CSVReader::GetDelimiter() const
{
  return m_delimiter;
}

CSVReader::Row const & CSVReader::GetHeader() const
{
  return m_header;
}

CSVReader::Rows CSVReader::ReadAll()
{
  Rows file;
  ForEachRow([&](auto const & row) { file.emplace_back(row); });
  return file;
}

std::optional<CSVReader::Row> CSVReader::ReadRow()
{
  auto const line = m_reader->ReadLine();
  if (!line)
    return {};

  Row row;
  strings::ParseCSVRow(*line, m_delimiter, row);
  ++m_currentLine;
  return row;
}

size_t CSVReader::GetCurrentLineNumber() const
{
  return m_currentLine;
}

CSVReader::IstreamWrapper::IstreamWrapper(std::istream & stream) : m_stream(stream) {}

std::optional<std::string> CSVReader::IstreamWrapper::ReadLine()
{
  std::string line;
  return std::getline(m_stream, line) ? line : std::optional<std::string>();
}

CSVReader::ReaderWrapper::ReaderWrapper(Reader const & reader) : m_reader(reader) {}

std::optional<std::string> CSVReader::ReaderWrapper::ReadLine()
{
  std::string line;
  char ch = '\0';
  while (m_pos < m_reader.Size() && ch != '\n')
  {
    m_reader.Read(m_pos, &ch, sizeof(ch));
    line.push_back(ch);
    ++m_pos;
  }

  if (line.empty())
    return {};

  if (line.back() == '\n')
    line.pop_back();

  return line;
}

CSVReader::DefaultReader::DefaultReader(std::string const & filename) : m_stream(filename)
{
  if (!m_stream)
    LOG(LERROR, ("Can't open file ", filename));

  m_stream.exceptions(std::ios::badbit);
}

std::optional<std::string> CSVReader::DefaultReader::ReadLine()
{
  return IstreamWrapper(m_stream).ReadLine();
}

CSVRunner::Iterator::Iterator(CSVReader & reader, bool isEnd) : m_reader(reader)
{
  if (!isEnd)
    m_current = m_reader.ReadRow();
}

CSVRunner::Iterator::Iterator(Iterator const & other) : m_reader(other.m_reader), m_current(other.m_current) {}

CSVRunner::Iterator & CSVRunner::Iterator::operator++()
{
  m_current = m_reader.ReadRow();
  return *this;
}

CSVRunner::Iterator CSVRunner::Iterator::operator++(int)
{
  Iterator tmp(*this);
  operator++();
  return tmp;
}

bool CSVRunner::Iterator::operator==(Iterator const & other) const
{
  return &m_reader == &other.m_reader && static_cast<bool>(m_current) == static_cast<bool>(other.m_current);
}

bool CSVRunner::Iterator::operator!=(Iterator const & other) const
{
  return !(*this == other);
}

CSVReader::Row & CSVRunner::Iterator::operator*()
{
  return *m_current;
}

CSVRunner::CSVRunner(CSVReader && reader) : m_reader(std::move(reader)) {}

CSVRunner::Iterator CSVRunner::begin()
{
  return Iterator(m_reader);
}

CSVRunner::Iterator CSVRunner::end()
{
  return Iterator(m_reader, true /* isEnd */);
}
}  // namespace coding