#include "fileio.h"
Go to the source code of this file.
Defines | |
#define | __CSVRD_h |
Typedefs | |
typedef short(* | t_cbfld )(const char *fld, int fldno, short perc, void *ctx) |
Type for callback functions retrieving one field. | |
typedef short(* | t_cbrec )(int recno, short perc, void *ctx) |
Type for callback functions called after each parsed record. | |
Enumerations | |
enum | t_csvrd_err { CSVRD_OK = 0, CSVRD_INT, CSVRD_PARAM, CSVRD_FILE, CSVRD_IO, CSVRD_MEM } |
Error codes. More... | |
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 __CSVRD_h |
typedef short(* t_cbfld)(const char *fld, int fldno, short perc, void *ctx) |
Type for callback functions retrieving one field.
fld | the field as 0 terminated text. Never NULL. | |
fldno | sequential number of the field in the record, 0 based. | |
perc | progress in percents. | |
ctx | context for the callback function. |
typedef short(* t_cbrec)(int recno, short perc, void *ctx) |
Type for callback functions called after each parsed record.
ctx | context for the callback function. | |
perc | progress in percents. |
enum t_csvrd_err |
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. |