|
cgv
|
csv reader for parsing csv files to a specific type T that can store one object with different entries per csv column. More...
#include <csv_reader.h>
Public Member Functions | |
| csv_reader (const std::string &file_name, CSV_Flags _flags=CSV_DEFAULT) | |
| construct from csv file with default flags (comma or tab as separator plus heading line) and extract column names from heading line | |
| bool | add_entry (size_t col_index, std::function< bool(const std::string &, T &)> s) |
| add new entry based on column index (0, 1, ...) with custom setter function | |
| template<typename M > | |
| bool | add_entry (M T::*ptr, size_t col_index) |
| add new entry based on column index (0, 1, ...) with default setter for member type | |
| template<typename M > | |
| bool | add_entry (M T::*ptr, const std::string &col_name) |
| add new entry based on heading of column (assumes construction with has_heading=true) with default setter for member type | |
| bool | add_entry (const std::string &col_name, std::function< bool(const std::string &, T &)> s) |
| add new entry based on heading of column with custom setter function | |
| bool | parse (std::vector< T > &data) |
| parse csv file content into given data vector extracting added entries into members of objects of type T | |
Public Member Functions inherited from cgv::utils::csv_reader_base | |
| bool | fail () const |
Protected Types | |
| typedef std::pair< size_t, std::function< bool(const std::string &, T &)> > | setter |
Protected Attributes | |
| std::vector< setter > | setters |
Protected Attributes inherited from cgv::utils::csv_reader_base | |
| std::string | last_error |
| bool | failed = false |
| size_t | li = 0 |
| size_t | nr_cols = 0 |
| CSV_Flags | flags |
| std::string | content |
| std::vector< std::string > | col_names |
| std::vector< cgv::utils::line > | lines |
Additional Inherited Members | |
Protected Member Functions inherited from cgv::utils::csv_reader_base | |
| const char * | get_ws () const |
| const char * | get_sep () const |
| bool | has_heading () const |
| bool | find_column_index (const std::string &col_name, size_t &col_index) const |
| csv_reader_base (const std::string &file_name, CSV_Flags _flags=CSV_DEFAULT) | |
| bool | parse_next_line (std::vector< cgv::utils::token > &tokens) const |
csv reader for parsing csv files to a specific type T that can store one object with different entries per csv column.
For usage simply construct reader from file name and flags, and first check for failure
struct object_type { int a; std::string s; double custom_time; }; csv_reader<object_type> cr(file_name, my_flags); if (cr.fail()) return false;
next add entries for each member to be extracted from a csv column
if (!( cr.add_entry(&object_type::a, "a") && cr.add_entry(&object_type::s, "s") && cr.add_entry("time", [](const std::string& token, object_type& obj)->bool { parse token into obj.custom_time return whether parsing was successful return true; }) ) ) return false;
finally parse all lines in the csv file
std::vector<object_type> objects; if (!cr.parse(objects)) return false;
Definition at line 113 of file csv_reader.h.
|
protected |
Definition at line 116 of file csv_reader.h.
|
inline |
construct from csv file with default flags (comma or tab as separator plus heading line) and extract column names from heading line
Definition at line 120 of file csv_reader.h.
|
inline |
add new entry based on heading of column with custom setter function
Definition at line 143 of file csv_reader.h.
References cgv::utils::csv_reader< T >::add_entry().
|
inline |
add new entry based on heading of column (assumes construction with has_heading=true) with default setter for member type
Definition at line 136 of file csv_reader.h.
References cgv::utils::csv_reader< T >::add_entry().
|
inline |
add new entry based on column index (0, 1, ...) with default setter for member type
Definition at line 128 of file csv_reader.h.
References cgv::utils::csv_reader< T >::add_entry(), and cgv::utils::from_string().
|
inline |
add new entry based on column index (0, 1, ...) with custom setter function
Definition at line 122 of file csv_reader.h.
Referenced by cgv::utils::csv_reader< T >::add_entry(), cgv::utils::csv_reader< T >::add_entry(), and cgv::utils::csv_reader< T >::add_entry().
|
inline |
parse csv file content into given data vector extracting added entries into members of objects of type T
Definition at line 150 of file csv_reader.h.
References cgv::utils::to_string().
|
protected |
Definition at line 117 of file csv_reader.h.