Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
csvfilereader.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2017 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
19/*! \file ored/utilities/csvfilereader.hpp
20 \brief utility class to access CSV files
21 \ingroup utilities
22*/
23
24#pragma once
25
26#include <ql/types.hpp>
27
28#include <boost/tokenizer.hpp>
29#include <fstream>
30#include <iostream>
31#include <sstream>
32#include <vector>
33
34namespace ore {
35namespace data {
36using QuantLib::Size;
37
38class CSVReader {
39public:
40 /*! Ctor */
41 CSVReader( const bool firstLineContainsHeaders, const std::string& delimiters = ",;\t",
42 const std::string& escapeCharacters = "\\", const std::string& quoteCharacters = "\"",
43 const char eolMarker = '\n');
44
45 virtual ~CSVReader() {} // Declare virtual destructor
46
47 /*! Set stream for function */
48 void setStream(std::istream* stream);
49 //void setStream(std::string);
50 /*! Returns the fields, if a header line is present, otherwise throws */
51 const std::vector<std::string>& fields() const;
52 /*! Return true if a field is present */
53 const bool hasField(const std::string& field) const;
54 /*! Returns the number of columns */
55 Size numberOfColumns() const;
56 /*! Go to next line in file, returns false if there are no more lines */
57 bool next();
58 /*! Number of the current data line */
59 Size currentLine() const;
60 /*! Get content of field in current data line, throws if field is not present */
61 std::string get(const std::string& field) const;
62 /*! Get content of column in current data line, throws if column is out of range */
63 std::string get(const Size column) const;
64 /*! Close the file */
65 virtual void close() {}
66
67private:
68 std::istream* stream_;
69 const bool hasHeaders_;
70 const char eolMarker_;
72 boost::tokenizer<boost::escaped_list_separator<char>> tokenizer_;
73 std::vector<std::string> headers_, data_;
74};
75
76class CSVFileReader : public CSVReader {
77public:
78 /*! Ctor */
79 CSVFileReader(const std::string& fileName, const bool firstLineContainsHeaders,
80 const std::string& delimiters = ",;\t", const std::string& escapeCharacters = "\\",
81 const std::string& quoteCharacters = "\"", const char eolMarker = '\n');
82 /*! Close the file */
83 void close() override;
84
85private:
86 const std::string fileName_;
87 std::ifstream* file_;
88};
89
90class CSVBufferReader : public CSVReader {
91public:
92 /*! Ctor */
93 CSVBufferReader(const std::string& CSVBuffer, const bool firstLineContainsHeaders,
94 const std::string& delimiters = ",;\t", const std::string& escapeCharacters = "\\",
95 const std::string& quoteCharacters = "\"", const char eolMarker = '\n');
96
97private:
98 const std::string bufferName_;
99
100};
101
102} // namespace data
103} // namespace ore
104
105
106
const std::string bufferName_
const std::string fileName_
Size currentLine() const
std::istream * stream_
std::vector< std::string > headers_
virtual void close()
Size numberOfColumns() const
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
@ data
Definition: log.hpp:77
Serializable Credit Default Swap.
Definition: namespaces.docs:23