Arduino CSV Parser
csv_parser.h
Go to the documentation of this file.
1 
21 #ifndef CSV_PARSER_H
22 #define CSV_PARSER_H
23 
24 #include <Arduino.h>
25 
26 class CSV_Parser {
27  char ** keys;
28  void ** values;
29  char * types; // Example type: s = char*, f = float, L = uint32_t, d = uint16_t etc. (see github page for full list of types)
30 
31  int rows_count, cols_count;
32 
33  bool has_header;
34  char delimiter;
35  char quote_char;
36 
37  char delim_chars[4]; // useful for parsing
38  static Stream * debug_serial;
39 
40  void parseHeader(const char *, int * chars_occupied);
41  char * parseStringValue(const char *, int * chars_occupied);
42  void removeEnclosingDoubleQuotes(char *);
43  void saveNewValue(const char * val, char type_specifier, int row, int col);
44 
45  int countRows(const char *s);
46 
47  static int8_t getTypeSize(char type_specifier);
48  static const char * getTypeName(char type_specifier);
49 public:
65  CSV_Parser(const char * s, const char * fmt, bool has_header=true, char delimiter=',', char quote_char='"');
66 
67 
71  CSV_Parser(const char * s, const char * fmt, bool hh, char d, const char * qc) : CSV_Parser(s, fmt, hh, d, qc[0]) {}
72 
75  ~CSV_Parser();
76 
77  int getColumnsCount();
78 
80  int getRowsCount();
81 
85  void * getValues(const char * key);
86 
90  void * getValues(int col_index);
91 
94  void * operator [] (const char *key);
95 
98  void * operator [] (int col_index);
99 
100  //operator String ();
101 
102  void printKeys(Stream &ser = Serial);
103 
112  void print(Stream &ser = Serial);
113 
119  static void setDebugSerial(Stream &ser) { debug_serial = &ser; }
120 };
121 
122 #endif
CSV_Parser::~CSV_Parser
~CSV_Parser()
Releases all dynamically allocated memory. Making values unusable once the CSV_Parser goes out of s...
Definition: csv_parser.cpp:55
CSV_Parser::setDebugSerial
static void setDebugSerial(Stream &ser)
If invalid parameters are supplied to this class, then debug serial is used to output error informati...
Definition: csv_parser.h:119
CSV_Parser::getRowsCount
int getRowsCount()
Excluding header (if it was part of supplied CSV).
Definition: csv_parser.cpp:237
CSV_Parser::print
void print(Stream &ser=Serial)
Prints whole parsed content including:
Definition: csv_parser.cpp:262
CSV_Parser::CSV_Parser
CSV_Parser(const char *s, const char *fmt, bool has_header=true, char delimiter=',', char quote_char='"')
Definition: csv_parser.cpp:5
CSV_Parser::operator[]
void * operator[](const char *key)
It's the same as GetValues(key) but allows to use operator instead of method call,...
Definition: csv_parser.cpp:247
CSV_Parser::getValues
void * getValues(const char *key)
Gets values given the column key name.
Definition: csv_parser.cpp:239
CSV_Parser
Definition: csv_parser.h:26
CSV_Parser::getColumnsCount
int getColumnsCount()
Definition: csv_parser.cpp:236
CSV_Parser::CSV_Parser
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 ...
Definition: csv_parser.h:71
CSV_Parser::printKeys
void printKeys(Stream &ser=Serial)
Definition: csv_parser.cpp:230