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

Class which parses a CSV file and provides the rows one at a time to a user The parser ignores any line which begins with a '#' character The parser uses the first non-comment row to determine column names The parser handles quotation marks in a standard manner. More...

#include <GeneralCSVLoader.h>

Public Member Functions

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 ()=0
 Get the next line, returning an empty string when there is no further data.
 

Private Attributes

std::vector< std::string > colNames_
 The column names.
 
std::vector< std::string > rowData_
 The row data.
 

Detailed Description

Class which parses a CSV file and provides the rows one at a time to a user The parser ignores any line which begins with a '#' character The parser uses the first non-comment row to determine column names The parser handles quotation marks in a standard manner.

Definition at line 19 of file GeneralCSVLoader.h.

Constructor & Destructor Documentation

◆ ~GeneralCSVLoader()

virtual conditions::GeneralCSVLoader::~GeneralCSVLoader ( )
inlinevirtual

Destructor.

Definition at line 22 of file GeneralCSVLoader.h.

22{}

◆ GeneralCSVLoader()

conditions::GeneralCSVLoader::GeneralCSVLoader ( )
inlineprotected

Definition at line 38 of file GeneralCSVLoader.h.

38{}

Member Function Documentation

◆ columnNames()

std::vector< std::string > conditions::GeneralCSVLoader::columnNames ( ) const
inline

Get the column names.

Definition at line 25 of file GeneralCSVLoader.h.

25{ return colNames_; }
std::vector< std::string > colNames_
The column names.

References colNames_.

◆ get()

const std::string & conditions::GeneralCSVLoader::get ( const std::string &  colname,
bool  ignore_case = true 
) const

Get the value for the given column in the current row.

Definition at line 14 of file GeneralCSVLoader.cxx.

15 {
16 size_t i;
17 for (i = 0; i < colNames_.size(); i++) {
18 if (ignore_case && !strcasecmp(colNames_[i].c_str(), colname.c_str()))
19 break;
20 if (colNames_[i] == colname) break;
21 }
22 if (i == colNames_.size()) {
23 EXCEPTION_RAISE("CSVNoSuchColumn",
24 "No such column '" + colname + "' reading CSV");
25 }
26 return rowData_[i];
27}
std::vector< std::string > rowData_
The row data.

References colNames_, and rowData_.

Referenced by getInteger().

◆ getInteger()

int conditions::GeneralCSVLoader::getInteger ( const std::string &  colname,
bool  ignore_case = true 
) const

Get the value for the given column in the current row as an integer.

Definition at line 29 of file GeneralCSVLoader.cxx.

30 {
31 return atoi(get(colname, ignore_case).c_str());
32}
const std::string & get(const std::string &colname, bool ignore_case=true) const
Get the value for the given column in the current row.

References get().

Referenced by hcal::HcalDetectorMap::HcalDetectorMap(), ecal::EcalDetectorMap::loadCellMap(), ecal::EcalDetectorMap::loadLayerMap(), and ecal::EcalDetectorMap::loadMotherboardMap().

◆ getNextLine()

virtual std::string conditions::GeneralCSVLoader::getNextLine ( )
protectedpure virtual

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

Implemented in conditions::StringCSVLoader, and conditions::StreamCSVLoader.

Referenced by nextRow().

◆ nextRow()

bool conditions::GeneralCSVLoader::nextRow ( )

Advance to next row if possible.

Definition at line 34 of file GeneralCSVLoader.cxx.

34 {
35 do {
36 std::string line = getNextLine();
37 if (line.empty()) return false;
38 // it's a comment!
39 if (line[0] == '#') continue;
40 // split into pieces
41 std::vector<std::string> line_split;
42 // explicitly erase trailing white space
43 line.erase(std::find_if(line.rbegin(), line.rend(),
44 [](unsigned char c) { return !std::isspace(c); })
45 .base(),
46 line.end());
47 boost::tokenizer<boost::escaped_list_separator<char>> tok(line);
48 for (auto chunk : tok) {
49 line_split.push_back(chunk);
50 }
51 if (colNames_.empty()) {
52 colNames_.swap(line_split);
53 continue;
54 }
55 if (line_split.size() == colNames_.size()) {
56 rowData_.swap(line_split);
57 } else {
58 EXCEPTION_RAISE("CSVLineMismatch",
59 "Reading CSV found line with " +
60 std::to_string(line_split.size()) + " in CSV with " +
61 std::to_string(colNames_.size()) + " columns");
62 }
63
64 return true;
65 } while (true);
66}
virtual std::string getNextLine()=0
Get the next line, returning an empty string when there is no further data.

References colNames_, getNextLine(), and rowData_.

Referenced by hcal::HcalDetectorMap::HcalDetectorMap(), ecal::EcalDetectorMap::loadCellMap(), ecal::EcalDetectorMap::loadLayerMap(), and ecal::EcalDetectorMap::loadMotherboardMap().

Member Data Documentation

◆ colNames_

std::vector<std::string> conditions::GeneralCSVLoader::colNames_
private

The column names.

Definition at line 48 of file GeneralCSVLoader.h.

Referenced by columnNames(), get(), and nextRow().

◆ rowData_

std::vector<std::string> conditions::GeneralCSVLoader::rowData_
private

The row data.

Definition at line 52 of file GeneralCSVLoader.h.

Referenced by get(), and nextRow().


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