bump product version to 4.2.0.1
[LibreOffice.git] / include / tools / stream.hxx
blobe390407a274e51f74acf9ba10d83b0bd3bd5fe6e
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/solar.h>
26 #include <tools/lineend.hxx>
27 #include <tools/errinf.hxx>
28 #include <tools/ref.hxx>
29 #include <tools/rtti.hxx>
30 #include <rtl/string.hxx>
32 class StreamData;
34 inline rtl_TextEncoding GetStoreCharSet( rtl_TextEncoding eEncoding )
36 if ( eEncoding == RTL_TEXTENCODING_ISO_8859_1 )
37 return RTL_TEXTENCODING_MS_1252;
38 else
39 return eEncoding;
42 // StreamTypes
44 typedef sal_uInt16 StreamMode;
46 // read, write, create,... options
47 #define STREAM_READ 0x0001 ///< allow read accesses
48 #define STREAM_WRITE 0x0002 ///< allow write accesses
49 // file i/o
50 #define STREAM_NOCREATE 0x0004 ///< 1 == Dont create file
51 #define STREAM_TRUNC 0x0008 ///< Truncate _existing_ file to zero length
52 #define STREAM_COPY_ON_SYMLINK 0x0010 ///< copy-on-write for symlinks (Unix)
54 #define STREAM_READWRITEBITS (STREAM_READ | STREAM_WRITE | \
55 STREAM_NOCREATE | STREAM_TRUNC)
57 // sharing options
58 #define STREAM_SHARE_DENYNONE 0x0100
59 #define STREAM_SHARE_DENYREAD 0x0200 // overrides denynone
60 #define STREAM_SHARE_DENYWRITE 0x0400 // overrides denynone
61 #define STREAM_SHARE_DENYALL 0x0800 // overrides denyread,write,none
63 #define STREAM_SHAREBITS (STREAM_SHARE_DENYNONE | STREAM_SHARE_DENYREAD |\
64 STREAM_SHARE_DENYWRITE | STREAM_SHARE_DENYALL)
66 #define STREAM_READWRITE (STREAM_READ | STREAM_WRITE)
67 #define STREAM_SHARE_DENYREADWRITE (STREAM_SHARE_DENYREAD | STREAM_SHARE_DENYWRITE)
69 #define STREAM_STD_READ (STREAM_READ | STREAM_SHARE_DENYNONE | STREAM_NOCREATE)
70 #define STREAM_STD_WRITE (STREAM_WRITE | STREAM_SHARE_DENYALL)
71 #define STREAM_STD_READWRITE (STREAM_READWRITE | STREAM_SHARE_DENYALL)
73 #define STREAM_SEEK_TO_BEGIN 0L
74 #define STREAM_SEEK_TO_END ULONG_MAX
76 #define NUMBERFORMAT_INT_BIGENDIAN (sal_uInt16)0x0000
77 #define NUMBERFORMAT_INT_LITTLEENDIAN (sal_uInt16)0xFFFF
79 #define COMPRESSMODE_NONE (sal_uInt16)0x0000
80 #define COMPRESSMODE_ZBITMAP (sal_uInt16)0x0001
81 #define COMPRESSMODE_NATIVE (sal_uInt16)0x0010
83 #define ID_STREAM 1
84 #define ID_FILESTREAM 2
85 #define ID_MEMORYSTREAM 3
86 #define ID_SHAREDMEMORYSTREAM 4
87 #define ID_STORAGESTREAM 5
88 #define ID_PERSISTSTREAM 6
90 class SvStream;
92 typedef SvStream& (*SvStrPtr)( SvStream& );
94 inline SvStream& operator<<( SvStream& rStr, SvStrPtr f );
96 // SvLockBytes
98 enum LockType {};
100 struct SvLockBytesStat
102 sal_Size nSize;
104 SvLockBytesStat() : nSize(0) {}
107 enum SvLockBytesStatFlag { SVSTATFLAG_DEFAULT };
109 class TOOLS_DLLPUBLIC SvLockBytes: public virtual SvRefBase
111 SvStream * m_pStream;
112 bool m_bOwner;
113 bool m_bSync;
115 protected:
116 void close();
118 public:
119 TYPEINFO();
121 SvLockBytes() : m_pStream(0), m_bOwner(false), m_bSync(false) {}
123 SvLockBytes(SvStream * pTheStream, bool bTheOwner = false) :
124 m_pStream(pTheStream), m_bOwner(bTheOwner), m_bSync(false) {}
126 virtual ~SvLockBytes() { close(); }
128 virtual const SvStream * GetStream() const { return m_pStream; }
130 virtual void SetSynchronMode(bool bTheSync = true) { m_bSync = bTheSync; }
131 virtual bool IsSynchronMode() const { return m_bSync; }
133 virtual ErrCode ReadAt(sal_Size nPos, void * pBuffer, sal_Size nCount,
134 sal_Size * pRead) const;
135 virtual ErrCode WriteAt(sal_Size nPos, const void * pBuffer, sal_Size nCount,
136 sal_Size * pWritten);
138 virtual ErrCode Flush() const;
140 virtual ErrCode SetSize(sal_Size nSize);
142 virtual ErrCode Stat(SvLockBytesStat * pStat, SvLockBytesStatFlag) const;
145 SV_DECL_IMPL_REF(SvLockBytes);
147 // SvOpenLockBytes
149 class TOOLS_DLLPUBLIC SvOpenLockBytes: public SvLockBytes
151 public:
152 TYPEINFO();
154 SvOpenLockBytes() : SvLockBytes(0, false) {}
155 SvOpenLockBytes(SvStream * pStream, bool bOwner):
156 SvLockBytes(pStream, bOwner) {}
158 virtual ErrCode FillAppend(const void * pBuffer, sal_Size nCount,
159 sal_Size * pWritten) = 0;
161 virtual sal_Size Tell() const = 0;
163 virtual sal_Size Seek(sal_Size nPos) = 0;
165 virtual void Terminate() = 0;
168 SV_DECL_IMPL_REF(SvOpenLockBytes);
170 // SvAsyncLockBytes
172 class SvAsyncLockBytes: public SvOpenLockBytes
174 sal_Size m_nSize;
175 bool m_bTerminated;
177 public:
178 TYPEINFO();
180 SvAsyncLockBytes(SvStream * pStream, bool bOwner):
181 SvOpenLockBytes(pStream, bOwner), m_nSize(0), m_bTerminated(false) {}
183 virtual ErrCode ReadAt(sal_Size nPos, void * pBuffer, sal_Size nCount,
184 sal_Size * pRead) const;
185 virtual ErrCode WriteAt(sal_Size nPos, const void * pBuffer, sal_Size nCount,
186 sal_Size * pWritten);
188 virtual ErrCode FillAppend(const void * pBuffer, sal_Size nCount,
189 sal_Size * pWritten);
191 virtual sal_Size Tell() const { return m_nSize; }
193 virtual sal_Size Seek(sal_Size nPos);
195 virtual void Terminate() { m_bTerminated = true; }
198 SV_DECL_IMPL_REF(SvAsyncLockBytes);
200 // SvStream
202 class TOOLS_DLLPUBLIC SvStream
204 private:
205 // LockBytes Interface
206 void* pImp; // unused
207 SvLockBytesRef xLockBytes; // Default implementation
208 sal_Size nActPos;
210 // Puffer-Verwaltung
211 sal_uInt8* pRWBuf; // Points to read/write buffer
212 sal_uInt8* pBufPos; // pRWBuf + nBufActualPos
213 sal_uInt16 nBufSize; // Allocated size of buffer
214 sal_uInt16 nBufActualLen; // Length of used segment of puffer
215 // = nBufSize, if EOF did not occur
216 sal_uInt16 nBufActualPos; // current position in buffer (0..nBufSize-1)
217 sal_uInt16 nBufFree; // number of free slots in buffer to IO of type eIOMode
218 bool bIoRead;
219 bool bIoWrite;
221 // Error codes, conversion, compression, ...
222 bool bIsDirty; // true: Stream != buffer content
223 bool bIsConsistent;// false: Buffer contains data, which were
224 // NOT allowed to be written by PutData
225 // into the derived stream (cf. PutBack)
226 bool bSwap;
227 bool bIsEof;
228 sal_uInt32 nError;
229 sal_uInt16 nNumberFormatInt;
230 sal_uInt16 nCompressMode;
231 LineEnd eLineDelimiter;
232 rtl_TextEncoding eStreamCharSet;
234 // Encryption
235 OString m_aCryptMaskKey;// aCryptMaskKey.getLength != 0 -> Encryption used
236 unsigned char nCryptMask;
238 // Userdata
239 long nVersion; // for external use
241 // helper methods
242 TOOLS_DLLPRIVATE void ImpInit();
244 SvStream ( const SvStream& rStream ); // not implemented
245 SvStream& operator=( const SvStream& rStream ); // not implemented
247 protected:
248 sal_Size nBufFilePos;///< File position of pBuf[0]
249 sal_uInt16 eStreamMode;
250 bool bIsWritable;
252 virtual sal_Size GetData( void* pData, sal_Size nSize );
253 virtual sal_Size PutData( const void* pData, sal_Size nSize );
254 virtual sal_Size SeekPos( sal_Size nPos );
255 virtual void FlushData();
256 virtual void SetSize( sal_Size nSize );
258 void ClearError();
259 void ClearBuffer();
261 // encrypt and write in blocks
262 sal_Size CryptAndWriteBuffer( const void* pStart, sal_Size nLen );
263 bool EncryptBuffer( void* pStart, sal_Size nLen );
265 void SyncSvStream( sal_Size nNewStreamPos ); ///< SvStream <- Medium
266 void SyncSysStream(); ///< SvStream -> Medium
268 public:
269 SvStream();
270 SvStream( SvLockBytes *pLockBytes);
271 virtual ~SvStream();
273 SvLockBytes* GetLockBytes() const { return xLockBytes; }
275 sal_uInt32 GetError() const { return ERRCODE_TOERROR(nError); }
276 sal_uInt32 GetErrorCode() const { return nError; }
278 void SetError( sal_uInt32 nErrorCode );
279 virtual void ResetError();
281 void SetNumberFormatInt( sal_uInt16 nNewFormat );
282 sal_uInt16 GetNumberFormatInt() const { return nNumberFormatInt; }
283 /// Enable/disable swapping of endians, may be needed for Unicode import/export
284 inline void SetEndianSwap( bool bVal );
285 /// returns status of endian swap flag
286 bool IsEndianSwap() const { return 0 != bSwap; }
288 void SetCompressMode( sal_uInt16 nNewMode )
289 { nCompressMode = nNewMode; }
290 sal_uInt16 GetCompressMode() const { return nCompressMode; }
292 void SetCryptMaskKey(const OString& rCryptMaskKey);
293 const OString& GetCryptMaskKey() const { return m_aCryptMaskKey; }
295 void SetStreamCharSet( rtl_TextEncoding eCharSet )
296 { eStreamCharSet = eCharSet; }
297 rtl_TextEncoding GetStreamCharSet() const { return eStreamCharSet; }
299 void SetLineDelimiter( LineEnd eLineEnd )
300 { eLineDelimiter = eLineEnd; }
301 LineEnd GetLineDelimiter() const { return eLineDelimiter; }
303 SvStream& operator>>( sal_uInt16& rUInt16 );
304 SvStream& operator>>( sal_uInt32& rUInt32 );
305 SvStream& operator>>( sal_uInt64& rUInt64 );
306 SvStream& operator>>( sal_Int16& rInt16 );
307 SvStream& operator>>( sal_Int32& rInt32 );
308 SvStream& operator>>( sal_Int64& rInt64 ) SAL_DELETED_FUNCTION;
309 SvStream& ReadInt64(sal_Int64 & rInt64);
311 SvStream& operator>>( signed char& rChar );
312 SvStream& operator>>( char& rChar );
313 SvStream& operator>>( unsigned char& rChar );
314 SvStream& operator>>( float& rFloat );
315 SvStream& operator>>( double& rDouble );
316 SvStream& operator>>( SvStream& rStream );
318 SvStream& operator<<( sal_uInt16 nUInt16 );
319 SvStream& operator<<( sal_uInt32 nUInt32 );
320 SvStream& operator<<( sal_uInt64 nuInt64 );
321 SvStream& operator<<( sal_Int16 nInt16 );
322 SvStream& operator<<( sal_Int32 nInt32 );
323 SvStream& operator<<( sal_Int64 nInt64 ) SAL_DELETED_FUNCTION;
324 SvStream& WriteInt64(sal_Int64 nInt64);
326 SvStream& operator<<( bool b )
327 { return operator<<(static_cast< sal_Bool >(b)); }
328 SvStream& operator<<( signed char nChar );
329 SvStream& operator<<( char nChar );
330 SvStream& operator<<( unsigned char nChar );
331 SvStream& operator<<( float nFloat );
332 SvStream& operator<<( const double& rDouble );
333 SvStream& operator<<( const char* pBuf );
334 SvStream& operator<<( const unsigned char* pBuf );
335 SvStream& operator<<( SvStream& rStream );
337 SvStream& WriteNumber( sal_uInt32 nUInt32 );
338 SvStream& WriteNumber( sal_Int32 nInt32 );
340 sal_Size Read( void* pData, sal_Size nSize );
341 sal_Size Write( const void* pData, sal_Size nSize );
342 sal_Size Seek( sal_Size nPos );
343 sal_Size SeekRel( sal_sSize nPos );
344 sal_Size Tell() const { return nBufFilePos+nBufActualPos; }
345 // length between current (Tell()) pos and end of stream
346 virtual sal_Size remainingSize();
347 void Flush();
348 bool IsEof() const { return bIsEof; }
349 // next Tell() <= nSize
350 bool SetStreamSize( sal_Size nSize );
352 /** Read a line of bytes.
354 @param nMaxBytesToRead
355 Maximum of bytes to read, if line is longer it will be
356 truncated.
358 @note NOTE that the default is one character less than STRING_MAXLEN to
359 prevent problems after conversion to String that may be lurking
360 in various places doing something like
361 @code
362 for (sal_uInt16 i=0; i < aString.Len(); ++i)
363 @endcode
364 causing endless loops ...
366 virtual bool ReadLine( OString& rStr, sal_Int32 nMaxBytesToRead = 0xFFFE );
367 bool WriteLine( const OString& rStr );
369 /** Read a line of bytes.
371 @param nMaxBytesToRead
372 Maximum of bytes to read, if line is longer it will be
373 truncated.
375 @note NOTE that the default is one character less than STRING_MAXLEN to
376 prevent problems after conversion to String that may be lurking
377 in various places doing something like
378 @code
379 for (sal_uInt16 i=0; i < aString.Len(); ++i)
380 @endcode
381 causing endless loops ...
383 bool ReadByteStringLine( OUString& rStr, rtl_TextEncoding eSrcCharSet,
384 sal_Int32 nMaxBytesToRead = 0xFFFE );
385 bool WriteByteStringLine( const OUString& rStr, rtl_TextEncoding eDestCharSet );
387 /// Switch to no endian swapping and write 0xfeff
388 bool StartWritingUnicodeText();
390 /** If eReadBomCharSet==RTL_TEXTENCODING_DONTKNOW: read 16bit, if 0xfeff do
391 nothing (UTF-16), if 0xfffe switch endian swapping (UTF-16), if 0xefbb
392 or 0xbbef read another byte and check for UTF-8. If no UTF-* BOM was
393 detected put all read bytes back. This means that if 2 bytes were read
394 it was an UTF-16 BOM, if 3 bytes were read it was an UTF-8 BOM. There
395 is no UTF-7, UTF-32 or UTF-EBCDIC BOM detection!
397 If eReadBomCharSet!=RTL_TEXTENCODING_DONTKNOW: only read a BOM of that
398 encoding and switch endian swapping if UTF-16 and 0xfffe. */
399 bool StartReadingUnicodeText( rtl_TextEncoding eReadBomCharSet );
401 /** Read a line of Unicode.
403 @param nMaxCodepointsToRead
404 Maximum of codepoints (UCS-2 or UTF-16 pairs, not bytes) to
405 read, if line is longer it will be truncated.
407 @note NOTE that the default is one character less than STRING_MAXLEN to
408 prevent problems after conversion to String that may be lurking in
409 various places doing something like
410 @code
411 for (sal_uInt16 i=0; i < aString.Len(); ++i)
412 @endcode
413 causing endless loops ...
415 bool ReadUniStringLine( OUString& rStr, sal_Int32 nMaxCodepointsToRead = 0xFFFE );
416 /** Read a 32bit length prefixed sequence of utf-16 if
417 eSrcCharSet==RTL_TEXTENCODING_UNICODE, otherwise read a 16bit length
418 prefixed sequence of bytes and convert from eSrcCharSet */
419 OUString ReadUniOrByteString(rtl_TextEncoding eSrcCharSet);
420 /** Write a 32bit length prefixed sequence of utf-16 if
421 eSrcCharSet==RTL_TEXTENCODING_UNICODE, otherwise convert to eSrcCharSet
422 and write a 16bit length prefixed sequence of bytes */
423 SvStream& WriteUniOrByteString( const OUString& rStr, rtl_TextEncoding eDestCharSet );
425 /** Read a line of Unicode if eSrcCharSet==RTL_TEXTENCODING_UNICODE,
426 otherwise read a line of Bytecode and convert from eSrcCharSet
428 @param nMaxCodepointsToRead
429 Maximum of codepoints (2 bytes if Unicode, bytes if not
430 Unicode) to read, if line is longer it will be truncated.
432 @note NOTE that the default is one character less than STRING_MAXLEN to
433 prevent problems after conversion to String that may be lurking in
434 various places doing something like
435 @code
436 for (sal_uInt16 i=0; i < aString.Len(); ++i)
437 @endcode
438 causing endless loops ...
440 bool ReadUniOrByteStringLine( OUString& rStr, rtl_TextEncoding eSrcCharSet,
441 sal_Int32 nMaxCodepointsToRead = 0xFFFE );
442 /** Write a sequence of Unicode characters if
443 eDestCharSet==RTL_TEXTENCODING_UNICODE, otherwise write a sequence of
444 Bytecodes converted to eDestCharSet */
445 bool WriteUnicodeOrByteText( const OUString& rStr, rtl_TextEncoding eDestCharSet );
446 bool WriteUnicodeOrByteText( const OUString& rStr )
447 { return WriteUnicodeOrByteText( rStr, GetStreamCharSet() ); }
449 /** Write a Unicode character if eDestCharSet==RTL_TEXTENCODING_UNICODE,
450 otherwise write as Bytecode converted to eDestCharSet.
452 This may result in more than one byte being written if a multi byte
453 encoding (e.g. UTF7, UTF8) is chosen. */
454 bool WriteUniOrByteChar( sal_Unicode ch, rtl_TextEncoding eDestCharSet );
455 bool WriteUniOrByteChar( sal_Unicode ch )
456 { return WriteUniOrByteChar( ch, GetStreamCharSet() ); }
458 void SetBufferSize( sal_uInt16 nBufSize );
459 sal_uInt16 GetBufferSize() const { return nBufSize; }
461 void RefreshBuffer();
462 SvStream& PutBack( char aCh );
464 bool IsWritable() const { return bIsWritable; }
465 StreamMode GetStreamMode() const { return eStreamMode; }
466 virtual sal_uInt16 IsA() const;
468 long GetVersion() { return nVersion; }
469 void SetVersion( long n ) { nVersion = n; }
471 friend SvStream& operator<<( SvStream& rStr, SvStrPtr f ); // for Manips
473 /// end of input seen during previous i/o operation
474 virtual bool eof() const { return bIsEof; }
476 /// stream is broken
477 virtual bool bad() const { return GetError() != 0; }
479 /** Get state
481 If the state is good() the previous i/o operation succeeded.
483 If the state is good(), the next input operation might succeed;
484 otherwise, it will fail.
486 Applying an input operation to a stream that is not in the good() state
487 is a null operation as far as the variable being read into is concerned.
489 If we try to read into a variable v and the operation fails, the value
490 of v should be unchanged,
492 virtual bool good() const { return !(eof() || bad()); }
495 inline SvStream& operator<<( SvStream& rStr, SvStrPtr f )
497 (*f)(rStr);
498 return rStr;
501 inline void SvStream::SetEndianSwap( bool bVal )
503 #ifdef OSL_BIGENDIAN
504 SetNumberFormatInt( bVal ? NUMBERFORMAT_INT_LITTLEENDIAN : NUMBERFORMAT_INT_BIGENDIAN );
505 #else
506 SetNumberFormatInt( bVal ? NUMBERFORMAT_INT_BIGENDIAN : NUMBERFORMAT_INT_LITTLEENDIAN );
507 #endif
510 TOOLS_DLLPUBLIC SvStream& endl( SvStream& rStr );
511 /// same as endl() but Unicode
512 TOOLS_DLLPUBLIC SvStream& endlu( SvStream& rStr );
513 /// call endlu() if eStreamCharSet==RTL_TEXTECODING_UNICODE otherwise endl()
514 TOOLS_DLLPUBLIC SvStream& endlub( SvStream& rStr );
516 /// Attempt to read nUnits 8bit units to an OString, returned OString's
517 /// length is number of units successfully read
518 TOOLS_DLLPUBLIC OString read_uInt8s_ToOString(SvStream& rStrm,
519 sal_Size nUnits);
521 /// Attempt to read nUnits 8bit units to an OUString
522 TOOLS_DLLPUBLIC inline OUString read_uInt8s_ToOUString(SvStream& rStrm,
523 sal_Size nUnits, rtl_TextEncoding eEnc)
525 return OStringToOUString(read_uInt8s_ToOString(rStrm, nUnits), eEnc);
528 /// Attempt to read nUnits 16bit units to an OUString, returned
529 /// OUString's length is number of units successfully read
530 TOOLS_DLLPUBLIC OUString read_uInt16s_ToOUString(SvStream& rStrm,
531 sal_Size nUnits);
533 /// Attempt to read a pascal-style length (of type prefix) prefixed sequence of
534 /// 16bit units to an OUString, returned OString's length is number of
535 /// units successfully read.
536 template<typename prefix>
537 OUString read_lenPrefixed_uInt16s_ToOUString(SvStream& rStrm)
539 prefix nUnits = 0;
540 rStrm >> nUnits;
541 return read_uInt16s_ToOUString(rStrm, nUnits);
544 /// Attempt to write a prefixed sequence of nUnits 16bit units from an OUString,
545 /// returned value is number of bytes written
546 TOOLS_DLLPUBLIC sal_Size write_uInt16s_FromOUString(SvStream& rStrm,
547 const OUString& rStr, sal_Size nUnits);
549 TOOLS_DLLPUBLIC inline sal_Size write_uInt16s_FromOUString(SvStream& rStrm,
550 const OUString& rStr)
552 return write_uInt16s_FromOUString(rStrm, rStr, rStr.getLength());
555 namespace streamdetail
557 /// Attempt to write a pascal-style length (of type prefix) prefixed
558 /// sequence of units from a string-type, returned value is number of bytes
559 /// written (including byte-count of prefix)
560 template<typename prefix, typename S, sal_Size (*writeOper)(SvStream&, const S&, sal_Size)>
561 sal_Size write_lenPrefixed_seq_From_str(SvStream& rStrm, const S &rStr)
563 sal_Size nWritten = 0;
564 prefix nUnits = std::min<sal_Size>(rStr.getLength(), std::numeric_limits<prefix>::max());
565 SAL_WARN_IF(static_cast<sal_Size>(nUnits) != static_cast<sal_Size>(rStr.getLength()),
566 "tools.stream",
567 "string too long for prefix count to fit in output type");
568 rStrm << nUnits;
569 if (rStrm.good())
571 nWritten += sizeof(prefix);
572 nWritten += writeOper(rStrm, rStr, nUnits);
574 return nWritten;
578 /// Attempt to write a pascal-style length (of type prefix) prefixed sequence
579 /// of 16bit units from an OUString, returned value is number of bytes written
580 /// (including byte-count of prefix)
581 template<typename prefix>
582 sal_Size write_lenPrefixed_uInt16s_FromOUString(SvStream& rStrm,
583 const OUString &rStr)
585 return streamdetail::write_lenPrefixed_seq_From_str<prefix, OUString, write_uInt16s_FromOUString>(rStrm, rStr);
588 /// Attempt to read 8bit units to an OString until a zero terminator is
589 /// encountered, returned OString's length is number of units *definitely*
590 /// successfully read, check SvStream::good() to see if null terminator was
591 /// successfully read
592 TOOLS_DLLPUBLIC OString read_zeroTerminated_uInt8s_ToOString(SvStream& rStrm);
594 /// Attempt to read 8bit units assuming source encoding eEnc to an OUString
595 /// until a zero terminator is encountered. Check SvStream::good() to see if
596 /// null terminator was successfully read
597 TOOLS_DLLPUBLIC OUString read_zeroTerminated_uInt8s_ToOUString(SvStream& rStrm, rtl_TextEncoding eEnc);
599 /// Attempt to read a pascal-style length (of type prefix) prefixed sequence of
600 /// 8bit units to an OString, returned OString's length is number of units
601 /// successfully read.
602 template<typename prefix>
603 OString read_lenPrefixed_uInt8s_ToOString(SvStream& rStrm)
605 prefix nUnits = 0;
606 rStrm >> nUnits;
607 return read_uInt8s_ToOString(rStrm, nUnits);
610 /// Attempt to read a pascal-style length (of type prefix) prefixed sequence of
611 /// 8bit units to an OUString
612 template<typename prefix>
613 OUString read_lenPrefixed_uInt8s_ToOUString(SvStream& rStrm,
614 rtl_TextEncoding eEnc)
616 return OStringToOUString(read_lenPrefixed_uInt8s_ToOString<prefix>(rStrm), eEnc);
619 /// Attempt to write a prefixed sequence of nUnits 8bit units from an OString,
620 /// returned value is number of bytes written
621 TOOLS_DLLPUBLIC inline sal_Size write_uInt8s_FromOString(SvStream& rStrm, const OString& rStr,
622 sal_Size nUnits)
624 return rStrm.Write(rStr.getStr(), nUnits);
627 TOOLS_DLLPUBLIC inline sal_Size write_uInt8s_FromOString(SvStream& rStrm, const OString& rStr)
629 return write_uInt8s_FromOString(rStrm, rStr, rStr.getLength());
632 /// Attempt to write a pascal-style length (of type prefix) prefixed sequence
633 /// of 8bit units from an OString, returned value is number of bytes written
634 /// (including byte-count of prefix)
635 template<typename prefix>
636 sal_Size write_lenPrefixed_uInt8s_FromOString(SvStream& rStrm,
637 const OString &rStr)
639 return streamdetail::write_lenPrefixed_seq_From_str<prefix, OString, write_uInt8s_FromOString>(rStrm, rStr);
642 /// Attempt to write a pascal-style length (of type prefix) prefixed sequence
643 /// of 8bit units from an OUString, returned value is number of bytes written
644 /// (including byte-count of prefix)
645 template<typename prefix>
646 sal_Size write_lenPrefixed_uInt8s_FromOUString(SvStream& rStrm,
647 const OUString &rStr,
648 rtl_TextEncoding eEnc)
650 return write_lenPrefixed_uInt8s_FromOString<prefix>(rStrm, OUStringToOString(rStr, eEnc));
653 // FileStream
655 class TOOLS_DLLPUBLIC SvFileStream : public SvStream
657 private:
658 StreamData* pInstanceData;
659 OUString aFilename;
660 sal_uInt16 nLockCounter;
661 bool bIsOpen;
663 // Forbidden and not implemented.
664 SvFileStream (const SvFileStream&);
665 SvFileStream & operator= (const SvFileStream&);
667 bool LockRange( sal_Size nByteOffset, sal_Size nBytes );
668 bool UnlockRange( sal_Size nByteOffset, sal_Size nBytes );
669 bool LockFile();
670 bool UnlockFile();
672 protected:
673 virtual sal_Size GetData( void* pData, sal_Size nSize );
674 virtual sal_Size PutData( const void* pData, sal_Size nSize );
675 virtual sal_Size SeekPos( sal_Size nPos );
676 virtual void SetSize( sal_Size nSize );
677 virtual void FlushData();
679 public:
680 // Switches to Read StreamMode on failed attempt of Write opening
681 SvFileStream( const OUString& rFileName, StreamMode eOpenMode );
682 SvFileStream();
683 ~SvFileStream();
685 virtual void ResetError();
687 void Open( const OUString& rFileName, StreamMode eOpenMode );
688 void Close();
689 bool IsOpen() const { return bIsOpen; }
690 bool IsLocked() const { return ( nLockCounter!=0 ); }
691 virtual sal_uInt16 IsA() const;
693 const OUString& GetFileName() const { return aFilename; }
696 // MemoryStream
698 class TOOLS_DLLPUBLIC SvMemoryStream : public SvStream
700 // Forbidden and not implemented.
701 SvMemoryStream (const SvMemoryStream&);
702 SvMemoryStream & operator= (const SvMemoryStream&);
704 sal_Size GetBufSize() const { return nSize; }
706 protected:
707 sal_Size nSize;
708 sal_Size nResize;
709 sal_Size nPos;
710 sal_Size nEndOfData;
711 sal_uInt8* pBuf;
712 bool bOwnsData;
714 virtual sal_Size GetData( void* pData, sal_Size nSize );
715 virtual sal_Size PutData( const void* pData, sal_Size nSize );
716 virtual sal_Size SeekPos( sal_Size nPos );
717 virtual void SetSize( sal_Size nSize );
718 virtual void FlushData();
720 /// AllocateMemory must update pBuf accordingly
721 /// - pBuf: Address of new block
722 virtual bool AllocateMemory( sal_Size nSize );
724 /// ReAllocateMemory must update the following variables:
725 /// - pBuf: Address of new block
726 /// - nEndOfData: Set to nNewSize-1L , if outside of block
727 /// Set to 0 , if new block size is 0 bytes
728 /// - nSize: New block size
729 /// - nPos: Set to 0 if position outside of block
730 virtual bool ReAllocateMemory( long nDiff );
732 /// Is called when this stream allocated the buffer or the buffer is
733 /// resized. FreeMemory may need to NULLify handles in derived classes.
734 virtual void FreeMemory();
736 SvMemoryStream(void*) { } // for sub-classes
738 public:
739 SvMemoryStream( void* pBuf, sal_Size nSize, StreamMode eMode);
740 SvMemoryStream( sal_Size nInitSize=512, sal_Size nResize=64 );
741 ~SvMemoryStream();
743 virtual void ResetError();
745 const void* GetBuffer();
746 sal_uIntPtr GetSize();
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 bool bOwnsData=true, sal_Size nEOF=0 );
756 void ObjectOwnsMemory( bool bOwn ) { bOwnsData = bOwn; }
757 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 GetBufSize() - Tell(); }
763 class TOOLS_DLLPUBLIC SvScriptStream: public SvStream
765 oslProcess mpProcess;
766 oslFileHandle mpHandle;
768 public:
769 SvScriptStream(const OUString& rUrl);
770 ~SvScriptStream();
772 virtual bool ReadLine(OString &rStr, sal_Int32) SAL_OVERRIDE;
773 virtual bool good() const SAL_OVERRIDE;
776 /** Data Copy Stream
778 This class is the foundation for all classes, using SvData
779 (SO2\DTRANS.HXX/CXX) for transportation (e.g., graphics).
781 class TOOLS_DLLPUBLIC SvDataCopyStream
783 public:
784 // repeated execution of Load or Assign is allowed
785 TYPEINFO();
786 virtual ~SvDataCopyStream(){}
787 virtual void Load( SvStream & ) = 0;
788 virtual void Save( SvStream & ) = 0;
789 virtual void Assign( const SvDataCopyStream & );
792 #endif
794 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */