4 #include "zipios++/zipios-config.h"
8 #include "zipios++/meta-iostreams.h"
11 #include "zipios++/fcollexceptions.h"
12 #include "zipios++/ziphead.h"
13 #include "zipios++/zipheadio.h"
14 #include "zipios++/virtualseeker.h"
15 #include "zipios_common.h"
23 /** A BackBuffer instance is useful for reading the last part of a
24 file in an efficient manner, when it is not known exactly how far
25 back (towards the front!) to go, to find the start of the desired
26 data block. BackBuffer is a vector< unsigned char > that fills
27 itself with data from a file by reading chunks from the end of the
28 file progressing towards the start. Upon construction the
29 BackBuffer instance is associated with a file and a chunksize can
30 be specified. To read a chunk of the file into the BackBuffer call
32 class BackBuffer : public vector< unsigned char > {
34 /** BackBuffer constructor.
35 @param is The istream to read the data from. The stream must be seekable,
36 as BackBuffer will reposition the file position to read chunks from the back
38 @param chunk_size specifies the size of the chunks to read the file into
40 @throw FCollException Thrown if the VirtualSeeker vs that has been specified is
41 invalid for the istream is. */
42 inline explicit BackBuffer( istream &is, VirtualSeeker vs = VirtualSeeker(),
43 int chunk_size = 1024 ) ;
44 /** Reads another chunk and returns the size of the chunk that has
45 been read. Returns 0 on I/O failure.
46 @param read_pointer When a new chunk is read in the already
47 stored bytes change position in the BackBuffer. read_pointer is
48 assumed by readChunk() to be a pointer into a position in the
49 BackBuffer, and is updated to point to the same position in the file
50 as it pointed to before the new chunk was read. */
51 inline int readChunk( int &read_pointer ) ;
61 BackBuffer::BackBuffer( istream &is, VirtualSeeker vs, int chunk_size )
63 _chunk_size( chunk_size ),
66 _vs.vseekg( is, 0, ios::end ) ;
67 _file_pos = _vs.vtellg( is ) ;
68 // Only happens if _vs.startOffset() is a position
69 // in the file that lies after _vs.endOffset(), which
70 // is clearly not a valid situation.
72 throw FCollException( "Invalid virtual file endings" ) ;
75 int BackBuffer::readChunk( int &read_pointer ) {
76 // Update chunk_size and file position
77 _chunk_size = min<int> ( static_cast< int >( _file_pos ), _chunk_size ) ;
78 _file_pos -= _chunk_size ;
79 _vs.vseekg( _is, static_cast< int >( _file_pos ), ios::beg ) ;
80 // Make space for _chunk_size new bytes first in buffer
81 insert ( begin(), _chunk_size, static_cast< char > ( 0 ) ) ;
82 // Read in the next _chunk_size of bytes
84 readByteSeq ( _is, &( (*this)[ 0 ] ), _chunk_size ) ;
85 read_pointer += _chunk_size ;
97 The header file for BackBuffer
101 Zipios++ - a small C++ library that provides easy access to .zip files.
102 Copyright (C) 2000 Thomas Søndergaard
104 This library is free software; you can redistribute it and/or
105 modify it under the terms of the GNU Lesser General Public
106 License as published by the Free Software Foundation; either
107 version 2 of the License, or (at your option) any later version.
109 This library is distributed in the hope that it will be useful,
110 but WITHOUT ANY WARRANTY; without even the implied warranty of
111 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
112 Lesser General Public License for more details.
114 You should have received a copy of the GNU Lesser General Public
115 License along with this library; if not, write to the Free Software
116 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA