STOFFInputStream.hxx Source File documentation

STOFFInputStream.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3/* libstaroffice
4* Version: MPL 2.0 / LGPLv2+
5*
6* The contents of this file are subject to the Mozilla Public License Version
7* 2.0 (the "License"); you may not use this file except in compliance with
8* the License or as specified alternatively below. You may obtain a copy of
9* the License at http://www.mozilla.org/MPL/
10*
11* Software distributed under the License is distributed on an "AS IS" basis,
12* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13* for the specific language governing rights and limitations under the
14* License.
15*
16* Major Contributor(s):
17* Copyright (C) 2002 William Lachance (wrlach@gmail.com)
18* Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net)
19* Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
20* Copyright (C) 2006, 2007 Andrew Ziem
21* Copyright (C) 2011, 2012 Alonso Laurent (alonso@loria.fr)
22*
23*
24* All Rights Reserved.
25*
26* For minor contributions see the git repository.
27*
28* Alternatively, the contents of this file may be used under the terms of
29* the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30* in which case the provisions of the LGPLv2+ are applicable
31* instead of those above.
32*/
33
34#ifndef STOFF_INPUT_STREAM_H
35#define STOFF_INPUT_STREAM_H
36
37#include <string>
38#include <vector>
39
40#include <librevenge/librevenge.h>
41#include <librevenge-stream/librevenge-stream.h>
42#include "libstaroffice_internal.hxx"
43
53 class STOFFInputStream
54{
55public:
60 STOFFInputStream(shared_ptr<librevenge::RVNGInputStream> input, bool inverted);
61
66 STOFFInputStream(librevenge::RVNGInputStream *input, bool inverted);
68 ~STOFFInputStream();
69
71 shared_ptr<librevenge::RVNGInputStream> input()
72 {
73 return m_stream;
74 }
75
76 static shared_ptr<STOFFInputStream> get(librevenge::RVNGBinaryData const &data, bool inverted);
77
79 bool readInverted() const
80 {
81 return m_inverseRead;
82 }
83
84 void setReadInverted(bool newVal)
85 {
86 m_inverseRead = newVal;
87 }
88 //
89 // Position: access
90 //
91
96 int seek(long offset, librevenge::RVNG_SEEK_TYPE seekType);
98 long tell();
100 long size() const
101 {
102 return m_streamSize;
103 }
104
105 bool checkPosition(long pos) const
106 {
107 if (pos < 0) return false;
108 if (m_readLimit > 0 && pos > m_readLimit) return false;
109 return pos<=m_streamSize;
110 }
111
112 bool isEnd();
113
117 void pushLimit(long newLimit)
118 {
119 m_prevLimits.push_back(m_readLimit);
120 m_readLimit = newLimit > m_streamSize ? m_streamSize : newLimit;
121 }
122
123 void popLimit()
124 {
125 if (m_prevLimits.size()) {
126 m_readLimit = m_prevLimits.back();
127 m_prevLimits.pop_back();
128 }
129 else m_readLimit = -1;
130 }
131
132 //
133 // get data
134 //
135
137 int peek();
139 STOFFInputStream &operator>>(bool &res)
140 {
141 res=readULong(1)!=0 ? true : false;
142 return *this;
143 }
144
145 STOFFInputStream &operator>>(uint8_t &res)
146 {
147 res=static_cast<uint8_t>(readULong(1));
148 return *this;
149 }
150
151 STOFFInputStream &operator>>(int8_t &res)
152 {
153 res=static_cast<int8_t>(readLong(1));
154 return *this;
155 }
156
157 STOFFInputStream &operator>>(uint16_t &res)
158 {
159 res=static_cast<uint16_t>(readULong(2));
160 return *this;
161 }
162
163 STOFFInputStream &operator>>(int16_t &res)
164 {
165 res=static_cast<int16_t>(readLong(2));
166 return *this;
167 }
168
169 STOFFInputStream &operator>>(uint32_t &res)
170 {
171 res=static_cast<uint32_t>(readULong(4));
172 return *this;
173 }
174
175 STOFFInputStream &operator>>(int32_t &res)
176 {
177 res=static_cast<int32_t>(readLong(4));
178 return *this;
179 }
180
181 STOFFInputStream &operator>>(double &res)
182 {
183 bool isNan;
184 long pos=tell();
185 if (!readDoubleReverted8(res, isNan)) {
186 STOFF_DEBUG_MSG(("STOFFInputStream::operator>>: can not read a double\n"));
187 seek(pos+8, librevenge::RVNG_SEEK_SET);
188 res=0;
189 }
190 return *this;
191 }
192
193 unsigned long readULong(int num)
194 {
195 return readULong(m_stream.get(), num, 0, m_inverseRead);
196 }
197
198 long readLong(int num);
200 bool readColor(STOFFColor &color);
202 bool readCompressedLong(long &res);
204 bool readCompressedULong(unsigned long &res);
206 bool readDouble8(double &res, bool &isNotANumber);
208 bool readDoubleReverted8(double &res, bool &isNotANumber);
210 bool readDouble10(double &res, bool &isNotANumber);
214 const uint8_t *read(size_t numBytes, unsigned long &numBytesRead);
218 static unsigned long readULong(librevenge::RVNGInputStream *stream, int num, unsigned long a, bool inverseRead);
219
221 bool readDataBlock(long size, librevenge::RVNGBinaryData &data);
223 bool readEndDataBlock(librevenge::RVNGBinaryData &data);
224
225 //
226 // OLE/Zip access
227 //
228
230 bool isStructured();
232 unsigned subStreamCount();
234 std::string subStreamName(unsigned id);
235
237 shared_ptr<STOFFInputStream> getSubStreamByName(std::string const &name);
239 shared_ptr<STOFFInputStream> getSubStreamById(unsigned id);
240
241 //
242 // Resource Fork access
243 //
244
246 bool hasDataFork() const
247 {
248 return bool(m_stream);
249 }
250
251protected:
253 void updateStreamSize();
255 static uint8_t readU8(librevenge::RVNGInputStream *stream);
256
257private:
258 STOFFInputStream(STOFFInputStream const &orig);
259 STOFFInputStream &operator=(STOFFInputStream const &orig);
260
261protected:
263 shared_ptr<librevenge::RVNGInputStream> m_stream;
265 long m_streamSize;
266
268 bool m_inverseRead;
269
271 long m_readLimit;
273 std::vector<long> m_prevLimits;
274};
275
276#endif
277// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab:
STOFFInputStream::m_readLimit
long m_readLimit
actual section limit (-1 if no limit)
Definition STOFFInputStream.hxx:271
STOFFInputStream::operator=
STOFFInputStream & operator=(STOFFInputStream const &orig)
STOFFInputStream::getSubStreamByName
shared_ptr< STOFFInputStream > getSubStreamByName(std::string const &name)
return a new stream for a ole zone
Definition STOFFInputStream.cxx:529
STOFFInputStream::m_prevLimits
std::vector< long > m_prevLimits
list of previous limits
Definition STOFFInputStream.hxx:273
STOFFInputStream::STOFFInputStream
STOFFInputStream(STOFFInputStream const &orig)
STOFFInputStream::m_inverseRead
bool m_inverseRead
big or normal endian
Definition STOFFInputStream.hxx:268
STOFFInputStream::readDataBlock
bool readDataBlock(long size, librevenge::RVNGBinaryData &data)
reads a librevenge::RVNGBinaryData with a given size in the actual section/file
Definition STOFFInputStream.cxx:575
STOFFInputStream::getSubStreamById
shared_ptr< STOFFInputStream > getSubStreamById(unsigned id)
return a new stream for a ole zone
Definition STOFFInputStream.cxx:549
STOFFInputStream::readDouble8
bool readDouble8(double &res, bool &isNotANumber)
try to read a double of size 8: 1.5 bytes exponent, 6.5 bytes mantisse
Definition STOFFInputStream.cxx:363
STOFFInputStream::seek
int seek(long offset, librevenge::RVNG_SEEK_TYPE seekType)
seeks to a offset position, from actual, beginning or ending position
Definition STOFFInputStream.cxx:114
STOFFInputStream::readColor
bool readColor(STOFFColor &color)
try to read a color
Definition STOFFInputStream.cxx:221
STOFFInputStream::checkPosition
bool checkPosition(long pos) const
checks if a position is or not a valid file position
Definition STOFFInputStream.hxx:105
STOFFInputStream::m_stream
shared_ptr< librevenge::RVNGInputStream > m_stream
the initial input
Definition STOFFInputStream.hxx:263
STOFFInputStream::operator>>
STOFFInputStream & operator>>(int32_t &res)
operator>> for int32_t
Definition STOFFInputStream.hxx:175
STOFFInputStream::tell
long tell()
returns actual offset position
Definition STOFFInputStream.cxx:107
STOFFInputStream::m_streamSize
long m_streamSize
the stream size
Definition STOFFInputStream.hxx:265
STOFFInputStream::STOFFInputStream
STOFFInputStream(shared_ptr< librevenge::RVNGInputStream > input, bool inverted)
creates a stream with given endian
Definition STOFFInputStream.cxx:47
STOFFInputStream::updateStreamSize
void updateStreamSize()
update the stream size ( must be called in the constructor )
Definition STOFFInputStream.cxx:88
STOFFInputStream::size
long size() const
returns the stream size
Definition STOFFInputStream.hxx:100
STOFFInputStream::subStreamCount
unsigned subStreamCount()
returns the number of substream
Definition STOFFInputStream.cxx:506
STOFFInputStream::readULong
unsigned long readULong(int num)
returns a uint8, uint16, uint32 readed from actualPos
Definition STOFFInputStream.hxx:193
STOFFInputStream::subStreamName
std::string subStreamName(unsigned id)
returns the name of the i^th substream
Definition STOFFInputStream.cxx:515
STOFFInputStream::operator>>
STOFFInputStream & operator>>(bool &res)
operator>> for bool
Definition STOFFInputStream.hxx:139
STOFFInputStream::popLimit
void popLimit()
pops a section defined by pushLimit
Definition STOFFInputStream.hxx:123
STOFFInputStream::readEndDataBlock
bool readEndDataBlock(librevenge::RVNGBinaryData &data)
reads a librevenge::RVNGBinaryData from actPos to the end of the section/file
Definition STOFFInputStream.cxx:594
STOFFInputStream::readDoubleReverted8
bool readDoubleReverted8(double &res, bool &isNotANumber)
try to read a double of size 8: 6.5 bytes mantisse, 1.5 bytes exponent
Definition STOFFInputStream.cxx:446
STOFFInputStream::~STOFFInputStream
~STOFFInputStream()
destructor
Definition STOFFInputStream.cxx:64
STOFFInputStream::get
static shared_ptr< STOFFInputStream > get(librevenge::RVNGBinaryData const &data, bool inverted)
returns a new input stream corresponding to a librevenge::RVNGBinaryData
Definition STOFFInputStream.cxx:68
STOFFInputStream::operator>>
STOFFInputStream & operator>>(uint32_t &res)
operator>> for uint32_t
Definition STOFFInputStream.hxx:169
STOFFInputStream::operator>>
STOFFInputStream & operator>>(uint8_t &res)
operator>> for uint8_t
Definition STOFFInputStream.hxx:145
STOFFInputStream::readU8
static uint8_t readU8(librevenge::RVNGInputStream *stream)
internal function used to read a byte
Definition STOFFInputStream.cxx:200
STOFFInputStream::readDouble10
bool readDouble10(double &res, bool &isNotANumber)
try to read a double of size 10: 2 bytes exponent, 8 bytes mantisse
Definition STOFFInputStream.cxx:405
STOFFInputStream::readCompressedULong
bool readCompressedULong(unsigned long &res)
read a compressed unsigned long (sw_sw3imp.cxx Sw3IoImp::InULong)
Definition STOFFInputStream.cxx:273
STOFFInputStream::setReadInverted
void setReadInverted(bool newVal)
sets the endian mode
Definition STOFFInputStream.hxx:84
STOFFInputStream::operator>>
STOFFInputStream & operator>>(int8_t &res)
operator>> for int8_t
Definition STOFFInputStream.hxx:151
STOFFInputStream::operator>>
STOFFInputStream & operator>>(uint16_t &res)
operator>> for uint16_t
Definition STOFFInputStream.hxx:157
STOFFInputStream::readCompressedLong
bool readCompressedLong(long &res)
read a compressed long (pstm.cxx:ReadCompressed)
Definition STOFFInputStream.cxx:321
STOFFInputStream::pushLimit
void pushLimit(long newLimit)
defines a new section in the file (from actualPos to newLimit) next call of seek, tell,...
Definition STOFFInputStream.hxx:117
STOFFInputStream::isStructured
bool isStructured()
return true if the stream is ole
Definition STOFFInputStream.cxx:497
STOFFInputStream::peek
int peek()
returns the value of the next caracters or -1, but does not increment the position counter
Definition STOFFInputStream.cxx:213
STOFFInputStream::operator>>
STOFFInputStream & operator>>(int16_t &res)
operator>> for int16_t
Definition STOFFInputStream.hxx:163
STOFFInputStream::readInverted
bool readInverted() const
returns the endian mode (see constructor)
Definition STOFFInputStream.hxx:79
STOFFInputStream::isEnd
bool isEnd()
returns true if we are at the end of the section/file
Definition STOFFInputStream.cxx:137
STOFFInputStream::readLong
long readLong(int num)
return a int8, int16, int32 readed from actualPos
Definition STOFFInputStream.cxx:182
STOFFInputStream::hasDataFork
bool hasDataFork() const
returns true if the data fork block exists
Definition STOFFInputStream.hxx:246
STOFFInputStream::read
const uint8_t * read(size_t numBytes, unsigned long &numBytesRead)
!
Definition STOFFInputStream.cxx:100
STOFFInputStream::input
shared_ptr< librevenge::RVNGInputStream > input()
returns the basic librevenge::RVNGInputStream
Definition STOFFInputStream.hxx:71
STOFFInputStream::operator>>
STOFFInputStream & operator>>(double &res)
operator>> for double
Definition STOFFInputStream.hxx:181
libstaroffice_internal.hxx
STOFF_DEBUG_MSG
#define STOFF_DEBUG_MSG(M)
Definition libstaroffice_internal.hxx:127
STOFFColor
the class to store a color
Definition libstaroffice_internal.hxx:179