Ignore:
Timestamp:
07/18/11 13:20:42 (13 years ago)
Author:
tbretz
Message:
Further optimized seeking so that we always seek relative.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Mars/mbase/MZlib.cc

    r11434 r11435  
    111111// --------------------------------------------------------------------------
    112112//
     113#include <iostream>
    113114# if (__GNUC__>2)
    114115streambuf::pos_type MZlib::seekoff(streambuf::off_type offset, ios_base::seekdir dir,
     
    125126    //  warning: case value `2' not in enumerated type `_Ios_Seekdir'
    126127
    127     if (dir==ios::cur)
     128    if (dir==ios::end)
    128129    {
    129         // Calculate future position in streambuffer
    130         const char *ptr = gptr()+offset;
     130        clear(rdstate()|ios::failbit);
     131        return EOF;
     132    }
    131133
    132         // Position in z-stream
    133         const z_off_t zpos = gztell(fFile)+gptr()-egptr(); //gzseek(fFile, 0, SEEK_CUR);
     134    // We only do relative seeking to avoid unnecessary decompression
     135    // of the whole file
     136    if (dir==ios::beg)
     137        offset -= tellg();
    134138
    135         // Check if the new position will still be in the buffer
    136         // In this case the target data was already decompressed.
    137         if (ptr<eback() || ptr>=egptr())
    138             return seekpos(zpos+ptr-egptr());
     139    // Calculate future position in streambuffer
     140    const char *ptr = gptr()+offset;
     141
     142    // This is the number of bytes still available in the buffer
     143    const size_t sbuf = egptr()-gptr();
     144
     145    // Check if the new position will still be in the buffer
     146    // In this case the target data was already decompressed.
     147    if (ptr>=eback() && ptr<egptr())
     148    {
     149        // Absolute position in z-stream
     150        const z_off_t zpos = gztell(fFile)-sbuf; //gzseek(fFile, 0, SEEK_CUR);
    139151
    140152        gbump(offset);
     153
    141154        return zpos+offset;
    142 
    143         // zpos-blen: Position in z-stream coresponding to buffer position
    144         // return seekpos(gztell(fFile)+gptr()-egptr()+offset);
    145155    }
    146156
    147     if (dir==ios::beg)
    148         return seekpos(offset);
     157    const streampos pos = gzseek(fFile, offset-sbuf, SEEK_CUR);
     158
     159    // Buffer is empty - force refilling
     160    setg(fBuffer+4, fBuffer+4, fBuffer+4);
     161
     162    return pos<0 ? streampos(EOF) : pos;
    149163
    150164    /*
     
    172186# endif
    173187{
    174     // Seek the z-stream to the given position
    175     if (gzseek(fFile, pos, SEEK_SET)<0)
    176         return EOF;
    177 
    178     // Buffer is empty
    179     setg(fBuffer+4, fBuffer+4, fBuffer+4);
    180 
    181     return pos;
     188    return seekoff(pos, ios::beg);
    182189}
Note: See TracChangeset for help on using the changeset viewer.