#include "csv.h"

#define true 1
#define false 0

typedef int Int;
typedef char * String;
typedef double Float;
typedef int Boolean;

static int __calls = 0;     /* Number of items loaded into dataArray */
static void * dataArray;    /* pointer to heap memory for items from input CSV */

#define OBJECT_HEADER  \
  __ProgramType_ type; \
  int fields;          \
  void (*__print_)();  \

void cb2( int c, void * p )
{
  __calls++;
}

void LoadCSVFile( char * fileName, 
  void (*cb1)( void * s, size_t i, void * p ),
  void (*cb2)( int c, void * p ),
  void * dataArray )
{
  struct csv_parser p;
  size_t bytesRead;
  char buf[1024];
  FILE * fp;

  if( csv_init(&p, CSV_APPEND_NULL) != 0 )
    {
    printf("Error: Could not initialize parser\n");
    return;
    }

  fp = fopen( "test.csv", "rb");
  if (!fp) 
    {
    printf("Error: Could not open CSV file\n");
    return;
    }

  while( (bytesRead=fread( buf, 1, 1024, fp)) > 0 )
    if (csv_parse(&p, buf, bytesRead, cb1, cb2, dataArray ) != bytesRead) 
      {
      fprintf(stderr, "Error while parsing file: %s\n",
              csv_strerror(csv_error(&p)) );
      return;
      }

  csv_fini(&p, cb1, cb2, dataArray );
  fclose(fp);
  csv_free(&p);
}
