Kea  1.5.0
csv_file.h
Go to the documentation of this file.
1 // Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef CSV_FILE_H
8 #define CSV_FILE_H
9 
10 #include <exceptions/exceptions.h>
11 #include <boost/lexical_cast.hpp>
12 #include <boost/shared_ptr.hpp>
13 #include <fstream>
14 #include <ostream>
15 #include <string>
16 #include <vector>
17 
18 namespace isc {
19 namespace util {
20 
22 class CSVFileError : public Exception {
23 public:
24  CSVFileError(const char* file, size_t line, const char* what) :
25  isc::Exception(file, line, what) { };
26 };
27 
51 class CSVRow {
52 public:
53 
67  CSVRow(const size_t cols = 0, const char separator = ',');
68 
82  CSVRow(const std::string& text, const char separator = ',');
83 
85  size_t getValuesCount() const {
86  return (values_.size());
87  }
88 
98  void parse(const std::string& line);
99 
110  std::string readAt(const size_t at) const;
111 
118  void trim(const size_t count);
119 
134  template<typename T>
135  T readAndConvertAt(const size_t at) const {
136  T cast_value;
137  try {
138  cast_value = boost::lexical_cast<T>(readAt(at).c_str());
139 
140  } catch (const boost::bad_lexical_cast& ex) {
141  isc_throw(CSVFileError, ex.what());
142  }
143  return (cast_value);
144  }
145 
155  std::string render() const;
156 
166  void writeAt(const size_t at, const char* value);
167 
177  void writeAt(const size_t at, const std::string& value) {
178  writeAt(at, value.c_str());
179  }
180 
185  template<typename T>
186  void append(const T value) {
187  try {
188  values_.push_back(boost::lexical_cast<std::string>(value));
189  } catch (const boost::bad_lexical_cast& ex) {
190  isc_throw(CSVFileError, "unable to stringify the value to be "
191  "appended to the CSV file row.");
192  }
193  }
194 
205  template<typename T>
206  void writeAt(const size_t at, const T value) {
207  checkIndex(at);
208  try {
209  values_[at] = boost::lexical_cast<std::string>(value);
210  } catch (const boost::bad_lexical_cast& ex) {
211  isc_throw(CSVFileError, "unable to stringify the value to be"
212  " written in the CSV file row at position '"
213  << at << "'");
214  }
215  }
216 
223  bool operator==(const CSVRow& other) const {
224  return (render() == other.render());
225  }
226 
233  bool operator!=(const CSVRow& other) const {
234  return (render() != other.render());
235  }
236 
237 private:
238 
245  void checkIndex(const size_t at) const;
246 
253  std::string separator_;
254 
256  std::vector<std::string> values_;
257 };
258 
266 std::ostream& operator<<(std::ostream& os, const CSVRow& row);
267 
290 class CSVFile {
291 public:
292 
296  CSVFile(const std::string& filename);
297 
299  virtual ~CSVFile();
300 
310  void addColumn(const std::string& col_name);
311 
318  void append(const CSVRow& row) const;
319 
321  void close();
322 
329  bool exists() const;
330 
332  void flush() const;
333 
335  size_t getColumnCount() const {
336  return (cols_.size());
337  }
338 
340  std::string getFilename() const {
341  return (filename_);
342  }
343 
348  std::string getReadMsg() const {
349  return (read_msg_);
350  }
351 
359  size_t getColumnIndex(const std::string& col_name) const;
360 
367  std::string getColumnName(const size_t col_index) const;
368 
380  bool next(CSVRow& row, const bool skip_validation = false);
381 
398 
399  virtual void open(const bool seek_to_end = false);
400 
407  virtual void recreate();
408 
418  void setReadMsg(const std::string& read_msg) {
419  read_msg_ = read_msg;
420  }
421 
423  static CSVRow EMPTY_ROW() {
424  static CSVRow row(0);
425  return (row);
426  }
427 
428 protected:
429 
441  void addColumnInternal(const std::string& col_name);
442 
459  virtual bool validate(const CSVRow& row);
460 
461 protected:
462 
474  virtual bool validateHeader(const CSVRow& header);
475 
476 private:
486  void checkStreamStatusAndReset(const std::string& operation) const;
487 
489  std::streampos size() const;
490 
492  std::string filename_;
493 
495  boost::shared_ptr<std::fstream> fs_;
496 
498  std::vector<std::string> cols_;
499 
501  std::string read_msg_;
502 };
503 
504 } // namespace isc::util
505 } // namespace isc
506 
507 #endif // CSV_FILE_H
isc::util::CSVFile::flush
void flush() const
Flushes a file.
Definition: csv_file.cc:110
isc::util::CSVRow::writeAt
void writeAt(const size_t at, const T value)
Replaces the value at specified index.
Definition: csv_file.h:206
isc::util::CSVRow::operator!=
bool operator!=(const CSVRow &other) const
Unequality operator.
Definition: csv_file.h:233
isc::util::CSVRow::operator==
bool operator==(const CSVRow &other) const
Equality operator.
Definition: csv_file.h:223
isc::util::CSVFile::open
virtual void open(const bool seek_to_end=false)
Opens existing file or creates a new one.
Definition: csv_file.cc:265
isc::util::CSVRow::writeAt
void writeAt(const size_t at, const std::string &value)
Replaces the value at specified index.
Definition: csv_file.h:177
isc::util::CSVFile::addColumn
void addColumn(const std::string &col_name)
Adds new column name.
Definition: csv_file.cc:116
isc::util::CSVFile::getReadMsg
std::string getReadMsg() const
Returns the description of the last error returned by the CSVFile::next function.
Definition: csv_file.h:348
isc::util::CSVFile::exists
bool exists() const
Checks if the CSV file exists and can be opened for reading.
Definition: csv_file.cc:102
isc::util::CSVFile::validateHeader
virtual bool validateHeader(const CSVRow &header)
This function validates the header of the CSV file.
Definition: csv_file.cc:378
isc::util::CSVFile::setReadMsg
void setReadMsg(const std::string &read_msg)
Sets error message after row validation.
Definition: csv_file.h:418
isc::util::CSVFile::append
void append(const CSVRow &row) const
Writes the CSV row into the file.
Definition: csv_file.cc:136
isc::Exception
This is a base class for exceptions thrown from the DNS library module.
Definition: exceptions/exceptions.h:23
isc::util::CSVFile::getColumnCount
size_t getColumnCount() const
Returns the number of columns in the file.
Definition: csv_file.h:335
isc::util::CSVFile::validate
virtual bool validate(const CSVRow &row)
Validate the row read from a file.
Definition: csv_file.cc:364
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition: exceptions/exceptions.cc:32
isc::util::CSVRow::writeAt
void writeAt(const size_t at, const char *value)
Replaces the value at specified index.
Definition: csv_file.cc:58
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
isc::util::CSVRow::readAt
std::string readAt(const size_t at) const
Retrieves a value from the internal container.
Definition: csv_file.cc:39
isc::util::CSVFile::recreate
virtual void recreate()
Creates a new CSV file.
Definition: csv_file.cc:333
isc::util::CSVRow
Represents a single row of the CSV file.
Definition: csv_file.h:51
isc::util::CSVFile
Provides input/output access to CSV files.
Definition: csv_file.h:290
isc::util::CSVFile::getFilename
std::string getFilename() const
Returns the path to the CSV file.
Definition: csv_file.h:340
isc::util::CSVFile::next
bool next(CSVRow &row, const bool skip_validation=false)
Reads next row from CSV file.
Definition: csv_file.cc:226
isc::util::CSVRow::readAndConvertAt
T readAndConvertAt(const size_t at) const
Retrieves a value from the internal container.
Definition: csv_file.h:135
isc::util::CSVRow::CSVRow
CSVRow(const size_t cols=0, const char separator=',')
Constructor, creates the raw to be used for output.
Definition: csv_file.cc:20
isc::util::CSVRow::append
void append(const T value)
Appends the value as a new column.
Definition: csv_file.h:186
isc::util::CSVRow::render
std::string render() const
Creates a text representation of the CSV file row.
Definition: csv_file.cc:45
isc::util::CSVFileError::CSVFileError
CSVFileError(const char *file, size_t line, const char *what)
Definition: csv_file.h:24
isc::util::operator<<
std::ostream & operator<<(std::ostream &os, const CSVRow &row)
Overrides standard output stream operator for CSVRow object.
Definition: csv_file.cc:69
isc::util::CSVFile::~CSVFile
virtual ~CSVFile()
Destructor.
Definition: csv_file.cc:87
isc::util::CSVFile::getColumnName
std::string getColumnName(const size_t col_index) const
Returns the name of the column.
Definition: csv_file.cc:216
isc::util::CSVFileError
Exception thrown when an error occurs during CSV file processing.
Definition: csv_file.h:22
isc::util::CSVRow::getValuesCount
size_t getValuesCount() const
Returns number of values in a CSV row.
Definition: csv_file.h:85
exceptions.h
isc::util::CSVRow::parse
void parse(const std::string &line)
Parse the CSV file row.
Definition: csv_file.cc:31
isc::util::CSVFile::addColumnInternal
void addColumnInternal(const std::string &col_name)
Adds a column regardless if the file is open or not.
Definition: csv_file.cc:127
isc::util::CSVFile::CSVFile
CSVFile(const std::string &filename)
Constructor.
Definition: csv_file.cc:83
isc::util::CSVFile::getColumnIndex
size_t getColumnIndex(const std::string &col_name) const
Returns the index of the column having specified name.
Definition: csv_file.cc:206
isc::util::CSVRow::trim
void trim(const size_t count)
Trims a given number of elements from the end of a row.
Definition: csv_file.cc:64
isc::util::CSVFile::EMPTY_ROW
static CSVRow EMPTY_ROW()
Represents empty row.
Definition: csv_file.h:423
isc::util::CSVFile::close
void close()
Closes the CSV file.
Definition: csv_file.cc:92