zipios++  2.0.2
Zipios++ – a small C++ library that provides easy access to .zip files.
backbuffer.cpp
Go to the documentation of this file.
1 /*
2  Zipios++ - a small C++ library that provides easy access to .zip files.
3 
4  Copyright (C) 2000-2007 Thomas Sondergaard
5  Copyright (C) 2015 Made to Order Software Corporation
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 
29 #include "backbuffer.hpp"
30 
32 
33 
34 namespace zipios
35 {
36 
79 BackBuffer::BackBuffer(std::istream& is, VirtualSeeker const& vs, ssize_t const chunk_size)
80  : m_vs(vs)
81  , m_chunk_size(chunk_size)
82  , m_is(is)
83  , m_file_pos(0)
84 {
85  if(!m_is)
86  {
87  throw InvalidException("BackBuffer() initialized with an invalid input stream.");
88  }
89  if(m_chunk_size <= 0)
90  {
91  throw InvalidException("Invalid chunk_size parameter, it has to be larger than zero.");
92  }
93 
94  m_vs.vseekg(m_is, 0, std::ios::end);
96 
97  // The following should only happens when m_vs.startOffset() is a
98  // position in the file that lies after m_vs.endOffset(), which
99  // is clearly not a valid situation. However, vtellg() may just
100  // fail too.
101  if(m_file_pos < 0)
102  {
103  throw IOException("Invalid virtual file endings.");
104  }
105 }
106 
107 
140 ssize_t BackBuffer::readChunk(ssize_t& read_pointer)
141 {
142  // Update m_chunk_size and file position
143  m_chunk_size = std::min<ssize_t>(static_cast<ssize_t>(m_file_pos), m_chunk_size);
145  m_vs.vseekg(m_is, m_file_pos, std::ios::beg);
146 
147  // Make space for m_chunk_size new bytes
148  insert(begin(), m_chunk_size, static_cast<char>(0));
149 
150  // Read in the next m_chunk_size bytes
151  zipRead(m_is, *this, m_chunk_size);
152  read_pointer += m_chunk_size;
153 
154  return m_is.good() ? m_chunk_size : 0;
155 }
156 
157 
158 } // zipios namespace
159 // Local Variables:
160 // mode: cpp
161 // indent-tabs-mode: nil
162 // c-basic-offset: 4
163 // tab-width: 4
164 // End:
165 
166 // vim: ts=4 sw=4 et
Various exceptions used throughout the Zipios++ library, all based on zipios::Exception.
void zipRead(std::istream &is, uint32_t &value)
std::istream & m_is
Definition: backbuffer.hpp:51
A virtual class used to see in a file embedded in another.
An IOException is used to signal an I/O error.
BackBuffer(std::istream &is, VirtualSeeker const &vs=VirtualSeeker(), ssize_t const chunk_size=1024)
Definition: backbuffer.cpp:79
The header file for zipios::BackBuffer.
void vseekg(std::istream &is, offset_t offset, std::ios::seekdir sd) const
Seek within the embedded file.
An InvalidException is used when invalid data is provided.
ssize_t readChunk(ssize_t &read_pointer)
Read a chunk of data.
Definition: backbuffer.cpp:140
std::streampos m_file_pos
Definition: backbuffer.hpp:52
std::streampos vtellg(std::istream &is) const
Current position within the sub-file.
VirtualSeeker m_vs
Definition: backbuffer.hpp:49