ICAROUS
All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros Modules Pages
Classes | Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | List of all members
larcfm::SeparatedInput Class Reference

#include <SeparatedInput.h>

Inheritance diagram for larcfm::SeparatedInput:
larcfm::ParameterReader larcfm::ErrorReporter

Classes

class  SeparatedInputException
 

Public Member Functions

 SeparatedInput ()
 
 SeparatedInput (std::istream *fs)
 
 SeparatedInput (const SeparatedInput &x)
 
SeparatedInputoperator= (const SeparatedInput &x)
 
void setColumnDelimiters (const std::string &delim)
 
std::string getColumnDelimiters () const
 
void setFixedColumn (const std::string &widths, const std::string &nameList, const std::string &unitList)
 
std::string getHeading (int i) const
 
int size () const
 
int findHeading (const std::string &heading) const
 
std::string getUnit (int i) const
 
bool unitFieldsDefined () const
 
void setCaseSensitive (bool b)
 
bool getCaseSensitive () const
 
void setQuoteCharacter (char q)
 
void setCsv ()
 
void setQuoteCharacter (char q, const std::vector< char > &delims, const std::string &sub)
 
char getQuoteCharacter () const
 
ParameterDatagetParametersRef ()
 
ParameterData getParameters () const
 
bool columnHasValue (int i) const
 
std::string getColumnString (int i) const
 
double getColumn (int i) const
 
double getColumn (int i, const std::string &default_unit) const
 
double getColumn (int i, double defaultValue, bool verbose) const
 
bool readLine ()
 
int lineNumber () const
 
std::string getLine () const
 
std::string getPreambleImage () const
 
bool hasError () const
 
bool hasMessage () const
 
std::string getMessage ()
 
std::string getMessageNoClear () const
 

Static Public Attributes

static const int maxLineSize = 800
 

Private Member Functions

double getUnitFactor (int i) const
 
bool process_units (const std::string &str)
 
bool process_preamble (std::string str)
 
void process_line (const std::string &str)
 
std::vector< std::string > processQuotes (const std::string &str) const
 
std::string readFullLine (std::istream *reader)
 

Private Attributes

std::istream * reader
 
ErrorLog error
 
bool header
 
std::vector< std::string > header_str
 
bool bunits
 
std::vector< std::string > units_str
 
std::vector< double > units_factor
 
std::vector< std::string > line_str
 
bool fixed_width
 
std::vector< int > width_int
 
char quoteCharacter
 
bool quoteCharDefined
 
int linenum
 
bool caseSensitive
 
std::string patternStr
 
std::string preambleImage
 
ParameterData parameters
 

Detailed Description

A class to parse files with input that are in columns separated by commas, spaces, or tabs.

General notes on the file:

Notes on the columns of data

Parameters are essentially a "key/value" pair. Once an parameter is entered (either from the setParameter method or from the file), then that parameter name is reserved. If any future updates to the parameter (either from the setParameter method or the file), then the previous value will be overwritten. Parameters have the following rules:

Constructor & Destructor Documentation

◆ SeparatedInput() [1/3]

larcfm::SeparatedInput::SeparatedInput ( )

Create an "empty" separated input that can only store parameters

◆ SeparatedInput() [2/3]

larcfm::SeparatedInput::SeparatedInput ( std::istream *  fs)
explicit

Create a new SeparatedInput from the given stream

◆ SeparatedInput() [3/3]

larcfm::SeparatedInput::SeparatedInput ( const SeparatedInput x)

Copy Constructor. This should not be used.

Member Function Documentation

◆ columnHasValue()

bool larcfm::SeparatedInput::columnHasValue ( int  i) const

Return true if the column entry has some (nonempty) value, otherwise return false. A value of hyphen (-) is considered a lack of a value.

Parameters
icolumn index
Returns
true if column has a value

◆ findHeading()

int larcfm::SeparatedInput::findHeading ( const std::string &  heading) const

Find the index of the column with given heading. If the heading is not found, then -1 is returned.

Note: If you are getting some oddly large indexes, there are probably some nonstandard characters in the input.

Parameters
headingname of heading
Returns
index of heading

◆ getCaseSensitive()

bool larcfm::SeparatedInput::getCaseSensitive ( ) const

If true, headers and parameters will be case sensitive. If false, all headers and parameters will be converted to lower case.

Returns
false if case is ignored

◆ getColumn() [1/3]

double larcfm::SeparatedInput::getColumn ( int  i) const

Returns the value of the given column (as a double) in internal units. This call will log errors, and has an arbitrary default value.

Parameters
icolumn index
Returns
value of column

◆ getColumn() [2/3]

double larcfm::SeparatedInput::getColumn ( int  i,
const std::string &  default_unit 
) const

Returns the value of the given column (as a double) in internal units. If no units were specified in the file, then this value is assumed to be in the given default units and an appropriate conversion takes place.

Parameters
icolumn index
default_unitunit, if none is specified
Returns
value of column

◆ getColumn() [3/3]

double larcfm::SeparatedInput::getColumn ( int  i,
double  defaultValue,
bool  verbose 
) const

Returns the value of the given column (as a double) in internal units.

