2 % (c) The GRASP/AQUA Project, Glasgow University, 1994
4 \subsection[fileLookAhead.lc]{hLookAhead Runtime Support}
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);
51 IOFileObject* fo = (IOFileObject*)ptr;
55 fprintf(stderr, "ug: %d %d %c\n",fo->bufRPtr, fo->bufWPtr,(char)c, fo->flags);
59 if ( !FILEOBJ_READABLE(fo) ) {
60 ghc_errno = GHC_EINVAL;
61 ghc_errstr = "object not readable";
65 /* For an unbuffered file object, we lazily
66 allocate a pushback buffer. The sizeof the pushback
67 buffer is (globally) configurable.
69 sz = getPushbackBufSize();
70 if ( FILEOBJ_UNBUFFERED(fo) && fo->buf==NULL && sz > 0 ) {
71 if ((fo->buf = malloc(sz*sizeof(char))) == NULL ) {
75 ((unsigned char*)fo->buf)[sz-1]=(unsigned char)c;
76 fo->bufWPtr = sz; /* Points one past the end of pushback buffer */
77 fo->bufRPtr = sz-1; /* points to current char. */
81 if ( fo->bufWPtr > 0 && fo->bufRPtr > 0 ) {
83 ((unsigned char*)fo->buf)[fo->bufRPtr]=(unsigned char)c;
85 } else if ( fo->buf != NULL &&
88 fo->bufRPtr==0 ) { /* empty buffer waiting to be filled up */
89 fo->bufRPtr=fo->bufSize-1;
90 ((unsigned char*)fo->buf)[fo->bufRPtr]=(unsigned char)c;
91 fo->bufWPtr=fo->bufSize;