Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Don't throw away buffer if a seek is totally within existing cache.
  • Loading branch information
icculus committed Aug 23, 2004
1 parent bf41b44 commit f3af262
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion physfs.c
Expand Up @@ -1875,7 +1875,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 */

Expand Down

0 comments on commit f3af262

Please sign in to comment.