More readable grep output from bin/ui-translatable.sh
[LibreOffice.git] / include / oox / helper / binaryinputstream.hxx
blob92e4aa1495af9bd4c0d9dfb16a1682290f74826c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_OOX_HELPER_BINARYINPUTSTREAM_HXX
21 #define INCLUDED_OOX_HELPER_BINARYINPUTSTREAM_HXX
23 #include <cstddef>
24 #include <memory>
25 #include <vector>
27 #include <com/sun/star/uno/Reference.hxx>
28 #include <oox/dllapi.h>
29 #include <oox/helper/binarystreambase.hxx>
30 #include <oox/helper/helper.hxx>
31 #include <rtl/string.hxx>
32 #include <rtl/textenc.h>
33 #include <rtl/ustring.hxx>
34 #include <sal/types.h>
36 namespace com::sun::star {
37 namespace io { class XInputStream; }
40 namespace oox {
42 class BinaryOutputStream;
45 /** Interface for binary input stream classes.
47 The binary data in the stream is assumed to be in little-endian format.
49 class OOX_DLLPUBLIC BinaryInputStream : public virtual BinaryStreamBase
51 public:
52 /** Derived classes implement reading nBytes bytes to the passed sequence.
53 The sequence will be reallocated internally.
55 @param nAtomSize
56 The size of the elements in the memory block, if available. Derived
57 classes may be interested in this information.
59 @return
60 Number of bytes really read.
62 virtual sal_Int32 readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize = 1 ) = 0;
64 /** Derived classes implement reading nBytes bytes to the (preallocated!)
65 memory buffer opMem.
67 @param nAtomSize
68 The size of the elements in the memory block, if available. Derived
69 classes may be interested in this information.
71 @return
72 Number of bytes really read.
74 virtual sal_Int32 readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize = 1 ) = 0;
76 /** Derived classes implement seeking the stream forward by the passed
77 number of bytes. This should work for non-seekable streams too.
79 @param nAtomSize
80 The size of the elements in the memory block, if available. Derived
81 classes may be interested in this information.
83 virtual void skip( sal_Int32 nBytes, size_t nAtomSize = 1 ) = 0;
85 /** Reads a value from the stream and converts it to platform byte order.
86 All data types supported by the ByteOrderConverter class can be used.
88 template< typename Type >
89 [[nodiscard]]
90 Type readValue();
92 [[nodiscard]]
93 sal_Int8 readInt8() { return readValue<sal_Int8>(); }
94 [[nodiscard]]
95 sal_uInt8 readuInt8() { return readValue<sal_uInt8>(); }
96 [[nodiscard]]
97 sal_Int16 readInt16() { return readValue<sal_Int16>(); }
98 [[nodiscard]]
99 sal_uInt16 readuInt16() { return readValue<sal_uInt16>(); }
100 [[nodiscard]]
101 sal_Int32 readInt32() { return readValue<sal_Int32>(); }
102 [[nodiscard]]
103 sal_uInt32 readuInt32() { return readValue<sal_uInt32>(); }
104 [[nodiscard]]
105 sal_Int64 readInt64() { return readValue<sal_Int64>(); }
106 [[nodiscard]]
107 float readFloat() { return readValue<float>(); }
108 [[nodiscard]]
109 double readDouble() { return readValue<double>(); }
110 [[nodiscard]]
111 unsigned char readuChar() { return readValue<unsigned char>(); }
113 /** Reads a (preallocated!) C array of values from the stream.
115 Converts all values in the array to platform byte order. All data types
116 supported by the ByteOrderConverter class can be used.
118 @param nElemCount
119 Number of array elements to read (NOT byte count).
121 @return
122 Number of array elements really read (NOT byte count).
124 template< typename Type >
125 sal_Int32 readArray( Type* opnArray, sal_Int32 nElemCount );
127 /** Reads a vector of values from the stream.
129 The vector will be resized internally. Converts all values in the
130 vector to platform byte order. All data types supported by the
131 ByteOrderConverter class can be used.
133 @param nElemCount
134 Number of elements to put into the vector (NOT byte count).
136 @return
137 Number of vector elements really read (NOT byte count).
139 template< typename Type >
140 sal_Int32 readArray( ::std::vector< Type >& orVector, sal_Int32 nElemCount );
142 /** Reads a NUL-terminated Unicode character array and returns the string.
144 OUString readNulUnicodeArray();
146 /** Reads a byte character array and returns the string.
147 NUL characters are replaced by question marks.
149 @param nChars
150 Number of characters (bytes) to read from the stream.
152 OString readCharArray( sal_Int32 nChars );
154 /** Reads a byte character array and returns a Unicode string.
155 NUL characters are replaced by question marks.
157 @param nChars
158 Number of characters (bytes) to read from the stream.
160 @param eTextEnc
161 The text encoding used to create the Unicode string.
163 OUString readCharArrayUC( sal_Int32 nChars, rtl_TextEncoding eTextEnc );
165 /** Reads a Unicode character array and returns the string.
166 NUL characters are replaced by question marks (default).
168 @param nChars
169 Number of 16-bit characters to read from the stream.
171 OUString readUnicodeArray( sal_Int32 nChars );
173 /** Reads a Unicode character array (may be compressed) and returns the
174 string.
175 NUL characters are replaced by question marks (default).
177 @param nChars
178 Number of 8-bit or 16-bit characters to read from the stream.
180 @param bCompressed
181 True = Character array is compressed (stored as 8-bit characters).
182 False = Character array is not compressed (stored as 16-bit characters).
184 OUString readCompressedUnicodeArray( sal_Int32 nChars, bool bCompressed );
186 /** Copies bytes from the current position to the passed output stream.
188 void copyToStream( BinaryOutputStream& rOutStrm );
190 protected:
191 BinaryInputStream() = default;
193 private:
194 BinaryInputStream( BinaryInputStream const& ) = delete;
195 BinaryInputStream& operator=( BinaryInputStream const& ) = delete;
198 typedef std::shared_ptr< BinaryInputStream > BinaryInputStreamRef;
201 template< typename Type >
202 Type BinaryInputStream::readValue()
204 Type ornValue = Type();
205 readMemory( &ornValue, static_cast< sal_Int32 >( sizeof( Type ) ), sizeof( Type ) );
206 ByteOrderConverter::convertLittleEndian( ornValue );
207 return ornValue;
210 template< typename Type >
211 sal_Int32 BinaryInputStream::readArray( Type* opnArray, sal_Int32 nElemCount )
213 sal_Int32 nRet = 0;
214 if( !mbEof )
216 sal_Int32 nReadSize = getLimitedValue< sal_Int32, sal_Int32 >( nElemCount, 0, SAL_MAX_INT32 / sizeof( Type ) ) * sizeof( Type );
217 nRet = readMemory( opnArray, nReadSize, sizeof( Type ) ) / sizeof( Type );
218 ByteOrderConverter::convertLittleEndianArray( opnArray, static_cast< size_t >( nRet ) );
220 return nRet;
223 template< typename Type >
224 sal_Int32 BinaryInputStream::readArray( ::std::vector< Type >& orVector, sal_Int32 nElemCount )
226 orVector.resize( static_cast< size_t >( nElemCount ) );
227 return orVector.empty() ? 0 : readArray(orVector.data(), nElemCount);
231 /** Wraps a UNO input stream and provides convenient access functions.
233 The binary data in the stream is assumed to be in little-endian format.
235 class OOX_DLLPUBLIC BinaryXInputStream final : public BinaryXSeekableStream, public BinaryInputStream
237 public:
238 /** Constructs the wrapper object for the passed input stream.
240 @param rxInStream
241 The com.sun.star.io.XInputStream interface of the UNO input stream
242 to be wrapped.
244 @param bAutoClose
245 True = automatically close the wrapped input stream on destruction
246 of this wrapper or when close() is called.
248 explicit BinaryXInputStream(
249 const css::uno::Reference< css::io::XInputStream >& rxInStrm,
250 bool bAutoClose );
252 virtual ~BinaryXInputStream() override;
254 /** Closes the input stream. Does also close the wrapped UNO input stream
255 if bAutoClose has been set to true in the constructor. */
256 virtual void close() override;
258 /** Reads nBytes bytes to the passed sequence.
259 @return Number of bytes really read. */
260 virtual sal_Int32 readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize = 1 ) override;
262 /** Reads nBytes bytes to the (existing) buffer opMem.
263 @return Number of bytes really read. */
264 virtual sal_Int32 readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize = 1 ) override;
266 /** Seeks the stream forward by the passed number of bytes. This works for
267 non-seekable streams too. */
268 virtual void skip( sal_Int32 nBytes, size_t nAtomSize = 1 ) override;
270 private:
271 StreamDataSequence maBuffer; ///< Data buffer used in readMemory() function.
272 css::uno::Reference< css::io::XInputStream >
273 mxInStrm; ///< Reference to the input stream.
274 bool mbAutoClose; ///< True = automatically close stream on destruction.
278 /** Wraps a StreamDataSequence and provides convenient access functions.
280 The binary data in the stream is assumed to be in little-endian format.
282 class OOX_DLLPUBLIC SequenceInputStream final : public SequenceSeekableStream, public BinaryInputStream
284 public:
285 /** Constructs the wrapper object for the passed data sequence.
287 @attention
288 The passed data sequence MUST live at least as long as this stream
289 wrapper. The data sequence MUST NOT be changed from outside as long
290 as this stream wrapper is used to read from it.
292 explicit SequenceInputStream( const StreamDataSequence& rData );
294 /** Reads nBytes bytes to the passed sequence.
295 @return Number of bytes really read. */
296 virtual sal_Int32 readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize = 1 ) override;
298 /** Reads nBytes bytes to the (existing) buffer opMem.
299 @return Number of bytes really read. */
300 virtual sal_Int32 readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize = 1 ) override;
302 /** Seeks the stream forward by the passed number of bytes. This works for
303 non-seekable streams too. */
304 virtual void skip( sal_Int32 nBytes, size_t nAtomSize = 1 ) override;
306 private:
307 /** Returns the number of bytes available in the sequence for the passed byte count. */
308 sal_Int32 getMaxBytes( sal_Int32 nBytes ) const
309 { return getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, mpData->getLength() - mnPos ); }
313 /** Wraps a BinaryInputStream and provides access to a specific part of the
314 stream data.
316 Provides access to the stream data block starting at the current position
317 of the stream, and with a specific length. If the wrapped stream is
318 seekable, this wrapper will treat the position of the wrapped stream at
319 construction time as position "0" (therefore the class name).
321 The passed input stream MUST live at least as long as this stream wrapper.
322 The stream MUST NOT be changed from outside as long as this stream wrapper
323 is used to read from it.
325 class RelativeInputStream final : public BinaryInputStream
327 public:
328 /** Constructs the wrapper object for the passed stream.
330 @param nSize
331 If specified, restricts the amount of data that can be read from
332 the passed input stream.
334 explicit RelativeInputStream(
335 BinaryInputStream& rInStrm,
336 sal_Int64 nSize );
338 /** Returns the size of the data block in the wrapped stream offered by
339 this wrapper. */
340 virtual sal_Int64 size() const override;
342 /** Returns the current relative stream position. */
343 virtual sal_Int64 tell() const override;
345 /** Seeks the stream to the passed relative position, if the wrapped stream
346 is seekable. */
347 virtual void seek( sal_Int64 nPos ) override;
349 /** Closes the input stream but not the wrapped stream. */
350 virtual void close() override;
352 /** Reads nBytes bytes to the passed sequence. Does not read out of the
353 data block whose size has been specified on construction.
354 @return Number of bytes really read. */
355 virtual sal_Int32 readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize = 1 ) override;
357 /** Reads nBytes bytes to the (existing) buffer opMem. Does not read out of
358 the data block whose size has been specified on construction.
359 @return Number of bytes really read. */
360 virtual sal_Int32 readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize = 1 ) override;
362 /** Seeks the stream forward by the passed number of bytes. This works for
363 non-seekable streams too. Does not seek out of the data block. */
364 virtual void skip( sal_Int32 nBytes, size_t nAtomSize = 1 ) override;
366 private:
367 /** Returns the number of bytes available in the sequence for the passed byte count. */
368 sal_Int32 getMaxBytes( sal_Int32 nBytes ) const
369 { return getLimitedValue< sal_Int32, sal_Int64 >( nBytes, 0, mnSize - mnRelPos ); }
371 private:
372 BinaryInputStream* mpInStrm;
373 sal_Int64 mnStartPos;
374 sal_Int64 mnRelPos;
375 sal_Int64 mnSize;
379 } // namespace oox
381 #endif
383 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */