tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / include / tools / stream.hxx
blob8a3bccad121c4920ce1dae20867c823731ad2c45
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 .
19 #pragma once
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>
30 #include <memory>
31 #include <string_view>
33 class StreamData;
35 inline rtl_TextEncoding GetStoreCharSet( rtl_TextEncoding eEncoding )
37 if ( eEncoding == RTL_TEXTENCODING_ISO_8859_1 )
38 return RTL_TEXTENCODING_MS_1252;
39 else
40 return eEncoding;
43 // StreamTypes
45 // read, write, create,... options
46 enum class StreamMode {
47 NONE = 0x0000,
48 READ = 0x0001, ///< allow read accesses
49 WRITE = 0x0002, ///< allow write accesses
50 // file i/o
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)
56 // sharing options
57 SHARE_DENYNONE = 0x0100,
58 SHARE_DENYREAD = 0x0200, // overrides denynone
59 SHARE_DENYWRITE = 0x0400, // overrides denynone
60 SHARE_DENYALL = 0x0800, // overrides denyread,write,none
61 // masks
62 READWRITE = READ | WRITE,
63 STD_READ = READ | SHARE_DENYNONE | NOCREATE,
64 STD_WRITE = WRITE | SHARE_DENYALL,
65 STD_READWRITE = READWRITE | SHARE_DENYALL
67 namespace o3tl
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 {
78 NONE = 0x0000,
79 ZBITMAP = 0x0001,
80 NATIVE = 0x0010,
82 namespace o3tl
84 template<> struct typed_flags<SvStreamCompressFlags> : is_typed_flags<SvStreamCompressFlags, 0x0011> {};
87 class SvStream;
89 typedef SvStream& (*SvStrPtr)( SvStream& );
91 inline SvStream& operator<<( SvStream& rStr, SvStrPtr f );
93 // SvLockBytes
95 struct SvLockBytesStat
97 std::size_t nSize;
99 SvLockBytesStat() : nSize(0) {}
102 /** This is only extended by UcbLockBytes in ucb/ and appears to exist
103 to allow UCB to do delayed feeding of data into a SvStream i.e. a kind of a pipe
104 mechanism to allow asynchronous fetching of data.
106 class UNLESS_MERGELIBS(TOOLS_DLLPUBLIC) SvLockBytes: public SvRefBase
108 bool m_bSync;
110 protected:
111 void close();
113 public:
115 SvLockBytes() : m_bSync(false) {}
117 virtual ~SvLockBytes() override { close(); }
119 void SetSynchronMode(bool bTheSync = true) { m_bSync = bTheSync; }
120 bool IsSynchronMode() const { return m_bSync; }
122 virtual ErrCode ReadAt(sal_uInt64 nPos, void * pBuffer, std::size_t nCount,
123 std::size_t * pRead) const;
124 virtual ErrCode WriteAt(sal_uInt64 nPos, const void * pBuffer, std::size_t nCount,
125 std::size_t * pWritten);
127 virtual ErrCode Flush() const;
129 virtual ErrCode SetSize(sal_uInt64 nSize);
131 virtual ErrCode Stat(SvLockBytesStat * pStat) const;
134 typedef tools::SvRef<SvLockBytes> SvLockBytesRef;
136 // SvStream
138 class TOOLS_DLLPUBLIC SvStream
140 private:
141 // LockBytes Interface
142 SvLockBytesRef m_xLockBytes; ///< Default implementation
143 sal_uInt64 m_nActPos;
145 // buffer management
146 std::unique_ptr<sal_uInt8[]>
147 m_pRWBuf; ///< Points to read/write buffer
148 sal_uInt8* m_pBufPos; ///< m_pRWBuf + m_nBufActualPos
149 sal_uInt16 m_nBufSize; ///< Allocated size of buffer
150 sal_uInt16 m_nBufActualLen; ///< Length of used segment of buffer
151 ///< = m_nBufSize, if EOF did not occur
152 sal_uInt16 m_nBufActualPos; ///< current position in buffer (0..m_nBufSize-1)
153 sal_uInt16 m_nBufFree; ///< number of free slots in buffer to IO of type eIOMode
154 bool m_isIoRead;
155 bool m_isIoWrite;
157 // Error codes, conversion, compression, ...
158 bool m_isDirty; ///< true: Stream != buffer content
159 bool m_isSwap;
160 bool m_isEof;
161 ErrCode m_nError;
162 SvStreamCompressFlags m_nCompressMode;
163 LineEnd m_eLineDelimiter;
164 rtl_TextEncoding m_eStreamCharSet;
166 // Encryption
167 OString m_aCryptMaskKey;// aCryptMaskKey.getLength != 0 -> Encryption used
168 unsigned char m_nCryptMask;
170 // Userdata
171 sal_Int32 m_nVersion; // for external use
173 SvStream ( const SvStream& rStream ) = delete;
174 SvStream& operator=( const SvStream& rStream ) = delete;
176 protected:
177 sal_uInt64 m_nBufFilePos; ///< File position of pBuf[0]
178 StreamMode m_eStreamMode;
179 bool m_isWritable;
181 virtual std::size_t GetData( void* pData, std::size_t nSize );
182 virtual std::size_t PutData( const void* pData, std::size_t nSize );
183 virtual sal_uInt64 SeekPos( sal_uInt64 nPos );
184 virtual void FlushData();
185 virtual void SetSize(sal_uInt64 nSize);
187 SAL_DLLPRIVATE void ClearError();
188 SAL_DLLPRIVATE void ClearBuffer();
190 // encrypt and write in blocks
191 SAL_DLLPRIVATE std::size_t CryptAndWriteBuffer( const void* pStart, std::size_t nLen );
192 SAL_DLLPRIVATE void EncryptBuffer( void* pStart, std::size_t nLen ) const;
194 public:
195 SvStream();
196 SvStream( SvLockBytes *pLockBytes);
197 virtual ~SvStream();
199 SvLockBytes* GetLockBytes() const { return m_xLockBytes.get(); }
201 ErrCode GetError() const { return m_nError.IgnoreWarning(); }
202 ErrCode const & GetErrorCode() const { return m_nError; }
203 void SetError( ErrCode nErrorCode );
204 virtual void ResetError();
206 void SetEndian( SvStreamEndian SvStreamEndian );
207 SvStreamEndian GetEndian() const;
208 /// returns status of endian swap flag
209 bool IsEndianSwap() const { return m_isSwap; }
211 void SetCompressMode( SvStreamCompressFlags nNewMode )
212 { m_nCompressMode = nNewMode; }
213 SvStreamCompressFlags GetCompressMode() const { return m_nCompressMode; }
215 void SetCryptMaskKey(const OString& rCryptMaskKey);
217 void SetStreamCharSet( rtl_TextEncoding eCharSet )
218 { m_eStreamCharSet = eCharSet; }
219 rtl_TextEncoding GetStreamCharSet() const { return m_eStreamCharSet; }
221 void SetLineDelimiter( LineEnd eLineEnd )
222 { m_eLineDelimiter = eLineEnd; }
223 LineEnd GetLineDelimiter() const { return m_eLineDelimiter; }
225 SvStream& ReadUInt16( sal_uInt16& rUInt16 );
226 SvStream& ReadUInt32( sal_uInt32& rUInt32 );
227 SvStream& ReadUInt64( sal_uInt64& rUInt64 );
228 SvStream& ReadInt16( sal_Int16& rInt16 );
229 SvStream& ReadInt32( sal_Int32& rInt32 );
230 SvStream& ReadInt64(sal_Int64 & rInt64);
231 SvStream& ReadSChar( signed char& rChar );
232 SvStream& ReadChar( char& rChar );
233 SvStream& ReadUChar( unsigned char& rChar );
234 SvStream& ReadUtf16( sal_Unicode& rUtf16 );
235 SvStream& ReadCharAsBool( bool& rBool );
236 SvStream& ReadFloat( float& rFloat );
237 SvStream& ReadDouble( double& rDouble );
238 SvStream& ReadStream( SvStream& rStream );
240 SvStream& WriteUInt16( sal_uInt16 nUInt16 );
241 SvStream& WriteUInt32( sal_uInt32 nUInt32 );
242 SvStream& WriteUInt64( sal_uInt64 nuInt64 );
243 SvStream& WriteInt16( sal_Int16 nInt16 );
244 SvStream& WriteInt32( sal_Int32 nInt32 );
245 SvStream& WriteInt64( sal_Int64 nInt64 );
246 SvStream& WriteUInt8( sal_uInt8 nuInt8 );
247 SvStream& WriteUnicode( sal_Unicode );
248 SvStream& WriteOString(std::string_view rStr)
249 { WriteBytes(rStr.data(), rStr.size()); return *this; }
250 SvStream& WriteStream( SvStream& rStream );
251 sal_uInt64 WriteStream( SvStream& rStream, sal_uInt64 nSize );
253 SvStream& WriteBool( bool b )
254 { return WriteUChar(static_cast<unsigned char>(b)); }
255 SvStream& WriteSChar( signed char nChar );
256 SvStream& WriteChar( char nChar );
257 SvStream& WriteUChar( unsigned char nChar );
258 SvStream& WriteFloat( float nFloat );
259 SvStream& WriteDouble( double nDouble );
261 template <typename N>
262 SvStream& WriteNumberAsString( N n ) { return WriteOString(OString::number(n)); }
264 std::size_t ReadBytes( void* pData, std::size_t nSize );
265 std::size_t WriteBytes( const void* pData, std::size_t nSize );
266 sal_uInt64 Seek( sal_uInt64 nPos );
267 sal_uInt64 SeekRel( sal_Int64 nPos );
268 sal_uInt64 Tell() const { return m_nBufFilePos + m_nBufActualPos; }
269 virtual sal_uInt64 TellEnd();
270 // length between current (Tell()) pos and end of stream
271 sal_uInt64 remainingSize();
272 /// If we have data in our internal buffers, write them out
273 void FlushBuffer();
274 /// Call FlushBuffer() and then call flush on the underlying OS stream
275 void Flush();
276 // next Tell() <= nSize
277 bool SetStreamSize( sal_uInt64 nSize );
279 /** Read a line of bytes.
281 @param nMaxBytesToRead
282 Maximum of bytes to read, if line is longer it will be
283 truncated.
285 @note NOTE that the default is one character less than STRING_MAXLEN to
286 prevent problems after conversion to String that may be lurking
287 in various places doing something like
288 @code
289 for (sal_uInt16 i=0; i < aString.Len(); ++i)
290 @endcode
291 causing endless loops ...
293 bool ReadLine( OStringBuffer& rStr, sal_Int32 nMaxBytesToRead = 0xFFFE );
294 bool ReadLine( OString& rStr, sal_Int32 nMaxBytesToRead = 0xFFFE );
295 bool WriteLine( std::string_view rStr );
297 /** Read a line of bytes.
299 @param nMaxBytesToRead
300 Maximum of bytes to read, if line is longer it will be
301 truncated.
303 @note NOTE that the default is one character less than STRING_MAXLEN to
304 prevent problems after conversion to String that may be lurking
305 in various places doing something like
306 @code
307 for (sal_uInt16 i=0; i < aString.Len(); ++i)
308 @endcode
309 causing endless loops ...
311 bool ReadByteStringLine( OUString& rStr, rtl_TextEncoding eSrcCharSet,
312 sal_Int32 nMaxBytesToRead = 0xFFFE );
313 bool WriteByteStringLine( std::u16string_view rStr, rtl_TextEncoding eDestCharSet );
315 /// Switch to no endian swapping and write 0xfeff
316 void StartWritingUnicodeText();
318 /** If eReadBomCharSet==RTL_TEXTENCODING_DONTKNOW: read 16bit, if 0xfeff do
319 nothing (UTF-16), if 0xfffe switch endian swapping (UTF-16), if 0xefbb
320 or 0xbbef read another byte and check for UTF-8. If no UTF-* BOM was
321 detected put all read bytes back. This means that if 2 bytes were read
322 it was an UTF-16 BOM, if 3 bytes were read it was an UTF-8 BOM. There
323 is no UTF-7, UTF-32 or UTF-EBCDIC BOM detection!
325 If eReadBomCharSet!=RTL_TEXTENCODING_DONTKNOW: only read a BOM of that
326 encoding and switch endian swapping if UTF-16 and 0xfffe. */
327 void StartReadingUnicodeText( rtl_TextEncoding eReadBomCharSet );
329 /** Read a line of Unicode.
331 @param nMaxCodepointsToRead
332 Maximum of codepoints (UCS-2 or UTF-16 pairs, not bytes) to
333 read, if line is longer it will be truncated.
335 SAL_DLLPRIVATE bool ReadUniStringLine(OUString& rStr, sal_Int32 nMaxCodepointsToRead);
336 /** Read a 32bit length prefixed sequence of utf-16 if
337 eSrcCharSet==RTL_TEXTENCODING_UNICODE, otherwise read a 16bit length
338 prefixed sequence of bytes and convert from eSrcCharSet */
339 OUString ReadUniOrByteString(rtl_TextEncoding eSrcCharSet);
340 /** Write a 32bit length prefixed sequence of utf-16 if
341 eSrcCharSet==RTL_TEXTENCODING_UNICODE, otherwise convert to eSrcCharSet
342 and write a 16bit length prefixed sequence of bytes */
343 SvStream& WriteUniOrByteString( std::u16string_view rStr, rtl_TextEncoding eDestCharSet );
345 /** Read a line of Unicode if eSrcCharSet==RTL_TEXTENCODING_UNICODE,
346 otherwise read a line of Bytecode and convert from eSrcCharSet
348 @param nMaxCodepointsToRead
349 Maximum of codepoints (2 bytes if Unicode, bytes if not
350 Unicode) to read, if line is longer it will be truncated.
352 @note NOTE that the default is one character less than STRING_MAXLEN to
353 prevent problems after conversion to String that may be lurking in
354 various places doing something like
355 @code
356 for (sal_uInt16 i=0; i < aString.Len(); ++i)
357 @endcode
358 causing endless loops ...
360 bool ReadUniOrByteStringLine( OUString& rStr, rtl_TextEncoding eSrcCharSet,
361 sal_Int32 nMaxCodepointsToRead = 0xFFFE );
362 /** Write a sequence of Unicode characters if
363 eDestCharSet==RTL_TEXTENCODING_UNICODE, otherwise write a sequence of
364 Bytecodes converted to eDestCharSet. Write trailing zero, if bZero is true. */
365 bool WriteUnicodeOrByteText(std::u16string_view rStr, rtl_TextEncoding eDestCharSet, bool bZero = false);
366 bool WriteUnicodeOrByteText(std::u16string_view rStr)
367 { return WriteUnicodeOrByteText(rStr, GetStreamCharSet(), /*bZero*/false); }
369 /** Write a Unicode character if eDestCharSet==RTL_TEXTENCODING_UNICODE,
370 otherwise write as Bytecode converted to eDestCharSet.
372 This may result in more than one byte being written if a multi byte
373 encoding (e.g. UTF7, UTF8) is chosen. */
374 bool WriteUniOrByteChar( sal_Unicode ch, rtl_TextEncoding eDestCharSet );
375 bool WriteUniOrByteChar( sal_Unicode ch )
376 { return WriteUniOrByteChar( ch, GetStreamCharSet() ); }
378 void SetBufferSize( sal_uInt16 m_nBufSize );
379 sal_uInt16 GetBufferSize() const { return m_nBufSize; }
381 void RefreshBuffer();
383 bool IsWritable() const { return m_isWritable; }
384 StreamMode GetStreamMode() const { return m_eStreamMode; }
386 sal_Int32 GetVersion() const { return m_nVersion; }
387 void SetVersion( sal_Int32 n ) { m_nVersion = n; }
389 friend SvStream& operator<<( SvStream& rStr, SvStrPtr f ); // for Manips
391 /// end of input seen during previous i/o operation
392 bool eof() const { return m_isEof; }
394 /// stream is broken
395 bool bad() const { return GetError() != ERRCODE_NONE; }
397 /** Get state
399 If the state is good() the previous i/o operation succeeded.
401 If the state is good(), the next input operation might succeed;
402 otherwise, it will fail.
404 Applying an input operation to a stream that is not in the good() state
405 is a null operation as far as the variable being read into is concerned.
407 If we try to read into a variable v and the operation fails, the value
408 of v should be unchanged,
410 bool good() const { return !(eof() || bad()); }
412 private:
413 template <typename T> SvStream& ReadNumber(T& r);
414 template <typename T> SvStream& WriteNumber(T n);
416 template<typename T>
417 void readNumberWithoutSwap(T& rDataDest)
418 { readNumberWithoutSwap_(&rDataDest, sizeof(rDataDest)); }
420 SAL_DLLPRIVATE void readNumberWithoutSwap_(void * pDataDest, int nDataSize);
422 template<typename T>
423 void writeNumberWithoutSwap(T const & rDataSrc)
424 { writeNumberWithoutSwap_(&rDataSrc, sizeof(rDataSrc)); }
426 SAL_DLLPRIVATE void writeNumberWithoutSwap_(const void * pDataSrc, int nDataSize);
429 inline SvStream& operator<<( SvStream& rStr, SvStrPtr f )
431 (*f)(rStr);
432 return rStr;
435 TOOLS_DLLPUBLIC SvStream& endl( SvStream& rStr );
436 /// same as endl() but Unicode
437 TOOLS_DLLPUBLIC SvStream& endlu( SvStream& rStr );
438 /// call endlu() if m_eStreamCharSet==RTL_TEXTECODING_UNICODE otherwise endl()
439 TOOLS_DLLPUBLIC SvStream& endlub( SvStream& rStr );
441 /// Attempt to read nUnits 8bit units to an OString, returned OString's
442 /// length is number of units successfully read
443 TOOLS_DLLPUBLIC OString read_uInt8s_ToOString(SvStream& rStrm,
444 std::size_t nUnits);
446 /// Attempt to read nUnits 8bit units to an OUString
447 inline OUString read_uInt8s_ToOUString(SvStream& rStrm,
448 std::size_t nUnits, rtl_TextEncoding eEnc)
450 return OStringToOUString(read_uInt8s_ToOString(rStrm, nUnits), eEnc);
453 /// Attempt to read nUnits 16bit units to an OUString, returned
454 /// OUString's length is number of units successfully read
455 TOOLS_DLLPUBLIC OUString read_uInt16s_ToOUString(SvStream& rStrm,
456 std::size_t nUnits);
458 /// Attempt to read a pascal-style length (of type prefix) prefixed sequence of
459 /// 16bit units to an OUString, returned OString's length is number of
460 /// units successfully read.
461 inline OUString read_uInt16_lenPrefixed_uInt16s_ToOUString(SvStream& rStrm)
463 sal_uInt16 nUnits = 0;
464 rStrm.ReadUInt16( nUnits );
465 return read_uInt16s_ToOUString(rStrm, nUnits);
468 inline OUString read_uInt32_lenPrefixed_uInt16s_ToOUString(SvStream& rStrm)
470 sal_uInt32 nUnits = 0;
471 rStrm.ReadUInt32( nUnits );
472 return read_uInt16s_ToOUString(rStrm, nUnits);
475 /// Attempt to write a pascal-style length (of type prefix) prefixed sequence
476 /// of 16bit units from an OUString, returned value is number of bytes written
477 /// (including byte-count of prefix)
478 std::size_t write_uInt32_lenPrefixed_uInt16s_FromOUString(SvStream& rStrm,
479 std::u16string_view rStr);
481 /// Attempt to write a pascal-style length (of type prefix) prefixed sequence
482 /// of 16bit units from an OUString, returned value is number of bytes written
483 /// (including byte-count of prefix)
484 UNLESS_MERGELIBS(TOOLS_DLLPUBLIC) std::size_t write_uInt16_lenPrefixed_uInt16s_FromOUString(SvStream& rStrm,
485 std::u16string_view rStr);
487 /// Attempt to read 8bit units to an OString until a zero terminator is
488 /// encountered, returned OString's length is number of units *definitely*
489 /// successfully read, check SvStream::good() to see if null terminator was
490 /// successfully read
491 TOOLS_DLLPUBLIC OString read_zeroTerminated_uInt8s_ToOString(SvStream& rStrm);
493 /// Attempt to read 8bit units assuming source encoding eEnc to an OUString
494 /// until a zero terminator is encountered. Check SvStream::good() to see if
495 /// null terminator was successfully read
496 TOOLS_DLLPUBLIC OUString read_zeroTerminated_uInt8s_ToOUString(SvStream& rStrm, rtl_TextEncoding eEnc);
498 /// Attempt to read a pascal-style length (of type prefix) prefixed sequence of
499 /// 8bit units to an OString, returned OString's length is number of units
500 /// successfully read.
501 inline OString read_uInt32_lenPrefixed_uInt8s_ToOString(SvStream& rStrm)
503 sal_uInt32 nUnits = 0;
504 rStrm.ReadUInt32(nUnits);
505 return read_uInt8s_ToOString(rStrm, nUnits);
507 inline OString read_uInt16_lenPrefixed_uInt8s_ToOString(SvStream& rStrm)
509 sal_uInt16 nUnits = 0;
510 rStrm.ReadUInt16(nUnits);
511 return read_uInt8s_ToOString(rStrm, nUnits);
514 inline OString read_uInt8_lenPrefixed_uInt8s_ToOString(SvStream& rStrm)
516 sal_uInt8 nUnits = 0;
517 rStrm.ReadUChar(nUnits);
518 return read_uInt8s_ToOString(rStrm, nUnits);
521 inline OUString read_uInt16_lenPrefixed_uInt8s_ToOUString(SvStream& rStrm,
522 rtl_TextEncoding eEnc)
524 return OStringToOUString(read_uInt16_lenPrefixed_uInt8s_ToOString(rStrm), eEnc);
527 inline OUString read_uInt8_lenPrefixed_uInt8s_ToOUString(SvStream& rStrm,
528 rtl_TextEncoding eEnc)
530 return OStringToOUString(read_uInt8_lenPrefixed_uInt8s_ToOString(rStrm), eEnc);
533 /// Attempt to write a pascal-style length (of type prefix) prefixed
534 /// sequence of units from a string-type, returned value is number of bytes
535 /// written (including byte-count of prefix)
536 TOOLS_DLLPUBLIC std::size_t write_uInt16_lenPrefixed_uInt8s_FromOString(SvStream& rStrm,
537 std::string_view rStr);
539 /// Attempt to write a pascal-style length (of type prefix) prefixed sequence
540 /// of 8bit units from an OUString, returned value is number of bytes written
541 /// (including byte-count of prefix)
542 inline std::size_t write_uInt16_lenPrefixed_uInt8s_FromOUString(SvStream& rStrm,
543 std::u16string_view rStr,
544 rtl_TextEncoding eEnc)
546 return write_uInt16_lenPrefixed_uInt8s_FromOString(rStrm, OUStringToOString(rStr, eEnc));
549 [[nodiscard]] TOOLS_DLLPUBLIC bool checkSeek(SvStream &rSt, sal_uInt64 nOffset);
551 namespace tools
553 /// Is rUrl a file:// URL with no contents?
554 TOOLS_DLLPUBLIC bool isEmptyFileUrl(const OUString& rUrl);
557 // FileStream
559 class TOOLS_DLLPUBLIC SvFileStream final : public SvStream
561 private:
562 void* mxFileHandle = nullptr; // on windows, it is a HANDLE, otherwise, it is a oslFileHandle
563 #if defined(_WIN32)
564 sal_uInt16 nLockCounter;
565 #endif
566 OUString aFilename;
567 bool bIsOpen;
569 SvFileStream (const SvFileStream&) = delete;
570 SvFileStream & operator= (const SvFileStream&) = delete;
572 bool LockFile();
573 void UnlockFile();
575 virtual std::size_t GetData( void* pData, std::size_t nSize ) override;
576 virtual std::size_t PutData( const void* pData, std::size_t nSize ) override;
577 virtual sal_uInt64 SeekPos( sal_uInt64 nPos ) override;
578 virtual void SetSize( sal_uInt64 nSize ) override;
579 virtual void FlushData() override;
581 public:
582 // Switches to Read StreamMode on failed attempt of Write opening
583 SvFileStream( const OUString& rFileName, StreamMode eOpenMode );
584 SvFileStream();
585 virtual ~SvFileStream() override;
587 virtual void ResetError() override;
589 void Open( const OUString& rFileName, StreamMode eOpenMode );
590 void Close();
591 bool IsOpen() const { return bIsOpen; }
593 const OUString& GetFileName() const { return aFilename; }
596 // MemoryStream
598 class TOOLS_DLLPUBLIC SvMemoryStream : public SvStream
600 SvMemoryStream (const SvMemoryStream&) = delete;
601 SvMemoryStream & operator= (const SvMemoryStream&) = delete;
603 protected:
604 std::size_t nSize;
605 std::size_t nResize;
606 std::size_t nPos;
607 std::size_t nEndOfData;
608 sal_uInt8* pBuf;
609 bool bOwnsData;
611 virtual std::size_t GetData( void* pData, std::size_t nSize ) override;
612 virtual std::size_t PutData( const void* pData, std::size_t nSize ) override;
613 virtual sal_uInt64 SeekPos( sal_uInt64 nPos ) override;
614 virtual void SetSize( sal_uInt64 nSize ) override;
615 virtual void FlushData() override;
617 /// AllocateMemory must update pBuf accordingly
618 /// - pBuf: Address of new block
619 void AllocateMemory( std::size_t nSize );
621 /// ReAllocateMemory must update the following variables:
622 /// - pBuf: Address of new block
623 /// - nEndOfData: Set to nNewSize-1 , if outside of block
624 /// Set to 0 , if new block size is 0 bytes
625 /// - nSize: New block size
626 /// - nPos: Set to 0 if position outside of block
627 bool ReAllocateMemory( tools::Long nDiff );
629 /// Is called when this stream allocated the buffer or the buffer is
630 /// resized. FreeMemory may need to NULLify handles in derived classes.
631 void FreeMemory();
633 public:
634 SvMemoryStream( void* pBuf, std::size_t nSize, StreamMode eMode);
635 SvMemoryStream( std::size_t nInitSize=512, std::size_t nResize=64 );
636 virtual ~SvMemoryStream() override;
638 virtual void ResetError() override final;
640 sal_uInt64 GetSize() { return TellEnd(); }
641 std::size_t GetEndOfData() const { return nEndOfData; }
642 const void* GetData() { FlushBuffer(); return pBuf; }
644 // return the buffer currently in use, and allocate a new buffer internally
645 void* SwitchBuffer();
646 // the buffer is not owned by this class
647 void SetBuffer( void* pBuf, std::size_t nSize, std::size_t nEOF );
649 void ObjectOwnsMemory( bool bOwn ) { bOwnsData = bOwn; }
650 /// Makes the stream read-only after it was (possibly) initially writable,
651 /// without having to copy the data or change buffers.
652 /// @since LibreOffice 7.5
653 void MakeReadOnly();
654 void SetResizeOffset( std::size_t nNewResize ) { nResize = nNewResize; }
655 virtual sal_uInt64 TellEnd() override final { FlushBuffer(); return nEndOfData; }
658 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */