Last active
December 15, 2015 23:29
-
-
Save iluvcapra/5341149 to your computer and use it in GitHub Desktop.
blah blah this isn't done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <string.h> | |
| #define BUFSZ 0x1000 | |
| /* CSV Parser per RFC 4180 */ | |
| /* enumerate records in a CSV file */ | |
| typedef struct rowCell { | |
| char *szContents; | |
| struct rowCell *next; | |
| } CSV_RowCell; | |
| typdef int (*CSV_Iter)( CSV_RowCell *aRow ); | |
| int CSV_EachRow(FILE * file, CSV_Iter iterationFunction); | |
| enum _states { | |
| startCell, | |
| endCell, | |
| inCell, | |
| inQuotedCell, | |
| endLine | |
| }; | |
| int CSV_EachRow(FILE * file, unsigned char ** fields, CSV_Iter iterationFunction) { | |
| unsigned char lineBuffer[BUFZS]; | |
| size_t bufferSize; | |
| unsigned char cursor; | |
| int state = startLine; | |
| CSV_RowCell *tail = *head = malloc( sizeof(CSV_RowCell) ); | |
| while( (bufferSize = fread( lineBuffer , bufferSize, file )) > 0 ) { | |
| do { | |
| switch ( state ) { | |
| case startCell: | |
| if (*cursor == QUOTE) { | |
| state = inQuotedCell; | |
| } else { | |
| state = inCell; | |
| } | |
| break; | |
| case endCell: | |
| tail = head->next = malloc(sizeof( CSV_RowCell ) ); | |
| break; | |
| case inCell: | |
| if (lineBuffer[pos] == DELIM) { | |
| state = endCell; | |
| } else if (lineBuffer[pos] == QUOTE) { | |
| } else { | |
| } | |
| break; | |
| case inQuotedCell: | |
| break; | |
| } | |
| } while (++cursor - lineBuffer < bufferSize); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment