1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include <config_options.h>
22 #include <tools/toolsdllapi.h>
23 #include <tools/lineend.hxx>
24 #include <tools/long.hxx>
25 #include <tools/ref.hxx>
26 #include <comphelper/errcode.hxx>
27 #include <rtl/string.hxx>
28 #include <rtl/strbuf.hxx>
29 #include <o3tl/typed_flags_set.hxx>
31 #include <string_view>
35 inline rtl_TextEncoding
GetStoreCharSet( rtl_TextEncoding eEncoding
)
37 if ( eEncoding
== RTL_TEXTENCODING_ISO_8859_1
)
38 return RTL_TEXTENCODING_MS_1252
;
45 // read, write, create,... options
46 enum class StreamMode
{
48 READ
= 0x0001, ///< allow read accesses
49 WRITE
= 0x0002, ///< allow write accesses
51 NOCREATE
= 0x0004, ///< 1 == Don't create file
52 TRUNC
= 0x0008, ///< Truncate _existing_ file to zero length
53 COPY_ON_SYMLINK
= 0x0010, ///< copy-on-write for symlinks (Unix-only)
54 TEMPORARY
= 0x0020, ///< temporary file attribute (Windows-only)
55 DELETE_ON_CLOSE
= 0x0040, ///< only for temporary files (Windows-only)
57 SHARE_DENYNONE
= 0x0100,
58 SHARE_DENYREAD
= 0x0200, // overrides denynone
59 SHARE_DENYWRITE
= 0x0400, // overrides denynone
60 SHARE_DENYALL
= 0x0800, // overrides denyread,write,none
62 READWRITE
= READ
| WRITE
,
63 STD_READ
= READ
| SHARE_DENYNONE
| NOCREATE
,
64 STD_WRITE
= WRITE
| SHARE_DENYALL
,
65 STD_READWRITE
= READWRITE
| SHARE_DENYALL
69 template<> struct typed_flags
<StreamMode
> : is_typed_flags
<StreamMode
, 0x0f7f> {};
72 #define STREAM_SEEK_TO_BEGIN 0L
73 #define STREAM_SEEK_TO_END SAL_MAX_UINT64
75 enum class SvStreamEndian
{ BIG
, LITTLE
};
77 enum class SvStreamCompressFlags
{
84 template<> struct typed_flags
<SvStreamCompressFlags
> : is_typed_flags
<SvStreamCompressFlags
, 0x0011> {};
89 typedef SvStream
& (*SvStrPtr
)( SvStream
& );
91 inline SvStream
& operator<<( SvStream
& rStr
, SvStrPtr f
);
95 struct SvLockBytesStat
99 SvLockBytesStat() : nSize(0) {}
102 class TOOLS_DLLPUBLIC SvLockBytes
: public SvRefBase
104 SvStream
* m_pStream
;
113 SvLockBytes() : m_pStream(nullptr), m_bOwner(false), m_bSync(false) {}
115 SvLockBytes(SvStream
* pTheStream
, bool bTheOwner
= false) :
116 m_pStream(pTheStream
), m_bOwner(bTheOwner
), m_bSync(false) {}
118 virtual ~SvLockBytes() override
{ close(); }
120 const SvStream
* GetStream() const { return m_pStream
; }
122 void SetSynchronMode(bool bTheSync
= true) { m_bSync
= bTheSync
; }
123 bool IsSynchronMode() const { return m_bSync
; }
125 virtual ErrCode
ReadAt(sal_uInt64 nPos
, void * pBuffer
, std::size_t nCount
,
126 std::size_t * pRead
) const;
127 virtual ErrCode
WriteAt(sal_uInt64 nPos
, const void * pBuffer
, std::size_t nCount
,
128 std::size_t * pWritten
);
130 virtual ErrCode
Flush() const;
132 virtual ErrCode
SetSize(sal_uInt64 nSize
);
134 virtual ErrCode
Stat(SvLockBytesStat
* pStat
) const;
137 typedef tools::SvRef
<SvLockBytes
> SvLockBytesRef
;
141 class TOOLS_DLLPUBLIC SvStream
144 // LockBytes Interface
145 SvLockBytesRef m_xLockBytes
; ///< Default implementation
146 sal_uInt64 m_nActPos
;
149 std::unique_ptr
<sal_uInt8
[]>
150 m_pRWBuf
; ///< Points to read/write buffer
151 sal_uInt8
* m_pBufPos
; ///< m_pRWBuf + m_nBufActualPos
152 sal_uInt16 m_nBufSize
; ///< Allocated size of buffer
153 sal_uInt16 m_nBufActualLen
; ///< Length of used segment of buffer
154 ///< = m_nBufSize, if EOF did not occur
155 sal_uInt16 m_nBufActualPos
; ///< current position in buffer (0..m_nBufSize-1)
156 sal_uInt16 m_nBufFree
; ///< number of free slots in buffer to IO of type eIOMode
160 // Error codes, conversion, compression, ...
161 bool m_isDirty
; ///< true: Stream != buffer content
165 SvStreamCompressFlags m_nCompressMode
;
166 LineEnd m_eLineDelimiter
;
167 rtl_TextEncoding m_eStreamCharSet
;
170 OString m_aCryptMaskKey
;// aCryptMaskKey.getLength != 0 -> Encryption used
171 unsigned char m_nCryptMask
;
174 sal_Int32 m_nVersion
; // for external use
176 SvStream ( const SvStream
& rStream
) = delete;
177 SvStream
& operator=( const SvStream
& rStream
) = delete;
180 sal_uInt64 m_nBufFilePos
; ///< File position of pBuf[0]
181 StreamMode m_eStreamMode
;
184 virtual std::size_t GetData( void* pData
, std::size_t nSize
);
185 virtual std::size_t PutData( const void* pData
, std::size_t nSize
);
186 virtual sal_uInt64
SeekPos( sal_uInt64 nPos
);
187 virtual void FlushData();
188 virtual void SetSize(sal_uInt64 nSize
);
190 SAL_DLLPRIVATE
void ClearError();
191 SAL_DLLPRIVATE
void ClearBuffer();
193 // encrypt and write in blocks
194 SAL_DLLPRIVATE
std::size_t CryptAndWriteBuffer( const void* pStart
, std::size_t nLen
);
195 SAL_DLLPRIVATE
void EncryptBuffer( void* pStart
, std::size_t nLen
) const;
199 SvStream( SvLockBytes
*pLockBytes
);
202 SvLockBytes
* GetLockBytes() const { return m_xLockBytes
.get(); }
204 ErrCode
GetError() const { return m_nError
.IgnoreWarning(); }
205 ErrCode
const & GetErrorCode() const { return m_nError
; }
206 void SetError( ErrCode nErrorCode
);
207 virtual void ResetError();
209 void SetEndian( SvStreamEndian SvStreamEndian
);
210 SvStreamEndian
GetEndian() const;
211 /// returns status of endian swap flag
212 bool IsEndianSwap() const { return m_isSwap
; }
214 void SetCompressMode( SvStreamCompressFlags nNewMode
)
215 { m_nCompressMode
= nNewMode
; }
216 SvStreamCompressFlags
GetCompressMode() const { return m_nCompressMode
; }
218 void SetCryptMaskKey(const OString
& rCryptMaskKey
);
220 void SetStreamCharSet( rtl_TextEncoding eCharSet
)
221 { m_eStreamCharSet
= eCharSet
; }
222 rtl_TextEncoding
GetStreamCharSet() const { return m_eStreamCharSet
; }
224 void SetLineDelimiter( LineEnd eLineEnd
)
225 { m_eLineDelimiter
= eLineEnd
; }
226 LineEnd
GetLineDelimiter() const { return m_eLineDelimiter
; }
228 SvStream
& ReadUInt16( sal_uInt16
& rUInt16
);
229 SvStream
& ReadUInt32( sal_uInt32
& rUInt32
);
230 SvStream
& ReadUInt64( sal_uInt64
& rUInt64
);
231 SvStream
& ReadInt16( sal_Int16
& rInt16
);
232 SvStream
& ReadInt32( sal_Int32
& rInt32
);
233 SvStream
& ReadInt64(sal_Int64
& rInt64
);
234 SvStream
& ReadSChar( signed char& rChar
);
235 SvStream
& ReadChar( char& rChar
);
236 SvStream
& ReadUChar( unsigned char& rChar
);
237 SvStream
& ReadUtf16( sal_Unicode
& rUtf16
);
238 SvStream
& ReadCharAsBool( bool& rBool
);
239 SvStream
& ReadFloat( float& rFloat
);
240 SvStream
& ReadDouble( double& rDouble
);
241 SvStream
& ReadStream( SvStream
& rStream
);
243 SvStream
& WriteUInt16( sal_uInt16 nUInt16
);
244 SvStream
& WriteUInt32( sal_uInt32 nUInt32
);
245 SvStream
& WriteUInt64( sal_uInt64 nuInt64
);
246 SvStream
& WriteInt16( sal_Int16 nInt16
);
247 SvStream
& WriteInt32( sal_Int32 nInt32
);
248 SvStream
& WriteInt64( sal_Int64 nInt64
);
249 SvStream
& WriteUInt8( sal_uInt8 nuInt8
);
250 SvStream
& WriteUnicode( sal_Unicode
);
251 SvStream
& WriteOString(std::string_view rStr
)
252 { WriteBytes(rStr
.data(), rStr
.size()); return *this; }
253 SvStream
& WriteStream( SvStream
& rStream
);
254 sal_uInt64
WriteStream( SvStream
& rStream
, sal_uInt64 nSize
);
256 SvStream
& WriteBool( bool b
)
257 { return WriteUChar(static_cast<unsigned char>(b
)); }
258 SvStream
& WriteSChar( signed char nChar
);
259 SvStream
& WriteChar( char nChar
);
260 SvStream
& WriteUChar( unsigned char nChar
);
261 SvStream
& WriteFloat( float nFloat
);
262 SvStream
& WriteDouble( const double& rDouble
);
264 template <typename N
>
265 SvStream
& WriteNumberAsString( N n
) { return WriteOString(OString::number(n
)); }
267 std::size_t ReadBytes( void* pData
, std::size_t nSize
);
268 std::size_t WriteBytes( const void* pData
, std::size_t nSize
);
269 sal_uInt64
Seek( sal_uInt64 nPos
);
270 sal_uInt64
SeekRel( sal_Int64 nPos
);
271 sal_uInt64
Tell() const { return m_nBufFilePos
+ m_nBufActualPos
; }
272 virtual sal_uInt64
TellEnd();
273 // length between current (Tell()) pos and end of stream
274 sal_uInt64
remainingSize();
275 /// If we have data in our internal buffers, write them out
277 /// Call FlushBuffer() and then call flush on the underlying OS stream
279 // next Tell() <= nSize
280 bool SetStreamSize( sal_uInt64 nSize
);
282 /** Read a line of bytes.
284 @param nMaxBytesToRead
285 Maximum of bytes to read, if line is longer it will be
288 @note NOTE that the default is one character less than STRING_MAXLEN to
289 prevent problems after conversion to String that may be lurking
290 in various places doing something like
292 for (sal_uInt16 i=0; i < aString.Len(); ++i)
294 causing endless loops ...
296 bool ReadLine( OStringBuffer
& rStr
, sal_Int32 nMaxBytesToRead
= 0xFFFE );
297 bool ReadLine( OString
& rStr
, sal_Int32 nMaxBytesToRead
= 0xFFFE );
298 bool WriteLine( std::string_view rStr
);
300 /** Read a line of bytes.
302 @param nMaxBytesToRead
303 Maximum of bytes to read, if line is longer it will be
306 @note NOTE that the default is one character less than STRING_MAXLEN to
307 prevent problems after conversion to String that may be lurking
308 in various places doing something like
310 for (sal_uInt16 i=0; i < aString.Len(); ++i)
312 causing endless loops ...
314 bool ReadByteStringLine( OUString
& rStr
, rtl_TextEncoding eSrcCharSet
,
315 sal_Int32 nMaxBytesToRead
= 0xFFFE );
316 bool WriteByteStringLine( std::u16string_view rStr
, rtl_TextEncoding eDestCharSet
);
318 /// Switch to no endian swapping and write 0xfeff
319 void StartWritingUnicodeText();
321 /** If eReadBomCharSet==RTL_TEXTENCODING_DONTKNOW: read 16bit, if 0xfeff do
322 nothing (UTF-16), if 0xfffe switch endian swapping (UTF-16), if 0xefbb
323 or 0xbbef read another byte and check for UTF-8. If no UTF-* BOM was
324 detected put all read bytes back. This means that if 2 bytes were read
325 it was an UTF-16 BOM, if 3 bytes were read it was an UTF-8 BOM. There
326 is no UTF-7, UTF-32 or UTF-EBCDIC BOM detection!
328 If eReadBomCharSet!=RTL_TEXTENCODING_DONTKNOW: only read a BOM of that
329 encoding and switch endian swapping if UTF-16 and 0xfffe. */
330 void StartReadingUnicodeText( rtl_TextEncoding eReadBomCharSet
);
332 /** Read a line of Unicode.
334 @param nMaxCodepointsToRead
335 Maximum of codepoints (UCS-2 or UTF-16 pairs, not bytes) to
336 read, if line is longer it will be truncated.
338 SAL_DLLPRIVATE
bool ReadUniStringLine(OUString
& rStr
, sal_Int32 nMaxCodepointsToRead
);
339 /** Read a 32bit length prefixed sequence of utf-16 if
340 eSrcCharSet==RTL_TEXTENCODING_UNICODE, otherwise read a 16bit length
341 prefixed sequence of bytes and convert from eSrcCharSet */
342 OUString
ReadUniOrByteString(rtl_TextEncoding eSrcCharSet
);
343 /** Write a 32bit length prefixed sequence of utf-16 if
344 eSrcCharSet==RTL_TEXTENCODING_UNICODE, otherwise convert to eSrcCharSet
345 and write a 16bit length prefixed sequence of bytes */
346 SvStream
& WriteUniOrByteString( std::u16string_view rStr
, rtl_TextEncoding eDestCharSet
);
348 /** Read a line of Unicode if eSrcCharSet==RTL_TEXTENCODING_UNICODE,
349 otherwise read a line of Bytecode and convert from eSrcCharSet
351 @param nMaxCodepointsToRead
352 Maximum of codepoints (2 bytes if Unicode, bytes if not
353 Unicode) to read, if line is longer it will be truncated.
355 @note NOTE that the default is one character less than STRING_MAXLEN to
356 prevent problems after conversion to String that may be lurking in
357 various places doing something like
359 for (sal_uInt16 i=0; i < aString.Len(); ++i)
361 causing endless loops ...
363 bool ReadUniOrByteStringLine( OUString
& rStr
, rtl_TextEncoding eSrcCharSet
,
364 sal_Int32 nMaxCodepointsToRead
= 0xFFFE );
365 /** Write a sequence of Unicode characters if
366 eDestCharSet==RTL_TEXTENCODING_UNICODE, otherwise write a sequence of
367 Bytecodes converted to eDestCharSet */
368 bool WriteUnicodeOrByteText(std::u16string_view rStr
, rtl_TextEncoding eDestCharSet
);
369 bool WriteUnicodeOrByteText(std::u16string_view rStr
)
370 { return WriteUnicodeOrByteText( rStr
, GetStreamCharSet() ); }
372 /** Write a Unicode character if eDestCharSet==RTL_TEXTENCODING_UNICODE,
373 otherwise write as Bytecode converted to eDestCharSet.
375 This may result in more than one byte being written if a multi byte
376 encoding (e.g. UTF7, UTF8) is chosen. */
377 bool WriteUniOrByteChar( sal_Unicode ch
, rtl_TextEncoding eDestCharSet
);
378 bool WriteUniOrByteChar( sal_Unicode ch
)
379 { return WriteUniOrByteChar( ch
, GetStreamCharSet() ); }
381 void SetBufferSize( sal_uInt16 m_nBufSize
);
382 sal_uInt16
GetBufferSize() const { return m_nBufSize
; }
384 void RefreshBuffer();
386 bool IsWritable() const { return m_isWritable
; }
387 StreamMode
GetStreamMode() const { return m_eStreamMode
; }
389 sal_Int32
GetVersion() const { return m_nVersion
; }
390 void SetVersion( sal_Int32 n
) { m_nVersion
= n
; }
392 friend SvStream
& operator<<( SvStream
& rStr
, SvStrPtr f
); // for Manips
394 /// end of input seen during previous i/o operation
395 bool eof() const { return m_isEof
; }
398 bool bad() const { return GetError() != ERRCODE_NONE
; }
402 If the state is good() the previous i/o operation succeeded.
404 If the state is good(), the next input operation might succeed;
405 otherwise, it will fail.
407 Applying an input operation to a stream that is not in the good() state
408 is a null operation as far as the variable being read into is concerned.
410 If we try to read into a variable v and the operation fails, the value
411 of v should be unchanged,
413 bool good() const { return !(eof() || bad()); }
416 template <typename T
> SvStream
& ReadNumber(T
& r
);
417 template <typename T
> SvStream
& WriteNumber(T n
);
420 void readNumberWithoutSwap(T
& rDataDest
)
421 { readNumberWithoutSwap_(&rDataDest
, sizeof(rDataDest
)); }
423 SAL_DLLPRIVATE
void readNumberWithoutSwap_(void * pDataDest
, int nDataSize
);
426 void writeNumberWithoutSwap(T
const & rDataSrc
)
427 { writeNumberWithoutSwap_(&rDataSrc
, sizeof(rDataSrc
)); }
429 SAL_DLLPRIVATE
void writeNumberWithoutSwap_(const void * pDataSrc
, int nDataSize
);
432 inline SvStream
& operator<<( SvStream
& rStr
, SvStrPtr f
)
438 TOOLS_DLLPUBLIC SvStream
& endl( SvStream
& rStr
);
439 /// same as endl() but Unicode
440 TOOLS_DLLPUBLIC SvStream
& endlu( SvStream
& rStr
);
441 /// call endlu() if m_eStreamCharSet==RTL_TEXTECODING_UNICODE otherwise endl()
442 TOOLS_DLLPUBLIC SvStream
& endlub( SvStream
& rStr
);
444 /// Attempt to read nUnits 8bit units to an OString, returned OString's
445 /// length is number of units successfully read
446 TOOLS_DLLPUBLIC OString
read_uInt8s_ToOString(SvStream
& rStrm
,
449 /// Attempt to read nUnits 8bit units to an OUString
450 inline OUString
read_uInt8s_ToOUString(SvStream
& rStrm
,
451 std::size_t nUnits
, rtl_TextEncoding eEnc
)
453 return OStringToOUString(read_uInt8s_ToOString(rStrm
, nUnits
), eEnc
);
456 /// Attempt to read nUnits 16bit units to an OUString, returned
457 /// OUString's length is number of units successfully read
458 TOOLS_DLLPUBLIC OUString
read_uInt16s_ToOUString(SvStream
& rStrm
,
461 /// Attempt to read a pascal-style length (of type prefix) prefixed sequence of
462 /// 16bit units to an OUString, returned OString's length is number of
463 /// units successfully read.
464 inline OUString
read_uInt16_lenPrefixed_uInt16s_ToOUString(SvStream
& rStrm
)
466 sal_uInt16 nUnits
= 0;
467 rStrm
.ReadUInt16( nUnits
);
468 return read_uInt16s_ToOUString(rStrm
, nUnits
);
471 inline OUString
read_uInt32_lenPrefixed_uInt16s_ToOUString(SvStream
& rStrm
)
473 sal_uInt32 nUnits
= 0;
474 rStrm
.ReadUInt32( nUnits
);
475 return read_uInt16s_ToOUString(rStrm
, nUnits
);
478 /// Attempt to write a prefixed sequence of nUnits 16bit units from an OUString,
479 /// returned value is number of bytes written
480 TOOLS_DLLPUBLIC
std::size_t write_uInt16s_FromOUString(SvStream
& rStrm
,
481 std::u16string_view rStr
, std::size_t nUnits
);
483 inline std::size_t write_uInt16s_FromOUString(SvStream
& rStrm
,
484 std::u16string_view rStr
)
486 return write_uInt16s_FromOUString(rStrm
, rStr
, rStr
.size());
489 /// Attempt to write a pascal-style length (of type prefix) prefixed sequence
490 /// of 16bit units from an OUString, returned value is number of bytes written
491 /// (including byte-count of prefix)
492 std::size_t write_uInt32_lenPrefixed_uInt16s_FromOUString(SvStream
& rStrm
,
493 std::u16string_view rStr
);
495 /// Attempt to write a pascal-style length (of type prefix) prefixed sequence
496 /// of 16bit units from an OUString, returned value is number of bytes written
497 /// (including byte-count of prefix)
498 UNLESS_MERGELIBS(TOOLS_DLLPUBLIC
) std::size_t write_uInt16_lenPrefixed_uInt16s_FromOUString(SvStream
& rStrm
,
499 std::u16string_view rStr
);
501 /// Attempt to read 8bit units to an OString until a zero terminator is
502 /// encountered, returned OString's length is number of units *definitely*
503 /// successfully read, check SvStream::good() to see if null terminator was
504 /// successfully read
505 TOOLS_DLLPUBLIC OString
read_zeroTerminated_uInt8s_ToOString(SvStream
& rStrm
);
507 /// Attempt to read 8bit units assuming source encoding eEnc to an OUString
508 /// until a zero terminator is encountered. Check SvStream::good() to see if
509 /// null terminator was successfully read
510 TOOLS_DLLPUBLIC OUString
read_zeroTerminated_uInt8s_ToOUString(SvStream
& rStrm
, rtl_TextEncoding eEnc
);
512 /// Attempt to read a pascal-style length (of type prefix) prefixed sequence of
513 /// 8bit units to an OString, returned OString's length is number of units
514 /// successfully read.
515 inline OString
read_uInt32_lenPrefixed_uInt8s_ToOString(SvStream
& rStrm
)
517 sal_uInt32 nUnits
= 0;
518 rStrm
.ReadUInt32(nUnits
);
519 return read_uInt8s_ToOString(rStrm
, nUnits
);
521 inline OString
read_uInt16_lenPrefixed_uInt8s_ToOString(SvStream
& rStrm
)
523 sal_uInt16 nUnits
= 0;
524 rStrm
.ReadUInt16(nUnits
);
525 return read_uInt8s_ToOString(rStrm
, nUnits
);
528 inline OString
read_uInt8_lenPrefixed_uInt8s_ToOString(SvStream
& rStrm
)
530 sal_uInt8 nUnits
= 0;
531 rStrm
.ReadUChar(nUnits
);
532 return read_uInt8s_ToOString(rStrm
, nUnits
);
535 inline OUString
read_uInt16_lenPrefixed_uInt8s_ToOUString(SvStream
& rStrm
,
536 rtl_TextEncoding eEnc
)
538 return OStringToOUString(read_uInt16_lenPrefixed_uInt8s_ToOString(rStrm
), eEnc
);
541 inline OUString
read_uInt8_lenPrefixed_uInt8s_ToOUString(SvStream
& rStrm
,
542 rtl_TextEncoding eEnc
)
544 return OStringToOUString(read_uInt8_lenPrefixed_uInt8s_ToOString(rStrm
), eEnc
);
547 /// Attempt to write a prefixed sequence of nUnits 8bit units from an OString,
548 /// returned value is number of bytes written
549 inline std::size_t write_uInt8s_FromOString(SvStream
& rStrm
, std::string_view rStr
,
552 return rStrm
.WriteBytes(rStr
.data(), nUnits
);
555 inline std::size_t write_uInt8s_FromOString(SvStream
& rStrm
, std::string_view rStr
)
557 return write_uInt8s_FromOString(rStrm
, rStr
, rStr
.size());
560 /// Attempt to write a pascal-style length (of type prefix) prefixed
561 /// sequence of units from a string-type, returned value is number of bytes
562 /// written (including byte-count of prefix)
563 TOOLS_DLLPUBLIC
std::size_t write_uInt16_lenPrefixed_uInt8s_FromOString(SvStream
& rStrm
,
564 std::string_view rStr
);
566 /// Attempt to write a pascal-style length (of type prefix) prefixed sequence
567 /// of 8bit units from an OUString, returned value is number of bytes written
568 /// (including byte-count of prefix)
569 inline std::size_t write_uInt16_lenPrefixed_uInt8s_FromOUString(SvStream
& rStrm
,
570 std::u16string_view rStr
,
571 rtl_TextEncoding eEnc
)
573 return write_uInt16_lenPrefixed_uInt8s_FromOString(rStrm
, OUStringToOString(rStr
, eEnc
));
576 [[nodiscard
]] TOOLS_DLLPUBLIC
bool checkSeek(SvStream
&rSt
, sal_uInt64 nOffset
);
580 /// Is rUrl a file:// URL with no contents?
581 TOOLS_DLLPUBLIC
bool isEmptyFileUrl(const OUString
& rUrl
);
586 class TOOLS_DLLPUBLIC SvFileStream final
: public SvStream
589 void* mxFileHandle
= nullptr; // on windows, it is a HANDLE, otherwise, it is a oslFileHandle
591 sal_uInt16 nLockCounter
;
596 SvFileStream (const SvFileStream
&) = delete;
597 SvFileStream
& operator= (const SvFileStream
&) = delete;
602 virtual std::size_t GetData( void* pData
, std::size_t nSize
) override
;
603 virtual std::size_t PutData( const void* pData
, std::size_t nSize
) override
;
604 virtual sal_uInt64
SeekPos( sal_uInt64 nPos
) override
;
605 virtual void SetSize( sal_uInt64 nSize
) override
;
606 virtual void FlushData() override
;
609 // Switches to Read StreamMode on failed attempt of Write opening
610 SvFileStream( const OUString
& rFileName
, StreamMode eOpenMode
);
612 virtual ~SvFileStream() override
;
614 virtual void ResetError() override
;
616 void Open( const OUString
& rFileName
, StreamMode eOpenMode
);
618 bool IsOpen() const { return bIsOpen
; }
620 const OUString
& GetFileName() const { return aFilename
; }
625 class TOOLS_DLLPUBLIC SvMemoryStream
: public SvStream
627 SvMemoryStream (const SvMemoryStream
&) = delete;
628 SvMemoryStream
& operator= (const SvMemoryStream
&) = delete;
634 std::size_t nEndOfData
;
638 virtual std::size_t GetData( void* pData
, std::size_t nSize
) override
;
639 virtual std::size_t PutData( const void* pData
, std::size_t nSize
) override
;
640 virtual sal_uInt64
SeekPos( sal_uInt64 nPos
) override
;
641 virtual void SetSize( sal_uInt64 nSize
) override
;
642 virtual void FlushData() override
;
644 /// AllocateMemory must update pBuf accordingly
645 /// - pBuf: Address of new block
646 void AllocateMemory( std::size_t nSize
);
648 /// ReAllocateMemory must update the following variables:
649 /// - pBuf: Address of new block
650 /// - nEndOfData: Set to nNewSize-1 , if outside of block
651 /// Set to 0 , if new block size is 0 bytes
652 /// - nSize: New block size
653 /// - nPos: Set to 0 if position outside of block
654 bool ReAllocateMemory( tools::Long nDiff
);
656 /// Is called when this stream allocated the buffer or the buffer is
657 /// resized. FreeMemory may need to NULLify handles in derived classes.
661 SvMemoryStream( void* pBuf
, std::size_t nSize
, StreamMode eMode
);
662 SvMemoryStream( std::size_t nInitSize
=512, std::size_t nResize
=64 );
663 virtual ~SvMemoryStream() override
;
665 virtual void ResetError() override
;
667 sal_uInt64
GetSize() { return TellEnd(); }
668 std::size_t GetEndOfData() const { return nEndOfData
; }
669 const void* GetData() { FlushBuffer(); return pBuf
; }
671 // return the buffer currently in use, and allocate a new buffer internally
672 void* SwitchBuffer();
673 // the buffer is not owned by this class
674 void SetBuffer( void* pBuf
, std::size_t nSize
, std::size_t nEOF
);
676 void ObjectOwnsMemory( bool bOwn
) { bOwnsData
= bOwn
; }
677 /// Makes the stream read-only after it was (possibly) initially writable,
678 /// without having to copy the data or change buffers.
679 /// @since LibreOffice 7.5
681 void SetResizeOffset( std::size_t nNewResize
) { nResize
= nNewResize
; }
682 virtual sal_uInt64
TellEnd() override
{ FlushBuffer(); return nEndOfData
; }
685 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */