11 #include <boost/lexical_cast.hpp> 12 #include <boost/shared_ptr.hpp> 67 CSVRow(
const size_t cols = 0,
const char separator =
',');
82 CSVRow(
const std::string& text,
const char separator =
',');
86 return (values_.size());
98 void parse(
const std::string& line);
110 std::string readAt(
const size_t at)
const;
131 std::string readAtEscaped(
const size_t at)
const;
139 void trim(
const size_t count);
159 cast_value = boost::lexical_cast<T>(readAt(at).c_str());
161 }
catch (
const boost::bad_lexical_cast& ex) {
176 std::string render()
const;
187 void writeAt(
const size_t at,
const char* value);
198 void writeAt(
const size_t at,
const std::string& value) {
199 writeAt(at, value.c_str());
213 void writeAtEscaped(
const size_t at,
const std::string& value);
222 values_.push_back(boost::lexical_cast<std::string>(value));
223 }
catch (
const boost::bad_lexical_cast& ex) {
225 "appended to the CSV file row.");
240 void writeAt(
const size_t at,
const T value) {
243 values_[at] = boost::lexical_cast<std::string>(value);
244 }
catch (
const boost::bad_lexical_cast& ex) {
246 " written in the CSV file row at position '" 258 return (render() == other.
render());
268 return (render() != other.
render());
289 static std::string escapeCharacters(
const std::string& orig_str,
290 const std::string& characters);
300 static std::string unescapeCharacters(
const std::string& escaped_str);
310 void checkIndex(
const size_t at)
const;
318 std::string separator_;
321 std::vector<std::string> values_;
324 static const std::string escape_tag;
364 CSVFile(
const std::string& filename);
378 void addColumn(
const std::string& col_name);
386 void append(
const CSVRow& row)
const;
404 return (cols_.size());
427 size_t getColumnIndex(
const std::string& col_name)
const;
435 std::string getColumnName(
const size_t col_index)
const;
448 bool next(
CSVRow& row,
const bool skip_validation =
false);
467 virtual void open(
const bool seek_to_end =
false);
475 virtual void recreate();
487 read_msg_ = read_msg;
509 void addColumnInternal(
const std::string& col_name);
527 virtual bool validate(
const CSVRow& row);
542 virtual bool validateHeader(
const CSVRow& header);
554 void checkStreamStatusAndReset(
const std::string& operation)
const;
557 std::streampos size()
const;
560 std::string filename_;
563 boost::shared_ptr<std::fstream> fs_;
566 std::vector<std::string> cols_;
569 std::string read_msg_;
T readAndConvertAt(const size_t at) const
Retrieves a value from the internal container.
size_t getColumnCount() const
Returns the number of columns in the file.
static CSVRow EMPTY_ROW()
Represents empty row.
std::ostream & operator<<(std::ostream &os, const CSVRow &row)
Overrides standard output stream operator for CSVRow object.
std::string getFilename() const
Returns the path to the CSV file.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
size_t getValuesCount() const
Returns number of values in a CSV row.
void writeAt(const size_t at, const T value)
Replaces the value at specified index.
Represents a single row of the CSV file.
void setReadMsg(const std::string &read_msg)
Sets error message after row validation.
bool operator!=(const CSVRow &other) const
Unequality operator.
void writeAt(const size_t at, const std::string &value)
Replaces the value at specified index.
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-lfc.
std::string getReadMsg() const
Returns the description of the last error returned by the CSVFile::next function. ...
bool operator==(const CSVRow &other) const
Equality operator.
Provides input/output access to CSV files.
CSVFileError(const char *file, size_t line, const char *what)
string trim(const string &instring)
Trim Leading and Trailing Spaces.
void append(const T value)
Appends the value as a new column.
std::string render() const
Creates a text representation of the CSV file row.
Exception thrown when an error occurs during CSV file processing.