Skip to content

Commit 8719cf9

Browse files
committed
README.md
1 parent 24ff864 commit 8719cf9

1 file changed

Lines changed: 20 additions & 34 deletions

File tree

README.md

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
* [What is this CSV parser](#what-is-this-csv-parser)
44
* [Installation](#installation)
55
* [Usage](#usage)
6-
* [Examples](#examples)
6+
* [Examples](#examples)
77
* [Things to consider](#things-to-consider)
88
* [Specifying value types](#specifying-value-types)
99
* [How to store unsigned types](#how-to-store-unsigned-types)
10-
* [Casting returned values](#casting-returned-values)
11-
* [Headerless files](#headerless-files)
12-
* [Custom delimiter](#custom-delimiter)
13-
* [Custom quote character](#custom-quote-character)
14-
* [Checking if the file was parsed correctly](#checking-if-the-file-was-parsed-correctly)
10+
* [Customization](#customization)
11+
* Headerless files
12+
* Custom delimiter
13+
* Custom quote character
1514
* [Troubleshooting](#troubleshooting)
15+
* Checking if the file was parsed correctly
1616
* Platformio and SD library issue
1717
* `cp << file.read()` requiring `(char)` cast (potential issue from version 1.0.0, which may break old code)
1818
* [Motivation](#motivation)
@@ -67,6 +67,8 @@ char * csv_str = "my_strings,my_longs,my_ints,my_chars,my_floats,my_hex,my_to_be
6767

6868
CSV_Parser cp(csv_str, /*format*/ "sLdcfx-");
6969

70+
// retrieving parsed values (and casting them to correct types,
71+
// corresponding to format string provided in constructor above)
7072
char **strings = (char**)cp["my_strings"];
7173
int32_t *longs = (int32_t*)cp["my_longs"];
7274
int16_t *ints = (int16_t*)cp["my_ints"];
@@ -89,8 +91,7 @@ Output:
8991
> world - 80000 - 150 - 20 - 7.77 - FF
9092
> noice - 90000 - 160 - 20 - 9.99 - FFFFFF
9193
92-
Notice how each character within `"sLdcfx-"` string specifies different type for each column. It is very important to set this format right.
93-
We could set each solumn to be strings like "sssssss", however this would use more memory than it's really needed. If we wanted to store a large array of small numerical values (e.g. under 128), then using "c" specifier would be appropriate. See [Specifying value types](#specifying-value-types) section for full list of available specifiers and their descriptions.
94+
Notice how each character within `"sLdcfx-"` string specifies different type for each column. It is very important to set this format right. It determines to what type we must cast parsed value arrays (char\* for "s", int32_t for "L"). We could set each solumn to be strings like "sssssss", however this would use more memory than it's really needed. If we wanted to store a large array of small numerical values (e.g. under 128), then using "c" specifier (int8_t) would be appropriate. See [Specifying value types](#specifying-value-types) section for full list of available specifiers and their descriptions.
9495
9596
![image didnt load](./images/format_string.png)
9697
@@ -150,10 +151,10 @@ cp << ",";
150151
cp << String(102) + ",103\n";
151152
```
152153
Floats can be supplied as well. In general, any types can be supplied, the principle is: if the type isn't "String", "char \*" or "char", then the String(supplied_value) will be appended (before being parsed and stored as a type specified in the format string).
154+
153155

154-
155-
## Examples
156-
Examples directory contains examples showing:
156+
### Examples
157+
Examples directory shows:
157158
* [basic usage](https://github.com/michalmonday/CSV-Parser-for-Arduino/tree/master/examples/basic_usage)
158159
* [how to handle unsigned types](https://github.com/michalmonday/CSV-Parser-for-Arduino/tree/master/examples/unsigned_values)
159160
* [how to supply csv by incomplete parts](https://github.com/michalmonday/CSV-Parser-for-Arduino/tree/master/examples/supplying_csv_by_incomplete_parts)
@@ -258,26 +259,9 @@ CSV_Parser cp(csv_str, /*format*/ "ucuc");
258259

259260
See [unsigned_values example](https://github.com/michalmonday/CSV-Parser-for-Arduino/blob/master/examples/unsigned_values/unsigned_values.ino) for more info.
260261

261-
## Casting returned values
262-
Let's suppose that we parse the following:
263-
```cpp
264-
char * csv_str = "my_strings,my_floats\n"
265-
"hello,1.1\n"
266-
"world,2.2\n";
267-
268-
CSV_Parser cp(csv_str, /*format*/ "sf"); // s = string, f = float
269-
```
270-
271-
To cast/retrieve the values we can use:
272-
```cpp
273-
char **strings = (char**)cp["my_strings"];
274-
float *floats = (float*)cp["my_floats"];
275-
```
276-
277-
"x" (hex input values), should be cast as "int32_t*" (or uint32_t*), because that's how they're stored. Casting them to "int*" could result in wrong address being computed when using `ints[index]`.
262+
## Customization
278263

279-
280-
## Headerless files
264+
### Headerless files
281265
To parse CSV files without header we can specify 3rd optional argument to the constructor. Example:
282266
```cpp
283267
CSV_Parser cp(csv_str, /*format*/ "---L", /*has_header*/ false);
@@ -289,7 +273,7 @@ int32_t * longs = (int32_t*)cp[3]; // 3 becuase L is at index 3 of "---L" format
289273
```
290274

291275

292-
## Custom delimiter
276+
### Custom delimiter
293277
Delimiter is 4th parameter of the constructor. It's comma (,) by default. We can customize it like this:
294278
```cpp
295279
char * csv_str = "my_strings;my_floats\n"
@@ -299,14 +283,16 @@ char * csv_str = "my_strings;my_floats\n"
299283
CSV_Parser cp(csv_str, /*format*/ "sf", /*has_header*/ true, /*delimiter*/ ';');
300284
```
301285
302-
## Custom quote character
286+
### Custom quote character
303287
Quote character is 5th parameter of the constructor. It's double quote (") by default. We can customize it like this:
304288
```cpp
305289
CSV_Parser cp(csv_str, /*format*/ "sLdcfxs", /*has_header*/ true, /*delimiter*/ ',', /*quote_char*/ "'");
306290
```
307291

308292

309-
## Checking if the file was parsed correctly
293+
## Troubleshooting
294+
295+
#### Checking if the file was parsed correctly
310296
Use CSV_Parser.print function and check serial monitor. Example:
311297
```cpp
312298
CSV_Parser cp(csv_str, /*format*/ "sLdcfx-");
@@ -326,7 +312,6 @@ It will display parsed header fields, their types and all the parsed values. Lik
326312
327313
**Important - cp.print() method is using "Serial" object, it assumes that "Serial.begin(baud_rate);" was previously called.**
328314
329-
## Troubleshooting
330315
331316
#### Platformio and SD library issue
332317
Platformio users reported compilation issues due to SD library import by the CSV_Parser.cpp file. Since 0.2.1 version of this library, the SD import can be disabled by placing `#define CSV_PARSER_DONT_IMPORT_SD` above (it won't work if it's below) the CSV_Parser library import like this:
@@ -353,4 +338,5 @@ I wanted to parse [covid-19 csv](https://github.com/tomwhite/covid-19-uk-data) d
353338

354339

355340
## Documentation
341+
Documentation is embedded into source code and automatically generated using doxygen.
356342
https://michalmonday.github.io/CSV-Parser-for-Arduino/index.html

0 commit comments

Comments
 (0)