Initializing help system before first use

Parsing a data file

To parse a data file, a bindrv reader context has to be allocated with a call to bindrv_newreader. This initialisation routine requires a callback function that is used for inputing data from some stream (e.g. use the C function fread to read from a file opened with the function fopen). The reader procedure has then to enter a loop calling first bindrv_nexttoken to get the type of the next token to read and then, depending on this data type, use the function suitable to read and decode this token. For instance if the function returns BINDRV_TYP_REAL, a real has to be read and the function bindrv_getreal can be used to get its value. If the file structure is known in advance, the data can be input with a sequence of calls to the decoding functions. For instance the data file of the example of the preceding section could be input with the following code:

int a,c;
char *l;

if(bindrv_getctrl(bctx,&c)==0 && (c==1))
{
 while(bindrv_getint(bctx,&a)==0)
  printf("got: %d\n",a);
 if(bindrv_getctrl(bctx,&c)!=0 || (c!=2))
  exit(1);
}
if(bindrv_getctrl(bctx,&c)==0 && (c==4))
{
 while(bindrv_getstring(bctx,&l)==0)
 {
  printf("got: %s\n",l);
  free(l);
 }
 if(bindrv_getctrl(bctx,&c)!=0 || (c!=2))
  exit(2);
}
if(bindrv_getctrl(bctx,&c)!=0 || (c!=3))
 exit(3);

As shown above, the application is in charge of releasing string buffers returned by bindrv_getstring: this routine allocates the returned memory block using the C function malloc. The application may replace this default memory allocator by calling function bindrv_setalloc.