29 #include "WReaderCSV.h"
42 static std::istream& safeGetLine( std::istream& is, std::string& t )
46 std::istream::sentry se( is,
true );
47 std::streambuf* sb = is.rdbuf();
50 while( ( c = sb->sbumpc() ) != std::streambuf::traits_type::eof() )
57 if( sb->sgetc() ==
'\n' )
69 is.setstate( std::ios::eofbit );
76 size_t columnCount = 0;
78 std::vector< std::string > row;
89 throw WException(
"File could not be opened!" );
93 safeGetLine( file, line );
99 rawRow->push_back( line );
101 columnCount = header->at( 0 ).size();
104 while( safeGetLine( file, line ) )
106 if( line.empty() && file.eof() )
110 rawRow->push_back( line );
112 if( row.size() != columnCount )
114 throw WException(
"Data row count does not equal header count!" );
118 data->push_back( row );
122 if( data->size() == 0 )
124 throw WException(
"CSV File does not contain data!" );
129 std::shared_ptr< WDataSetCSV > datasetcsv = std::shared_ptr< WDataSetCSV >(
new WDataSetCSV( header, data ) );
130 datasetcsv->setRawDataSet( rawRow );
138 std::vector< std::string > row;
141 while( ( pos = line.find(
',' ) ) != std::string::npos )
143 cell = line.substr( 0, pos );
144 row.push_back( cell );
145 line.erase( 0, pos + 1 );
148 row.push_back( line );
Represents a CSV dataset.
std::shared_ptr< std::vector< std::vector< std::string > > > ContentSPtr
represents a pointer to the Content
std::shared_ptr< std::vector< std::string > > SeperatedRowSPtr
represents a pointer to a vector of csv-raw-row as string
std::vector< std::vector< std::string > > Content
represents a vector containing a vector of strings.
virtual ~WReaderCSV()
Destroys this instance.
WReaderCSV(std::string fname)
Constructs a CSV reader object.
virtual std::shared_ptr< WDataSetCSV > read()
Read the file and create a dataset as a vector.
std::vector< std::string > transformLineToVector(std::string line)
Transforms a given string into a string vector, by a predefined delimiter.
Read some data from a given file.
std::string m_fname
Absolute path of the file to read from.