fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / include / tools / stream.hxx
blob7897d7f7efc4b6c57a0fba787a79570822ff8a9b
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 _STREAM_HXX
20 #define _STREAM_HXX
22 #include <limits>
23 #include "tools/toolsdllapi.h"
24 #include <tools/solar.h>
25 #include <tools/string.hxx>
26 #include <tools/errinf.hxx>
27 #include <tools/ref.hxx>
28 #include <tools/rtti.hxx>
29 #include <rtl/string.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 typedef sal_uInt16 StreamMode;
45 // read, write, create,... options
46 #define STREAM_READ 0x0001 ///< allow read accesses
47 #define STREAM_WRITE 0x0002 ///< allow write accesses
48 // file i/o
49 #define STREAM_NOCREATE 0x0004 ///< 1 == Dont create file
50 #define STREAM_TRUNC 0x0008 ///< Truncate _existing_ file to zero length
51 #define STREAM_COPY_ON_SYMLINK 0x0010 ///< copy-on-write for symlinks (UNX)
53 #define STREAM_READWRITEBITS (STREAM_READ | STREAM_WRITE | \
54 STREAM_NOCREATE | STREAM_TRUNC)
56 // sharing options
57 #define STREAM_SHARE_DENYNONE 0x0100
58 #define STREAM_SHARE_DENYREAD 0x0200 // overrides denynone
59 #define STREAM_SHARE_DENYWRITE 0x0400 // overrides denynone
60 #define STREAM_SHARE_DENYALL 0x0800 // overrides denyread,write,none
62 #define STREAM_SHAREBITS (STREAM_SHARE_DENYNONE | STREAM_SHARE_DENYREAD |\
63 STREAM_SHARE_DENYWRITE | STREAM_SHARE_DENYALL)
65 #define STREAM_READWRITE (STREAM_READ | STREAM_WRITE)
66 #define STREAM_SHARE_DENYREADWRITE (STREAM_SHARE_DENYREAD | STREAM_SHARE_DENYWRITE)
68 #define STREAM_STD_READ (STREAM_READ | STREAM_SHARE_DENYNONE | STREAM_NOCREATE)
69 #define STREAM_STD_WRITE (STREAM_WRITE | STREAM_SHARE_DENYALL)
70 #define STREAM_STD_READWRITE (STREAM_READWRITE | STREAM_SHARE_DENYALL)
72 #define STREAM_SEEK_TO_BEGIN 0L
73 #define STREAM_SEEK_TO_END ULONG_MAX
75 #define NUMBERFORMAT_INT_BIGENDIAN (sal_uInt16)0x0000
76 #define NUMBERFORMAT_INT_LITTLEENDIAN (sal_uInt16)0xFFFF
78 #define COMPRESSMODE_NONE (sal_uInt16)0x0000
79 #define COMPRESSMODE_ZBITMAP (sal_uInt16)0x0001
80 #define COMPRESSMODE_NATIVE (sal_uInt16)0x0010
82 #define STREAM_IO_DONTKNOW 0
83 #define STREAM_IO_READ 1
84 #define STREAM_IO_WRITE 2
86 #define ID_STREAM 1
87 #define ID_FILESTREAM 2
88 #define ID_MEMORYSTREAM 3
89 #define ID_SHAREDMEMORYSTREAM 4
90 #define ID_STORAGESTREAM 5
91 #define ID_PERSISTSTREAM 6
93 class SvStream;
95 typedef SvStream& (*SvStrPtr)( SvStream& );
97 inline SvStream& operator<<( SvStream& rStr, SvStrPtr f );
99 // SvLockBytes
101 enum LockType {};
103 struct SvLockBytesStat
105 sal_Size nSize;
107 SvLockBytesStat() : nSize(0) {}
110 enum SvLockBytesStatFlag { SVSTATFLAG_DEFAULT };
112 class TOOLS_DLLPUBLIC SvLockBytes: public virtual SvRefBase
114 SvStream * m_pStream;
115 sal_Bool m_bOwner;
116 sal_Bool m_bSync;
118 protected:
119 void close();
121 public:
122 TYPEINFO();
124 SvLockBytes() : m_pStream(0), m_bOwner(sal_False), m_bSync(sal_False) {}
126 SvLockBytes(SvStream * pTheStream, sal_Bool bTheOwner = sal_False) :
127 m_pStream(pTheStream), m_bOwner(bTheOwner), m_bSync(sal_False) {}
129 virtual ~SvLockBytes() { close(); }
131 virtual const SvStream * GetStream() const { return m_pStream; }
133 virtual void SetSynchronMode(sal_Bool bTheSync = sal_True) { m_bSync = bTheSync; }
134 virtual sal_Bool IsSynchronMode() const { return m_bSync; }
136 virtual ErrCode ReadAt(sal_Size nPos, void * pBuffer, sal_Size nCount,
137 sal_Size * pRead) const;
138 virtual ErrCode WriteAt(sal_Size nPos, const void * pBuffer, sal_Size nCount,
139 sal_Size * pWritten);
141 virtual ErrCode Flush() const;
143 virtual ErrCode SetSize(sal_Size nSize);
145 virtual ErrCode Stat(SvLockBytesStat * pStat, SvLockBytesStatFlag) const;
148 SV_DECL_IMPL_REF(SvLockBytes);
150 // SvOpenLockBytes
152 class TOOLS_DLLPUBLIC SvOpenLockBytes: public SvLockBytes
154 public:
155 TYPEINFO();
157 SvOpenLockBytes() : SvLockBytes(0, sal_False) {}
158 SvOpenLockBytes(SvStream * pStream, sal_Bool bOwner):
159 SvLockBytes(pStream, bOwner) {}
161 virtual ErrCode FillAppend(const void * pBuffer, sal_Size nCount,
162 sal_Size * pWritten) = 0;
164 virtual sal_Size Tell() const = 0;
166 virtual sal_Size Seek(sal_Size nPos) = 0;
168 virtual void Terminate() = 0;
171 SV_DECL_IMPL_REF(SvOpenLockBytes);
173 // SvAsyncLockBytes
175 class SvAsyncLockBytes: public SvOpenLockBytes
177 sal_Size m_nSize;
178 sal_Bool m_bTerminated;
180 public:
181 TYPEINFO();
183 SvAsyncLockBytes(SvStream * pStream, sal_Bool bOwner):
184 SvOpenLockBytes(pStream, bOwner), m_nSize(0), m_bTerminated(sal_False) {}
186 virtual ErrCode ReadAt(sal_Size nPos, void * pBuffer, sal_Size nCount,
187 sal_Size * pRead) const;
188 virtual ErrCode WriteAt(sal_Size nPos, const void * pBuffer, sal_Size nCount,
189 sal_Size * pWritten);
191 virtual ErrCode FillAppend(const void * pBuffer, sal_Size nCount,
192 sal_Size * pWritten);
194 virtual sal_Size Tell() const { return m_nSize; }
196 virtual sal_Size Seek(sal_Size nPos);
198 virtual void Terminate() { m_bTerminated = sal_True; }
201 SV_DECL_IMPL_REF(SvAsyncLockBytes);
203 // SvStream
205 class TOOLS_DLLPUBLIC SvStream
207 private:
208 // LockBytes Interface
209 void* pImp; // unused
210 SvLockBytesRef xLockBytes; // Default implementation
211 sal_Size nActPos;
213 // Puffer-Verwaltung
214 sal_uInt8* pRWBuf; // Points to read/write buffer
215 sal_uInt8* pBufPos; // pRWBuf + nBufActualPos
216 sal_uInt16 nBufSize; // Allocated size of buffer
217 sal_uInt16 nBufActualLen; // Length of used segment of puffer
218 // = nBufSize, if EOF did not occur
219 sal_uInt16 nBufActualPos; // current position in buffer (0..nBufSize-1)
220 sal_uInt16 nBufFree; // number of free slots in buffer to IO of type eIOMode
221 unsigned int eIOMode : 2;// STREAM_IO_*
223 // Error codes, conversion, compression, ...
224 int bIsDirty : 1; // sal_True: Stream != buffer content
225 int bIsConsistent : 1;// sal_False: Buffer contains data, which were
226 // NOT allowed to be written by PutData
227 // into the derived stream (cf. PutBack)
228 int bSwap : 1;
229 int bIsEof : 1;
230 sal_uInt32 nError;
231 sal_uInt16 nNumberFormatInt;
232 sal_uInt16 nCompressMode;
233 LineEnd eLineDelimiter;
234 CharSet eStreamCharSet;
236 // Encryption
237 OString m_aCryptMaskKey;// aCryptMaskKey.getLength != 0 -> Encryption used
238 unsigned char nCryptMask;
240 // Userdata
241 long nVersion; // for external use
243 // helper methods
244 TOOLS_DLLPRIVATE void ImpInit();
246 SvStream ( const SvStream& rStream ); // not implemented
247 SvStream& operator=( const SvStream& rStream ); // not implemented
249 protected:
250 sal_Size nBufFilePos;///< File position of pBuf[0]
251 sal_uInt16 eStreamMode;
252 sal_Bool bIsWritable;
254 virtual sal_Size GetData( void* pData, sal_Size nSize );
255 virtual sal_Size PutData( const void* pData, sal_Size nSize );
256 virtual sal_Size SeekPos( sal_Size nPos );
257 virtual void FlushData();
258 virtual void SetSize( sal_Size nSize );
260 void ClearError();
261 void ClearBuffer();
263 // encrypt and write in blocks
264 sal_Size CryptAndWriteBuffer( const void* pStart, sal_Size nLen );
265 sal_Bool EncryptBuffer( void* pStart, sal_Size nLen );
267 void SyncSvStream( sal_Size nNewStreamPos ); ///< SvStream <- Medium
268 void SyncSysStream(); ///< SvStream -> Medium
270 public:
271 SvStream();
272 SvStream( SvLockBytes *pLockBytes);
273 virtual ~SvStream();
275 SvLockBytes* GetLockBytes() const { return xLockBytes; }
277 sal_uInt32 GetError() const { return ERRCODE_TOERROR(nError); }
278 sal_uInt32 GetErrorCode() const { return nError; }
280 void SetError( sal_uInt32 nErrorCode );
281 virtual void ResetError();
283 void SetNumberFormatInt( sal_uInt16 nNewFormat );
284 sal_uInt16 GetNumberFormatInt() const { return nNumberFormatInt; }
285 /// Enable/disable swapping of endians, may be needed for Unicode import/export
286 inline void SetEndianSwap( sal_Bool bVal );
287 /// returns status of endian swap flag
288 sal_Bool IsEndianSwap() const { return 0 != bSwap; }
290 void SetCompressMode( sal_uInt16 nNewMode )
291 { nCompressMode = nNewMode; }
292 sal_uInt16 GetCompressMode() const { return nCompressMode; }
294 void SetCryptMaskKey(const OString& rCryptMaskKey);
295 const OString& GetCryptMaskKey() const { return m_aCryptMaskKey; }
297 void SetStreamCharSet( CharSet eCharSet )
298 { eStreamCharSet = eCharSet; }
299 CharSet GetStreamCharSet() const { return eStreamCharSet; }
301 void SetLineDelimiter( LineEnd eLineEnd )
302 { eLineDelimiter = eLineEnd; }
303 LineEnd GetLineDelimiter() const { return eLineDelimiter; }
305 SvStream& operator>>( sal_uInt16& rUInt16 );
306 SvStream& operator>>( sal_uInt32& rUInt32 );
307 SvStream& operator>>( sal_uInt64& rUInt64 );
308 SvStream& operator>>( sal_Int16& rInt16 );
309 SvStream& operator>>( sal_Int32& rInt32 );
310 SvStream& operator>>( sal_Int64& rInt64 ) SAL_DELETED_FUNCTION;
311 SvStream& ReadInt64(sal_Int64 & rInt64);
313 SvStream& operator>>( signed char& rChar );
314 SvStream& operator>>( char& rChar );
315 SvStream& operator>>( unsigned char& rChar );
316 SvStream& operator>>( float& rFloat );
317 SvStream& operator>>( double& rDouble );
318 SvStream& operator>>( SvStream& rStream );
320 SvStream& operator<<( sal_uInt16 nUInt16 );
321 SvStream& operator<<( sal_uInt32 nUInt32 );
322 SvStream& operator<<( sal_uInt64 nuInt64 );
323 SvStream& operator<<( sal_Int16 nInt16 );
324 SvStream& operator<<( sal_Int32 nInt32 );
325 SvStream& operator<<( sal_Int64 nInt64 ) SAL_DELETED_FUNCTION;
326 SvStream& WriteInt64(sal_Int64 nInt64);
328 SvStream& operator<<( bool b )
329 { return operator<<(static_cast< sal_Bool >(b)); }
330 SvStream& operator<<( signed char nChar );
331 SvStream& operator<<( char nChar );
332 SvStream& operator<<( unsigned char nChar );
333 SvStream& operator<<( float nFloat );
334 SvStream& operator<<( const double& rDouble );
335 SvStream& operator<<( const char* pBuf );
336 SvStream& operator<<( const unsigned char* pBuf );
337 SvStream& operator<<( SvStream& rStream );
339 SvStream& WriteNumber( sal_uInt32 nUInt32 );
340 SvStream& WriteNumber( sal_Int32 nInt32 );
342 sal_Size Read( void* pData, sal_Size nSize );
343 sal_Size Write( const void* pData, sal_Size nSize );
344 sal_Size Seek( sal_Size nPos );
345 sal_Size SeekRel( sal_sSize nPos );
346 sal_Size Tell() const { return nBufFilePos+nBufActualPos; }
347 // length between current (Tell()) pos and end of stream
348 virtual sal_Size remainingSize();
349 void Flush();
350 sal_Bool IsEof() const { return bIsEof; }
351 // next Tell() <= nSize
352 sal_Bool SetStreamSize( sal_Size nSize );
354 /** Read a line of bytes.
356 @param nMaxBytesToRead
357 Maximum of bytes to read, if line is longer it will be
358 truncated.
360 @note NOTE that the default is one character less than STRING_MAXLEN to
361 prevent problems after conversion to String that may be lurking
362 in various places doing something like
363 @code
364 for (sal_uInt16 i=0; i < aString.Len(); ++i)
365 @endcode
366 causing endless loops ...
368 sal_Bool ReadLine( OString& rStr, sal_Int32 nMaxBytesToRead = 0xFFFE );
369 sal_Bool WriteLine( const OString& rStr );
371 /** Read a line of bytes.
373 @param nMaxBytesToRead
374 Maximum of bytes to read, if line is longer it will be
375 truncated.
377 @note NOTE that the default is one character less than STRING_MAXLEN to
378 prevent problems after conversion to String that may be lurking
379 in various places doing something like
380 @code
381 for (sal_uInt16 i=0; i < aString.Len(); ++i)
382 @endcode
383 causing endless loops ...
385 sal_Bool ReadByteStringLine( OUString& rStr, rtl_TextEncoding eSrcCharSet,
386 sal_Int32 nMaxBytesToRead = 0xFFFE );
387 sal_Bool ReadByteStringLine( String& rStr, rtl_TextEncoding eSrcCharSet );
388 sal_Bool WriteByteStringLine( const String& rStr, rtl_TextEncoding eDestCharSet );
390 /// Switch to no endian swapping and write 0xfeff
391 sal_Bool StartWritingUnicodeText();
393 /** If eReadBomCharSet==RTL_TEXTENCODING_DONTKNOW: read 16bit, if 0xfeff do
394 nothing (UTF-16), if 0xfffe switch endian swapping (UTF-16), if 0xefbb
395 or 0xbbef read another byte and check for UTF-8. If no UTF-* BOM was
396 detected put all read bytes back. This means that if 2 bytes were read
397 it was an UTF-16 BOM, if 3 bytes were read it was an UTF-8 BOM. There
398 is no UTF-7, UTF-32 or UTF-EBCDIC BOM detection!
400 If eReadBomCharSet!=RTL_TEXTENCODING_DONTKNOW: only read a BOM of that
401 encoding and switch endian swapping if UTF-16 and 0xfffe. */
402 sal_Bool StartReadingUnicodeText( rtl_TextEncoding eReadBomCharSet );
404 /** Read a line of Unicode.
406 @param nMaxCodepointsToRead
407 Maximum of codepoints (UCS-2 or UTF-16 pairs, not bytes) to
408 read, if line is longer it will be truncated.
410 @note NOTE that the default is one character less than STRING_MAXLEN to
411 prevent problems after conversion to String that may be lurking in
412 various places doing something like
413 @code
414 for (sal_uInt16 i=0; i < aString.Len(); ++i)
415 @endcode
416 causing endless loops ...
418 sal_Bool ReadUniStringLine( OUString& rStr, sal_Int32 nMaxCodepointsToRead = 0xFFFE );
419 /** Read a 32bit length prefixed sequence of utf-16 if
420 eSrcCharSet==RTL_TEXTENCODING_UNICODE, otherwise read a 16bit length
421 prefixed sequence of bytes and convert from eSrcCharSet */
422 OUString ReadUniOrByteString(rtl_TextEncoding eSrcCharSet);
423 /** Write a 32bit length prefixed sequence of utf-16 if
424 eSrcCharSet==RTL_TEXTENCODING_UNICODE, otherwise convert to eSrcCharSet
425 and write a 16bit length prefixed sequence of bytes */
426 SvStream& WriteUniOrByteString( const OUString& rStr, rtl_TextEncoding eDestCharSet );
428 /** Read a line of Unicode if eSrcCharSet==RTL_TEXTENCODING_UNICODE,
429 otherwise read a line of Bytecode and convert from eSrcCharSet
431 @param nMaxCodepointsToRead
432 Maximum of codepoints (2 bytes if Unicode, bytes if not
433 Unicode) to read, if line is longer it will be truncated.
435 @note NOTE that the default is one character less than STRING_MAXLEN to
436 prevent problems after conversion to String that may be lurking in
437 various places doing something like
438 @code
439 for (sal_uInt16 i=0; i < aString.Len(); ++i)
440 @endcode
441 causing endless loops ...
443 sal_Bool ReadUniOrByteStringLine( OUString& rStr, rtl_TextEncoding eSrcCharSet,
444 sal_Int32 nMaxCodepointsToRead = 0xFFFE );
445 /** Write a sequence of Unicode characters if
446 eDestCharSet==RTL_TEXTENCODING_UNICODE, otherwise write a sequence of
447 Bytecodes converted to eDestCharSet */
448 sal_Bool WriteUnicodeOrByteText( const String& rStr, rtl_TextEncoding eDestCharSet );
449 sal_Bool WriteUnicodeOrByteText( const String& rStr )
450 { return WriteUnicodeOrByteText( rStr, GetStreamCharSet() ); }
452 /** Write a Unicode character if eDestCharSet==RTL_TEXTENCODING_UNICODE,
453 otherwise write as Bytecode converted to eDestCharSet.
455 This may result in more than one byte being written if a multi byte
456 encoding (e.g. UTF7, UTF8) is chosen. */
457 sal_Bool WriteUniOrByteChar( sal_Unicode ch, rtl_TextEncoding eDestCharSet );
458 sal_Bool WriteUniOrByteChar( sal_Unicode ch )
459 { return WriteUniOrByteChar( ch, GetStreamCharSet() ); }
461 void SetBufferSize( sal_uInt16 nBufSize );
462 sal_uInt16 GetBufferSize() const { return nBufSize; }
464 void RefreshBuffer();
465 SvStream& PutBack( char aCh );
467 sal_Bool IsWritable() const { return bIsWritable; }
468 StreamMode GetStreamMode() const { return eStreamMode; }
469 virtual sal_uInt16 IsA() const;
471 long GetVersion() { return nVersion; }
472 void SetVersion( long n ) { nVersion = n; }
474 friend SvStream& operator<<( SvStream& rStr, SvStrPtr f ); // for Manips
476 /// end of input seen during previous i/o operation
477 bool eof() const { return bIsEof; }
479 /// stream is broken
480 bool bad() const { return GetError() != 0; }
482 /** Get state
484 If the state is good() the previous i/o operation succeeded.
486 If the state is good(), the next input operation might succeed;
487 otherwise, it will fail.
489 Applying an input operation to a stream that is not in the good() state
490 is a null operation as far as the variable being read into is concerned.
492 If we try to read into a variable v and the operation fails, the value
493 of v should be unchanged,
495 bool good() const { return !(eof() || bad()); }
498 inline SvStream& operator<<( SvStream& rStr, SvStrPtr f )
500 (*f)(rStr);
501 return rStr;
504 inline void SvStream::SetEndianSwap( sal_Bool bVal )
506 #ifdef OSL_BIGENDIAN
507 SetNumberFormatInt( bVal ? NUMBERFORMAT_INT_LITTLEENDIAN : NUMBERFORMAT_INT_BIGENDIAN );
508 #else
509 SetNumberFormatInt( bVal ? NUMBERFORMAT_INT_BIGENDIAN : NUMBERFORMAT_INT_LITTLEENDIAN );
510 #endif
513 TOOLS_DLLPUBLIC SvStream& endl( SvStream& rStr );
514 /// same as endl() but Unicode
515 TOOLS_DLLPUBLIC SvStream& endlu( SvStream& rStr );
516 /// call endlu() if eStreamCharSet==RTL_TEXTECODING_UNICODE otherwise endl()
517 TOOLS_DLLPUBLIC SvStream& endlub( SvStream& rStr );
519 /// Attempt to read nUnits 8bit units to an OString, returned OString's
520 /// length is number of units successfully read
521 TOOLS_DLLPUBLIC OString read_uInt8s_ToOString(SvStream& rStrm,
522 sal_Size nUnits);
524 /// Attempt to read nUnits 8bit units to an OUString
525 TOOLS_DLLPUBLIC inline OUString read_uInt8s_ToOUString(SvStream& rStrm,
526 sal_Size nUnits, rtl_TextEncoding eEnc)
528 return OStringToOUString(read_uInt8s_ToOString(rStrm, nUnits), eEnc);
531 /// Attempt to read nUnits 16bit units to an OUString, returned
532 /// OUString's length is number of units successfully read
533 TOOLS_DLLPUBLIC OUString read_uInt16s_ToOUString(SvStream& rStrm,
534 sal_Size nUnits);
536 /// Attempt to read a pascal-style length (of type prefix) prefixed sequence of
537 /// 16bit units to an OUString, returned OString's length is number of
538 /// units successfully read.
539 template<typename prefix>
540 OUString read_lenPrefixed_uInt16s_ToOUString(SvStream& rStrm)
542 prefix nUnits = 0;
543 rStrm >> nUnits;
544 return read_uInt16s_ToOUString(rStrm, nUnits);
547 /// Attempt to write a prefixed sequence of nUnits 16bit units from an OUString,
548 /// returned value is number of bytes written
549 TOOLS_DLLPUBLIC sal_Size write_uInt16s_FromOUString(SvStream& rStrm,
550 const OUString& rStr, sal_Size nUnits);
552 TOOLS_DLLPUBLIC inline sal_Size write_uInt16s_FromOUString(SvStream& rStrm,
553 const OUString& rStr)
555 return write_uInt16s_FromOUString(rStrm, rStr, rStr.getLength());
558 namespace streamdetail
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 template<typename prefix, typename S, sal_Size (*writeOper)(SvStream&, const S&, sal_Size)>
564 sal_Size write_lenPrefixed_seq_From_str(SvStream& rStrm, const S &rStr)
566 sal_Size nWritten = 0;
567 prefix nUnits = std::min<sal_Size>(rStr.getLength(), std::numeric_limits<prefix>::max());
568 SAL_WARN_IF(static_cast<sal_Size>(nUnits) != static_cast<sal_Size>(rStr.getLength()),
569 "tools.stream",
570 "string too long for prefix count to fit in output type");
571 rStrm << nUnits;
572 if (rStrm.good())
574 nWritten += sizeof(prefix);
575 nWritten += writeOper(rStrm, rStr, nUnits);
577 return nWritten;
581 /// Attempt to write a pascal-style length (of type prefix) prefixed sequence
582 /// of 16bit units from an OUString, returned value is number of bytes written
583 /// (including byte-count of prefix)
584 template<typename prefix> sal_Size write_lenPrefixed_uInt16s_FromOUString(SvStream& rStrm,
585 const OUString &rStr)
587 return streamdetail::write_lenPrefixed_seq_From_str<prefix, OUString, write_uInt16s_FromOUString>(rStrm, rStr);
590 /// Attempt to read 8bit units to an OString until a zero terminator is
591 /// encountered, returned OString's length is number of units *definitely*
592 /// successfully read, check SvStream::good() to see if null terminator was
593 /// successfully read
594 TOOLS_DLLPUBLIC OString read_zeroTerminated_uInt8s_ToOString(SvStream& rStrm);
596 /// Attempt to read 8bit units assuming source encoding eEnc to an OUString
597 /// until a zero terminator is encountered. Check SvStream::good() to see if
598 /// null terminator was successfully read
599 TOOLS_DLLPUBLIC OUString read_zeroTerminated_uInt8s_ToOUString(SvStream& rStrm, rtl_TextEncoding eEnc);
601 /// Attempt to read a pascal-style length (of type prefix) prefixed sequence of
602 /// 8bit units to an OString, returned OString's length is number of units
603 /// successfully read.
604 template<typename prefix>
605 OString read_lenPrefixed_uInt8s_ToOString(SvStream& rStrm)
607 prefix nUnits = 0;
608 rStrm >> nUnits;
609 return read_uInt8s_ToOString(rStrm, nUnits);
612 /// Attempt to read a pascal-style length (of type prefix) prefixed sequence of
613 /// 8bit units to an OUString
614 template<typename prefix>
615 OUString read_lenPrefixed_uInt8s_ToOUString(SvStream& rStrm,
616 rtl_TextEncoding eEnc)
618 return OStringToOUString(read_lenPrefixed_uInt8s_ToOString<prefix>(rStrm), eEnc);
621 /// Attempt to write a prefixed sequence of nUnits 8bit units from an OString,
622 /// returned value is number of bytes written
623 TOOLS_DLLPUBLIC inline sal_Size write_uInt8s_FromOString(SvStream& rStrm, const OString& rStr,
624 sal_Size nUnits)
626 return rStrm.Write(rStr.getStr(), nUnits);
629 TOOLS_DLLPUBLIC inline sal_Size write_uInt8s_FromOString(SvStream& rStrm, const OString& rStr)
631 return write_uInt8s_FromOString(rStrm, rStr, rStr.getLength());
634 /// Attempt to write a pascal-style length (of type prefix) prefixed sequence
635 /// of 8bit units from an OString, returned value is number of bytes written
636 /// (including byte-count of prefix)
637 template<typename prefix> sal_Size write_lenPrefixed_uInt8s_FromOString(SvStream& rStrm,
638 const OString &rStr)
640 return streamdetail::write_lenPrefixed_seq_From_str<prefix, OString, write_uInt8s_FromOString>(rStrm, rStr);
643 /// Attempt to write a pascal-style length (of type prefix) prefixed sequence
644 /// of 8bit units from an OUString, returned value is number of bytes written
645 /// (including byte-count of prefix)
646 template<typename prefix> sal_Size write_lenPrefixed_uInt8s_FromOUString(SvStream& rStrm,
647 const OUString &rStr, rtl_TextEncoding eEnc)
649 return write_lenPrefixed_uInt8s_FromOString<prefix>(rStrm, OUStringToOString(rStr, eEnc));
652 // FileStream
654 class TOOLS_DLLPUBLIC SvFileStream : public SvStream
656 private:
657 StreamData* pInstanceData;
658 String aFilename;
659 sal_uInt16 nLockCounter;
660 sal_Bool bIsOpen;
661 #ifdef UNX
662 sal_uInt32 GetFileHandle() const;
663 #endif
664 // Forbidden and not implemented.
665 SvFileStream (const SvFileStream&);
666 SvFileStream & operator= (const SvFileStream&);
668 sal_Bool LockRange( sal_Size nByteOffset, sal_Size nBytes );
669 sal_Bool UnlockRange( sal_Size nByteOffset, sal_Size nBytes );
670 sal_Bool LockFile();
671 sal_Bool UnlockFile();
673 protected:
674 virtual sal_Size GetData( void* pData, sal_Size nSize );
675 virtual sal_Size PutData( const void* pData, sal_Size nSize );
676 virtual sal_Size SeekPos( sal_Size nPos );
677 virtual void SetSize( sal_Size nSize );
678 virtual void FlushData();
680 public:
681 // Switches to Read StreamMode on failed attempt of Write opening
682 SvFileStream( const String& rFileName, StreamMode eOpenMode );
683 SvFileStream();
684 ~SvFileStream();
686 virtual void ResetError();
688 void Open( const String& rFileName, StreamMode eOpenMode );
689 void Close();
690 sal_Bool IsOpen() const { return bIsOpen; }
691 sal_Bool IsLocked() const { return ( nLockCounter!=0 ); }
692 virtual sal_uInt16 IsA() const;
694 const String& GetFileName() const { return aFilename; }
697 // MemoryStream
699 class TOOLS_DLLPUBLIC SvMemoryStream : public SvStream
701 // Forbidden and not implemented.
702 SvMemoryStream (const SvMemoryStream&);
703 SvMemoryStream & operator= (const SvMemoryStream&);
705 friend class SvCacheStream;
706 sal_Size GetSize() const { return nSize; }
708 protected:
709 sal_Size nSize;
710 sal_Size nResize;
711 sal_Size nPos;
712 sal_Size nEndOfData;
713 sal_uInt8* pBuf;
714 sal_Bool bOwnsData;
716 virtual sal_Size GetData( void* pData, sal_Size nSize );
717 virtual sal_Size PutData( const void* pData, sal_Size nSize );
718 virtual sal_Size SeekPos( sal_Size nPos );
719 virtual void SetSize( sal_Size nSize );
720 virtual void FlushData();
722 /// AllocateMemory must update pBuf accordingly
723 /// - pBuf: Address of new block
724 virtual sal_Bool AllocateMemory( sal_Size nSize );
726 /// ReAllocateMemory must update the following variables:
727 /// - pBuf: Address of new block
728 /// - nEndOfData: Set to nNewSize-1L , if outside of block
729 /// Set to 0 , if new block size is 0 bytes
730 /// - nSize: New block size
731 /// - nPos: Set to 0 if position outside of block
732 virtual sal_Bool ReAllocateMemory( long nDiff );
734 /// Is called when this stream allocated the buffer or the buffer is
735 /// resized. FreeMemory may need to NULLify handles in derived classes.
736 virtual void FreeMemory();
738 SvMemoryStream(void*) { } // for sub-classes
740 public:
741 SvMemoryStream( void* pBuf, sal_Size nSize, StreamMode eMode);
742 SvMemoryStream( sal_Size nInitSize=512, sal_Size nResize=64 );
743 ~SvMemoryStream();
745 virtual void ResetError();
747 sal_Size GetEndOfData() const { return nEndOfData; }
748 const void* GetData() { Flush(); return pBuf; }
749 operator const void*() { Flush(); return pBuf; }
750 virtual sal_uInt16 IsA() const;
752 void* SwitchBuffer( sal_Size nInitSize=512, sal_Size nResize=64 );
753 void* SetBuffer( void* pBuf, sal_Size nSize,
754 sal_Bool bOwnsData=sal_True, sal_Size nEOF=0 );
756 void ObjectOwnsMemory( sal_Bool bOwn ) { bOwnsData = bOwn; }
757 sal_Bool IsObjectMemoryOwner() { return bOwnsData; }
758 void SetResizeOffset( sal_Size nNewResize ) { nResize = nNewResize; }
759 sal_Size GetResizeOffset() const { return nResize; }
760 virtual sal_Size remainingSize() { return GetSize() - Tell(); }
763 /** Data Copy Stream
765 This class is the foundation for all classes, using SvData
766 (SO2\DTRANS.HXX/CXX) for transportation (e.g., graphics).
768 class TOOLS_DLLPUBLIC SvDataCopyStream
770 public:
771 // repeated execution of Load or Assign is allowed
772 TYPEINFO();
773 virtual ~SvDataCopyStream(){}
774 virtual void Load( SvStream & ) = 0;
775 virtual void Save( SvStream & ) = 0;
776 virtual void Assign( const SvDataCopyStream & );
779 #endif
781 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */