csvpp on Github
API Documentation
csvpp is a collection of RFC 4180 compliant CSV readers and writers. All 3 modules are totally independent of each other, so feel free to use whichever best suits you application
csv.hpp - A c++ header-only library
Reader
- Read in rows of data as std::vectors or std::tuples, a stream, field-by-field, iterators, or with variadic argmuents
- Template type conversion available for all of the above (when reading a vector, the whole row will be converted to the same type)
Some example usages:
…
std::ifstream mycsv_file{"mycsv.csv"};
for(auto && row: mycsv)
{
for(auto && column: row)
{
}
}
char a;
std::string b;
My_type c;
auto row = mycsv3.get_row();
row >> a >> b >> c;
…
row = mycsv3.get_row();
row.read_v(a, b, c);
…
row = mycsv3.get_row();
auto row_tuple = row.read_tuple<char, std::string, My_type>();
Parses CSV data.
Definition csv.hpp:243
static constexpr input_string_t input_string
Disambiguation tag.
Definition csv.hpp:620
std::vector< std::vector< T > > read_all()
Read entire CSV data into a vector of vectors.
Definition csv.hpp:842
Writer
- Write data as vectors, tuples, a stream, field-by-field, iterators, or variadicaly
Some example usages:
std::ofstream mycsv_file{"mycsv.csv"};
std::vector row = …;
mycsv.
write_row(std::begin(row), std::end(row));
mycsv.write_row(row);
int field1 = …;
std::string field2 = …;
My_type field3 = …";
mycsv << field1 << field2 << field3 << csv::end_row;
mycsv.write_row_v(field1, field2, field3);
CSV writer.
Definition csv.hpp:1285
void write_row(Iter first, Iter last)
Definition csv.hpp:1518
Map_reader_iter / Map_writer_iter
- Read / Write rows as std::maps with column headers as keys Some example usages:
{
assert((*row)["Col1"] == "A");
for(auto && [k,v]: *row)
{
std::cout<<k<<": "<<v<<"\n";
}
}
std::map<std::string, int> row;
row["Col1"] = 1;
row["Col2"] = 2;
row["Col3"] = 3;
*mycsv++ = row;
Map-based Reader iterator.
Definition csv.hpp:1088
Map-based Writer iterator.
Definition csv.hpp:1639
csv.h - A C CSV library
CSV_reader
Some example usages:
…
CSV_row * row = NULL;
{
if(num_fields >= 1)
{
free(field);
}
my_process_row(row);
}
while(1)
{
if(!field)
{
break;
}
free(field);
}
CSV_reader * CSV_reader_init_from_filename(const char *filename)
Create a new CSV_reader object parsing from a file.
const char * CSV_reader_get_error_msg(const CSV_reader *reader)
Get message for last error.
CSV_row * CSV_reader_read_row(CSV_reader *reader)
Read the current row from the CSV file and advance to the next.
void CSV_reader_free(CSV_reader *reader)
Free a CSV_reader object.
char * CSV_reader_read_field(CSV_reader *reader)
Read a single field.
CSV_status CSV_reader_get_error(const CSV_reader *reader)
Get error code for last error.
bool CSV_reader_end_of_row(const CSV_reader *reader)
Check for end of current row.
CSV_reader * CSV_reader_init_from_str(const char *input)
Create a new CSV_reader object parsing from an in-memory string.
size_t CSV_row_size(const CSV_row *rec)
Get CSV_Record array size.
const char *const * CSV_row_arr(const CSV_row *rec)
CSV_row array access.
void CSV_row_free(CSV_row *rec)
Free a CSV_row.
const char * CSV_row_get(const CSV_row *rec, size_t i)
CSV_row array element access.
char * CSV_strdup(const char *src)
strdup implementation, in case it's not implemented in string.h
@ CSV_EOF
Reached end of file.
Definition csv.h:47
CSV_writer
Some example usages:
…
char * data[10];
…
CSV_row * CSV_row_init(void)
Create a new CSV_row.
void CSV_row_append(CSV_row *rec, char *field)
Append a new field.
CSV_status CSV_writer_write_row_ptr(CSV_writer *writer, const char *const *fields, const size_t num_fields)
Write an array of strings as a row.
CSV_status CSV_writer_end_row(CSV_writer *writer)
End the current row.
CSV_status CSV_writer_write_row_v(CSV_writer *writer,...)
Write a row from the given variadic parameters.
CSV_status CSV_writer_write_row(CSV_writer *writer, const CSV_row *fields)
Write a CSV_row.
CSV_writer * CSV_writer_init_from_filename(const char *filename)
Create a new CSV_writer object writing to a file.
void CSV_writer_free(CSV_writer *writer)
Free a CSV_writer object.
CSV_status CSV_writer_write_field(CSV_writer *writer, const char *field)
Write a single field.
embcsv.h - A stripped-down CSV reader ideal for embedded environments
- Allows reading input character-by-character, so unbuffered input may be read
- Compile with EMBCSV_NO_MALLOC to prevent heap memory allocation
Some example usages:
…
while(!my_done_reading_serial())
{
char c = my_wait_for_serial_byte();
const char * field_out = NULL;
my_handle_error();
{
my_process_field(field);
my_process_end_of_row();
}
}
CSV library for embedded environments.
void EMBCSV_reader_free(EMBCSV_reader *r)
Free an EMBCSV_reader.
EMBCSV_reader * EMBCSV_reader_init(void)
Create a new EMBCSV_reader with default settings.
EMBCSV_result
Parser result type.
Definition embcsv.h:93
EMBCSV_result EMBCSV_reader_parse_char(EMBCSV_reader *r, int c, const char **field_out)
Parse a character.
@ EMBCSV_PARSE_ERROR
Syntax error found in input.
Definition embcsv.h:97
@ EMBCSV_END_OF_ROW
A field has been parsed, and parsing has reached the end of a row.
Definition embcsv.h:96
@ EMBCSV_FIELD
A field has been parsed.
Definition embcsv.h:95
Compiling & Installation
Requirements:
- CMake
- A C++ compiler with C++17 support
- A C compiler with C99 support
- CMake 3.14.2 or higher (only required if installing or building tests / examples)
- No external libraries required!
Installation is not strictly required. You can directly include the relevant files into your own project:
- C++ library
- C library
- Embedded C library
To build and install:
mkdir build
cd build
cmake .. <build option arguments>
make
# installation
make install
# OR
cpack
Build options
-DCSVPP_ENABLE_CPP=1
- Enable C++ library. Enabled by default
-DCSVPP_ENABLE_C=1
- Enable C library.
-DCSVPP_ENABLE_EMBEDDED=1
- Enable embedded C library.
-DCSVPP_ENABLE_ALL=1
- Enable all libraries
-DCSVPP_EMBEDDED_NO_MALLOC=1
- Disable heap allocation for embedded library
-DCSVPP_ENABLE_EXAMPLES=1
- Enable example utility programs
-DCSVPP_INTERNAL_DOCS=1
- Include private method documentation for doc target
-DBUILD_TESTING=1
- Enable tests
Including libraries in your project
If installed, pkg-config and CMake files will be installed.
To include libraries with pkg-config:
- C++ library
pkg-config --cflags csvpp
- C library
pkg-config --cflags --libs csvpp-c
- Embedded C library
pkg-config --cflags --libs csvpp-embcsv
To include libraries with CMake:
find_package(csvpp)
add_executable(myproj …)
target_link_libraries(myproj … csvpp::csvpp) # for C++ library
target_link_libraries(myproj … csvpp::csv) # for C library
target_link_libraries(myproj … csvpp::embcsv) # for embedded C library
Or, if you have included the csvpp as a submodule of your project:
add_subdirectory(csvpp)
add_executable(myproj …)
target_link_libraries(myproj … csvpp::csvpp) # for C++ library
target_link_libraries(myproj … csvpp::csv) # for C library
target_link_libraries(myproj … csvpp::embcsv) # for embedded C library
Testing
To run test suite, generate with -DBUILD_TESTING=1
and make test
Documentation
Documentation can be built with doxygen (if installed). make doc