Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
CSVReader Class Reference

#include <ored/utilities/csvfilereader.hpp>

+ Inheritance diagram for CSVReader:
+ Collaboration diagram for CSVReader:

Public Member Functions

 CSVReader (const bool firstLineContainsHeaders, const std::string &delimiters=",;\t", const std::string &escapeCharacters="\\", const std::string &quoteCharacters="\"", const char eolMarker='\n')
 
virtual ~CSVReader ()
 
void setStream (std::istream *stream)
 
const std::vector< std::string > & fields () const
 
const bool hasField (const std::string &field) const
 
Size numberOfColumns () const
 
bool next ()
 
Size currentLine () const
 
std::string get (const std::string &field) const
 
std::string get (const Size column) const
 
virtual void close ()
 

Private Attributes

std::istream * stream_
 
const bool hasHeaders_
 
const char eolMarker_
 
Size currentLine_
 
Size numberOfColumns_
 
boost::tokenizer< boost::escaped_list_separator< char > > tokenizer_
 
std::vector< std::string > headers_
 
std::vector< std::string > data_
 

Detailed Description

Definition at line 38 of file csvfilereader.hpp.

Constructor & Destructor Documentation

◆ CSVReader()

CSVReader ( const bool  firstLineContainsHeaders,
const std::string &  delimiters = ",;\t",
const std::string &  escapeCharacters = "\\",
const std::string &  quoteCharacters = "\"",
const char  eolMarker = '\n' 
)

Ctor

Definition at line 31 of file csvfilereader.cpp.

33 : hasHeaders_(firstLineContainsHeaders), eolMarker_(eolMarker), currentLine_(Null<Size>()),
34 numberOfColumns_(Null<Size>()),
35 tokenizer_(std::string(), boost::escaped_list_separator<char>(escapeCharacters, delimiters, quoteCharacters)) {
36}
boost::tokenizer< boost::escaped_list_separator< char > > tokenizer_

◆ ~CSVReader()

virtual ~CSVReader ( )
virtual

Definition at line 45 of file csvfilereader.hpp.

45{} // Declare virtual destructor

Member Function Documentation

◆ setStream()

void setStream ( std::istream *  stream)

Set stream for function

Definition at line 38 of file csvfilereader.cpp.

38 {
39 // set stream
40 stream_ = stream;
41
42 if (hasHeaders_) {
43 QL_REQUIRE(!stream_->eof(), "CSVReader: stream is empty");
44 std::string line;
45 getline(*stream_, line, eolMarker_);
46 boost::trim(line);
47 tokenizer_.assign(line);
48 std::copy(tokenizer_.begin(), tokenizer_.end(), std::back_inserter(headers_));
50 }
51}
std::istream * stream_
std::vector< std::string > headers_
+ Here is the caller graph for this function:

◆ fields()

const std::vector< std::string > & fields ( ) const

Returns the fields, if a header line is present, otherwise throws

Definition at line 52 of file csvfilereader.cpp.

52 {
53 //QL_REQUIRE(hasHeaders_, "CSVFileReader: no headers were specified for \"" << fileName_ << "\"");
54 return headers_;
55}
+ Here is the caller graph for this function:

◆ hasField()

const bool hasField ( const std::string &  field) const

Return true if a field is present

Definition at line 57 of file csvfilereader.cpp.

57 {
58 return std::find(fields().begin(), fields().end(), field) != fields().end();
59}
const std::vector< std::string > & fields() const
+ Here is the call graph for this function:

◆ numberOfColumns()

Size numberOfColumns ( ) const

Returns the number of columns

Definition at line 61 of file csvfilereader.cpp.

61 {
62 QL_REQUIRE(numberOfColumns_ != Null<Size>(), "CSVFileReader: number of columns not known (need call to next())");
63 return numberOfColumns_;
64}
+ Here is the caller graph for this function:

◆ next()

bool next ( )

Go to next line in file, returns false if there are no more lines

