Allolib  1.0
C++ Components For Interactive Multimedia
al::CSVReader Class Reference

The CSVReader class reads simple CSV files. More...

#include <C:/Users/Andres/source/repos/casm_viewer/external/tinc/external/allolib/include/al/io/al_CSVReader.hpp>

Public Types

enum  DataType {
  STRING , REAL , INT64 , BOOLEAN ,
  IGNORE_COLUMN
}
 

Public Member Functions

bool readFile (std::string fileName, bool hasColumnNames=true)
 readFile reads the CSV file into internal memory More...
 
void addType (DataType type)
 addType More...
 
void clearTypes ()
 
template<class DataStruct >
std::vector< DataStruct > copyToStruct ()
 getColumn returns a column from the csv file More...
 
std::vector< double > getColumn (int index)
 getColumn returns a column from the csv file More...
 
std::vector< std::string > getColumnNames ()
 get names of the columns in CSV file More...
 
void setBasePath (std::string basePath)
 

Protected Member Functions

size_t calculateRowLength ()
 

Protected Attributes

const size_t maxStringSize = 32
 
std::vector< std::string > mColumnNames
 
std::vector< DataType > mDataTypes
 
std::vector< char * > mData
 
std::string mBasePath
 

Detailed Description

The CSVReader class reads simple CSV files.

To use, first create a CSVReader object and call addType() to add the type of a column. Then call readFile().

Once the file is in memory it can be read as columns of floats using getColumn(). Or the whole CSV data can be copied to memory by defining a struct that will hold the values from each row from the csv file and calling copyToStruct() to create a vector with the data from the CSV file.

This reader is currently very naive (but efficient) and might choke with complex or malformed CSV files.

typedef struct {
char s[32];
double val1, val2, val3;
bool b;
} RowTypes;
int main(int argc, char *argv[]) {
CSVReader reader;
reader.addType(CSVReader::STRING);
reader.addType(CSVReader::REAL);
reader.addType(CSVReader::REAL);
reader.addType(CSVReader::REAL);
reader.addType(CSVReader::BOOLEAN);
reader.readFile("examples/csv/I13893-gm-21DIV-0001.csv");
std::vector<RowTypes> rows = reader.copyToStruct<RowTypes>();
for(auto row: rows) {
std::cout << std::string(row.s) << " : "
<< row.val1 << " "
<< row.val2 << " "
<< row.val3 << " "
<< (row.b ? "+" : "-")
<< std::endl;
}
std::vector<double> column1 = reader.getColumn(1);
for(auto value: column1) {
std::cout << value << std::endl;
}
std::cout << " Num rows:" << rows.size() << std::endl;
return 0;
}

Definition at line 111 of file al_CSVReader.hpp.

Member Function Documentation

◆ addType()

void al::CSVReader::addType ( DataType  type)
inline

addType

Parameters
type

Definition at line 133 of file al_CSVReader.hpp.

◆ copyToStruct()

template<class DataStruct >
std::vector<DataStruct> al::CSVReader::copyToStruct ( )
inline

getColumn returns a column from the csv file

Parameters
indexcolumn index
Returns
vector with the data

Definition at line 143 of file al_CSVReader.hpp.

◆ getColumn()

std::vector<double> al::CSVReader::getColumn ( int  index)

getColumn returns a column from the csv file

Parameters
indexcolumn index
Returns
vector with the data

◆ getColumnNames()

std::vector<std::string> al::CSVReader::getColumnNames ( )
inline

get names of the columns in CSV file

Returns
array with column names

Must be called after readFile(), otherwise an empty vector is returned.

Definition at line 173 of file al_CSVReader.hpp.

◆ readFile()

bool al::CSVReader::readFile ( std::string  fileName,
bool  hasColumnNames = true 
)

readFile reads the CSV file into internal memory

Parameters
fileNamethe csv file name
hasColumnNamesif true, the first line in the file is interpreted as column names

The documentation for this class was generated from the following file: