From ffdc9baf2892008902e09f0d894947203403679f Mon Sep 17 00:00:00 2001 From: simonmar <unknown> Date: Tue, 21 Mar 2000 11:22:35 +0000 Subject: [PATCH] [project @ 2000-03-21 11:22:35 by simonmar] Fix several bugs in readChunk, mainly to do with copying already-buffered data into the user buffer. --- ghc/lib/std/cbits/readFile.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/ghc/lib/std/cbits/readFile.c b/ghc/lib/std/cbits/readFile.c index a68f3aaf8142..69e608b6721e 100644 --- a/ghc/lib/std/cbits/readFile.c +++ b/ghc/lib/std/cbits/readFile.c @@ -1,7 +1,7 @@ /* * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998 * - * $Id: readFile.c,v 1.11 2000/03/10 15:23:40 simonmar Exp $ + * $Id: readFile.c,v 1.12 2000/03/21 11:22:35 simonmar Exp $ * * hGetContents Runtime Support */ @@ -155,24 +155,29 @@ readChunk(StgForeignPtr ptr, StgAddr buf, StgInt off, StgInt len) } fo->flags = (fo->flags & ~FILEOBJ_RW_WRITE) | FILEOBJ_RW_READ; + p = buf+off; + /* copy the unread parts of the file buffer..*/ if ( FILEOBJ_READABLE(fo) && fo->bufRPtr > 0 && fo->bufWPtr >= fo->bufRPtr ) { - count = ( len < (fo->bufWPtr - fo->bufRPtr)) ? len : (fo->bufWPtr - fo->bufRPtr); - memcpy(buf,fo->buf, count); - fo->bufWPtr=0; - fo->bufRPtr=0; - - } - - if (len - count <= 0) - return count; - len -= count; - p = buf+off; - p += count; - total_count = count; + if (fo->bufWPtr - fo->bufRPtr >= len) { + /* buffer has enough data to fulfill the request */ + memcpy(buf, fo->buf + fo->bufRPtr, len); + fo->bufRPtr += len; + return len; + } else { + /* can only partially fulfill the request from the buffer */ + count = fo->bufWPtr - fo->bufRPtr; + memcpy(buf, fo->buf + fo->bufRPtr, count); + fo->bufWPtr=0; + fo->bufRPtr=0; + len -= count; + p += count; + total_count = count; + } + } while ((count = ( -- GitLab