Logo Search packages:      
Sourcecode: djvulibre version File versions  Download package

int ByteStream::seek ( long  offset,
int  whence = SEEK_SET,
bool  nothrow = false 
) [virtual]

Sets the current position for reading or writing the ByteStream. Class ByteStream# provides a default implementation able to seek forward by calling function read# until reaching the desired position. Subclasses implementing better seek capabilities must override this default implementation. The new current position is computed by applying displacement offset# to the position represented by argument whence#. The following values are recognized for argument whence#: {description} [SEEK_SET#] Argument offset# indicates the position relative to the beginning of the ByteStream. [SEEK_CUR#] Argument offset# is a signed displacement relative to the current position. [SEEK_END#] Argument offset# is a displacement relative to the end of the file. It is then advisable to provide a negative value for offset#. {description} Results are undefined whenever the new position is greater than the total size of the ByteStream.

{ Error reporting}: If seek()# succeeds, #0# is returned. Otherwise it either returns #-1# (if nothrow# is set to FALSE#) or throws the {GException} exception.

Reimplemented in ByteStream::Stdio, ByteStream::Memory, ByteStream::Static, and UnicodeByteStream.

Definition at line 346 of file ByteStream.cpp.

References EndOfFile, read(), and tell().

Referenced by DjVuDocEditor::create_shared_anno_file(), DjVuANT::encode_raw(), DjVuImage::get_anno(), DjVuFile::get_anno(), DjVuFile::get_merged_anno(), DjVuImage::get_meta(), DjVuFile::get_meta(), DjVuImage::get_text(), DjVuFile::get_text(), DjVuImage::get_XML(), DjVuTXT::get_xmlText(), GPixmap::init(), readat(), DjVuDocument::save_as(), and size().

{
  int nwhere = 0;
  int ncurrent = tell();
  switch (whence)
    {
    case SEEK_SET:
      nwhere=0; break;
    case SEEK_CUR:
      nwhere=ncurrent; break;
    case SEEK_END: 
    {
      if(offset)
      {
        if (nothrow)
          return -1;
        G_THROW( ERR_MSG("ByteStream.backward") );
      }
      char buffer[1024];
      int bytes;
      while((bytes=read(buffer, sizeof(buffer))))
        EMPTY_LOOP;
      return 0;
    }
    default:
      G_THROW( ERR_MSG("ByteStream.bad_arg") );       //  Illegal argument in seek
    }
  nwhere += offset;
  if (nwhere < ncurrent) 
  {
    //  Seeking backwards is not supported by this ByteStream
    if (nothrow)
      return -1;
    G_THROW( ERR_MSG("ByteStream.backward") );
  }
  while (nwhere>ncurrent)
  {
    char buffer[1024];
    const int xbytes=(ncurrent+(int)sizeof(buffer)>nwhere)
      ?(nwhere - ncurrent):(int)sizeof(buffer);
    const int bytes = read(buffer, xbytes);
    ncurrent += bytes;
    if (!bytes)
      G_THROW( ByteStream::EndOfFile );
    //  Seeking works funny on this ByteStream (ftell() acts strange)
    if (ncurrent!=tell())
      G_THROW( ERR_MSG("ByteStream.seek") );
  }
  return 0;
}


Generated by  Doxygen 1.6.0   Back to index