Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / tools / stream.hxx
blob0bc3766807fa6707c5a47aa5eaa4b5f0d38b8f74
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 #ifndef INCLUDED_TOOLS_STREAM_HXX
20 #define INCLUDED_TOOLS_STREAM_HXX
22 #include <limits>
23 #include <osl/process.h>
24 #include <tools/toolsdllapi.h>
25 #include <tools/lineend.hxx>
26 #include <tools/ref.hxx>
27 #include <vcl/errcode.hxx>
28 #include <rtl/string.hxx>
29 #include <o3tl/typed_flags_set.hxx>
31 class StreamData;
33 inline rtl_TextEncoding GetStoreCharSet( rtl_TextEncoding eEncoding )
35 if ( eEncoding == RTL_TEXTENCODING_ISO_8859_1 )
36 return RTL_TEXTENCODING_MS_1252;
37 else
38 return eEncoding;
41 // StreamTypes
43 // read, write, create,... options
44 enum class StreamMode {
45 NONE = 0x0000,
46 READ = 0x0001, ///< allow read accesses
47 WRITE = 0x0002, ///< allow write accesses
48 // file i/o
49 NOCREATE = 0x0004, ///< 1 == Don't create file
50 TRUNC = 0x0008, ///< Truncate _existing_ file to zero length
51 COPY_ON_SYMLINK = 0x0010, ///< copy-on-write for symlinks (Unix)
52 // sharing options
53 SHARE_DENYNONE = 0x0100,
54 SHARE_DENYREAD = 0x0200, // overrides denynone
55 SHARE_DENYWRITE = 0x0400, // overrides denynone
56 SHARE_DENYALL = 0x0800, // overrides denyread,write,none
57 // masks
58 READWRITE = READ | WRITE,
59 STD_READ = READ | SHARE_DENYNONE | NOCREATE,
60 STD_WRITE = WRITE | SHARE_DENYALL,
61 STD_READWRITE = READWRITE | SHARE_DENYALL
63 namespace o3tl
65 template<> struct typed_flags<StreamMode> : is_typed_flags<StreamMode, 0x0f1f> {};
68 #define STREAM_SEEK_TO_BEGIN 0L
69 #define STREAM_SEEK_TO_END SAL_MAX_UINT64
71 enum class SvStreamEndian { BIG, LITTLE };
73 enum class SvStreamCompressFlags {
74 NONE = 0x0000,
75 ZBITMAP = 0x0001,
76 NATIVE = 0x0010,
78 namespace o3tl
80 template<> struct typed_flags<SvStreamCompressFlags> : is_typed_flags<SvStreamCompressFlags, 0x0011> {};
83 class SvStream;
85 typedef SvStream& (*SvStrPtr)( SvStream& );
87 inline SvStream& operator<<( SvStream& rStr, SvStrPtr f );
89 // SvLockBytes
91 struct SvLockBytesStat
93 std::size_t nSize;
95 SvLockBytesStat() : nSize(0) {}
98 enum SvLockBytesStatFlag { SVSTATFLAG_DEFAULT };
100 class TOOLS_DLLPUBLIC SvLockBytes: public virtual SvRefBase
102 SvStream * m_pStream;
103 bool m_bOwner;
104 bool m_bSync;
106 protected:
107 void close();
109 public:
111 SvLockBytes() : m_pStream(nullptr), m_bOwner(false), m_bSync(false) {}
113 SvLockBytes(SvStream * pTheStream, bool bTheOwner = false) :
114 m_pStream(pTheStream), m_bOwner(bTheOwner), m_bSync(false) {}
116 virtual ~SvLockBytes() override { close(); }
118 const SvStream * GetStream() const { return m_pStream; }
120 void SetSynchronMode(bool bTheSync = true) { m_bSync = bTheSync; }
121 bool IsSynchronMode() const { return m_bSync; }
123 virtual ErrCode ReadAt(sal_uInt64 nPos, void * pBuffer, std::size_t nCount,
124 std::size_t * pRead) const;
125 virtual ErrCode WriteAt(sal_uInt64 nPos, const void * pBuffer, std::size_t nCount,
126 std::size_t * pWritten);
128 virtual ErrCode Flush() const;
130 virtual ErrCode SetSize(sal_uInt64 nSize);
132 virtual ErrCode Stat(SvLockBytesStat * pStat, SvLockBytesStatFlag) const;
135 typedef tools::SvRef<SvLockBytes> SvLockBytesRef;
137 // SvStream
139 class TOOLS_DLLPUBLIC SvStream
141 private:
142 // LockBytes Interface
143 SvLockBytesRef m_xLockBytes; ///< Default implementation
144 sal_uInt64 m_nActPos;
146 // buffer management
147 sal_uInt8* 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 puffer
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_isConsistent; ///< false: Buffer contains data, which were
160 ///< NOT allowed to be written by PutData
161 ///< into the derived stream (cf. PutBack)
162 bool m_isSwap;
163 bool m_isEof;
164 sal_uInt32 m_nError;
165 SvStreamEndian m_nEndian;
166 SvStreamCompressFlags m_nCompressMode;
167 LineEnd m_eLineDelimiter;
168 rtl_TextEncoding m_eStreamCharSet;
170 // Encryption
171 OString m_aCryptMaskKey;// aCryptMaskKey.getLength != 0 -> Encryption used
172 unsigned char m_nCryptMask;
174 // Userdata
175 long m_nVersion; // for external use
177 SvStream ( const SvStream& rStream ) = delete;
178 SvStream& operator=( const SvStream& rStream ) = delete;
180 protected:
181 sal_uInt64 m_nBufFilePos; ///< File position of pBuf[0]
182 StreamMode m_eStreamMode;
183 bool m_isWritable;
185 virtual std::size_t GetData( void* pData, std::size_t nSize );
186 virtual std::size_t PutData( const void* pData, std::size_t nSize );
187 virtual sal_uInt64 SeekPos( sal_uInt64 nPos );
188 virtual void FlushData();
189 virtual void SetSize(sal_uInt64 nSize);
191 void FlushBuffer(bool isConsistent);
192 void ClearError();
193 void ClearBuffer();
195 // encrypt and write in blocks
196 std::size_t CryptAndWriteBuffer( const void* pStart, std::size_t nLen );
197 bool EncryptBuffer( void* pStart, std::size_t nLen );
199 void SyncSvStream( std::size_t nNewStreamPos ); ///< SvStream <- Medium
200 void SyncSysStream(); ///< SvStream -> Medium
202 public:
203 SvStream();
204 SvStream( SvLockBytes *pLockBytes);
205 virtual ~SvStream();
207 SvLockBytes* GetLockBytes() const { return m_xLockBytes.get(); }
209 sal_uInt32 GetError() const { return ERRCODE_TOERROR(m_nError); }
210 sal_uInt32 GetErrorCode() const { return m_nError; }
212 void SetError( sal_uInt32 nErrorCode );
213 virtual void ResetError();
215 void SetEndian( SvStreamEndian SvStreamEndian );
216 SvStreamEndian GetEndian() const { return m_nEndian; }
217 /// returns status of endian swap flag
218 bool IsEndianSwap() const { return m_isSwap; }
220 void SetCompressMode( SvStreamCompressFlags nNewMode )
221 { m_nCompressMode = nNewMode; }
222 SvStreamCompressFlags GetCompressMode() const { return m_nCompressMode; }
224 void SetCryptMaskKey(const OString& rCryptMaskKey);
226 void SetStreamCharSet( rtl_TextEncoding eCharSet )
227 { m_eStreamCharSet = eCharSet; }
228 rtl_TextEncoding GetStreamCharSet() const { return m_eStreamCharSet; }
230 void SetLineDelimiter( LineEnd eLineEnd )
231 { m_eLineDelimiter = eLineEnd; }
232 LineEnd GetLineDelimiter() const { return m_eLineDelimiter; }
234 SvStream& ReadUInt16( sal_uInt16& rUInt16 );
235 SvStream& ReadUInt32( sal_uInt32& rUInt32 );
236 SvStream& ReadUInt64( sal_uInt64& rUInt64 );
237 SvStream& ReadInt16( sal_Int16& rInt16 );
238 SvStream& ReadInt32( sal_Int32& rInt32 );
239 SvStream& ReadInt64(sal_Int64 & rInt64);
240 SvStream& ReadSChar( signed char& rChar );
241 SvStream& ReadChar( char& rChar );
242 SvStream& ReadUChar( unsigned char& rChar );
243 SvStream& ReadUtf16( sal_Unicode& rUtf16 );
244 SvStream& ReadCharAsBool( bool& rBool );
245 SvStream& ReadFloat( float& rFloat );
246 SvStream& ReadDouble( double& rDouble );
247 SvStream& ReadStream( SvStream& rStream );
249 SvStream& WriteUInt16( sal_uInt16 nUInt16 );
250 SvStream& WriteUInt32( sal_uInt32 nUInt32 );
251 SvStream& WriteUInt64( sal_uInt64 nuInt64 );
252 SvStream& WriteInt16( sal_Int16 nInt16 );
253 SvStream& WriteInt32( sal_Int32 nInt32 );
254 SvStream& WriteInt64( sal_Int64 nInt64 );
255 SvStream& WriteUInt8( sal_uInt8 nuInt8 );
256 SvStream& WriteUnicode( sal_Unicode );
257 SvStream& WriteOString(const OString& rStr)
258 { return WriteCharPtr(rStr.getStr()); }
259 SvStream& WriteStream( SvStream& rStream );
261 SvStream& WriteBool( bool b )
262 { return WriteUChar(static_cast<unsigned char>(b)); }
263 SvStream& WriteSChar( signed char nChar );
264 SvStream& WriteChar( char nChar );
265 SvStream& WriteUChar( unsigned char nChar );
266 SvStream& WriteFloat( float nFloat );
267 SvStream& WriteDouble( const double& rDouble );
268 SvStream& WriteCharPtr( const char* pBuf );
270 SvStream& WriteUInt32AsString( sal_uInt32 nUInt32 );
271 SvStream& WriteInt32AsString( sal_Int32 nInt32 );
273 std::size_t ReadBytes( void* pData, std::size_t nSize );
274 std::size_t WriteBytes( const void* pData, std::size_t nSize );
275 sal_uInt64 Seek( sal_uInt64 nPos );
276 sal_uInt64 SeekRel( sal_Int64 nPos );
277 sal_uInt64 Tell() const { return m_nBufFilePos + m_nBufActualPos; }
278 // length between current (Tell()) pos and end of stream
279 virtual sal_uInt64 remainingSize();
280 void Flush();
281 bool IsEof() const { return m_isEof; }
282 // next Tell() <= nSize
283 bool SetStreamSize( sal_uInt64 nSize );
285 /** Read a line of bytes.
287 @param nMaxBytesToRead
288 Maximum of bytes to read, if line is longer it will be
289 truncated.
291 @note NOTE that the default is one character less than STRING_MAXLEN to
292 prevent problems after conversion to String that may be lurking
293 in various places doing something like
294 @code
295 for (sal_uInt16 i=0; i < aString.Len(); ++i)
296 @endcode
297 causing endless loops ...
299 virtual bool ReadLine( OString& rStr, sal_Int32 nMaxBytesToRead = 0xFFFE );
300 bool WriteLine( const OString& rStr );
302 /** Read a line of bytes.
304 @param nMaxBytesToRead
305 Maximum of bytes to read, if line is longer it will be
306 truncated.
308 @note NOTE that the default is one character less than STRING_MAXLEN to
309 prevent problems after conversion to String that may be lurking
310 in various places doing something like
311 @code
312 for (sal_uInt16 i=0; i < aString.Len(); ++i)
313 @endcode
314 causing endless loops ...
316 bool ReadByteStringLine( OUString& rStr, rtl_TextEncoding eSrcCharSet,
317 sal_Int32 nMaxBytesToRead = 0xFFFE );
318 bool WriteByteStringLine( const OUString& rStr, rtl_TextEncoding eDestCharSet );
320 /// Switch to no endian swapping and write 0xfeff
321 void StartWritingUnicodeText();
323 /** If eReadBomCharSet==RTL_TEXTENCODING_DONTKNOW: read 16bit, if 0xfeff do
324 nothing (UTF-16), if 0xfffe switch endian swapping (UTF-16), if 0xefbb
325 or 0xbbef read another byte and check for UTF-8. If no UTF-* BOM was
326 detected put all read bytes back. This means that if 2 bytes were read
327 it was an UTF-16 BOM, if 3 bytes were read it was an UTF-8 BOM. There
328 is no UTF-7, UTF-32 or UTF-EBCDIC BOM detection!
330 If eReadBomCharSet!=RTL_TEXTENCODING_DONTKNOW: only read a BOM of that
331 encoding and switch endian swapping if UTF-16 and 0xfffe. */
332 void StartReadingUnicodeText( rtl_TextEncoding eReadBomCharSet );
334 /** Read a line of Unicode.
336 @param nMaxCodepointsToRead
337 Maximum of codepoints (UCS-2 or UTF-16 pairs, not bytes) to
338 read, if line is longer it will be truncated.
340 bool ReadUniStringLine(OUString& rStr, sal_Int32 nMaxCodepointsToRead);
341 /** Read a 32bit length prefixed sequence of utf-16 if
342 eSrcCharSet==RTL_TEXTENCODING_UNICODE, otherwise read a 16bit length
343 prefixed sequence of bytes and convert from eSrcCharSet */
344 OUString ReadUniOrByteString(rtl_TextEncoding eSrcCharSet);
345 /** Write a 32bit length prefixed sequence of utf-16 if
346 eSrcCharSet==RTL_TEXTENCODING_UNICODE, otherwise convert to eSrcCharSet
347 and write a 16bit length prefixed sequence of bytes */
348 SvStream& WriteUniOrByteString( const OUString& rStr, rtl_TextEncoding eDestCharSet );
350 /** Read a line of Unicode if eSrcCharSet==RTL_TEXTENCODING_UNICODE,
351 otherwise read a line of Bytecode and convert from eSrcCharSet
353 @param nMaxCodepointsToRead
354 Maximum of codepoints (2 bytes if Unicode, bytes if not
355 Unicode) to read, if line is longer it will be truncated.
357 @note NOTE that the default is one character less than STRING_MAXLEN to
358 prevent problems after conversion to String that may be lurking in
359 various places doing something like
360 @code
361 for (sal_uInt16 i=0; i < aString.Len(); ++i)
362 @endcode
363 causing endless loops ...
365 bool ReadUniOrByteStringLine( OUString& rStr, rtl_TextEncoding eSrcCharSet,
366 sal_Int32 nMaxCodepointsToRead = 0xFFFE );
367 /** Write a sequence of Unicode characters if
368 eDestCharSet==RTL_TEXTENCODING_UNICODE, otherwise write a sequence of
369 Bytecodes converted to eDestCharSet */
370 bool WriteUnicodeOrByteText( const OUString& rStr, rtl_TextEncoding eDestCharSet );
371 bool WriteUnicodeOrByteText( const OUString& rStr )
372 { return WriteUnicodeOrByteText( rStr, GetStreamCharSet() ); }
374 /** Write a Unicode character if eDestCharSet==RTL_TEXTENCODING_UNICODE,
375 otherwise write as Bytecode converted to eDestCharSet.
377 This may result in more than one byte being written if a multi byte
378 encoding (e.g. UTF7, UTF8) is chosen. */
379 bool WriteUniOrByteChar( sal_Unicode ch, rtl_TextEncoding eDestCharSet );
380 bool WriteUniOrByteChar( sal_Unicode ch )
381 { return WriteUniOrByteChar( ch, GetStreamCharSet() ); }
383 void SetBufferSize( sal_uInt16 m_nBufSize );
384 sal_uInt16 GetBufferSize() const { return m_nBufSize; }
386 void RefreshBuffer();
388 bool IsWritable() const { return m_isWritable; }
389 StreamMode GetStreamMode() const { return m_eStreamMode; }
391 long GetVersion() { return m_nVersion; }
392 void SetVersion( long n ) { m_nVersion = n; }
394 friend SvStream& operator<<( SvStream& rStr, SvStrPtr f ); // for Manips
396 /// end of input seen during previous i/o operation
397 bool eof() const { return m_isEof; }
399 /// stream is broken
400 bool bad() const { return GetError() != 0; }
402 /** Get state
404 If the state is good() the previous i/o operation succeeded.
406 If the state is good(), the next input operation might succeed;
407 otherwise, it will fail.
409 Applying an input operation to a stream that is not in the good() state
410 is a null operation as far as the variable being read into is concerned.
412 If we try to read into a variable v and the operation fails, the value
413 of v should be unchanged,
415 virtual bool good() const { return !(eof() || bad()); }
418 inline SvStream& operator<<( SvStream& rStr, SvStrPtr f )
420 (*f)(rStr);
421 return rStr;
424 TOOLS_DLLPUBLIC SvStream& endl( SvStream& rStr );
425 /// same as endl() but Unicode
426 TOOLS_DLLPUBLIC SvStream& endlu( SvStream& rStr );
427 /// call endlu() if m_eStreamCharSet==RTL_TEXTECODING_UNICODE otherwise endl()
428 TOOLS_DLLPUBLIC SvStream& endlub( SvStream& rStr );
430 /// Attempt to read nUnits 8bit units to an OString, returned OString's
431 /// length is number of units successfully read
432 TOOLS_DLLPUBLIC OString read_uInt8s_ToOString(SvStream& rStrm,
433 std::size_t nUnits);
435 /// Attempt to read nUnits 8bit units to an OUString
436 inline OUString read_uInt8s_ToOUString(SvStream& rStrm,
437 std::size_t nUnits, rtl_TextEncoding eEnc)
439 return OStringToOUString(read_uInt8s_ToOString(rStrm, nUnits), eEnc);
442 /// Attempt to read nUnits 16bit units to an OUString, returned
443 /// OUString's length is number of units successfully read
444 TOOLS_DLLPUBLIC OUString read_uInt16s_ToOUString(SvStream& rStrm,
445 std::size_t nUnits);
447 /// Attempt to read a pascal-style length (of type prefix) prefixed sequence of
448 /// 16bit units to an OUString, returned OString's length is number of
449 /// units successfully read.
450 inline OUString read_uInt16_lenPrefixed_uInt16s_ToOUString(SvStream& rStrm)
452 sal_uInt16 nUnits = 0;
453 rStrm.ReadUInt16( nUnits );
454 return read_uInt16s_ToOUString(rStrm, nUnits);
457 inline OUString read_uInt32_lenPrefixed_uInt16s_ToOUString(SvStream& rStrm)
459 sal_uInt32 nUnits = 0;
460 rStrm.ReadUInt32( nUnits );
461 return read_uInt16s_ToOUString(rStrm, nUnits);
464 /// Attempt to write a prefixed sequence of nUnits 16bit units from an OUString,
465 /// returned value is number of bytes written
466 TOOLS_DLLPUBLIC std::size_t write_uInt16s_FromOUString(SvStream& rStrm,
467 const OUString& rStr, std::size_t nUnits);
469 inline std::size_t write_uInt16s_FromOUString(SvStream& rStrm,
470 const OUString& rStr)
472 return write_uInt16s_FromOUString(rStrm, rStr, rStr.getLength());
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 TOOLS_DLLPUBLIC std::size_t write_uInt32_lenPrefixed_uInt16s_FromOUString(SvStream& rStrm,
479 const OUString &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 TOOLS_DLLPUBLIC std::size_t write_uInt16_lenPrefixed_uInt16s_FromOUString(SvStream& rStrm,
485 const OUString &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 /// Attempt to read a pascal-style length (of type prefix) prefixed sequence of
522 /// 8bit units to an OUString
523 inline OUString read_uInt32_lenPrefixed_uInt8s_ToOUString(SvStream& rStrm,
524 rtl_TextEncoding eEnc)
526 return OStringToOUString(read_uInt32_lenPrefixed_uInt8s_ToOString(rStrm), eEnc);
529 inline OUString read_uInt16_lenPrefixed_uInt8s_ToOUString(SvStream& rStrm,
530 rtl_TextEncoding eEnc)
532 return OStringToOUString(read_uInt16_lenPrefixed_uInt8s_ToOString(rStrm), eEnc);
535 inline OUString read_uInt8_lenPrefixed_uInt8s_ToOUString(SvStream& rStrm,
536 rtl_TextEncoding eEnc)
538 return OStringToOUString(read_uInt8_lenPrefixed_uInt8s_ToOString(rStrm), eEnc);
541 /// Attempt to write a prefixed sequence of nUnits 8bit units from an OString,
542 /// returned value is number of bytes written
543 inline std::size_t write_uInt8s_FromOString(SvStream& rStrm, const OString& rStr,
544 std::size_t nUnits)
546 return rStrm.WriteBytes(rStr.getStr(), nUnits);
549 inline std::size_t write_uInt8s_FromOString(SvStream& rStrm, const OString& rStr)
551 return write_uInt8s_FromOString(rStrm, rStr, rStr.getLength());
554 /// Attempt to write a pascal-style length (of type prefix) prefixed
555 /// sequence of units from a string-type, returned value is number of bytes
556 /// written (including byte-count of prefix)
557 TOOLS_DLLPUBLIC std::size_t write_uInt16_lenPrefixed_uInt8s_FromOString(SvStream& rStrm,
558 const OString &rStr);
560 /// Attempt to write a pascal-style length (of type prefix) prefixed sequence
561 /// of 8bit units from an OUString, returned value is number of bytes written
562 /// (including byte-count of prefix)
563 inline std::size_t write_uInt16_lenPrefixed_uInt8s_FromOUString(SvStream& rStrm,
564 const OUString &rStr,
565 rtl_TextEncoding eEnc)
567 return write_uInt16_lenPrefixed_uInt8s_FromOString(rStrm, OUStringToOString(rStr, eEnc));
570 // FileStream
572 class TOOLS_DLLPUBLIC SvFileStream : public SvStream
574 private:
575 StreamData* pInstanceData;
576 OUString aFilename;
577 sal_uInt16 nLockCounter;
578 bool bIsOpen;
580 SvFileStream (const SvFileStream&) = delete;
581 SvFileStream & operator= (const SvFileStream&) = delete;
583 bool LockRange( sal_uInt64 nByteOffset, std::size_t nBytes );
584 bool UnlockRange( sal_uInt64 nByteOffset, std::size_t nBytes );
585 bool LockFile();
586 void UnlockFile();
588 protected:
589 virtual std::size_t GetData( void* pData, std::size_t nSize ) override;
590 virtual std::size_t PutData( const void* pData, std::size_t nSize ) override;
591 virtual sal_uInt64 SeekPos( sal_uInt64 nPos ) override;
592 virtual void SetSize( sal_uInt64 nSize ) override;
593 virtual void FlushData() override;
595 public:
596 // Switches to Read StreamMode on failed attempt of Write opening
597 SvFileStream( const OUString& rFileName, StreamMode eOpenMode );
598 SvFileStream();
599 virtual ~SvFileStream() override;
601 virtual void ResetError() override;
603 void Open( const OUString& rFileName, StreamMode eOpenMode );
604 void Close();
605 bool IsOpen() const { return bIsOpen; }
607 const OUString& GetFileName() const { return aFilename; }
610 // MemoryStream
612 class TOOLS_DLLPUBLIC SvMemoryStream : public SvStream
614 SvMemoryStream (const SvMemoryStream&) = delete;
615 SvMemoryStream & operator= (const SvMemoryStream&) = delete;
617 protected:
618 std::size_t nSize;
619 std::size_t nResize;
620 std::size_t nPos;
621 std::size_t nEndOfData;
622 sal_uInt8* pBuf;
623 bool bOwnsData;
625 virtual std::size_t GetData( void* pData, std::size_t nSize ) override;
626 virtual std::size_t PutData( const void* pData, std::size_t nSize ) override;
627 virtual sal_uInt64 SeekPos( sal_uInt64 nPos ) override;
628 virtual void SetSize( sal_uInt64 nSize ) override;
629 virtual void FlushData() override;
631 /// AllocateMemory must update pBuf accordingly
632 /// - pBuf: Address of new block
633 bool AllocateMemory( std::size_t nSize );
635 /// ReAllocateMemory must update the following variables:
636 /// - pBuf: Address of new block
637 /// - nEndOfData: Set to nNewSize-1L , if outside of block
638 /// Set to 0 , if new block size is 0 bytes
639 /// - nSize: New block size
640 /// - nPos: Set to 0 if position outside of block
641 bool ReAllocateMemory( long nDiff );
643 /// Is called when this stream allocated the buffer or the buffer is
644 /// resized. FreeMemory may need to NULLify handles in derived classes.
645 void FreeMemory();
647 public:
648 SvMemoryStream( void* pBuf, std::size_t nSize, StreamMode eMode);
649 SvMemoryStream( std::size_t nInitSize=512, std::size_t nResize=64 );
650 virtual ~SvMemoryStream() override;
652 virtual void ResetError() override;
654 const void* GetBuffer();
655 sal_uInt64 GetSize();
656 std::size_t GetEndOfData() const { return nEndOfData; }
657 const void* GetData() { Flush(); return pBuf; }
659 void* SwitchBuffer();
660 // the buffer is not owned by this class
661 void SetBuffer( void* pBuf, std::size_t nSize, std::size_t nEOF );
663 void ObjectOwnsMemory( bool bOwn ) { bOwnsData = bOwn; }
664 void SetResizeOffset( std::size_t nNewResize ) { nResize = nNewResize; }
665 virtual sal_uInt64 remainingSize() override { FlushBuffer(true); return GetEndOfData() - Tell(); }
668 class TOOLS_DLLPUBLIC SvScriptStream: public SvStream
670 oslProcess mpProcess;
671 oslFileHandle mpHandle;
673 public:
674 SvScriptStream(const OUString& rUrl);
675 virtual ~SvScriptStream() override;
677 /** Read a line of bytes.
679 @param nMaxBytesToRead
680 Maximum of bytes to read, if line is longer it will be
681 truncated.
683 @note NOTE that the default is one character less than STRING_MAXLEN to
684 prevent problems after conversion to String that may be lurking
685 in various places doing something like
686 @code
687 for (sal_uInt16 i=0; i < aString.Len(); ++i)
688 @endcode
689 causing endless loops ...
691 virtual bool ReadLine(OString& rStr, sal_Int32 nMaxBytesToRead = 0xFFFE ) override;
692 virtual bool good() const override;
695 #endif
697 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */