Skip to content

Commit 793d545

Browse files
committed
Set callbacks for row by row parsing
1 parent 4e2a916 commit 793d545

File tree

5 files changed

+77
-21
lines changed

5 files changed

+77
-21
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
documentation_config.txt
1+
documentation_config.txt
2+
*.exe
3+
**/*.exe
4+
*.o
5+
**/*.o

CSV_Parser.cpp

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@
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-
124137
bool 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-
331351
void 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

337363
int 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) */
349375
void * 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. */
353378
void 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-
419442
void 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+
}

CSV_Parser.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@
2323
#ifndef CSV_PARSER_H
2424
#define CSV_PARSER_H
2525

26-
#include <Arduino.h>
26+
#ifndef NON_ARDUINO
27+
#include <Arduino.h>
28+
#else
29+
#include <non_arduino_adaptations.h>
30+
extern SerialClass Serial;
31+
#endif
32+
33+
#include <functional>
2734

2835
class CSV_Parser {
2936
char ** keys;
@@ -61,6 +68,10 @@ class CSV_Parser {
6168
int current_col;
6269
bool header_parsed;
6370

71+
std::function<char()> feedRowParser_callback;
72+
std::function<char*()> feedRowParserStr_callback;
73+
std::function<bool()> rowParserFinished_callback;
74+
6475
/* Private methods */
6576
char * parseStringValue(const char *, int * chars_occupied);
6677
void saveNewValue(const char * val, char type_specifier, int row, int col, bool is_unsigned);
@@ -192,12 +203,17 @@ class CSV_Parser {
192203
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". */
193204
void parseLeftover();
194205

206+
void setFeedRowParserCallback(std::function<char()> feedRowParser_callback);
207+
void setFeedRowParserStrCallback(std::function<char*()> feedRowParserStr_callback);
208+
void setRowParserFinishedCallback(std::function<bool()> rowParserFinished_callback);
209+
195210
/** @brief If invalid parameters are supplied to this class, then debug serial is used to output error information.
196211
This function is static, which means that it supposed to be called like:
197212
CSV_Parser::SetDebugSerial(stream_object);
198213
@param ser - Stream object like "Serial" (by default), "Serial1" or an object of
199214
"SoftwareSerial.h" library. */
200215
//static void setDebugSerial(Stream &ser) { debug_serial = &ser; }
216+
201217
};
202218

203219

keywords.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ setDebugSerial KEYWORD2
2020
parseLeftover KEYWORD2
2121
readSDfile KEYWORD2
2222
parseRow KEYWORD2
23+
setFeedRowParserCallback KEYWORD2
24+
setFeedRowParserStrCallback KEYWORD2
25+
setRowParserFinishedCallback KEYWORD2
2326

2427
######################################
2528
# Constants (LITERAL1)

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=CSV Parser
2-
version=1.3.0
2+
version=1.4.0
33
author=Michal Borowski <[email protected]>
44
maintainer=Michal Borowski <[email protected]>
55
sentence=CSV Parser for Arduino.

0 commit comments

Comments
 (0)