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
00070
00071
00072 if ( _file_pos < 0 )
00073 throw FCollException( "Invalid virtual file endings" ) ;
00074 }
00075
00076 int BackBuffer::readChunk( int &read_pointer ) {
00077
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
00082 insert ( begin(), _chunk_size, static_cast< char > ( 0 ) ) ;
00083
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
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118