Allolib  1.0
C++ Components For Interactive Multimedia
al_CSVReader.hpp
1 #ifndef INCLUDE_AL_CSVREADER_HPP
2 #define INCLUDE_AL_CSVREADER_HPP
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology,
8  UCSB. Copyright (C) 2012. The Regents of the University of California. All
9  rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are
13  met:
14 
15  Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17 
18  Redistributions in binary form must reproduce the above
19  copyright notice, this list of conditions and the following disclaimer in the
20  documentation and/or other materials provided with the
21  distribution.
22 
23  Neither the name of the University of California nor the names
24  of its contributors may be used to endorse or promote products derived from
25  this software without specific prior written permission.
26 
27  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
31  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
34  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 
39 
40  File description:
41  CSV File reader
42 
43  File author(s):
44  Andres Cabrera mantaraya36@gmail.com 2017
45 */
46 
47 #include <algorithm>
48 #include <cstring>
49 #include <fstream>
50 #include <iostream>
51 #include <memory>
52 #include <sstream>
53 #include <string>
54 #include <vector>
55 
56 namespace al {
57 
111 class CSVReader {
112  public:
113  typedef enum { STRING, REAL, INT64, BOOLEAN, IGNORE_COLUMN } DataType;
114 
115  CSVReader() {
116  // TODO We could automatically add types by trying to parse the file
117  }
118 
119  ~CSVReader();
120 
127  bool readFile(std::string fileName, bool hasColumnNames = true);
128 
133  void addType(DataType type) { mDataTypes.push_back(type); }
134 
135  void clearTypes() { mDataTypes.clear(); }
136 
142  template <class DataStruct>
143  std::vector<DataStruct> copyToStruct() {
144  std::vector<DataStruct> output;
145  if (sizeof(DataStruct) < calculateRowLength()) {
146  std::cout << "WARNING: DataStruct size is too small for CSV file!"
147  << std::endl;
148  return output;
149  }
150  for (auto row : mData) {
151  DataStruct newValues;
152  memset(&newValues, 0, sizeof(DataStruct));
153  memcpy(&newValues, row, sizeof(newValues));
154  output.push_back(newValues);
155  }
156 
157  return output;
158  }
159 
165  std::vector<double> getColumn(int index);
166 
173  std::vector<std::string> getColumnNames() { return mColumnNames; }
174 
175  void setBasePath(std::string basePath) { mBasePath = basePath; }
176 
177  protected:
178  size_t calculateRowLength();
179 
180  const size_t maxStringSize = 32;
181 
182  std::vector<std::string> mColumnNames;
183  std::vector<DataType> mDataTypes;
184  std::vector<char *> mData;
185 
186  std::string mBasePath;
187 };
188 
189 } // namespace al
190 
191 #endif // INCLUDE_AL_CSVREADER_HPP
The CSVReader class reads simple CSV files.
std::vector< DataStruct > copyToStruct()
getColumn returns a column from the csv file
bool readFile(std::string fileName, bool hasColumnNames=true)
readFile reads the CSV file into internal memory
void addType(DataType type)
addType
std::vector< std::string > getColumnNames()
get names of the columns in CSV file
std::vector< double > getColumn(int index)
getColumn returns a column from the csv file
Definition: al_App.hpp:23