Table.h
Go to the documentation of this file.
1 /****
2  * Table.h
3  *
4  * Copyright 2024 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 <CSV/Reader.h>
23 
24 namespace CSV
25 {
29 struct Record {
31 
33  {
34  }
35 
37  {
38  }
39 
40  explicit operator bool() const
41  {
42  return row;
43  }
44 
45  const char* operator[](unsigned index) const
46  {
47  return row[index];
48  }
49 };
50 
55 template <class Record = Record> class Table : public Reader
56 {
57 public:
58  class Iterator
59  {
60  public:
61  static constexpr unsigned end{UINT32_MAX};
62 
63  Iterator(Reader* reader, unsigned index) : reader(reader), index(index)
64  {
65  }
66 
68  {
69  if(reader && reader->next()) {
70  ++index;
71  } else {
72  index = end;
73  }
74  return *this;
75  }
76 
77  Record operator*() const
78  {
79  return (reader && index != end) ? Record(reader->getRow()) : Record();
80  }
81 
82  bool operator==(const Iterator& other) const
83  {
84  return reader == other.reader && index == other.index;
85  }
86 
87  bool operator!=(const Iterator& other) const
88  {
89  return !operator==(other);
90  }
91 
92  private:
93  Reader* reader;
94  unsigned index;
95  };
96 
97  using Reader::Reader;
98 
103  {
104  return Reader::next() ? getRow() : Record();
105  }
106 
108  {
109  reset();
110  return Table::next() ? Iterator(this, 0) : end();
111  }
112 
114  {
115  return Iterator(this, Iterator::end);
116  }
117 };
118 
119 } // namespace CSV
Class to read a CSV file.
Definition: CsvReader/src/include/CSV/Reader.h:35
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
void reset()
Reset reader to start of CSV file.
Definition: CsvReader/src/include/CSV/Reader.h:69
Reader(IDataSourceStream *source, const Options &options, const CStringArray &headings=nullptr)
Construct a CSV reader.
Definition: Table.h:59
static constexpr unsigned end
Definition: Table.h:61
bool operator!=(const Iterator &other) const
Definition: Table.h:87
bool operator==(const Iterator &other) const
Definition: Table.h:82
Record operator*() const
Definition: Table.h:77
Iterator & operator++()
Definition: Table.h:67
Iterator(Reader *reader, unsigned index)
Definition: Table.h:63
Class template for accessing CSV file as set of records.
Definition: Table.h:56
Iterator begin()
Definition: Table.h:107
Iterator end()
Definition: Table.h:113
Record next()
Fetch next record.
Definition: Table.h:102
Class to manage a double null-terminated list of strings, such as "one\0two\0three\0".
Definition: CStringArray.h:22
Definition: Parser.h:27
Base class for interpreting a record (line) in a CSV file.
Definition: Table.h:29
CStringArray row
Definition: Table.h:30
Record()
Definition: Table.h:32
Record(const CStringArray &row)
Definition: Table.h:36
const char * operator[](unsigned index) const
Definition: Table.h:45