2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: fileLookAhead.c,v 1.5 1999/12/08 15:47:07 simonmar Exp $
6 * hLookAhead Runtime Support
13 fileLookAhead(StgForeignPtr ptr)
15 IOFileObject* fo = (IOFileObject*)ptr;
19 fprintf(stderr, "flh: %d %d %d\n",fo->bufRPtr, fo->bufWPtr, fo->flags);
23 * fileLookahead reads the next character (hopefully from the buffer),
24 * before putting it back and returning the char.
28 if ( FILEOBJ_IS_EOF(fo) ) {
29 ghc_errtype = ERR_EOF;
34 if ( (c = fileGetc(ptr)) < 0 ) {
38 rc = ungetChar(ptr,(char)c);
47 ungetChar(StgForeignPtr ptr, StgChar c)
49 IOFileObject* fo = (IOFileObject*)ptr;
53 fprintf(stderr, "ug: %d %d %c\n",fo->bufRPtr, fo->bufWPtr,(char)c, fo->flags);
57 if ( !FILEOBJ_READABLE(fo) ) {
58 ghc_errno = GHC_EINVAL;
59 ghc_errstr = "object not readable";
63 /* For an unbuffered file object, we lazily
64 allocate a pushback buffer. The sizeof the pushback
65 buffer is (globally) configurable.
67 sz = getPushbackBufSize();
68 if ( FILEOBJ_UNBUFFERED(fo) && fo->buf==NULL && sz > 0 ) {
69 if ((fo->buf = malloc(sz*sizeof(char))) == NULL ) {
73 ((unsigned char*)fo->buf)[sz-1]=(unsigned char)c;
74 fo->bufWPtr = sz; /* Points one past the end of pushback buffer */
75 fo->bufRPtr = sz-1; /* points to current char. */
79 if ( fo->bufWPtr > 0 && fo->bufRPtr > 0 ) {
81 ((unsigned char*)fo->buf)[fo->bufRPtr]=(unsigned char)c;
83 } else if ( fo->buf != NULL &&
86 fo->bufRPtr==0 ) { /* empty buffer waiting to be filled up */
87 fo->bufRPtr=fo->bufSize-1;
88 ((unsigned char*)fo->buf)[fo->bufRPtr]=(unsigned char)c;
89 fo->bufWPtr=fo->bufSize;