CSV Parser for Arduino
CSV_Parser.h
Go to the documentation of this file.
1 /* https://github.com/michalmonday/CSV-Parser-for-Arduino */
2 
23 #ifndef CSV_PARSER_H
24 #define CSV_PARSER_H
25 
26 #include <Arduino.h>
27 
28 class CSV_Parser {
29  char ** keys;
30  void ** values;
31  char * fmt; // Example type: s = char*, f = float, L = uint32_t, d = uint16_t etc. (see github page for full list of types)
32  // https://github.com/michalmonday/CSV-Parser-for-Arduino#specifying-value-types
33  char * is_fmt_unsigned;
34 
35  /* What is stored at fmt and is_fmt_unsigned?
36 
37  When supplied format is "dd", then:
38  fmt = "dd"
39  is_fmt_unsigned = {false, false}
40 
41  When supplied format is "udud", then:
42  fmt = "dd"
43  is_fmt_unsigned = {true, true}
44  */
45 
46  int rows_count, cols_count;
47 
48  bool has_header;
49  char delimiter;
50  char quote_char;
51 
52  char delim_chars[4]; // useful for parsing
53  static Stream * debug_serial;
54 
55  bool whole_csv_supplied; // if the whole csv was supplied at once, then the last value does not have to end with "\n"
56  // if csv was supplied by chunks then the last value must end with "\n"
57  // this member will allow to check it throughout the class
58 
59  /* Members responsible for keeping track of chunked supply of csv string. */
60  char * leftover; // string that wasn't parsed yet because it doesn't end with delimiter or new line
61  uint16_t current_col;
62  bool header_parsed;
63 
64  /* Private methods */
65  char * parseStringValue(const char *, int * chars_occupied);
66  void saveNewValue(const char * val, char type_specifier, int row, int col, bool is_unsigned);
67 
68  static int8_t getTypeSize(char type_specifier);
69  static const char * getTypeName(char type_specifier, bool is_unsigned);
70 
71  void AssignIsFmtUnsignedArray(const char * fmt_);
72 
73  /* Helper functions useful for handling unsigned format specifiers. */
74  static char * strdup_ignoring_u(const char *s);
75  static size_t strlen_ignoring_u(const char *s);
76  static char * strdup_trimmed(const char * s);
77 
78  /* Passes part of csv string to be parsed.
79  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).
80  This function should be called repetitively until the whole csv string is supplied.
81 
82  chunk - part of the csv string (it can be incomplete value, does not have to end with delimiter, could be a single char string) */
83  void supplyChunk(const char *s);
84 public:
100  CSV_Parser(const char * s, const char * fmt, bool has_header=true, char delimiter=',', char quote_char='"');
101 
105  CSV_Parser(const char * s, const char * fmt, bool hh, char d, const char * qc) : CSV_Parser(s, fmt, hh, d, qc[0]) {}
106 
108  CSV_Parser(const char * fmt_, bool hh=true, char d=',', char qc='"') : CSV_Parser(0, fmt_, hh, d, qc) {}
109  CSV_Parser(const char * fmt_, bool hh, char d, const char * qc) : CSV_Parser(0, fmt_, hh, d, qc[0]) {}
110 
113  ~CSV_Parser();
114 
119  bool readSDfile(const char *f_name);
120 
121  int getColumnsCount();
122 
124  int getRowsCount();
125 
129  void * getValues(const char * key);
130 
134  void * getValues(int col_index);
135 
139  void * operator [] (const char *key);
140 
144  void * operator [] (int col_index);
145 
146  void printKeys(Stream &ser = Serial);
147 
156  void print(Stream &ser = Serial);
157 
160  CSV_Parser & operator << (const char *s);
161 
164  CSV_Parser & operator << (char c);
165 
170  void parseLeftover();
171 
177  //static void setDebugSerial(Stream &ser) { debug_serial = &ser; }
178 };
179 
180 
181 
182 
183 
184 #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:93
CSV_Parser::readSDfile
bool readSDfile(const char *f_name)
Reads file from SD card.
Definition: CSV_Parser.cpp:108
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:431
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:109
CSV_Parser::getRowsCount
int getRowsCount()
Excluding header (if it was part of supplied CSV).
Definition: CSV_Parser.cpp:279
CSV_Parser::print
void print(Stream &ser=Serial)
Prints whole parsed content including:
Definition: CSV_Parser.cpp:294
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:444
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:67
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:282
CSV_Parser::getValues
void * getValues(int col_index)
Gets values given the column index.
CSV_Parser
Definition: CSV_Parser.h:28
CSV_Parser::getColumnsCount
int getColumnsCount()
Definition: CSV_Parser.cpp:278
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:105
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:108
CSV_Parser::printKeys
void printKeys(Stream &ser=Serial)
Definition: CSV_Parser.cpp:272