Zipios++
backbuffer.h
Go to the documentation of this file.
1 #ifndef BACKBUFFER_H
2 #define BACKBUFFER_H
3 
4 #include "zipios++/zipios-config.h"
5 
6 #include <algorithm>
7 
8 #include "zipios++/meta-iostreams.h"
9 #include <vector>
10 
12 #include "zipios++/ziphead.h"
13 #include "zipios++/zipheadio.h"
14 #include "zipios++/virtualseeker.h"
15 #include "zipios_common.h"
16 
17 namespace zipios {
18 
19 using std::ios ;
20 using std::cerr ;
21 using std::endl ;
22 
32 class BackBuffer : public vector< unsigned char > {
33 public:
42  inline explicit BackBuffer( istream &is, VirtualSeeker vs = VirtualSeeker(),
43  int chunk_size = 1024 ) ;
51  inline int readChunk( int &read_pointer ) ;
52 
53 private:
54  VirtualSeeker _vs ;
55  int _chunk_size ;
56  istream &_is ;
57  streampos _file_pos ;
58 
59 };
60 
61 BackBuffer::BackBuffer( istream &is, VirtualSeeker vs, int chunk_size )
62  : _vs ( vs ),
63  _chunk_size( chunk_size ),
64  _is ( is )
65 {
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.
71  if ( _file_pos < 0 )
72  throw FCollException( "Invalid virtual file endings" ) ;
73 }
74 
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, _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
83 
84  readByteSeq ( _is, &( (*this)[ 0 ] ), _chunk_size ) ;
85  read_pointer += _chunk_size ;
86 
87  if ( _is.good() )
88  return _chunk_size ;
89  else
90  return 0 ;
91 }
92 
93 }
94 #endif
95 
100 /*
101  Zipios++ - a small C++ library that provides easy access to .zip files.
102  Copyright (C) 2000 Thomas Søndergaard
103 
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.
108 
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.
113 
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
117 */
A BackBuffer instance is useful for reading the last part of a file in an efficient manner,...
Definition: backbuffer.h:32
int readChunk(int &read_pointer)
Reads another chunk and returns the size of the chunk that has been read.
Definition: backbuffer.h:75
BackBuffer(istream &is, VirtualSeeker vs=VirtualSeeker(), int chunk_size=1024)
BackBuffer constructor.
Definition: backbuffer.h:61
An FCollException is used to signal a problem with a FileCollection.
VirtualSeeker is a simple class that keeps track of a set of specified 'virtual' file endings that ma...
Definition: virtualseeker.h:20
Header file that defines a number of exceptions used by FileCollection and its subclasses.
Header file that defines VirtualSeeker.
Header file containing classes and functions for reading the central directory and local header field...
Header file that defines I/O functions for the header structures defined in ziphead....
Header file containing miscellaneous small functions.