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