Definition at line 66 of file csvfilereader.cpp.

66 {
67 //QL_REQUIRE(stream_->is_open(), "CSVFileReader: file is not open, can not move to next line");
68 std::string line = "";
69 // skip empty lines
70 while (line.size() == 0 && !stream_->eof()) {
71 getline(*stream_, line, eolMarker_);
72 boost::trim(line);
73 }
74 if (stream_->eof() && line.empty()) {
75 close();
76 return false;
77 }
78 if (currentLine_ == Null<Size>())
79 currentLine_ = 0;
80 else
82 tokenizer_.assign(line);
83 data_.clear();
84 std::copy(tokenizer_.begin(), tokenizer_.end(), std::back_inserter(data_));
85 if (numberOfColumns_ == Null<Size>())
86 numberOfColumns_ = data_.size();
87 else
88 QL_REQUIRE(data_.size() == numberOfColumns_, "CSVFileReader: data line #"
89 << currentLine_ << " has " << data_.size()
90 << " fields, expected " << numberOfColumns_);
91 return true;
92}
virtual void close()
std::vector< std::string > data_
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ currentLine()

Size currentLine ( ) const

Number of the current data line

Definition at line 94 of file csvfilereader.cpp.

94 {
95 QL_REQUIRE(currentLine_ != Null<Size>(), "CSVFileReader: current line not known (need call to next())");
96 return currentLine_;
97}

◆ get() [1/2]

std::string get ( const std::string &  field) const

Get content of field in current data line, throws if field is not present

Definition at line 99 of file csvfilereader.cpp.

99 {
100 QL_REQUIRE(hasHeaders_, "CSVFileReader: can not get data by field, file does not have headers");
101 QL_REQUIRE(currentLine_ != Null<Size>(), "CSVFileReader: can not get data, need call to next() first");
102 Size index = std::find(headers_.begin(), headers_.end(), field) - headers_.begin();
103 QL_REQUIRE(index < headers_.size(), "CSVFileReader: field \"" << field << "\" not found.");
104 QL_REQUIRE(index < data_.size(), "CSVFileReader: unexpected data size (" << data_.size() << "), required at least "
105 << index + 1 << ", while reading field \""
106 << field << "\"");
107 return data_[index];
108}
+ Here is the caller graph for this function:

◆ get() [2/2]

std::string get ( const Size  column) const

Get content of column in current data line, throws if column is out of range

Definition at line 110 of file csvfilereader.cpp.

110 {
111 QL_REQUIRE(column < numberOfColumns_,
112 "CSVFileReader: column " << column << " out of bounds 0..." << (numberOfColumns_ - 1));
113 QL_REQUIRE(column < data_.size(),
114 "CSVFileReader: unexpected data size (" << data_.size() << "), while reading column " << column);
115 return data_[column];
116}

◆ close()

virtual void close ( )
virtual

Close the file

Reimplemented in CSVFileReader.

Definition at line 65 of file csvfilereader.hpp.

65{}
+ Here is the caller graph for this function:

Member Data Documentation

◆ stream_

std::istream* stream_
private

Definition at line 68 of file csvfilereader.hpp.

◆ hasHeaders_

const bool hasHeaders_
private

Definition at line 69 of file csvfilereader.hpp.

◆ eolMarker_

const char eolMarker_
private

Definition at line 70 of file csvfilereader.hpp.

◆ currentLine_

Size currentLine_
private

Definition at line 71 of file csvfilereader.hpp.

◆ numberOfColumns_

Size numberOfColumns_
private

Definition at line 71 of file csvfilereader.hpp.

◆ tokenizer_

boost::tokenizer<boost::escaped_list_separator<char> > tokenizer_
private

Definition at line 72 of file csvfilereader.hpp.

◆ headers_

std::vector<std::string> headers_
private

Definition at line 73 of file csvfilereader.hpp.

◆ data_

std::vector<std::string> data_
private

Definition at line 73 of file csvfilereader.hpp.