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;
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) {
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 '"
290 const std::string& characters);
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;
334std::ostream&
operator<<(std::ostream& os,
const CSVRow& row);
364 CSVFile(
const std::string& filename);
378 void addColumn(
const std::string& col_name);
404 return (cols_.size());
448 bool next(
CSVRow& row,
const bool skip_validation =
false);
467 virtual void open(
const bool seek_to_end =
false);
487 read_msg_ = read_msg;
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_;
then both cases could be regarded as being with the completion being signalled by the posting of the completion event However UDP is the most common case and that would involve extra overhead So the open() returns a status indicating whether the operation completed asynchronously. If it did
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Exception thrown when an error occurs during CSV file processing.
CSVFileError(const char *file, size_t line, const char *what)
std::string getColumnName(const size_t col_index) const
Returns the name of the column.
void close()
Closes the CSV file.
size_t getColumnCount() const
Returns the number of columns in the file.
virtual ~CSVFile()
Destructor.
bool exists() const
Checks if the CSV file exists and can be opened for reading.
virtual bool validate(const CSVRow &row)
Validate the row read from a file.
static CSVRow EMPTY_ROW()
Represents empty row.
void setReadMsg(const std::string &read_msg)
Sets error message after row validation.
CSVFile(const std::string &filename)
Constructor.
std::string getFilename() const
Returns the path to the CSV file.
void flush() const
Flushes a file.
virtual bool validateHeader(const CSVRow &header)
This function validates the header of the CSV file.
void addColumnInternal(const std::string &col_name)
Adds a column regardless if the file is open or not.
virtual void recreate()
Creates a new CSV file.
std::string getReadMsg() const
Returns the description of the last error returned by the CSVFile::next function.
void append(const CSVRow &row) const
Writes the CSV row into the file.
void addColumn(const std::string &col_name)
Adds new column name.
size_t getColumnIndex(const std::string &col_name) const
Returns the index of the column having specified name.
bool next(CSVRow &row, const bool skip_validation=false)
Reads next row from CSV file.
Represents a single row of the CSV file.
T readAndConvertAt(const size_t at) const
Retrieves a value from the internal container.
std::string render() const
Creates a text representation of the CSV file row.
static std::string unescapeCharacters(const std::string &escaped_str)
Returns a copy of a string with special characters unescaped.
std::string readAtEscaped(const size_t at) const
Retrieves a value from the internal container, free of escaped characters.
size_t getValuesCount() const
Returns number of values in a CSV row.
void trim(const size_t count)
Trims a given number of elements from the end of a row.
CSVRow(const size_t cols=0, const char separator=',')
Constructor, creates the raw to be used for output.
void writeAt(const size_t at, const char *value)
Replaces the value at specified index.
static std::string escapeCharacters(const std::string &orig_str, const std::string &characters)
Returns a copy of a string with special characters escaped.
std::string readAt(const size_t at) const
Retrieves a value from the internal container.
bool operator!=(const CSVRow &other) const
Unequality operator.
bool operator==(const CSVRow &other) const
Equality operator.
void writeAt(const size_t at, const std::string &value)
Replaces the value at specified index.
void append(const T value)
Appends the value as a new column.
void writeAtEscaped(const size_t at, const std::string &value)
Replaces the value at the specified index with a value that has had special characters escaped.
void parse(const std::string &line)
Parse the CSV file row.
void writeAt(const size_t at, const T value)
Replaces the value at specified index.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::ostream & operator<<(std::ostream &os, const CSVRow &row)
Overrides standard output stream operator for CSVRow object.
Defines the logger used by the top-level component of kea-lfc.