From 58fac19bb4f85ba27a0034dc8d90262c976c56c8 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Tue, 21 Sep 2004 17:34:27 +0000 Subject: [PATCH] Backported buffered seeking fix from dev branch. --- physfs.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/physfs.c b/physfs.c index a261168d..28359edc 100644 --- a/physfs.c +++ b/physfs.c @@ -1863,7 +1863,22 @@ int PHYSFS_seek(PHYSFS_file *handle, PHYSFS_uint64 pos) { FileHandle *h = (FileHandle *) handle->opaque; BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, 0); - h->buffill = h->bufpos = 0; /* just in case. */ + + if (h->buffer && h->forReading) + { + /* avoid throwing away our precious buffer if seeking within it. */ + PHYSFS_sint64 offset = pos - PHYSFS_tell(handle); + if ( /* seeking within the already-buffered range? */ + ((offset >= 0) && (offset <= h->buffill - h->bufpos)) /* forwards */ + || ((offset < 0) && (-offset <= h->bufpos)) /* backwards */ ) + { + h->bufpos += offset; + return(1); /* successful seek */ + } /* if */ + } /* if */ + + /* we have to fall back to a 'raw' seek. */ + h->buffill = h->bufpos = 0; return(h->funcs->seek(h, pos)); } /* PHYSFS_seek */