physfs.c
branchstable-1.0
changeset 637 6f3206c9f892
parent 609 3a91332776e0
child 664 d7729a9feebd
--- a/physfs.c	Tue Sep 21 17:30:23 2004 +0000
+++ b/physfs.c	Tue Sep 21 17:34:27 2004 +0000
@@ -1863,7 +1863,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 */