Branch libreoffice-5-0-4
[LibreOffice.git] / include / tools / stream.hxx
blob75d7445c5411eef6efd31c325722f15889de4344
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/errinf.hxx>
27 #include <tools/ref.hxx>
28 #include <tools/rtti.hxx>
29 #include <rtl/string.hxx>
30 #include <o3tl/typed_flags_set.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 // read, write, create,... options
45 enum class StreamMode {
46 NONE = 0x0000,
47 READ = 0x0001, ///< allow read accesses
48 WRITE = 0x0002, ///< allow write accesses
49 // file i/o
50 NOCREATE = 0x0004, ///< 1 == Dont create file
51 TRUNC = 0x0008, ///< Truncate _existing_ file to zero length
52 COPY_ON_SYMLINK = 0x0010, ///< copy-on-write for symlinks (Unix)
53 // sharing options
54 SHARE_DENYNONE = 0x0100,
55 SHARE_DENYREAD = 0x0200, // overrides denynone
56 SHARE_DENYWRITE = 0x0400, // overrides denynone
57 SHARE_DENYALL = 0x0800, // overrides denyread,write,none
59 namespace o3tl
61 template<> struct typed_flags<StreamMode> : is_typed_flags<StreamMode, 0x0f1f> {};
64 #define STREAM_READWRITEBITS (StreamMode::READ | StreamMode::WRITE | \
65 StreamMode::NOCREATE | StreamMode::TRUNC)
67 #define STREAM_SHAREBITS (StreamMode::SHARE_DENYNONE | StreamMode::SHARE_DENYREAD |\
68 StreamMode::SHARE_DENYWRITE | StreamMode::SHARE_DENYALL)
70 #define STREAM_READWRITE (StreamMode::READ | StreamMode::WRITE)
71 #define STREAM_SHARE_DENYREADWRITE (StreamMode::SHARE_DENYREAD | StreamMode::SHARE_DENYWRITE)
73 #define STREAM_STD_READ (StreamMode::READ | StreamMode::SHARE_DENYNONE | StreamMode::NOCREATE)
74 #define STREAM_STD_WRITE (StreamMode::WRITE | StreamMode::SHARE_DENYALL)
75 #define STREAM_STD_READWRITE (STREAM_READWRITE | StreamMode::SHARE_DENYALL)
77 #define STREAM_SEEK_TO_BEGIN 0L
78 #define STREAM_SEEK_TO_END SAL_MAX_UINT64
80 enum class SvStreamEndian { BIG, LITTLE };
82 enum class SvStreamCompressFlags {
83 NONE = 0x0000,
84 ZBITMAP = 0x0001,
85 NATIVE = 0x0010,
87 namespace o3tl
89 template<> struct typed_flags<SvStreamCompressFlags> : is_typed_flags<SvStreamCompressFlags, 0x0011> {};
92 class SvStream;
94 typedef SvStream& (*SvStrPtr)( SvStream& );
96 inline SvStream& operator<<( SvStream& rStr, SvStrPtr f );
98 // SvLockBytes
100 enum LockType {};
102 struct SvLockBytesStat
104 sal_Size nSize;
106 SvLockBytesStat() : nSize(0) {}
109 enum SvLockBytesStatFlag { SVSTATFLAG_DEFAULT };
111 class TOOLS_DLLPUBLIC SvLockBytes: public virtual SvRefBase
113 SvStream * m_pStream;
114 bool m_bOwner;
115 bool m_bSync;
117 protected:
118 void close();
120 public:
121 TYPEINFO();
123 SvLockBytes() : m_pStream(0), m_bOwner(false), m_bSync(false) {}
125 SvLockBytes(SvStream * pTheStream, bool bTheOwner = false) :
126 m_pStream(pTheStream), m_bOwner(bTheOwner), m_bSync(false) {}
128 virtual ~SvLockBytes() { close(); }
130 const SvStream * GetStream() const { return m_pStream; }
132 virtual void SetSynchronMode(bool bTheSync = true) { m_bSync = bTheSync; }
133 bool IsSynchronMode() const { return m_bSync; }
135 virtual ErrCode ReadAt(sal_uInt64 nPos, void * pBuffer, sal_Size nCount,
136 sal_Size * pRead) const;
137 virtual ErrCode WriteAt(sal_uInt64 nPos, const void * pBuffer, sal_Size nCount,
138 sal_Size * pWritten);
140 virtual ErrCode Flush() const;
142 virtual ErrCode SetSize(sal_uInt64 nSize);
144 virtual ErrCode Stat(SvLockBytesStat * pStat, SvLockBytesStatFlag) const;
147 typedef tools::SvRef<SvLockBytes> SvLockBytesRef;
149 // SvOpenLockBytes
151 class TOOLS_DLLPUBLIC SvOpenLockBytes: public SvLockBytes
153 public:
154 TYPEINFO_OVERRIDE();
156 SvOpenLockBytes() : SvLockBytes(0, false) {}
157 SvOpenLockBytes(SvStream * pStream, bool bOwner):
158 SvLockBytes(pStream, bOwner) {}
160 virtual ErrCode FillAppend(const void * pBuffer, sal_Size nCount,
161 sal_Size * pWritten) = 0;
163 virtual sal_uInt64 Tell() const = 0;
165 virtual sal_uInt64 Seek(sal_uInt64 nPos) = 0;
167 virtual void Terminate() = 0;
171 // SvAsyncLockBytes
173 class SvAsyncLockBytes: public SvOpenLockBytes
175 sal_uInt64 m_nSize;
176 bool m_bTerminated;
178 public:
179 TYPEINFO_OVERRIDE();
181 SvAsyncLockBytes(SvStream * pStream, bool bOwner):
182 SvOpenLockBytes(pStream, bOwner), m_nSize(0), m_bTerminated(false) {}
184 virtual ErrCode ReadAt(sal_uInt64 nPos, void * pBuffer, sal_Size nCount,
185 sal_Size * pRead) const SAL_OVERRIDE;
186 virtual ErrCode WriteAt(sal_uInt64 nPos, const void * pBuffer, sal_Size nCount,
187 sal_Size * pWritten) SAL_OVERRIDE;
189 virtual ErrCode FillAppend(const void * pBuffer, sal_Size nCount,
190 sal_Size * pWritten) SAL_OVERRIDE;
192 virtual sal_uInt64 Tell() const SAL_OVERRIDE { return m_nSize; }
194 virtual sal_uInt64 Seek(sal_uInt64 nPos) SAL_OVERRIDE;
196 virtual void Terminate() SAL_OVERRIDE { m_bTerminated = true; }
200 // SvStream
202 class TOOLS_DLLPUBLIC SvStream
204 private:
205 // LockBytes Interface
206 SvLockBytesRef xLockBytes; // Default implementation
207 sal_uInt64 m_nActPos;
209 // Puffer-Verwaltung
210 sal_uInt8* pRWBuf; // Points to read/write buffer
211 sal_uInt8* pBufPos; // pRWBuf + nBufActualPos
212 sal_uInt16 nBufSize; // Allocated size of buffer
213 sal_uInt16 nBufActualLen; // Length of used segment of puffer
214 // = nBufSize, if EOF did not occur
215 sal_uInt16 nBufActualPos; // current position in buffer (0..nBufSize-1)
216 sal_uInt16 nBufFree; // number of free slots in buffer to IO of type eIOMode
217 bool bIoRead;
218 bool bIoWrite;
220 // Error codes, conversion, compression, ...
221 bool bIsDirty; // true: Stream != buffer content
222 bool bIsConsistent;// false: Buffer contains data, which were
223 // NOT allowed to be written by PutData
224 // into the derived stream (cf. PutBack)
225 bool bSwap;
226 bool bIsEof;
227 sal_uInt32 nError;
228 SvStreamEndian nEndian;
229 SvStreamCompressFlags nCompressMode;
230 LineEnd eLineDelimiter;
231 rtl_TextEncoding eStreamCharSet;
233 // Encryption
234 OString m_aCryptMaskKey;// aCryptMaskKey.getLength != 0 -> Encryption used
235 unsigned char nCryptMask;
237 // Userdata
238 long nVersion; // for external use
240 // helper methods
241 TOOLS_DLLPRIVATE void ImpInit();
243 SvStream ( const SvStream& rStream ) SAL_DELETED_FUNCTION;
244 SvStream& operator=( const SvStream& rStream ) SAL_DELETED_FUNCTION;
246 protected:
247 sal_uInt64 m_nBufFilePos; ///< File position of pBuf[0]
248 StreamMode eStreamMode;
249 bool bIsWritable;
251 virtual sal_Size GetData( void* pData, sal_Size nSize );
252 virtual sal_Size PutData( const void* pData, sal_Size nSize );
253 virtual sal_uInt64 SeekPos( sal_uInt64 nPos );
254 virtual void FlushData();
255 virtual void SetSize(sal_uInt64 nSize);
257 void ClearError();
258 void ClearBuffer();
260 // encrypt and write in blocks
261 sal_Size CryptAndWriteBuffer( const void* pStart, sal_Size nLen );
262 bool EncryptBuffer( void* pStart, sal_Size nLen );
264 void SyncSvStream( sal_Size nNewStreamPos ); ///< SvStream <- Medium
265 void SyncSysStream(); ///< SvStream -> Medium
267 public:
268 SvStream();
269 SvStream( SvLockBytes *pLockBytes);
270 virtual ~SvStream();
272 SvLockBytes* GetLockBytes() const { return xLockBytes; }
274 sal_uInt32 GetError() const { return ERRCODE_TOERROR(nError); }
275 sal_uInt32 GetErrorCode() const { return nError; }
277 void SetError( sal_uInt32 nErrorCode );
278 virtual void ResetError();
280 void SetEndian( SvStreamEndian SvStreamEndian );
281 SvStreamEndian GetEndian() const { return nEndian; }
282 /// returns status of endian swap flag
283 bool IsEndianSwap() const { return bSwap; }
285 void SetCompressMode( SvStreamCompressFlags nNewMode )
286 { nCompressMode = nNewMode; }
287 SvStreamCompressFlags GetCompressMode() const { return nCompressMode; }
289 void SetCryptMaskKey(const OString& rCryptMaskKey);
290 const OString& GetCryptMaskKey() const { return m_aCryptMaskKey; }
292 void SetStreamCharSet( rtl_TextEncoding eCharSet )
293 { eStreamCharSet = eCharSet; }
294 rtl_TextEncoding GetStreamCharSet() const { return eStreamCharSet; }
296 void SetLineDelimiter( LineEnd eLineEnd )
297 { eLineDelimiter = eLineEnd; }
298 LineEnd GetLineDelimiter() const { return eLineDelimiter; }
300 SvStream& ReadUInt16( sal_uInt16& rUInt16 );
301 SvStream& ReadUInt32( sal_uInt32& rUInt32 );
302 SvStream& ReadUInt64( sal_uInt64& rUInt64 );
303 SvStream& ReadInt16( sal_Int16& rInt16 );
304 SvStream& ReadInt32( sal_Int32& rInt32 );
305 SvStream& ReadInt64(sal_Int64 & rInt64);
306 SvStream& ReadSChar( signed char& rChar );
307 SvStream& ReadChar( char& rChar );
308 SvStream& ReadUChar( unsigned char& rChar );
309 SvStream& ReadCharAsBool( bool& rBool );
310 SvStream& ReadFloat( float& rFloat );
311 SvStream& ReadDouble( double& rDouble );
312 SvStream& ReadStream( SvStream& rStream );
314 SvStream& WriteUInt16( sal_uInt16 nUInt16 );
315 SvStream& WriteUInt32( sal_uInt32 nUInt32 );
316 SvStream& WriteUInt64( sal_uInt64 nuInt64 );
317 SvStream& WriteInt16( sal_Int16 nInt16 );
318 SvStream& WriteInt32( sal_Int32 nInt32 );
319 SvStream& WriteInt64( sal_Int64 nInt64 );
320 SvStream& WriteUInt8( sal_uInt8 nuInt8 );
321 SvStream& WriteUnicode( sal_Unicode );
322 SvStream& WriteOString(const OString& rStr)
323 { return WriteCharPtr(rStr.getStr()); }
324 SvStream& WriteStream( SvStream& rStream );
326 SvStream& WriteBool( bool b )
327 { return WriteUChar(static_cast<unsigned char>(b)); }
328 SvStream& WriteSChar( signed char nChar );
329 SvStream& WriteChar( char nChar );
330 SvStream& WriteUChar( unsigned char nChar );
331 SvStream& WriteFloat( float nFloat );
332 SvStream& WriteDouble( const double& rDouble );
333 SvStream& WriteCharPtr( const char* pBuf );
335 SvStream& WriteUInt32AsString( sal_uInt32 nUInt32 );
336 SvStream& WriteInt32AsString( sal_Int32 nInt32 );
338 sal_Size Read( void* pData, sal_Size nSize );
339 sal_Size Write( const void* pData, sal_Size nSize );
340 sal_uInt64 Seek( sal_uInt64 nPos );
341 sal_uInt64 SeekRel( sal_Int64 nPos );
342 sal_uInt64 Tell() const { return m_nBufFilePos + nBufActualPos; }
343 // length between current (Tell()) pos and end of stream
344 virtual sal_uInt64 remainingSize();
345 void Flush();
346 bool IsEof() const { return bIsEof; }
347 // next Tell() <= nSize
348 bool SetStreamSize( sal_uInt64 nSize );
350 /** Read a line of bytes.
352 @param nMaxBytesToRead
353 Maximum of bytes to read, if line is longer it will be
354 truncated.
356 @note NOTE that the default is one character less than STRING_MAXLEN to
357 prevent problems after conversion to String that may be lurking
358 in various places doing something like
359 @code
360 for (sal_uInt16 i=0; i < aString.Len(); ++i)
361 @endcode
362 causing endless loops ...
364 virtual bool ReadLine( OString& rStr, sal_Int32 nMaxBytesToRead = 0xFFFE );
365 bool WriteLine( const OString& rStr );
367 /** Read a line of bytes.
369 @param nMaxBytesToRead
370 Maximum of bytes to read, if line is longer it will be
371 truncated.
373 @note NOTE that the default is one character less than STRING_MAXLEN to
374 prevent problems after conversion to String that may be lurking
375 in various places doing something like
376 @code
377 for (sal_uInt16 i=0; i < aString.Len(); ++i)
378 @endcode
379 causing endless loops ...
381 bool ReadByteStringLine( OUString& rStr, rtl_TextEncoding eSrcCharSet,
382 sal_Int32 nMaxBytesToRead = 0xFFFE );
383 bool WriteByteStringLine( const OUString& rStr, rtl_TextEncoding eDestCharSet );
385 /// Switch to no endian swapping and write 0xfeff
386 bool StartWritingUnicodeText();
388 /** If eReadBomCharSet==RTL_TEXTENCODING_DONTKNOW: read 16bit, if 0xfeff do
389 nothing (UTF-16), if 0xfffe switch endian swapping (UTF-16), if 0xefbb
390 or 0xbbef read another byte and check for UTF-8. If no UTF-* BOM was
391 detected put all read bytes back. This means that if 2 bytes were read
392 it was an UTF-16 BOM, if 3 bytes were read it was an UTF-8 BOM. There
393 is no UTF-7, UTF-32 or UTF-EBCDIC BOM detection!
395 If eReadBomCharSet!=RTL_TEXTENCODING_DONTKNOW: only read a BOM of that
396 encoding and switch endian swapping if UTF-16 and 0xfffe. */
397 bool StartReadingUnicodeText( rtl_TextEncoding eReadBomCharSet );
399 /** Read a line of Unicode.
401 @param nMaxCodepointsToRead
402 Maximum of codepoints (UCS-2 or UTF-16 pairs, not bytes) to
403 read, if line is longer it will be truncated.
405 bool ReadUniStringLine(OUString& rStr, sal_Int32 nMaxCodepointsToRead);
406 /** Read a 32bit length prefixed sequence of utf-16 if
407 eSrcCharSet==RTL_TEXTENCODING_UNICODE, otherwise read a 16bit length
408 prefixed sequence of bytes and convert from eSrcCharSet */
409 OUString ReadUniOrByteString(rtl_TextEncoding eSrcCharSet);
410 /** Write a 32bit length prefixed sequence of utf-16 if
411 eSrcCharSet==RTL_TEXTENCODING_UNICODE, otherwise convert to eSrcCharSet
412 and write a 16bit length prefixed sequence of bytes */
413 SvStream& WriteUniOrByteString( const OUString& rStr, rtl_TextEncoding eDestCharSet );
415 /** Read a line of Unicode if eSrcCharSet==RTL_TEXTENCODING_UNICODE,
416 otherwise read a line of Bytecode and convert from eSrcCharSet
418 @param nMaxCodepointsToRead
419 Maximum of codepoints (2 bytes if Unicode, bytes if not
420 Unicode) to read, if line is longer it will be truncated.
422 @note NOTE that the default is one character less than STRING_MAXLEN to
423 prevent problems after conversion to String that may be lurking in
424 various places doing something like
425 @code
426 for (sal_uInt16 i=0; i < aString.Len(); ++i)
427 @endcode
428 causing endless loops ...
430 bool ReadUniOrByteStringLine( OUString& rStr, rtl_TextEncoding eSrcCharSet,
431 sal_Int32 nMaxCodepointsToRead = 0xFFFE );
432 /** Write a sequence of Unicode characters if
433 eDestCharSet==RTL_TEXTENCODING_UNICODE, otherwise write a sequence of
434 Bytecodes converted to eDestCharSet */
435 bool WriteUnicodeOrByteText( const OUString& rStr, rtl_TextEncoding eDestCharSet );
436 bool WriteUnicodeOrByteText( const OUString& rStr )
437 { return WriteUnicodeOrByteText( rStr, GetStreamCharSet() ); }
439 /** Write a Unicode character if eDestCharSet==RTL_TEXTENCODING_UNICODE,
440 otherwise write as Bytecode converted to eDestCharSet.
442 This may result in more than one byte being written if a multi byte
443 encoding (e.g. UTF7, UTF8) is chosen. */
444 bool WriteUniOrByteChar( sal_Unicode ch, rtl_TextEncoding eDestCharSet );
445 bool WriteUniOrByteChar( sal_Unicode ch )
446 { return WriteUniOrByteChar( ch, GetStreamCharSet() ); }
448 void SetBufferSize( sal_uInt16 nBufSize );
449 sal_uInt16 GetBufferSize() const { return nBufSize; }
451 void RefreshBuffer();
452 SvStream& PutBack( char aCh );
454 bool IsWritable() const { return bIsWritable; }
455 StreamMode GetStreamMode() const { return eStreamMode; }
457 long GetVersion() { return nVersion; }
458 void SetVersion( long n ) { nVersion = n; }
460 friend SvStream& operator<<( SvStream& rStr, SvStrPtr f ); // for Manips
462 /// end of input seen during previous i/o operation
463 bool eof() const { return bIsEof; }
465 /// stream is broken
466 bool bad() const { return GetError() != 0; }
468 /** Get state
470 If the state is good() the previous i/o operation succeeded.
472 If the state is good(), the next input operation might succeed;
473 otherwise, it will fail.
475 Applying an input operation to a stream that is not in the good() state
476 is a null operation as far as the variable being read into is concerned.
478 If we try to read into a variable v and the operation fails, the value
479 of v should be unchanged,
481 virtual bool good() const { return !(eof() || bad()); }
484 inline SvStream& operator<<( SvStream& rStr, SvStrPtr f )
486 (*f)(rStr);
487 return rStr;
490 TOOLS_DLLPUBLIC SvStream& endl( SvStream& rStr );
491 /// same as endl() but Unicode
492 TOOLS_DLLPUBLIC SvStream& endlu( SvStream& rStr );
493 /// call endlu() if eStreamCharSet==RTL_TEXTECODING_UNICODE otherwise endl()
494 TOOLS_DLLPUBLIC SvStream& endlub( SvStream& rStr );
496 /// Attempt to read nUnits 8bit units to an OString, returned OString's
497 /// length is number of units successfully read
498 TOOLS_DLLPUBLIC OString read_uInt8s_ToOString(SvStream& rStrm,
499 sal_Size nUnits);
501 /// Attempt to read nUnits 8bit units to an OUString
502 inline OUString read_uInt8s_ToOUString(SvStream& rStrm,
503 sal_Size nUnits, rtl_TextEncoding eEnc)
505 return OStringToOUString(read_uInt8s_ToOString(rStrm, nUnits), eEnc);
508 /// Attempt to read nUnits 16bit units to an OUString, returned
509 /// OUString's length is number of units successfully read
510 TOOLS_DLLPUBLIC OUString read_uInt16s_ToOUString(SvStream& rStrm,
511 sal_Size nUnits);
513 /// Attempt to read a pascal-style length (of type prefix) prefixed sequence of
514 /// 16bit units to an OUString, returned OString's length is number of
515 /// units successfully read.
516 inline OUString read_uInt16_lenPrefixed_uInt16s_ToOUString(SvStream& rStrm)
518 sal_uInt16 nUnits = 0;
519 rStrm.ReadUInt16( nUnits );
520 return read_uInt16s_ToOUString(rStrm, nUnits);
523 inline OUString read_uInt32_lenPrefixed_uInt16s_ToOUString(SvStream& rStrm)
525 sal_uInt32 nUnits = 0;
526 rStrm.ReadUInt32( nUnits );
527 return read_uInt16s_ToOUString(rStrm, nUnits);
530 /// Attempt to write a prefixed sequence of nUnits 16bit units from an OUString,
531 /// returned value is number of bytes written
532 TOOLS_DLLPUBLIC sal_Size write_uInt16s_FromOUString(SvStream& rStrm,
533 const OUString& rStr, sal_Size nUnits);
535 inline sal_Size write_uInt16s_FromOUString(SvStream& rStrm,
536 const OUString& rStr)
538 return write_uInt16s_FromOUString(rStrm, rStr, rStr.getLength());
541 /// Attempt to write a pascal-style length (of type prefix) prefixed sequence
542 /// of 16bit units from an OUString, returned value is number of bytes written
543 /// (including byte-count of prefix)
544 TOOLS_DLLPUBLIC sal_Size write_uInt32_lenPrefixed_uInt16s_FromOUString(SvStream& rStrm,
545 const OUString &rStr);
547 /// Attempt to write a pascal-style length (of type prefix) prefixed sequence
548 /// of 16bit units from an OUString, returned value is number of bytes written
549 /// (including byte-count of prefix)
550 TOOLS_DLLPUBLIC sal_Size write_uInt16_lenPrefixed_uInt16s_FromOUString(SvStream& rStrm,
551 const OUString &rStr);
553 /// Attempt to read 8bit units to an OString until a zero terminator is
554 /// encountered, returned OString's length is number of units *definitely*
555 /// successfully read, check SvStream::good() to see if null terminator was
556 /// successfully read
557 TOOLS_DLLPUBLIC OString read_zeroTerminated_uInt8s_ToOString(SvStream& rStrm);
559 /// Attempt to read 8bit units assuming source encoding eEnc to an OUString
560 /// until a zero terminator is encountered. Check SvStream::good() to see if
561 /// null terminator was successfully read
562 TOOLS_DLLPUBLIC OUString read_zeroTerminated_uInt8s_ToOUString(SvStream& rStrm, rtl_TextEncoding eEnc);
564 /// Attempt to read a pascal-style length (of type prefix) prefixed sequence of
565 /// 8bit units to an OString, returned OString's length is number of units
566 /// successfully read.
567 inline OString read_uInt16_lenPrefixed_uInt8s_ToOString(SvStream& rStrm)
569 sal_uInt16 nUnits = 0;
570 rStrm.ReadUInt16( nUnits );
571 return read_uInt8s_ToOString(rStrm, nUnits);
574 inline OString read_uInt8_lenPrefixed_uInt8s_ToOString(SvStream& rStrm)
576 sal_uInt8 nUnits = 0;
577 rStrm.ReadUChar( nUnits );
578 return read_uInt8s_ToOString(rStrm, nUnits);
581 inline OString read_uInt32_lenPrefixed_uInt8s_ToOString(SvStream& rStrm)
583 sal_uInt32 nUnits = 0;
584 rStrm.ReadUInt32( nUnits );
585 return read_uInt8s_ToOString(rStrm, nUnits);
588 /// Attempt to read a pascal-style length (of type prefix) prefixed sequence of
589 /// 8bit units to an OUString
590 inline OUString read_uInt16_lenPrefixed_uInt8s_ToOUString(SvStream& rStrm,
591 rtl_TextEncoding eEnc)
593 return OStringToOUString(read_uInt16_lenPrefixed_uInt8s_ToOString(rStrm), eEnc);
596 inline OUString read_uInt8_lenPrefixed_uInt8s_ToOUString(SvStream& rStrm,
597 rtl_TextEncoding eEnc)
599 return OStringToOUString(read_uInt8_lenPrefixed_uInt8s_ToOString(rStrm), eEnc);
602 /// Attempt to write a prefixed sequence of nUnits 8bit units from an OString,
603 /// returned value is number of bytes written
604 inline sal_Size write_uInt8s_FromOString(SvStream& rStrm, const OString& rStr,
605 sal_Size nUnits)
607 return rStrm.Write(rStr.getStr(), nUnits);
610 inline sal_Size write_uInt8s_FromOString(SvStream& rStrm, const OString& rStr)
612 return write_uInt8s_FromOString(rStrm, rStr, rStr.getLength());
615 /// Attempt to write a pascal-style length (of type prefix) prefixed
616 /// sequence of units from a string-type, returned value is number of bytes
617 /// written (including byte-count of prefix)
618 TOOLS_DLLPUBLIC sal_Size write_uInt16_lenPrefixed_uInt8s_FromOString(SvStream& rStrm,
619 const OString &rStr);
621 /// Attempt to write a pascal-style length (of type prefix) prefixed sequence
622 /// of 8bit units from an OUString, returned value is number of bytes written
623 /// (including byte-count of prefix)
624 inline sal_Size write_uInt16_lenPrefixed_uInt8s_FromOUString(SvStream& rStrm,
625 const OUString &rStr,
626 rtl_TextEncoding eEnc)
628 return write_uInt16_lenPrefixed_uInt8s_FromOString(rStrm, OUStringToOString(rStr, eEnc));
631 // FileStream
633 class TOOLS_DLLPUBLIC SvFileStream : public SvStream
635 private:
636 StreamData* pInstanceData;
637 OUString aFilename;
638 sal_uInt16 nLockCounter;
639 bool bIsOpen;
641 SvFileStream (const SvFileStream&) SAL_DELETED_FUNCTION;
642 SvFileStream & operator= (const SvFileStream&) SAL_DELETED_FUNCTION;
644 bool LockRange( sal_Size nByteOffset, sal_Size nBytes );
645 bool UnlockRange( sal_Size nByteOffset, sal_Size nBytes );
646 bool LockFile();
647 bool UnlockFile();
649 protected:
650 virtual sal_Size GetData( void* pData, sal_Size nSize ) SAL_OVERRIDE;
651 virtual sal_Size PutData( const void* pData, sal_Size nSize ) SAL_OVERRIDE;
652 virtual sal_uInt64 SeekPos( sal_uInt64 nPos ) SAL_OVERRIDE;
653 virtual void SetSize( sal_uInt64 nSize ) SAL_OVERRIDE;
654 virtual void FlushData() SAL_OVERRIDE;
656 public:
657 // Switches to Read StreamMode on failed attempt of Write opening
658 SvFileStream( const OUString& rFileName, StreamMode eOpenMode );
659 SvFileStream();
660 virtual ~SvFileStream();
662 virtual void ResetError() SAL_OVERRIDE;
664 void Open( const OUString& rFileName, StreamMode eOpenMode );
665 void Close();
666 bool IsOpen() const { return bIsOpen; }
667 bool IsLocked() const { return ( nLockCounter!=0 ); }
669 const OUString& GetFileName() const { return aFilename; }
672 // MemoryStream
674 class TOOLS_DLLPUBLIC SvMemoryStream : public SvStream
676 SvMemoryStream (const SvMemoryStream&) SAL_DELETED_FUNCTION;
677 SvMemoryStream & operator= (const SvMemoryStream&) SAL_DELETED_FUNCTION;
679 sal_Size GetBufSize() const { return nSize; }
681 protected:
682 sal_Size nSize;
683 sal_Size nResize;
684 sal_Size nPos;
685 sal_Size nEndOfData;
686 sal_uInt8* pBuf;
687 bool bOwnsData;
689 virtual sal_Size GetData( void* pData, sal_Size nSize ) SAL_OVERRIDE;
690 virtual sal_Size PutData( const void* pData, sal_Size nSize ) SAL_OVERRIDE;
691 virtual sal_uInt64 SeekPos( sal_uInt64 nPos ) SAL_OVERRIDE;
692 virtual void SetSize( sal_uInt64 nSize ) SAL_OVERRIDE;
693 virtual void FlushData() SAL_OVERRIDE;
695 /// AllocateMemory must update pBuf accordingly
696 /// - pBuf: Address of new block
697 bool AllocateMemory( sal_Size nSize );
699 /// ReAllocateMemory must update the following variables:
700 /// - pBuf: Address of new block
701 /// - nEndOfData: Set to nNewSize-1L , if outside of block
702 /// Set to 0 , if new block size is 0 bytes
703 /// - nSize: New block size
704 /// - nPos: Set to 0 if position outside of block
705 bool ReAllocateMemory( long nDiff );
707 /// Is called when this stream allocated the buffer or the buffer is
708 /// resized. FreeMemory may need to NULLify handles in derived classes.
709 void FreeMemory();
711 SvMemoryStream(void*) { } // for sub-classes
713 public:
714 SvMemoryStream( void* pBuf, sal_Size nSize, StreamMode eMode);
715 SvMemoryStream( sal_Size nInitSize=512, sal_Size nResize=64 );
716 virtual ~SvMemoryStream();
718 virtual void ResetError() SAL_OVERRIDE;
720 const void* GetBuffer();
721 sal_uIntPtr GetSize();
722 sal_Size GetEndOfData() const { return nEndOfData; }
723 const void* GetData() { Flush(); return pBuf; }
724 operator const void*() { Flush(); return pBuf; }
726 void* SwitchBuffer( sal_Size nInitSize=512, sal_Size nResize=64 );
727 void* SetBuffer( void* pBuf, sal_Size nSize,
728 bool bOwnsData=true, sal_Size nEOF=0 );
730 void ObjectOwnsMemory( bool bOwn ) { bOwnsData = bOwn; }
731 bool IsObjectMemoryOwner() { return bOwnsData; }
732 void SetResizeOffset( sal_Size nNewResize ) { nResize = nNewResize; }
733 sal_Size GetResizeOffset() const { return nResize; }
734 virtual sal_uInt64 remainingSize() SAL_OVERRIDE { return GetEndOfData() - Tell(); }
737 class TOOLS_DLLPUBLIC SvScriptStream: public SvStream
739 oslProcess mpProcess;
740 oslFileHandle mpHandle;
742 public:
743 SvScriptStream(const OUString& rUrl);
744 virtual ~SvScriptStream();
746 virtual bool ReadLine(OString &rStr, sal_Int32) SAL_OVERRIDE;
747 virtual bool good() const SAL_OVERRIDE;
750 /** Data Copy Stream
752 This class is the foundation for all classes, using SvData
753 (SO2\DTRANS.HXX/CXX) for transportation (e.g., graphics).
755 class TOOLS_DLLPUBLIC SvDataCopyStream
757 public:
758 // repeated execution of Load or Assign is allowed
759 TYPEINFO();
760 virtual ~SvDataCopyStream(){}
761 virtual void Load( SvStream & ) = 0;
762 virtual void Save( SvStream & ) = 0;
763 virtual void Assign( const SvDataCopyStream & );
766 #endif
768 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */