backbuffer.h

Go to the documentation of this file.
00001 #ifndef BACKBUFFER_H
00002 #define BACKBUFFER_H
00003 
00004 #include "zipios-config.h"
00005 
00006 #include <algorithm>
00007 
00008 #include "meta-iostreams.h"
00009 #include <vector>
00010 
00011 #include "fcollexceptions.h"
00012 #include "ziphead.h"
00013 #include "zipheadio.h"
00014 #include "virtualseeker.h"
00015 #include "zipios_common.h"
00016 
00017 namespace zipios {
00018 
00019 using std::ios ;
00020 using std::cerr ;
00021 using std::endl ;
00022 
00032 class BackBuffer : public vector< unsigned char > {
00033 public:
00043   inline explicit BackBuffer( istream &is, VirtualSeeker vs = VirtualSeeker(), 
00044                               int chunk_size = 1024 ) ;
00052   inline int readChunk( int &read_pointer ) ;
00053 
00054 private:
00055   VirtualSeeker _vs ;
00056   int _chunk_size ;
00057   istream &_is ;
00058   streampos _file_pos ;
00059   
00060 };
00061 
00062 BackBuffer::BackBuffer( istream &is, VirtualSeeker vs, int chunk_size ) 
00063   : _vs        ( vs         ),
00064     _chunk_size( chunk_size ),
00065     _is        ( is         ) 
00066 {
00067   _vs.vseekg( is, 0, ios::end ) ;
00068   _file_pos = _vs.vtellg( is ) ;
00069   // Only happens if _vs.startOffset() is a position
00070   // in the file that lies after _vs.endOffset(), which
00071   // is clearly not a valid situation.
00072   if ( _file_pos < 0 )
00073     throw FCollException( "Invalid virtual file endings" ) ;
00074 }
00075 
00076 int BackBuffer::readChunk( int &read_pointer ) {
00077   // Update chunk_size and file position
00078   _chunk_size = min<int> ( static_cast< int >( _file_pos ), _chunk_size ) ;
00079   _file_pos -= _chunk_size ;
00080   _vs.vseekg( _is, _file_pos, ios::beg ) ;
00081   // Make space for _chunk_size new bytes first in buffer
00082   insert ( begin(), _chunk_size, static_cast< char > ( 0 ) ) ; 
00083   // Read in the next _chunk_size of bytes
00084 
00085   readByteSeq ( _is, &( (*this)[ 0 ] ), _chunk_size ) ;
00086   read_pointer += _chunk_size ;
00087 
00088   if ( _is.good() )
00089     return _chunk_size ;
00090   else
00091     return 0 ;
00092 }
00093 
00094 }
00095 #endif
00096 
00101 /*
00102   Zipios++ - a small C++ library that provides easy access to .zip files.
00103   Copyright (C) 2000  Thomas Søndergaard
00104   
00105   This library is free software; you can redistribute it and/or
00106   modify it under the terms of the GNU Lesser General Public
00107   License as published by the Free Software Foundation; either
00108   version 2 of the License, or (at your option) any later version.
00109   
00110   This library is distributed in the hope that it will be useful,
00111   but WITHOUT ANY WARRANTY; without even the implied warranty of
00112   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00113   Lesser General Public License for more details.
00114   
00115   You should have received a copy of the GNU Lesser General Public
00116   License along with this library; if not, write to the Free Software
00117   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00118 */

Generated on Wed Nov 23 18:59:57 2011 for FreeCAD by  doxygen 1.6.1