#include "csvrd.h"
#include <stdlib.h>
#include <string.h>
Defines | |
#define | TOKENLEN 255 |
Maximum length of one csv token. | |
#define | RDBUFLEN 1048576 |
Read buffer length. | |
Functions | |
t_csvrd_err | csvrd (const t_filename *aFileName, const char *fldseps, const char *recseps, const char *ignchars, t_cbfld aCbFld, t_cbrec aCbRec, void *ctx) |
Parses CSV files. |
#define RDBUFLEN 1048576 |
Read buffer length.
#define TOKENLEN 255 |
Maximum length of one csv token.
t_csvrd_err csvrd | ( | const t_filename * | aFileName, | |
const char * | fldseps, | |||
const char * | recseps, | |||
const char * | ignchars, | |||
t_cbfld | aCbFld, | |||
t_cbrec | aCbRec, | |||
void * | ctx | |||
) |
Parses CSV files.
Example for parsing a comma separated file with CR-LF on line ends:
#include <stdio.h> #include "csvrd.h" static const char *FLDSEPS = ","; static const char *RECSEPS = "\n"; static const char *CH2IGNORE = "\r"; static short _cbfld(const char *fld, int fldno, short perc, void *ctx) { printf("fld[%d]=%s ", fldno, fld); return 1; } static short _cbrec(int recno, short perc, void *ctx) { printf("rec[%d] finished\n", recno); return 1; } int main(int argc, char **argv) { int i, retval; retval = CSVRD_OK; for(i = 1; i < argc; i++) { retval = (int) csvrd(argv[i], FLDSEPS, RECSEPS, CH2IGNORE, _cbfld, _cbrec, NULL); if (retval != CSVRD_OK) break; } return retval; }
aFileName | name of the CSV file to parse. | |
fldseps | field separators, set of characters as 0 terminated string. Usually something like ",", ",;" etc. Must not be NULL. | |
recseps | record separators, set of characters as 0 terminated string. Example "\n". Must not be NULL. | |
ignchars | characters to ignore while reading the CSV file. Example "\r". Must not be NULL, pass an empty string if you don't want to ignore characters. | |
aCbFld | callback function for each parsed field. May be NULL. | |
aCbRec | callback function called after each parsed record. | |
ctx | context for callback functions. |