CsvReader/src/include/CSV/Reader.h
Go to the documentation of this file.
1 /****
2  * Reader.h
3  *
4  * Copyright 2021 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the CsvReader Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  ****/
19 
20 #pragma once
21 
22 #include "Parser.h"
23 #include <memory>
24 
25 namespace CSV
26 {
34 class Reader : private Parser
35 {
36 public:
43  Reader(IDataSourceStream* source, const Options& options, const CStringArray& headings = nullptr);
44 
52  Reader(IDataSourceStream* source, char fieldSeparator = ',', const CStringArray& headings = nullptr,
53  uint16_t maxLineLength = 2048)
54  : Reader(source,
55  Options{
56  .lineLength = maxLineLength,
57  .fieldSeparator = fieldSeparator,
58  },
59  headings)
60  {
61  }
62 
69  void reset()
70  {
71  seek(BOF);
72  }
73 
78  bool next()
79  {
80  return source ? readRow(*source) : false;
81  }
82 
86  unsigned count() const
87  {
88  return headings.count();
89  }
90 
91  using Parser::getRow;
92 
98  const char* getValue(unsigned index) const
99  {
100  return getRow()[index];
101  }
102 
108  const char* getValue(const char* name) const
109  {
110  return getValue(getColumn(name));
111  }
112 
118  int getColumn(const char* name) const
119  {
120  return headings.indexOf(name);
121  }
122 
126  explicit operator bool() const
127  {
128  return bool(source);
129  }
130 
134  const CStringArray& getHeadings() const
135  {
136  return headings;
137  }
138 
139  using Parser::getCursor;
140 
141  using Parser::tell;
142 
154  bool seek(int offset);
155 
156  bool seek(const Cursor& cursor)
157  {
158  return seek(cursor.start);
159  }
160 
161 private:
162  std::unique_ptr<IDataSourceStream> source;
163  CStringArray headings;
164  unsigned start{0};
165 };
166 
167 } // namespace CSV
Class to parse a CSV file.
Definition: Parser.h:81
bool readRow(IDataSourceStream &source)
Read a single data row using data from provided DataSourceStream.
const CStringArray & getRow() const
Get current row.
Definition: Parser.h:162
const Cursor & getCursor() const
Get cursor position for current row.
Definition: Parser.h:181
int tell() const
Get cursor position for current row.
Definition: Parser.h:173
static constexpr int BOF
Indicates 'Before First Record'.
Definition: Parser.h:110
Class to read a CSV file.
Definition: CsvReader/src/include/CSV/Reader.h:35
const char * getValue(unsigned index) const
Get a value from the current row.
Definition: CsvReader/src/include/CSV/Reader.h:98
bool seek(int offset)
Set reader to previously noted position.
bool next()
Seek to next record.
Definition: CsvReader/src/include/CSV/Reader.h:78
const CStringArray & getRow() const
Get current row.
Definition: Parser.h:162
Reader(IDataSourceStream *source, char fieldSeparator=',', const CStringArray &headings=nullptr, uint16_t maxLineLength=2048)
Construct a CSV reader.
Definition: CsvReader/src/include/CSV/Reader.h:52
unsigned count() const
Get number of columns.
Definition: CsvReader/src/include/CSV/Reader.h:86
void reset()
Reset reader to start of CSV file.
Definition: CsvReader/src/include/CSV/Reader.h:69
bool seek(const Cursor &cursor)
Definition: CsvReader/src/include/CSV/Reader.h:156
const CStringArray & getHeadings() const
Get headings.
Definition: CsvReader/src/include/CSV/Reader.h:134
Reader(IDataSourceStream *source, const Options &options, const CStringArray &headings=nullptr)
Construct a CSV reader.
int getColumn(const char *name) const
Get index of column given its name.
Definition: CsvReader/src/include/CSV/Reader.h:118
const char * getValue(const char *name) const
Get a value from the current row.
Definition: CsvReader/src/include/CSV/Reader.h:108
Class to manage a double null-terminated list of strings, such as "one\0two\0three\0".
Definition: CStringArray.h:22
int indexOf(const char *str, bool ignoreCase=true) const
Find the given string and return its index.
unsigned count() const
Get quantity of strings in array.
Base class for read-only stream.
Definition: DataSourceStream.h:46
Definition: Parser.h:27
Contains location details of the current record in the source stream.
Definition: Parser.h:31
int start
BOF if there is no current record.
Definition: Parser.h:32
Parsing options.
Definition: Parser.h:86