21#include <ql/errors.hpp>
22#include <ql/utilities/null.hpp>
24#include <boost/algorithm/string/trim.hpp>
32 const std::string& escapeCharacters,
const std::string& quoteCharacters,
const char eolMarker)
33 : hasHeaders_(firstLineContainsHeaders), eolMarker_(eolMarker), currentLine_(Null<Size>()),
34 numberOfColumns_(Null<Size>()),
35 tokenizer_(std::string(),
boost::escaped_list_separator<char>(escapeCharacters, delimiters, quoteCharacters)) {
43 QL_REQUIRE(!
stream_->eof(),
"CSVReader: stream is empty");
62 QL_REQUIRE(
numberOfColumns_ != Null<Size>(),
"CSVFileReader: number of columns not known (need call to next())");
68 std::string line =
"";
70 while (line.size() == 0 && !
stream_->eof()) {
74 if (
stream_->eof() && line.empty()) {
95 QL_REQUIRE(
currentLine_ != Null<Size>(),
"CSVFileReader: current line not known (need call to next())");
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");
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 \""
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];
119 const std::string& delimiters,
const std::string& escapeCharacters,
120 const std::string& quoteCharacters,
const char eolMarker)
121 :
CSVReader(firstLineContainsHeaders, delimiters, escapeCharacters, quoteCharacters,
123 fileName_(fileName) {
126 file_ =
new std::ifstream(fileName);
136 const std::string& delimiters,
const std::string& escapeCharacters,
137 const std::string& quoteCharacters,
const char eolMarker)
138 :
CSVReader(firstLineContainsHeaders, delimiters, escapeCharacters, quoteCharacters, eolMarker),
139 bufferName_(bufferName) {
142 std::stringstream *buffer =
new std::stringstream(bufferName);
CSVBufferReader(const std::string &CSVBuffer, const bool firstLineContainsHeaders, const std::string &delimiters=",;\t", const std::string &escapeCharacters="\\", const std::string "eCharacters="\"", const char eolMarker='\n')
CSVFileReader(const std::string &fileName, const bool firstLineContainsHeaders, const std::string &delimiters=",;\t", const std::string &escapeCharacters="\\", const std::string "eCharacters="\"", const char eolMarker='\n')
std::vector< std::string > headers_
Size numberOfColumns() const
CSVReader(const bool firstLineContainsHeaders, const std::string &delimiters=",;\t", const std::string &escapeCharacters="\\", const std::string "eCharacters="\"", const char eolMarker='\n')
const std::vector< std::string > & fields() const
void setStream(std::istream *stream)
const bool hasField(const std::string &field) const
boost::tokenizer< boost::escaped_list_separator< char > > tokenizer_
std::vector< std::string > data_
std::string get(const std::string &field) const
utility class to access CSV files
Serializable Credit Default Swap.