]> git.donarmstrong.com Git - flightcrew.git/blob - src/zipios/src/backbuffer.h
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / zipios / src / backbuffer.h
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
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"
16
17 namespace zipios {
18
19 using std::ios ;
20 using std::cerr ;
21 using std::endl ;
22
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
31     readChunk(). */
32 class BackBuffer : public vector< unsigned char > {
33 public:
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
37       of the file.
38       @param chunk_size specifies the size of the chunks to read the file into 
39       the BackBuffer in.
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 ) ;
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, 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
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
96 /** \file
97     The header file for BackBuffer
98 */
99
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 */