Don't throw away buffer if a seek is totally within existing cache.
authorRyan C. Gordon <icculus@icculus.org>
Mon, 23 Aug 2004 03:34:03 +0000
changeset 634 ac8404ebf984
parent 633 0178d6f71e15
child 639 2a3577347fc8
Don't throw away buffer if a seek is totally within existing cache.
physfs.c
--- a/physfs.c	Fri Aug 13 15:54:10 2004 +0000
+++ b/physfs.c	Mon Aug 23 03:34:03 2004 +0000
@@ -1875,7 +1875,22 @@
 {
     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 */