| 162 } |
162 } |
| 163 // close the file automatically at pool destruction |
163 // close the file automatically at pool destruction |
| 164 cxMempoolRegister(pool, f, (cx_destructor_func) fclose); |
164 cxMempoolRegister(pool, f, (cx_destructor_func) fclose); |
| 165 |
165 |
| 166 // create a buffer using the memory pool for destruction |
166 // create a buffer using the memory pool for destruction |
| 167 CxBuffer *content = cxBufferCreate( |
167 CxBuffer *content = cxBufferCreate(pool->allocator, |
| 168 NULL, 256, pool->allocator, CX_BUFFER_AUTO_EXTEND |
168 NULL, 256, CX_BUFFER_AUTO_EXTEND |
| 169 ); |
169 ); |
| 170 |
170 |
| 171 // read the file into the buffer and turn it into a string |
171 // read the file into the buffer and turn it into a string |
| 172 cx_stream_copy( |
172 cx_stream_copy( |
| 173 f, content, (cx_read_func) fread, cxBufferWriteFunc |
173 f, content, (cx_read_func) fread, cxBufferWriteFunc |
| 174 ); |
174 ); |
| 175 fclose(f); |
|
| 176 cxstring contentstr = cx_bstr(content); |
175 cxstring contentstr = cx_bstr(content); |
| 177 |
176 |
| 178 // split the string into lines |
177 // split the string into lines |
| 179 // use the memory pool to allocate the target array |
178 // use the memory pool to allocate the target array |
| 180 cxstring* lines; |
179 cxstring* lines; |
| 183 ); |
182 ); |
| 184 |
183 |
| 185 // skip the header and parse the remaining data into a linked list |
184 // skip the header and parse the remaining data into a linked list |
| 186 // the nodes of the list shall also be allocated by the pool |
185 // the nodes of the list shall also be allocated by the pool |
| 187 CxList* datalist = cxLinkedListCreate( |
186 CxList* datalist = cxLinkedListCreate( |
| 188 pool->allocator, NULL, sizeof(CSVData) |
187 pool->allocator, sizeof(CSVData) |
| 189 ); |
188 ); |
| 190 for (size_t i = 1 ; i < lc ; i++) { |
189 for (size_t i = 1 ; i < lc ; i++) { |
| 191 if (lines[i].length == 0) continue; |
190 if (lines[i].length == 0) continue; |
| 192 cxstring fields[3]; |
191 cxstring fields[3]; |
| 193 size_t fc = cx_strsplit(lines[i], ";", 3, fields); |
192 size_t fc = cx_strsplit(lines[i], ";", 3, fields); |