zipios 2.3.4
Zipios -- a small C++ library providing 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-2022 Made to Order Software Corp. All Rights Reserved
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
35namespace zipios
36{
37
80BackBuffer::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
141ssize_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
The header file for zipios::BackBuffer.
ssize_t readChunk(ssize_t &read_pointer)
Read a chunk of data.
std::streampos m_file_pos
VirtualSeeker m_vs
BackBuffer(std::istream &is, VirtualSeeker const &vs=VirtualSeeker(), ssize_t const chunk_size=1024)
std::istream & m_is
An IOException is used to signal an I/O error.
An InvalidException is used when invalid data is provided.
A virtual class used to see in a file embedded in another.
void vseekg(std::istream &is, offset_t offset, std::ios::seekdir sd) const
Seek within the embedded file.
std::streampos vtellg(std::istream &is) const
Current position within the sub-file.
The zipios namespace includes the Zipios library definitions.
void zipRead(std::istream &is, uint32_t &value)
Various exceptions used throughout the Zipios library, all based on zipios::Exception.