/*--------------------------------*/ /* $PROROOT/demos/c/readatom.c */ /*--------------------------------*/
#include "cpred.h"
#include
char line[MAXLEN+1]; /* max line length */ FILE *fp, *fopen(); char *name; typedef struct { FILE *fp } READSTATE; /* STATE */
Cboot() { CPRED("readatom", 2, readatom, sizeof(READSTATE) ); }
/*-------------------------------------*/
COROUTINE( readatom, READSTATE, state)
RESUME(1, Label1); HASEPILOG;
BEGIN
if(!XChk(IsString(Arg(1), &name), "atom expected for file name")) { FAIL; } if(!XChk( (((state->fp)=fopen(name, "r")) != (FILE *)NULL), "Could not open file")) { FAIL; }
EnableEpilog(); printf("epilog enabled\n"); while ( readline(state->fp) ) { if( UnifyArg(2, (StrAt(line))) ) DETACH(1, Label1) }
EPILOG: printf("start of epilog\n"); if(state->fp){ fclose(state->fp); printf("file closed by epilog\n"); } END
/*-------------------------------------*/
after C preprocessor on cpred.h:
#include
char line[100+1]; FILE *fp, *fopen(); char *name; typedef struct { FILE *fp } READSTATE;
Cboot() { { int readatom (); Cpred("readatom", 2, readatom, sizeof(READSTATE) ); }; } /*-------------------------------------*/
int readatom(port, _ps) int port; OBJECT *_ps { READSTATE * state; state = (READSTATE *)_ps; { switch(port){ case 0:break; case 1: goto Label1; case -1: goto _epilog; default: return 0;};{ if(!XChk(IsString(GoalArg(1), &name), "atom expected for file name")) { return 0; } if(!XChk( (((state->fp)=fopen(name, "r")) != (FILE *)NULL), "Could not open file")){ return 0; }
EnableEpilog();
while ( readline(state->fp) ) { if( UnifyArg(2, (StrAt(line))) ) { return 1; Label1: ;} }
return 0; _epilog: if(state->fp){ fclose(state->fp); printf("file closed by epilog\n"); } } return 0; }
/* ------------- readling() is not changed ----------------*/