66 #include < SD.h>
77#endif
88
9+ #ifdef NON_ARDUINO
10+ #include < stdlib.h>
11+ #include < string.h>
12+ #include < string>
13+ #include < stdio.h>
14+ #include < ctype.h>
15+ #endif
16+
17+ // external function declaration for feeding characters to parser it must return a char
18+ extern char __attribute__ ((weak)) feedRowParser();
19+ char __attribute__ ((weak)) feedRowParser() { return ' -' ; }
20+ extern char * __attribute__ ((weak)) feedRowParserStr();
21+ char *__attribute__ ((weak)) feedRowParserStr() { return 0 ; }
22+ // external function declaration for checking if row parser finished parsing
23+ extern bool __attribute__ ((weak)) rowParserFinished();
24+ bool __attribute__ ((weak)) rowParserFinished() { return true ; }
25+ // both functions above must be defined by the user, weak attribute is to avoid compilation
26+ // fail if the user doesn't define them (because not every user will use parseRow() method)
27+
928// #include "mem_check.h" // COMMENT-OUT BEFORE UPLOAD
1029
1130// Stream * CSV_Parser::debug_serial = &Serial;
@@ -79,7 +98,10 @@ CSV_Parser::CSV_Parser(const char * s, const char * fmt_, bool has_header_, char
7998 // whole_csv_supplied((bool)s ? true : false), // in constructor where whole csv is not supplied at once it should be set to false
8099 leftover(0 ),
81100 current_col(0 ),
82- header_parsed(!has_header_)
101+ header_parsed(!has_header_),
102+ feedRowParser_callback(feedRowParser),
103+ feedRowParserStr_callback(feedRowParserStr),
104+ rowParserFinished_callback(rowParserFinished)
83105{
84106 AssignIsFmtUnsignedArray (fmt_);
85107
@@ -112,15 +134,6 @@ CSV_Parser::~CSV_Parser() {
112134 free (is_fmt_unsigned);
113135}
114136
115- // external function declaration for feeding characters to parser it must return a char
116- extern char __attribute__ ((weak)) feedRowParser();
117- char __attribute__ ((weak)) feedRowParser() { return ' -' ; }
118- // external function declaration for checking if row parser finished parsing
119- extern bool __attribute__ ((weak)) rowParserFinished();
120- bool __attribute__ ((weak)) rowParserFinished() { return true ; }
121- // both functions above must be defined by the user, weak attribute is to avoid compilation
122- // fail if the user doesn't define them (because not every user will use parseRow() method)
123-
124137bool CSV_Parser::parseRow () {
125138 // parseRow() should never be used together with the original way of parsing csv
126139 // (by "original way of parsing" I mean: by using "cp <<" operator or by supplying whole csv at once)
@@ -132,7 +145,7 @@ bool CSV_Parser::parseRow() {
132145 // char *str = strings[0];
133146 // }
134147
135- if (rowParserFinished ())
148+ if (rowParserFinished_callback ())
136149 return false ;
137150
138151 // It is necessary to deallocate memory for previously saved strings
@@ -145,11 +158,19 @@ bool CSV_Parser::parseRow() {
145158 rows_count = 0 ;
146159 }
147160
148- while (!rowParserFinished () && rows_count == 0 )
149- *this << feedRowParser ();
161+ while (!rowParserFinished_callback () && rows_count == 0 ) {
162+ char c = feedRowParser_callback ();
163+ if (c) {
164+ *this << c;
165+ }
166+ char *str = feedRowParserStr_callback ();
167+ if (str) {
168+ *this << str;
169+ }
170+ }
150171 // thanks to the line below the csv could end without '\n' and the last value
151172 // would still be parsed if the rowParserFinished() returns true
152- if (rowParserFinished ())
173+ if (rowParserFinished_callback ())
153174 parseLeftover ();
154175 return rows_count > 0 ;
155176}
@@ -327,11 +348,16 @@ void CSV_Parser::saveNewValue(const char * val, char type_specifier, int row, in
327348 }
328349}
329350
330-
331351void CSV_Parser::printKeys (Stream &ser) {
352+ #ifndef NON_ARDUINO
332353 ser.println (" Keys:" );
333354 for (int col = 0 ; col < cols_count; col++)
334355 ser.println (String (col) + " . Key = " + String (keys[col] ? keys[col] : " unused" ));
356+ #else
357+ printf (" Keys:\n " );
358+ for (int col = 0 ; col < cols_count; col++)
359+ printf (" %d. Key = %s\n " , col, keys[col] ? keys[col] : " unused" );
360+ #endif
335361}
336362
337363int CSV_Parser::getColumnsCount () { return cols_count; }
@@ -348,7 +374,6 @@ void * CSV_Parser::operator [] (const char *key) {
348374/* Get values pointer given column index (0 being the first column) */
349375void * CSV_Parser::operator [] (int index) { return index < cols_count ? values[index] : (void *)0 ; }
350376
351-
352377/* Prints column names, their types and all stored values. */
353378void CSV_Parser::print (Stream &ser) {
354379 ser.println (" CSV_Parser content:" );
@@ -414,8 +439,6 @@ void CSV_Parser::print(Stream &ser) {
414439 ser.println (sizeof (CSV_Parser), DEC);
415440}
416441
417-
418-
419442void CSV_Parser::supplyChunk (const char *s) {
420443 whole_csv_supplied = false ;
421444
@@ -527,3 +550,13 @@ void CSV_Parser::parseLeftover() {
527550 leftover = 0 ;
528551 }
529552}
553+
554+ void CSV_Parser::setFeedRowParserCallback (std::function<char ()> func) {
555+ this ->feedRowParser_callback = func;
556+ }
557+ void CSV_Parser::setFeedRowParserStrCallback (std::function<char *()> func) {
558+ this ->feedRowParserStr_callback = func;
559+ }
560+ void CSV_Parser::setRowParserFinishedCallback (std::function<bool ()> func) {
561+ this ->rowParserFinished_callback = func;
562+ }
0 commit comments