Parameters
icolumn index
verboseif true, log a message on errors, if false, fail silently and return an arbitrary value
defaultValuethis is the value returned if there is some error
Returns
value of column

◆ getColumnDelimiters()

std::string larcfm::SeparatedInput::getColumnDelimiters ( ) const

Return the regular expression used to divide each line into columns.

Returns
pattern

◆ getColumnString()

string larcfm::SeparatedInput::getColumnString ( int  i) const

Returns the raw string of the given column read.

Parameters
icolumn index
Returns
column value (as a string)

◆ getHeading()

string larcfm::SeparatedInput::getHeading ( int  i) const

Return the heading for the given column

Parameters
icolumn index
Returns
heading

◆ getLine()

string larcfm::SeparatedInput::getLine ( ) const

Return the last line read as a comma-delineated string

Returns
line

◆ getMessage()

std::string larcfm::SeparatedInput::getMessage ( )
inlinevirtual

Return a string representation of any errors or warnings. Calling this method will clear any messages and reset both the error and warning status to none.

Returns
error and warning messages. If there are no messages, an empty string is returned.

Implements larcfm::ErrorReporter.

◆ getMessageNoClear()

std::string larcfm::SeparatedInput::getMessageNoClear ( ) const
inlinevirtual

Return a string representation of any errors or warnings. Calling this method will not clear the error or warning status (i.e., hasError() will return the same value before and after this call.)

Returns
error and warning messages. If there are no messages, an empty string is returned.

Implements larcfm::ErrorReporter.

◆ getParametersRef()

ParameterData & larcfm::SeparatedInput::getParametersRef ( )
virtual

◆ getPreambleImage()

std::string larcfm::SeparatedInput::getPreambleImage ( ) const

Return the raw header information for the file.
This includes any comments or excess whitespace, before the column definition line.

Returns
preamble

◆ getQuoteCharacter()

char larcfm::SeparatedInput::getQuoteCharacter ( ) const

This returns 0 if no character is defined.

Returns
the character used as a quote

◆ getUnit()

string larcfm::SeparatedInput::getUnit ( int  i) const

Returns the units string for the i-th column. If an invalid column is entered, then "unspecified" is returned.

Parameters
icolumn index
Returns
unit

◆ hasError()

bool larcfm::SeparatedInput::hasError ( ) const
inlinevirtual

Does this object have an error?

Returns
true if there is an error.

Implements larcfm::ErrorReporter.

◆ hasMessage()

bool larcfm::SeparatedInput::hasMessage ( ) const
inlinevirtual

Does this object have an error or a warning?

Returns
true if there is an error or warning.

Implements larcfm::ErrorReporter.

◆ lineNumber()

int larcfm::SeparatedInput::lineNumber ( ) const

Returns the number of the most recently read in line

Returns
line number

◆ operator=()

SeparatedInput & larcfm::SeparatedInput::operator= ( const SeparatedInput x)

Assignment Operator.

◆ readLine()

bool larcfm::SeparatedInput::readLine ( )

Reads a line of the input. The first call to readLine will read the column headings, units, etc.

Returns
true if end of file

◆ setCaseSensitive()

void larcfm::SeparatedInput::setCaseSensitive ( bool  b)

If set to false, all read-in headers and parameters will be converted to lower case.

Parameters
bfalse means ignore case

◆ setColumnDelimiters()

void larcfm::SeparatedInput::setColumnDelimiters ( const std::string &  delim)

Sets the regular expression used to divide each line into columns. If the supplied parameter is not a valid regular expression, then the current delimiter is retained. This should be set before the first "readLine" is called.

Parameters
delim

◆ setCsv()

void larcfm::SeparatedInput::setCsv ( )

Sets this object to formally read comma-separated-value (CSV) input. This format the delimiter is a comma and the quote character is a double-quote. Any amount of whitespace before or after a comma is removed. So the string a, "b " ,c will be read three fields: a, b with a trailing space, and c. Like any quote character, if you to put the quote in the field, duplicate it, so the line: a, b"", c will be three fields: a, b" and c.

◆ setQuoteCharacter()

void larcfm::SeparatedInput::setQuoteCharacter ( char  q)

This sets the 'quote' character. The quote character is a way to contain a delimiter within a field of the data. So if the delimiter is a comma and ' is the quote character, then 'hello, folks' is a valid field.

A quote character does not need to be set, the default is null, meaning, don't use a quote characters.

A quote character can be put in the field by duplicating it. So if the delimiter is a comma and ' is a quote character, then ''hello'', fol''ks will be two fields: 'hello' and fol'ks. Be careful with nested quotes 'a''b' is interpreted as a'b not as two quoted strings: a and b. However, 'a' 'b' will be interpreted as the string: a b

Notes

  • Quotes do not apply to parameters
  • This will generate an error if the quote character matches the delimiters
  • This is not used for parameters or fixed-width columns.
Parameters
qquotation character, if set to null do not treat quote characters specially

◆ size()

int larcfm::SeparatedInput::size ( ) const

Return the number of columns

Returns
number of columns

◆ unitFieldsDefined()

bool larcfm::SeparatedInput::unitFieldsDefined ( ) const

Returns true if a line defining column units was detected.

Returns
true means a units line exists

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