LDMX Software
Public Member Functions | Protected Member Functions | Private Attributes | List of all members
conditions::StreamCSVLoader Class Reference

Specialization of the GeneralCSVLoader for loading from a file/stream. More...

#include <GeneralCSVLoader.h>

Public Member Functions

 StreamCSVLoader (const std::string &filename)
 Constructor a loader from the provided file name.
 
 StreamCSVLoader (std::istream &stream)
 Construct a loader from the provided input stream.
 
virtual ~StreamCSVLoader ()
 Clean-up the stream if we own it.
 
- Public Member Functions inherited from conditions::GeneralCSVLoader
virtual ~GeneralCSVLoader ()
 Destructor.
 
std::vector< std::string > columnNames () const
 Get the column names.
 
bool nextRow ()
 Advance to next row if possible.
 
const std::string & get (const std::string &colname, bool ignore_case=true) const
 Get the value for the given column in the current row.
 
int getInteger (const std::string &colname, bool ignore_case=true) const
 Get the value for the given column in the current row as an integer.
 

Protected Member Functions

virtual std::string getNextLine ()
 Get the next line, returning an empty string when there is no further data.
 

Private Attributes

std::istream * source_
 The stream.
 
bool ownStream_
 Own stream?
 
std::string line_
 Line buffer.
 

Detailed Description

Specialization of the GeneralCSVLoader for loading from a file/stream.

Definition at line 85 of file GeneralCSVLoader.h.

Constructor & Destructor Documentation

◆ StreamCSVLoader() [1/2]

conditions::StreamCSVLoader::StreamCSVLoader ( const std::string &  filename)

Constructor a loader from the provided file name.

We expand the file-name using wordexp and then open an input file stream to it. We own the stream in this case.

Definition at line 94 of file GeneralCSVLoader.cxx.

95 : source_{0}, ownStream_{true} {
96 std::string expanded_fname = filename;
97 wordexp_t p;
98
99 if (wordexp(filename.c_str(), &p, 0)) {
100 EXCEPTION_RAISE("StreamCSVFileNotFound",
101 "Error expanding '" + filename + "'");
102 }
103
104 if (!p.we_wordc) {
105 EXCEPTION_RAISE("StreamCSVFileNotFound",
106 "No file matching '" + filename + "' found");
107 }
108
109 if (p.we_wordc != 1) {
110 int nfound = p.we_wordc;
111 wordfree(&p);
112 EXCEPTION_RAISE("StreamCSVFileNotFound",
113 "Multiple files (" + std::to_string(nfound) +
114 ") matching '" + filename + "' found");
115 }
116
117 expanded_fname = p.we_wordv[0];
118 wordfree(&p);
119
120 source_ = new std::ifstream(expanded_fname);
121 if (source_->fail()) {
122 EXCEPTION_RAISE("StreamCSVFileNotFound",
123 "Unable to open '" + expanded_fname + "'");
124 }
125}
std::istream * source_
The stream.

References source_.

◆ StreamCSVLoader() [2/2]

conditions::StreamCSVLoader::StreamCSVLoader ( std::istream &  stream)

Construct a loader from the provided input stream.

We are given an already-created stream, so we do not own the stream in this case.

Definition at line 127 of file GeneralCSVLoader.cxx.

128 : source_{&stream}, ownStream_{false} {}

◆ ~StreamCSVLoader()

conditions::StreamCSVLoader::~StreamCSVLoader ( )
virtual

Clean-up the stream if we own it.

Definition at line 130 of file GeneralCSVLoader.cxx.

130 {
131 if (ownStream_ && source_) delete source_;
132 source_ = 0;
133}

References ownStream_, and source_.

Member Function Documentation

◆ getNextLine()

std::string conditions::StreamCSVLoader::getNextLine ( )
protectedvirtual

Get the next line, returning an empty string when there is no further data.

Implements conditions::GeneralCSVLoader.

Definition at line 135 of file GeneralCSVLoader.cxx.

135 {
136 // if no stream, no line...
137 if (!source_) return "";
138
139 do {
140 line_.clear();
141 std::getline(*source_, line_);
142 if (line_.empty() && source_->eof()) return "";
143 } while (line_.empty()); // skip blank lines
144
145 return line_;
146}
std::string line_
Line buffer.

References line_, and source_.

Member Data Documentation

◆ line_

std::string conditions::StreamCSVLoader::line_
private

Line buffer.

Definition at line 122 of file GeneralCSVLoader.h.

Referenced by getNextLine().

◆ ownStream_

bool conditions::StreamCSVLoader::ownStream_
private

Own stream?

Definition at line 118 of file GeneralCSVLoader.h.

Referenced by ~StreamCSVLoader().

◆ source_

std::istream* conditions::StreamCSVLoader::source_
private

The stream.

Definition at line 114 of file GeneralCSVLoader.h.

Referenced by getNextLine(), StreamCSVLoader(), and ~StreamCSVLoader().


The documentation for this class was generated from the following files: