CSV Parser for Arduino
Public Member Functions | List of all members
CSV_Parser Class Reference

#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...
 
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_Parseroperator<< (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_Parseroperator<< (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...
 

Constructor & Destructor Documentation

◆ CSV_Parser() [1/4]

CSV_Parser::CSV_Parser ( const char *  s,
const char *  fmt,
bool  has_header = true,
char  delimiter = ',',
char  quote_char = '"' 
)
Parameters
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.

◆ CSV_Parser() [2/4]

CSV_Parser::CSV_Parser ( const char *  s,
const char *  fmt,
bool  hh,
char  d,
const char *  qc 
)
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 '\''

◆ CSV_Parser() [3/4]

CSV_Parser::CSV_Parser ( const char *  fmt_,
bool  hh = true,
char  d = ',',
char  qc = '"' 
)
inline

Constructor for supplying csv string by chunks.

◆ CSV_Parser() [4/4]

CSV_Parser::CSV_Parser ( const char *  fmt_,
bool  hh,
char  d,
const char *  qc 
)
inline

◆ ~CSV_Parser()

CSV_Parser::~CSV_Parser ( )

Releases all dynamically allocated memory.
Making values unusable once the CSV_Parser goes out of scope.

Member Function Documentation

◆ getColumnsCount()

int CSV_Parser::getColumnsCount ( )

◆ getRowsCount()

int CSV_Parser::getRowsCount ( )

Excluding header (if it was part of supplied CSV).

◆ getValues() [1/2]

void* CSV_Parser::getValues ( const char *  key)

Gets values given the column key name.

Parameters
key- column name
Returns
pointer to the first value (it must be cast by the user)

◆ getValues() [2/2]

void* CSV_Parser::getValues ( int  col_index)

Gets values given the column index.

Parameters
col_index- column number (starting with 0)
Returns
pointer to the first value (it must be cast by the user)

◆ operator<<() [1/2]

CSV_Parser & CSV_Parser::operator<< ( char  c)

Example:
cp << 'a' << ',' << 'b';

◆ operator<<() [2/2]

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";

◆ operator[]() [1/2]

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"];

Parameters
key- column name

◆ operator[]() [2/2]

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];

Parameters
col_index- column index (0 being the first column)

◆ parseLeftover()

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".

◆ print()

void CSV_Parser::print ( Stream &  ser = Serial)

Prints whole parsed content including:

  • column names
  • column types
  • all parsed values
Parameters
ser(optional) - Stream object like "Serial" (by default) or "Serial1". For example, it allows to supply "Serial1" or an object of "SoftwareSerial.h" library.

◆ printKeys()

void CSV_Parser::printKeys ( Stream &  ser = Serial)

The documentation for this class was generated from the following files: