2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: fileLookAhead.c,v 1.3 1998/12/02 13:27:25 simonm Exp $
6 * hLookAhead Runtime Support
16 IOFileObject* fo = (IOFileObject*)ptr;
20 fprintf(stderr, "flh: %d %d %d\n",fo->bufRPtr, fo->bufWPtr, fo->flags);
24 * fileLookahead reads the next character (hopefully from the buffer),
25 * before putting it back and returning the char.
29 if ( FILEOBJ_IS_EOF(fo) ) {
30 ghc_errtype = ERR_EOF;
35 if ( (c = fileGetc(ptr)) < 0 ) {
39 rc = ungetChar(ptr,(char)c);
52 IOFileObject* fo = (IOFileObject*)ptr;
56 fprintf(stderr, "ug: %d %d %c\n",fo->bufRPtr, fo->bufWPtr,(char)c, fo->flags);
60 if ( !FILEOBJ_READABLE(fo) ) {
61 ghc_errno = GHC_EINVAL;
62 ghc_errstr = "object not readable";
66 /* For an unbuffered file object, we lazily
67 allocate a pushback buffer. The sizeof the pushback
68 buffer is (globally) configurable.
70 sz = getPushbackBufSize();
71 if ( FILEOBJ_UNBUFFERED(fo) && fo->buf==NULL && sz > 0 ) {
72 if ((fo->buf = malloc(sz*sizeof(char))) == NULL ) {
76 ((unsigned char*)fo->buf)[sz-1]=(unsigned char)c;
77 fo->bufWPtr = sz; /* Points one past the end of pushback buffer */
78 fo->bufRPtr = sz-1; /* points to current char. */
82 if ( fo->bufWPtr > 0 && fo->bufRPtr > 0 ) {
84 ((unsigned char*)fo->buf)[fo->bufRPtr]=(unsigned char)c;
86 } else if ( fo->buf != NULL &&
89 fo->bufRPtr==0 ) { /* empty buffer waiting to be filled up */
90 fo->bufRPtr=fo->bufSize-1;
91 ((unsigned char*)fo->buf)[fo->bufRPtr]=(unsigned char)c;
92 fo->bufWPtr=fo->bufSize;