Commit 39e273aca7859af1b573a9055e34bdf57e38ffe4
1 parent
9854f9b8
sauvegarde fichier initial
Showing
1 changed file
with
20 additions
and
3 deletions
Show diff stats
src/csv_reader.c
... | ... | @@ -4,7 +4,7 @@ |
4 | 4 | #include <stdlib.h> |
5 | 5 | |
6 | 6 | // Conditionals |
7 | -const bool IS_DEBUG = false; | |
7 | +const bool IS_DEBUG = true; | |
8 | 8 | |
9 | 9 | // Constants |
10 | 10 | const unsigned int BUFFER_SIZE = 2048; |
... | ... | @@ -15,6 +15,23 @@ const char CSV_DELIMITERS[] = ","; |
15 | 15 | char** CSV_HEADER_FIELDS; |
16 | 16 | unsigned int CSV_NB_FIELDS; |
17 | 17 | |
18 | +char *strtok_new(char * string, char const * delimiter) | |
19 | +{ | |
20 | + static char *source = NULL; | |
21 | + char *p, *ret = 0; | |
22 | + if(string != NULL) source = string; | |
23 | + if(source == NULL) return NULL; | |
24 | + | |
25 | + if( (p = strpbrk(source, delimiter)) != NULL) | |
26 | + { | |
27 | + *p = 0; | |
28 | + ret = source; | |
29 | + source = ++p; | |
30 | + } | |
31 | + return ret; | |
32 | +} | |
33 | + | |
34 | + | |
18 | 35 | void display_header() |
19 | 36 | { |
20 | 37 | for(unsigned int i = 0; i < CSV_NB_FIELDS; i++) |
... | ... | @@ -81,7 +98,7 @@ void read_csv_file(const char * filename) |
81 | 98 | unsigned int i = 0; |
82 | 99 | |
83 | 100 | // strtok init. |
84 | - token = strtok(buffer, CSV_DELIMITERS); | |
101 | + token = strtok_new(buffer, CSV_DELIMITERS); | |
85 | 102 | |
86 | 103 | while (NULL != token) |
87 | 104 | { |
... | ... | @@ -91,7 +108,7 @@ void read_csv_file(const char * filename) |
91 | 108 | // you can strcpy the `token` string in your data structures |
92 | 109 | // ... |
93 | 110 | |
94 | - token = strtok(NULL, CSV_DELIMITERS); | |
111 | + token = strtok_new(NULL, CSV_DELIMITERS); | |
95 | 112 | } |
96 | 113 | } |
97 | 114 | ... | ... |