CSV Parser for Arduino
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 * fmt; // Example type: s = char*, f = float, L = uint32_t, d = uint16_t etc. (see github page for full list of types)
30  // https://github.com/michalmonday/CSV-Parser-for-Arduino#specifying-value-types
31 
32  int rows_count, cols_count;
33 
34  bool has_header;
35  char delimiter;
36  char quote_char;
37 
38  char delim_chars[4]; // useful for parsing
39  static Stream * debug_serial;
40 
41  bool whole_csv_supplied; // if the whole csv was supplied at once, then the last value does not have to end with "\n"
42  // if csv was supplied by chunks then the last value must end with "\n"
43  // this member will allow to check it throughout the class
44 
45  /* Members responsible for keeping track of chunked supply of csv string. */
46  char * leftover; // string that wasn't parsed yet because it doesn't end with delimiter or new line
47  uint16_t current_col;
48  bool header_parsed;
49 
50  /* Private methods */
51  char * parseStringValue(const char *, int * chars_occupied);
52  void saveNewValue(const char * val, char type_specifier, int row, int col);
53 
54  static int8_t getTypeSize(char type_specifier);
55  static const char * getTypeName(char type_specifier);
56 
57  /* Passes part of csv string to be parsed.
58  Passing the string by chunks will allow the program using CSV_Parser to occupy much less memory (because it won't have to store the whole string).
59  This function should be called repetitively until the whole csv string is supplied.
60 
61  chunk - part of the csv string (it can be incomplete value, does not have to end with delimiter, could be a single char string) */
62  void supplyChunk(const char *s);
63 public:
79  CSV_Parser(const char * s, const char * fmt, bool has_header=true, char delimiter=',', char quote_char='"');
80 
84  CSV_Parser(const char * s, const char * fmt, bool hh, char d, const char * qc) : CSV_Parser(s, fmt, hh, d, qc[0]) {}
85 
87  CSV_Parser(const char * fmt_, bool hh=true, char d=',', char qc='"') : CSV_Parser(0, fmt_, hh, d, qc) {}
88  CSV_Parser(const char * fmt_, bool hh, char d, const char * qc) : CSV_Parser(0, fmt_, hh, d, qc[0]) {}
89 
92  ~CSV_Parser();
93 
94  int getColumnsCount();
95 
97  int getRowsCount();
98 
102  void * getValues(const char * key);
103 
107  void * getValues(int col_index);
108 
112  void * operator [] (const char *key);
113 
117  void * operator [] (int col_index);
118 
119  void printKeys(Stream &ser = Serial);
120 
129  void print(Stream &ser = Serial);
130 
133  CSV_Parser & operator << (const char *s);
134 
137  CSV_Parser & operator << (char c);
138 
143  void parseLeftover();
144 
150  //static void setDebugSerial(Stream &ser) { debug_serial = &ser; }
151 };
152 
153 
154 
155 
156 
157 #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:34
CSV_Parser::operator<<
CSV_Parser & operator<<(const char *s)
It's the same as supplyChunk(s) but allows to use operator instead of method call,...
Definition: CSV_Parser.cpp:308
CSV_Parser::getValues
void * getValues(const char *key)
Gets values given the column key name.
CSV_Parser::CSV_Parser
CSV_Parser(const char *fmt_, bool hh, char d, const char *qc)
Definition: CSV_Parser.h:88
CSV_Parser::getRowsCount
int getRowsCount()
Excluding header (if it was part of supplied CSV).
Definition: CSV_Parser.cpp:180
CSV_Parser::print
void print(Stream &ser=Serial)
Prints whole parsed content including:
Definition: CSV_Parser.cpp:195
CSV_Parser::parseLeftover
void parseLeftover()
Forces the previously supplied (but not parsed) chunks to be parsed despite not ending with "\n" or "...
Definition: CSV_Parser.cpp:321
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:10
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:183
CSV_Parser::getValues
void * getValues(int col_index)
Gets values given the column index.
CSV_Parser
Definition: CSV_Parser.h:26
CSV_Parser::getColumnsCount
int getColumnsCount()
Definition: CSV_Parser.cpp:179
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:84
CSV_Parser::CSV_Parser
CSV_Parser(const char *fmt_, bool hh=true, char d=',', char qc='"')
Constructor for supplying csv string by chunks.
Definition: CSV_Parser.h:87
CSV_Parser::printKeys
void printKeys(Stream &ser=Serial)
Definition: CSV_Parser.cpp:173