Kea 2.7.6
|
Provides input/output access to CSV files. More...
#include <csv_file.h>
Public Member Functions | |
CSVFile (const std::string &filename) | |
Constructor. | |
virtual | ~CSVFile () |
Destructor. | |
void | addColumn (const std::string &col_name) |
Adds new column name. | |
void | append (const CSVRow &row) const |
Writes the CSV row into the file. | |
void | close () |
Closes the CSV file. | |
bool | exists () const |
Checks if the CSV file exists and can be opened for reading. | |
void | flush () const |
Flushes a file. | |
size_t | getColumnCount () const |
Returns the number of columns in the file. | |
size_t | getColumnIndex (const std::string &col_name) const |
Returns the index of the column having specified name. | |
std::string | getColumnName (const size_t col_index) const |
Returns the name of the column. | |
std::string | getFilename () const |
Returns the path to the CSV file. | |
std::string | getReadMsg () const |
Returns the description of the last error returned by the CSVFile::next function. | |
bool | next (CSVRow &row, const bool skip_validation=false) |
Reads next row from CSV file. | |
virtual void | open (const bool seek_to_end=false) |
Opens existing file or creates a new one. | |
virtual void | recreate () |
Creates a new CSV file. | |
void | setReadMsg (const std::string &read_msg) |
Sets error message after row validation. | |
Static Public Member Functions | |
static CSVRow | EMPTY_ROW () |
Represents empty row. | |
Protected Member Functions | |
void | addColumnInternal (const std::string &col_name) |
Adds a column regardless if the file is open or not. | |
virtual bool | validate (const CSVRow &row) |
Validate the row read from a file. | |
virtual bool | validateHeader (const CSVRow &header) |
This function validates the header of the CSV file. | |
Provides input/output access to CSV files.
This class provides basic methods to access (parse) and create CSV files. The file is identified by its name qualified with the absolute path. The name of the file is passed to the constructor. Constructor doesn't open/create a file, but simply records a file name specified by a caller.
There are two functions that can be used to open a file:
open
- opens an existing file; if the file doesn't exist it creates it,recreate
- removes existing file and creates a new one.When the file is opened its header file is parsed and column names are identified. At this point it is already possible to get the list of the column names using appropriate accessors. The data rows are not parsed at this time. The row parsing is triggered by calling next
function. The result of parsing a row is stored in the CSVRow
object passed as a parameter.
When the new file is created (when recreate
is called), the CSV header is immediately written into it. The header consists of the column names specified with the addColumn
function. The subsequent rows are written into this file by calling append
.
Definition at line 358 of file csv_file.h.
isc::util::CSVFile::CSVFile | ( | const std::string & | filename | ) |
|
virtual |
Destructor.
Definition at line 118 of file csv_file.cc.
References close().
void isc::util::CSVFile::addColumn | ( | const std::string & | col_name | ) |
Adds new column name.
This column adds a new column but doesn't write it to the file yet. The name of the column will be placed in the CSV header when new file is created by calling recreate
or open
function.
col_name | Name of the column. |
CSVFileError | if a column with the specified name exists. |
Definition at line 147 of file csv_file.cc.
References addColumnInternal(), getFilename(), and isc_throw.
Referenced by isc::util::VersionedCSVFile::addColumn().
|
protected |
Adds a column regardless if the file is open or not.
This function adds as new column to the collection. It is meant to be called internally by the methods of the base class and derived classes. It must not be used in the public scope. The CSVFile::addColumn
must be used in the public scope instead, because it prevents addition of the new column when the file is open.
col_name | Name of the column. |
CSVFileError | if a column with the specified name exists. |
Definition at line 158 of file csv_file.cc.
References isc_throw.
Referenced by addColumn(), and open().
void isc::util::CSVFile::append | ( | const CSVRow & | row | ) | const |
Writes the CSV row into the file.
row | Object representing a CSV file row. |
CSVFileError | When error occurred during IO operation or if the size of the row doesn't match the number of columns. |
Definition at line 167 of file csv_file.cc.
References getColumnCount(), isc::util::CSVRow::getValuesCount(), isc_throw, and isc::util::CSVRow::render().
void isc::util::CSVFile::close | ( | ) |
Closes the CSV file.
Definition at line 123 of file csv_file.cc.
Referenced by ~CSVFile(), open(), and recreate().
|
inlinestatic |
Represents empty row.
Definition at line 491 of file csv_file.h.
Referenced by isc::dhcp::CSVLeaseFile4::next(), isc::dhcp::CSVLeaseFile6::next(), next(), and isc::util::VersionedCSVFile::next().
bool isc::util::CSVFile::exists | ( | ) | const |
Checks if the CSV file exists and can be opened for reading.
This method doesn't check if the existing file has a correct file format.
Definition at line 133 of file csv_file.cc.
Referenced by isc::lfc::LFCController::launch().
void isc::util::CSVFile::flush | ( | ) | const |
Flushes a file.
Definition at line 141 of file csv_file.cc.
|
inline |
Returns the number of columns in the file.
Definition at line 403 of file csv_file.h.
Referenced by isc::dhcp::CSVLeaseFile4::append(), isc::dhcp::CSVLeaseFile6::append(), append(), isc::util::VersionedCSVFile::getSchemaVersion(), isc::util::VersionedCSVFile::getVersionedColumn(), isc::util::VersionedCSVFile::next(), open(), isc::util::VersionedCSVFile::open(), recreate(), isc::util::VersionedCSVFile::recreate(), validate(), validateHeader(), and isc::util::VersionedCSVFile::validateHeader().
size_t isc::util::CSVFile::getColumnIndex | ( | const std::string & | col_name | ) | const |
Returns the index of the column having specified name.
This function is exception safe.
col_name | Name of the column. |
OutOfRange | if column with such name doesn't exist. |
Definition at line 237 of file csv_file.cc.
References isc_throw.
Referenced by isc::dhcp::CSVLeaseFile4::append(), isc::dhcp::CSVLeaseFile6::append(), and isc::util::VersionedCSVFile::setMinimumValidColumns().
std::string isc::util::CSVFile::getColumnName | ( | const size_t | col_index | ) | const |
Returns the name of the column.
col_index | Index of the column. |
CSVFileError | if the specified index is out of range. |
Definition at line 247 of file csv_file.cc.
References isc_throw.
Referenced by recreate(), validateHeader(), and isc::util::VersionedCSVFile::validateHeader().
|
inline |
Returns the path to the CSV file.
Definition at line 408 of file csv_file.h.
Referenced by addColumn(), isc::util::VersionedCSVFile::columnCountError(), isc::util::VersionedCSVFile::getVersionedColumn(), isc::util::VersionedCSVFile::open(), and isc::util::VersionedCSVFile::recreate().
|
inline |
Returns the description of the last error returned by the CSVFile::next
function.
Definition at line 416 of file csv_file.h.
Referenced by open().
bool isc::util::CSVFile::next | ( | CSVRow & | row, |
const bool | skip_validation = false ) |
Reads next row from CSV file.
This function will return the CSVRow
object representing a parsed row if parsing is successful. If the end of file has been reached, the empty row is returned (a row containing no values).
[out] | row | Object receiving the parsed CSV file. |
skip_validation | Do not perform validation. |
Definition at line 257 of file csv_file.cc.
References EMPTY_ROW(), isc::util::CSVRow::parse(), setReadMsg(), validate(), and isc::Exception::what().
Referenced by isc::util::VersionedCSVFile::next(), and open().
|
virtual |
Opens existing file or creates a new one.
This function will try to open existing file if this file has size greater than 0. If the file doesn't exist or has size of 0, the file is recreated. If the existing file has been opened, the header is parsed and column names are initialized in the CSVFile
object. By default, the data pointer in the file is set to the beginning of the first row. In order to retrieve the row contents the next
function should be called. If a seek_to_end
parameter is set to true, the file will be opened and the internal pointer will be set to the end of file.
seek_to_end | A boolean value which indicates if the input and output file pointer should be set at the end of file. |
CSVFileError | when IO operation fails. |
Reimplemented in isc::dhcp::CSVLeaseFile4, isc::dhcp::CSVLeaseFile6, and isc::util::VersionedCSVFile.
Definition at line 302 of file csv_file.cc.
References addColumnInternal(), close(), getColumnCount(), getReadMsg(), isc::util::CSVRow::getValuesCount(), isc_throw, next(), isc::util::CSVRow::readAt(), recreate(), and validateHeader().
Referenced by isc::util::VersionedCSVFile::open().
|
virtual |
Creates a new CSV file.
The file creation will fail if there are no columns specified. Otherwise, this function will write the header to the file. In order to write rows to opened file, the append
function should be called.
Reimplemented in isc::util::VersionedCSVFile.
Definition at line 370 of file csv_file.cc.
References close(), getColumnCount(), getColumnName(), isc_throw, and isc::util::CSVRow::writeAt().
Referenced by open(), and isc::util::VersionedCSVFile::recreate().
|
inline |
Sets error message after row validation.
The CSVFile::validate
function is responsible for setting the error message after validation of the row read from the CSV file. It will use this function to set this message. Note, that the validate
function can set a message after successful validation too. Such message could say "success", or something similar.
read_msg | Error message to be set. |
Definition at line 486 of file csv_file.h.
Referenced by isc::util::VersionedCSVFile::columnCountError(), isc::dhcp::CSVLeaseFile4::next(), isc::dhcp::CSVLeaseFile6::next(), next(), isc::util::VersionedCSVFile::next(), validate(), and isc::util::VersionedCSVFile::validateHeader().
|
protectedvirtual |
Validate the row read from a file.
This function implements a basic validation for the row read from the CSV file. It is virtual so as it may be customized in derived classes.
This default implementation checks that the number of values in the row corresponds to the number of columns specified for this file.
If row validation fails, the error message is noted and can be retrieved using CSVFile::getReadMsg
. The function which overrides this base implementation is responsible for setting the error message using CSVFile::setReadMsg
.
row | A row to be validated. |
Definition at line 401 of file csv_file.cc.
References getColumnCount(), isc::util::CSVRow::getValuesCount(), and setReadMsg().
Referenced by next().
|
protectedvirtual |
This function validates the header of the CSV file.
If there are any columns added to the CSVFile
object, it will compare that they exactly match (including order) the header read from the file.
This function is called internally by CSVFile::open. Derived classes may add extra validation steps.
header | A row holding a header. |
Reimplemented in isc::util::VersionedCSVFile.
Definition at line 415 of file csv_file.cc.
References getColumnCount(), getColumnName(), isc::util::CSVRow::getValuesCount(), and isc::util::CSVRow::readAt().
Referenced by open().