CSV Parser for Arduino
|
#include <CSV_Parser.h>
Public Member Functions | |
CSV_Parser (const char *s, const char *fmt, bool has_header=true, char delimiter=',', char quote_char='"') | |
CSV_Parser (const char *s, const char *fmt, bool hh, char d, const char *qc) | |
Additional constructor to allow supplying quote char as a string. Why? Because supplied quote char is likely to be a single-quote, which would require escaping using backslash if it was supplied as char. I bet some people would rather use "'" instead of '\'' More... | |
CSV_Parser (const char *fmt_, bool hh=true, char d=',', char qc='"') | |
Constructor for supplying csv string by chunks. More... | |
CSV_Parser (const char *fmt_, bool hh, char d, const char *qc) | |
~CSV_Parser () | |
Releases all dynamically allocated memory. Making values unusable once the CSV_Parser goes out of scope. More... | |
bool | readSDfile (const char *f_name) |
Reads file from SD card. More... | |
int | getColumnsCount () |
int | getRowsCount () |
Excluding header (if it was part of supplied CSV). More... | |
void * | getValues (const char *key) |
Gets values given the column key name. More... | |
void * | getValues (int col_index) |
Gets values given the column index. More... | |
void * | operator[] (const char *key) |
It's the same as GetValues(key) but allows to use operator instead of method call, like: int32_t * my_values = (int32_t*)cp["my_key"]; More... | |
void * | operator[] (int col_index) |
It's the same as GetValues(col_index) but allows to use operator instead of method call, like: int32_t * my_values = (int32_t*)cp[0]; More... | |
void | printKeys (Stream &ser=Serial) |
void | print (Stream &ser=Serial) |
Prints whole parsed content including: More... | |
CSV_Parser & | operator<< (const char *s) |
It's the same as supplyChunk(s) but allows to use operator instead of method call, like: cp << "my_strings,my_ints\n" << "hello,1\n" << "world,2\n"; More... | |
CSV_Parser & | operator<< (char c) |
Example: cp << 'a' << ',' << 'b'; More... | |
void | parseLeftover () |
Forces the previously supplied (but not parsed) chunks to be parsed despite not ending with "\n" or "\r\n" or delimiter. This function should be called after full csv string is supplied with repetitive supplyChunk method calls. If the csv string ended with "\n" or "\r\n" then endChunks() call is not necessary. If the csv string did not end with "\n" or "\r\n" then endChunks() must be called, otherwise the last row won't be returned when using "GetValues". More... | |
CSV_Parser::CSV_Parser | ( | const char * | s, |
const char * | fmt, | ||
bool | has_header = true , |
||
char | delimiter = ',' , |
||
char | quote_char = '"' |
||
) |
s | - string containing csv |
fmt | - string containing format specifiers for each column Acceptable format types are: s - string (C-like string, not a "String" Arduino object, just a char pointer, terminated by 0) f - float L - int32_t (32-bit signed value, can't be used for values over 2147483647) d - int16_t (16-bit signed value, can't be used for values over 32767) c - char (8-bit signed value, can't be used for values over 127) x - hex (stored as int32_t) "-" (dash character) means that value is unused/not-parsed (this way memory won't be allocated for values from that column) |
has_header | (optional) - If the supplied csv string does not have header line then "false" may be supplied |
delimiter | (optional) - It's a character that separates values. By default it's a comma. If the delimiter is not a comma (e.g. if it's ";" or "\t" instead) then it may be supplied. |
quote_char | (optional) - Quote char allows to include delimiter or new line characters to be part of the string value itself. By default it's double quote character. |
|
inline |
Additional constructor to allow supplying quote char as a string. Why?
Because supplied quote char is likely to be a single-quote, which would require escaping using backslash if it was supplied as char. I bet some people would rather use "'" instead of '\''
|
inline |
Constructor for supplying csv string by chunks.
|
inline |
CSV_Parser::~CSV_Parser | ( | ) |
Releases all dynamically allocated memory.
Making values unusable once the CSV_Parser goes out of scope.
int CSV_Parser::getColumnsCount | ( | ) |
int CSV_Parser::getRowsCount | ( | ) |
Excluding header (if it was part of supplied CSV).
void* CSV_Parser::getValues | ( | const char * | key | ) |
Gets values given the column key name.
key | - column name |
void* CSV_Parser::getValues | ( | int | col_index | ) |
Gets values given the column index.
col_index | - column number (starting with 0) |
CSV_Parser & CSV_Parser::operator<< | ( | char | c | ) |
Example:
cp << 'a' << ',' << 'b';
CSV_Parser & CSV_Parser::operator<< | ( | const char * | s | ) |
It's the same as supplyChunk(s) but allows to use operator instead of method call, like:
cp << "my_strings,my_ints\n" << "hello,1\n" << "world,2\n";
void * CSV_Parser::operator[] | ( | const char * | key | ) |
It's the same as GetValues(key) but allows to use operator instead of method call, like:
int32_t * my_values = (int32_t*)cp["my_key"];
key | - column name |
void * CSV_Parser::operator[] | ( | int | col_index | ) |
It's the same as GetValues(col_index) but allows to use operator instead of method call, like:
int32_t * my_values = (int32_t*)cp[0];
col_index | - column index (0 being the first column) |
void CSV_Parser::parseLeftover | ( | ) |
Forces the previously supplied (but not parsed) chunks to be parsed despite not ending with "\n" or "\r\n" or delimiter.
This function should be called after full csv string is supplied with repetitive supplyChunk method calls.
If the csv string ended with "\n" or "\r\n" then endChunks() call is not necessary.
If the csv string did not end with "\n" or "\r\n" then endChunks() must be called, otherwise the last row won't be returned when using "GetValues".
void CSV_Parser::print | ( | Stream & | ser = Serial | ) |
Prints whole parsed content including:
ser | (optional) - Stream object like "Serial" (by default) or "Serial1". For example, it allows to supply "Serial1" or an object of "SoftwareSerial.h" library. |
void CSV_Parser::printKeys | ( | Stream & | ser = Serial | ) |
bool CSV_Parser::readSDfile | ( | const char * | f_name | ) |
Reads file from SD card.
f_name | - file name (provided file must have format that was supplied in CSV_Parser constructor) |