zipios  2.2.6
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-2019 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.1 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 
29 #include "backbuffer.hpp"
30 
32 
33 #include <algorithm>
34 
35 namespace zipios
36 {
37 
80 BackBuffer::BackBuffer(std::istream & is, VirtualSeeker const & vs, ssize_t const chunk_size)
81  : m_vs(vs)
82  , m_chunk_size(chunk_size)
83  , m_is(is)
84  , m_file_pos(0)
85 {
86  if(!m_is)
87  {
88  throw InvalidException("BackBuffer() initialized with an invalid input stream.");
89  }
90  if(m_chunk_size <= 0)
91  {
92  throw InvalidException("Invalid chunk_size parameter, it has to be larger than zero.");
93  }
94 
95  m_vs.vseekg(m_is, 0, std::ios::end);
97 
98  // The following should only happens when m_vs.startOffset() is a
99  // position in the file that lies after m_vs.endOffset(), which
100  // is clearly not a valid situation. However, vtellg() may just
101  // fail too.
102  if(m_file_pos < 0)
103  {
104  throw IOException("Invalid virtual file endings.");
105  }
106 }
107 
108 
141 ssize_t BackBuffer::readChunk(ssize_t& read_pointer)
142 {
143  // Update m_chunk_size and file position
144  m_chunk_size = std::min<ssize_t>(static_cast<ssize_t>(m_file_pos), m_chunk_size);
146  m_vs.vseekg(m_is, m_file_pos, std::ios::beg);
147 
148  // Make space for m_chunk_size new bytes
149  insert(begin(), m_chunk_size, static_cast<char>(0));
150 
151  // Read in the next m_chunk_size bytes
152  zipRead(m_is, *this, m_chunk_size);
153  read_pointer += m_chunk_size;
154 
155  return m_is.good() ? m_chunk_size : 0;
156 }
157 
158 
159 } // zipios namespace
160 // Local Variables:
161 // mode: cpp
162 // indent-tabs-mode: nil
163 // c-basic-offset: 4
164 // tab-width: 4
165 // End:
166 
167 // vim: ts=4 sw=4 et
zipios::VirtualSeeker
A virtual class used to see in a file embedded in another.
Definition: virtualseeker.hpp:47
zipios::BackBuffer::BackBuffer
BackBuffer(std::istream &is, VirtualSeeker const &vs=VirtualSeeker(), ssize_t const chunk_size=1024)
Definition: backbuffer.cpp:80
zipiosexceptions.hpp
Various exceptions used throughout the Zipios library, all based on zipios::Exception.
zipios::VirtualSeeker::vseekg
void vseekg(std::istream &is, offset_t offset, std::ios::seekdir sd) const
Seek within the embedded file.
Definition: virtualseeker.cpp:178
zipios::BackBuffer::readChunk
ssize_t readChunk(ssize_t &read_pointer)
Read a chunk of data.
Definition: backbuffer.cpp:141
zipios::IOException
An IOException is used to signal an I/O error.
Definition: zipiosexceptions.hpp:71
zipios::VirtualSeeker::vtellg
std::streampos vtellg(std::istream &is) const
Current position within the sub-file.
Definition: virtualseeker.cpp:218
zipios::BackBuffer::m_chunk_size
ssize_t m_chunk_size
Definition: backbuffer.hpp:50
zipios::BackBuffer::m_vs
VirtualSeeker m_vs
Definition: backbuffer.hpp:49
zipios::BackBuffer::m_file_pos
std::streampos m_file_pos
Definition: backbuffer.hpp:52
zipios::BackBuffer::m_is
std::istream & m_is
Definition: backbuffer.hpp:51
zipios::InvalidException
An InvalidException is used when invalid data is provided.
Definition: zipiosexceptions.hpp:83
zipios::zipRead
void zipRead(std::istream &is, uint32_t &value)
Definition: zipios_common.cpp:74
backbuffer.hpp
The header file for zipios::BackBuffer.
zipios
The zipios namespace includes the Zipios library definitions.
Definition: backbuffer.cpp:36