zipios  2.2.0
Zipios – a small C++ library that provides easy access to .zip files.
virtualseeker.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 
34 #include "zipios/virtualseeker.hpp"
35 
37 
38 
39 namespace zipios
40 {
41 
42 
93  : m_start_offset(start_offset)
94  , m_end_offset(end_offset)
95 {
96  if(m_start_offset < 0
97  || m_end_offset < 0)
98  {
99  throw InvalidException("VirtualSeeker::VirtualSeeker(): the start and end offsets cannot be negative.");
100  }
101 }
102 
103 
115 void VirtualSeeker::setOffsets(offset_t start_offset, offset_t end_offset)
116 {
117  if(start_offset < 0
118  || end_offset < 0)
119  {
120  throw InvalidException("VirtualSeeker::VirtualSeeker(): the start and end offsets cannot be negative.");
121  }
122 
123  m_start_offset = start_offset;
124  m_end_offset = end_offset;
125 }
126 
127 
136 void VirtualSeeker::getOffsets(offset_t& start_offset, offset_t& end_offset) const
137 {
138  start_offset = m_start_offset;
139  end_offset = m_end_offset;
140 }
141 
142 
150 {
151  return m_start_offset;
152 }
153 
154 
162 {
163  return m_end_offset;
164 }
165 
166 
178 void VirtualSeeker::vseekg(std::istream &is, offset_t offset, std::ios::seekdir sd) const
179 {
180  switch(sd)
181  {
182  case std::ios::cur:
183  break;
184 
185  case std::ios::beg:
186  offset += m_start_offset;
187  break;
188 
189  case std::ios::end:
190  // This definitively looks weird because this class makes use
191  // of a POSITIVE offset from the end of the file as the end
192  // offset. The parameter 'offset' is expected to be negative
193  // or zero in this case.
194  offset -= m_end_offset;
195  break;
196 
197  default:
198  throw std::logic_error("VirtualSeekManager::vseekg(): error - unknown seekdir");
199 
200  }
201 
202  is.seekg(offset, sd);
203 }
204 
205 
218 std::streampos VirtualSeeker::vtellg(std::istream& is) const
219 {
227  return is.tellg() - m_start_offset;
228 }
229 
230 
231 } // zipios namespace
232 
233 // Local Variables:
234 // mode: cpp
235 // indent-tabs-mode: nil
236 // c-basic-offset: 4
237 // tab-width: 4
238 // End:
239 
240 // vim: ts=4 sw=4 et
The zipios namespace includes the Zipios library definitions.
Definition: backbuffer.cpp:35
offset_t startOffset() const
Return the start offset.
Various exceptions used throughout the Zipios library, all based on zipios::Exception.
offset_t endOffset() const
Return the end offset.
Define the zipios::VirtualSeeker class.
void setOffsets(offset_t start_offset, offset_t end_offset)
Set the offsets of the virtual seeker.
void getOffsets(offset_t &start_offset, offset_t &end_offset) const
Retrieve the current offsets.
VirtualSeeker(offset_t start_offset=0, offset_t end_offset=0)
Create a virtual seeker.
void vseekg(std::istream &is, offset_t offset, std::ios::seekdir sd) const
Seek within the embedded file.
std::streamoff offset_t
An InvalidException is used when invalid data is provided.
std::streampos vtellg(std::istream &is) const
Current position within the sub-file.