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
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
18namespace isc {
19namespace util {
20
22class CSVFileError : public Exception {
23public:
24 CSVFileError(const char* file, size_t line, const char* what) :
25 isc::Exception(file, line, what) { };
26};
27
51class CSVRow {
52public:
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) {
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
237private:
238
245 void checkIndex(const size_t at) const;
246
253 std::string separator_;
254
256 std::vector<std::string> values_;
257};
258
266std::ostream& operator<<(std::ostream& os, const CSVRow& row);
267
290class CSVFile {
291public:
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
428protected:
429
441 void addColumnInternal(const std::string& col_name);
442
459 virtual bool validate(const CSVRow& row);
460
461protected:
462
474 virtual bool validateHeader(const CSVRow& header);
475
476private:
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
This is a base class for exceptions thrown from the DNS library module.
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.
Definition: csv_file.h:22
CSVFileError(const char *file, size_t line, const char *what)
Definition: csv_file.h:24
Provides input/output access to CSV files.
Definition: csv_file.h:290
std::string getColumnName(const size_t col_index) const
Returns the name of the column.
Definition: csv_file.cc:216
void close()
Closes the CSV file.
Definition: csv_file.cc:92
size_t getColumnCount() const
Returns the number of columns in the file.
Definition: csv_file.h:335
virtual ~CSVFile()
Destructor.
Definition: csv_file.cc:87
bool exists() const
Checks if the CSV file exists and can be opened for reading.
Definition: csv_file.cc:102
virtual bool validate(const CSVRow &row)
Validate the row read from a file.
Definition: csv_file.cc:364
static CSVRow EMPTY_ROW()
Represents empty row.
Definition: csv_file.h:423
void setReadMsg(const std::string &read_msg)
Sets error message after row validation.
Definition: csv_file.h:418
std::string getFilename() const
Returns the path to the CSV file.
Definition: csv_file.h:340
void flush() const
Flushes a file.
Definition: csv_file.cc:110
virtual bool validateHeader(const CSVRow &header)
This function validates the header of the CSV file.
Definition: csv_file.cc:378
void addColumnInternal(const std::string &col_name)
Adds a column regardless if the file is open or not.
Definition: csv_file.cc:127
virtual void recreate()
Creates a new CSV file.
Definition: csv_file.cc:333
std::string getReadMsg() const
Returns the description of the last error returned by the CSVFile::next function.
Definition: csv_file.h:348
void append(const CSVRow &row) const
Writes the CSV row into the file.
Definition: csv_file.cc:136
void addColumn(const std::string &col_name)
Adds new column name.
Definition: csv_file.cc:116
size_t getColumnIndex(const std::string &col_name) const
Returns the index of the column having specified name.
Definition: csv_file.cc:206
virtual void open(const bool seek_to_end=false)
Opens existing file or creates a new one.
Definition: csv_file.cc:265
bool next(CSVRow &row, const bool skip_validation=false)
Reads next row from CSV file.
Definition: csv_file.cc:226
Represents a single row of the CSV file.
Definition: csv_file.h:51
T readAndConvertAt(const size_t at) const
Retrieves a value from the internal container.
Definition: csv_file.h:135
std::string render() const
Creates a text representation of the CSV file row.
Definition: csv_file.cc:45
size_t getValuesCount() const
Returns number of values in a CSV row.
Definition: csv_file.h:85
void trim(const size_t count)
Trims a given number of elements from the end of a row.
Definition: csv_file.cc:64
void writeAt(const size_t at, const char *value)
Replaces the value at specified index.
Definition: csv_file.cc:58
std::string readAt(const size_t at) const
Retrieves a value from the internal container.
Definition: csv_file.cc:39
bool operator!=(const CSVRow &other) const
Unequality operator.
Definition: csv_file.h:233
bool operator==(const CSVRow &other) const
Equality operator.
Definition: csv_file.h:223
void writeAt(const size_t at, const std::string &value)
Replaces the value at specified index.
Definition: csv_file.h:177
void append(const T value)
Appends the value as a new column.
Definition: csv_file.h:186
void parse(const std::string &line)
Parse the CSV file row.
Definition: csv_file.cc:31
void writeAt(const size_t at, const T value)
Replaces the value at specified index.
Definition: csv_file.h:206
#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.
Definition: csv_file.cc:69
Defines the logger used by the top-level component of kea-dhcp-ddns.