Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
csvfilereader.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2023 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
20
21#include <ql/errors.hpp>
22#include <ql/utilities/null.hpp>
23
24#include <boost/algorithm/string/trim.hpp>
25
26using QuantLib::Null;
27
28namespace ore {
29namespace data {
30
31CSVReader::CSVReader( const bool firstLineContainsHeaders, const std::string& delimiters,
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)) {
36}
37
38void CSVReader::setStream(std::istream* stream) {
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}
52const std::vector<std::string>& CSVReader::fields() const {
53 //QL_REQUIRE(hasHeaders_, "CSVFileReader: no headers were specified for \"" << fileName_ << "\"");
54 return headers_;
55}
56
57const bool CSVReader::hasField(const std::string& field) const {
58 return std::find(fields().begin(), fields().end(), field) != fields().end();
59}
60
62 QL_REQUIRE(numberOfColumns_ != Null<Size>(), "CSVFileReader: number of columns not known (need call to next())");
63 return numberOfColumns_;
64}
65
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}
93
95 QL_REQUIRE(currentLine_ != Null<Size>(), "CSVFileReader: current line not known (need call to next())");
96 return currentLine_;
97}
98
99std::string CSVReader::get(const std::string& field) const {
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}
109
110std::string CSVReader::get(const Size column) const {
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}
117
118CSVFileReader::CSVFileReader(const std::string& fileName, const bool firstLineContainsHeaders,
119 const std::string& delimiters, const std::string& escapeCharacters,
120 const std::string& quoteCharacters, const char eolMarker)
121 : CSVReader(firstLineContainsHeaders, delimiters, escapeCharacters, quoteCharacters,
122 eolMarker),
123 fileName_(fileName) {
124
125 // set file name
126 file_ = new std::ifstream(fileName);
127
128 // pass stream to function set stream
130
131}
132
133void CSVFileReader::close() { file_->close(); }
134
135CSVBufferReader::CSVBufferReader(const std::string& bufferName, const bool firstLineContainsHeaders,
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) {
140
141 // process the buffer
142 std::stringstream *buffer = new std::stringstream(bufferName);
143
144 // set buffer stream
145 setStream(buffer);
146
147}
148
149} // namespace data
150} // namespace ore
CSVBufferReader(const std::string &CSVBuffer, const bool firstLineContainsHeaders, const std::string &delimiters=",;\t", const std::string &escapeCharacters="\\", const std::string &quoteCharacters="\"", const char eolMarker='\n')
CSVFileReader(const std::string &fileName, const bool firstLineContainsHeaders, const std::string &delimiters=",;\t", const std::string &escapeCharacters="\\", const std::string &quoteCharacters="\"", const char eolMarker='\n')
Size currentLine() const
std::istream * stream_
std::vector< std::string > headers_
virtual void close()
Size numberOfColumns() const
CSVReader(const bool firstLineContainsHeaders, const std::string &delimiters=",;\t", const std::string &escapeCharacters="\\", const std::string &quoteCharacters="\"", 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
@ data
Definition: log.hpp:77
Serializable Credit Default Swap.
Definition: namespaces.docs:23