-
Notifications
You must be signed in to change notification settings - Fork 12
/
parsing_row_by_row.ino
113 lines (85 loc) · 2.78 KB
/
parsing_row_by_row.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* parsing_row_by_row example for: https://github.com/michalmonday/CSV-Parser-for-Arduino
The user must define the following 2 functions/callbacks:
- char feedRowParser()
- bool rowParserFinished()
That is because cp.parseRow() calls these functions continuously "under the hood".
*/
#include <CSV_Parser.h>
/*
This code prints:
Accessing values by column name:
0. String = hello
0. Number = 5
1. String = world
1. Number = 10
CSV_Parser content:
rows_count = 0, cols_count = 2
Header:
my_strings | my_numbers
Types:
char* | int32_t
Values:
Memory occupied by values themselves = 22
sizeof(CSV_Parser) = 25
*/
char * csv_str = "my_strings,my_numbers\r\n"
"hello,5\r\n"
"world,10\r\n";
int csv_str_index = 0;
// This function is responsible for supplying characters to be parsed.
// csv_str may be replaced with SD card reading code or any other
// source of CSV text, like serial port or HTTP request
char feedRowParser() {
return csv_str[csv_str_index++];
}
// This function must return true when the whole CSV file was supplied
// to feedRowParser() function. It will make sure that cp.parseRow()
// returns false when the end of CSV was reached.
bool rowParserFinished() {
return csv_str[csv_str_index] == 0;
}
/*
Alternative way to write both of these functions:
bool csv_end_was_supplied = false;
char feedRowParser() {
if (csv_str_index >= strlen(csv_str)) {
csv_end_was_supplied = true;
return 0;
}
return csv_str[csv_str_index++];
}
bool rowParserFinished() {
return csv_end_was_supplied;
}
*/
void setup() {
Serial.begin(115200);
delay(5000);
CSV_Parser cp(/*format*/ "sL");
Serial.println("Accessing values by column name:");
// WARNING: String indexing can't be used here because the header was not supplied to the cp object yet.
// char **strings = (char**)cp["my_strings"];
// int32_t *numbers = (int32_t*)cp["my_numbers"];
char **strings = (char**)cp[0];
int32_t *numbers = (int32_t*)cp[1];
// parseRow calls feedRowParser() continuously until it reads a
// full row or until the rowParserFinished() returns true
int row_index = 0;
while (cp.parseRow()) {
// Here we could use string indexing but it would be much slower.
// char *string = ((char**)cp["my_strings"])[0];
// int32_t number = ((int32_t*)cp["my_numbers"])[0];
char *string = strings[0];
int32_t number = numbers[0];
Serial.print(String(row_index) + ". String = ");
Serial.println(string);
Serial.print(String(row_index) + ". Number = ");
Serial.println(number, DEC);
Serial.println();
row_index++;
}
Serial.println();
cp.print();
}
void loop() {
}