Skyward boardcore
Loading...
Searching...
No Matches
CSVReader.h
Go to the documentation of this file.
1/* Copyright (c) 2021 Skyward Experimental Rocketry
2 * Author: Alberto Nidasio
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
56#pragma once
57
58#include <algorithm>
59#include <fstream>
60#include <iterator>
61#include <string>
62#include <vector>
63
64namespace Boardcore
65{
66
76template <typename Data>
78{
79public:
80 CSVIterator(std::istream& inputStream)
81 : inputStream(inputStream.good() ? &inputStream : nullptr)
82 {
83 ++(*this);
84 }
85
86 CSVIterator() : inputStream(nullptr) {}
87
88 Data const& operator*() const { return parsedData; }
89
90 Data const* operator->() const { return &parsedData; }
91
94 {
95 // Checks for end of file or errors in the stream
96 if (inputStream && !((*inputStream) >> parsedData))
97 inputStream = nullptr;
98
99 return *this;
100 }
101
104 {
105 CSVIterator tmp = *this;
106 ++(*this);
107 return tmp;
108 }
109
110 bool operator==(CSVIterator& iterator)
111 {
112 return (this == &iterator || (this->inputStream == nullptr &&
113 iterator.inputStream == nullptr));
114 }
115
116 bool operator!=(CSVIterator& iterator) { return !(*this == iterator); }
117
118private:
119 std::istream* inputStream;
120 Data parsedData;
121};
122
136template <typename Data>
138{
139public:
140 CSVParser(const char* fileName, bool hasHeader = false)
141 : fileStream(fileName)
142 {
143 // If the file has the header, ignore everithing in the first line
144 if (hasHeader)
145 {
146 fileStream.ignore(std::numeric_limits<std::streamsize>::max(),
147 '\n');
148 }
149 }
150
151 ~CSVParser() { fileStream.close(); }
152
153 CSVIterator<Data> begin() { return CSVIterator<Data>(fileStream); }
154
155 CSVIterator<Data> end() { return CSVIterator<Data>(); }
156
157 void close() { fileStream.close(); }
158
159 std::vector<Data> collect()
160 {
161 std::vector<Data> fileData;
162
163 std::for_each(begin(), end(), [&fileData](const Data& data)
164 { fileData.push_back(data); });
165
166 return fileData;
167 }
168
169private:
170 std::ifstream fileStream;
171};
172
173} // namespace Boardcore
Iterable CSV data.
Definition CSVReader.h:78
bool operator==(CSVIterator &iterator)
Definition CSVReader.h:110
Data const * operator->() const
Prefix increment.
Definition CSVReader.h:90
bool operator!=(CSVIterator &iterator)
Definition CSVReader.h:116
CSVIterator(std::istream &inputStream)
Definition CSVReader.h:80
Data const & operator*() const
Definition CSVReader.h:88
CSVIterator & operator++()
Postfix increment.
Definition CSVReader.h:93
CSVIterator operator++(int)
Definition CSVReader.h:103
Iterable parser of CSV files.
Definition CSVReader.h:138
std::vector< Data > collect()
Definition CSVReader.h:159
CSVIterator< Data > end()
Definition CSVReader.h:155
CSVIterator< Data > begin()
Definition CSVReader.h:153
CSVParser(const char *fileName, bool hasHeader=false)
Definition CSVReader.h:140
This file includes all the types the logdecoder script will decode.