ce/csvrd.h File Reference

Types and functions for reading CSV files. More...

#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.


Detailed Description

Types and functions for reading CSV files.

Author:
Daniel Stoinski
Version:
$Revision$

Define Documentation

#define __CSVRD_h


Typedef Documentation

typedef short(* t_cbfld)(const char *fld, int fldno, short perc, void *ctx)

Type for callback functions retrieving one field.

Parameters:
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.
Returns:
!= 0, if parsing has to continue, 0 in order to break the parsing.

typedef short(* t_cbrec)(int recno, short perc, void *ctx)

Type for callback functions called after each parsed record.

Parameters:
ctx context for the callback function.
perc progress in percents.
Returns:
!= 0, if parsing has to continue, 0 in order to break the parsing.


Enumeration Type Documentation

Error codes.

Enumerator:
CSVRD_OK  OK.
CSVRD_INT  Interrupted by the user.
CSVRD_PARAM  Wrong parameters.
CSVRD_FILE  Can't open file.
CSVRD_IO  IO error.
CSVRD_MEM  No memory.


Function Documentation

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;
 }
Parameters:
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.
Returns:
CSVRD_OK or error code.


Generated on Mon Jul 12 14:51:51 2010 for cetoys by  doxygen 1.5.6