Update ooo320-m1
[ooovba.git] / tools / source / stream / stream.cxx
blobae4dfff5e1b007c22e22f6a1b45dad75cab4cced
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: stream.cxx,v $
10 * $Revision: 1.27 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_tools.hxx"
34 // ToDo:
35 // - Read->RefreshBuffer->Auf Aenderungen von nBufActualLen reagieren
37 #include <cstddef>
39 #include <string.h>
40 #include <stdio.h>
41 #include <ctype.h> // isspace
42 #include <stdlib.h> // strtol, _crotl
44 #include "boost/static_assert.hpp"
47 #if defined( DBG_UTIL ) && (OSL_DEBUG_LEVEL > 1)
48 // prueft Synchronisation des Buffers nach allen Read, Write, Seek
49 #define OV_DEBUG
50 #endif
53 #include <tools/solar.h>
55 #if defined(BLC)
56 #define SWAPNIBBLES(c) c=_crotl(c,4);
57 #else
58 #define SWAPNIBBLES(c) \
59 unsigned char nSwapTmp=c; \
60 nSwapTmp <<= 4; \
61 c >>= 4; \
62 c |= nSwapTmp;
63 #endif
65 #include <tools/debug.hxx>
66 #define ENABLE_BYTESTRING_STREAM_OPERATORS
67 #include <tools/stream.hxx>
68 #include <osl/thread.h>
69 #include <algorithm>
71 // -----------------------------------------------------------------------
73 DBG_NAME( Stream )
75 // -----------------------------------------------------------------------
77 // sprintf Param-Mode
78 #define SPECIAL_PARAM_NONE 0 // Format-Str, Number
79 #define SPECIAL_PARAM_WIDTH 1 // Format-Str, Width, Number
80 #define SPECIAL_PARAM_PRECISION 2 // Format-Str, Precision, Number
81 #define SPECIAL_PARAM_BOTH 3 // Format-Str, Width, Precision, Number
83 // -----------------------------------------------------------------------
85 // !!! Nicht inline, wenn Operatoren <<,>> inline sind
86 inline static void SwapUShort( sal_uInt16& r )
87 { r = SWAPSHORT(r); }
88 inline static void SwapShort( short& r )
89 { r = SWAPSHORT(r); }
90 inline static void SwapLong( long& r )
91 { r = SWAPLONG(r); }
92 inline static void SwapULong( sal_uInt32& r )
93 { r = SWAPLONG(r); }
94 inline static void SwapLongInt( int& r )
95 { r = SWAPLONG(r); }
96 inline static void SwapLongUInt( unsigned int& r )
97 { r = SWAPLONG(r); }
98 #ifdef UNX
99 inline static void SwapFloat( float& r )
101 *((sal_uInt32*)(void*)&r) = SWAPLONG( *((sal_uInt32*)(void*)&r) );
103 inline static void SwapDouble( double& r )
105 if( sizeof(double) != 8 )
107 DBG_ASSERT( FALSE, "Can only swap 8-Byte-doubles\n" );
109 else
111 sal_uInt32* c = (sal_uInt32*)(void*)&r;
112 c[0] ^= c[1]; // zwei 32-Bit-Werte in situ vertauschen
113 c[1] ^= c[0];
114 c[0] ^= c[1];
115 c[0] = SWAPLONG(c[0]); // und die beiden 32-Bit-Werte selbst in situ drehen
116 c[1] = SWAPLONG(c[1]);
119 #endif
121 //SDO
123 #define READNUMBER_WITHOUT_SWAP(datatype,value) \
125 int tmp = eIOMode; \
126 if( (tmp == STREAM_IO_READ) && sizeof(datatype)<=nBufFree) \
128 for (std::size_t i = 0; i < sizeof(datatype); i++)\
129 ((char *)&value)[i] = pBufPos[i];\
130 nBufActualPos += sizeof(datatype);\
131 pBufPos += sizeof(datatype);\
132 nBufFree -= sizeof(datatype);\
134 else\
135 Read( (char*)&value, sizeof(datatype) );\
138 #define WRITENUMBER_WITHOUT_SWAP(datatype,value) \
140 int tmp = eIOMode; \
141 if( (tmp==STREAM_IO_WRITE) && sizeof(datatype) <= nBufFree)\
143 for (std::size_t i = 0; i < sizeof(datatype); i++)\
144 pBufPos[i] = ((char *)&value)[i];\
145 nBufFree -= sizeof(datatype);\
146 nBufActualPos += sizeof(datatype);\
147 if( nBufActualPos > nBufActualLen )\
148 nBufActualLen = nBufActualPos;\
149 pBufPos += sizeof(datatype);\
150 bIsDirty = TRUE;\
152 else\
153 Write( (char*)&value, sizeof(datatype) );\
156 //============================================================================
158 // class SvLockBytes
160 //============================================================================
162 void SvLockBytes::close()
164 if (m_bOwner)
165 delete m_pStream;
166 m_pStream = 0;
169 //============================================================================
170 TYPEINIT0(SvLockBytes);
172 //============================================================================
173 // virtual
174 ErrCode SvLockBytes::ReadAt(sal_Size nPos, void * pBuffer, sal_Size nCount,
175 sal_Size * pRead) const
177 if (!m_pStream)
179 DBG_ERROR("SvLockBytes::ReadAt(): Bad stream");
180 return ERRCODE_NONE;
183 m_pStream->Seek(nPos);
184 sal_Size nTheRead = m_pStream->Read(pBuffer, nCount);
185 if (pRead)
186 *pRead = nTheRead;
187 return m_pStream->GetErrorCode();
190 //============================================================================
191 // virtual
192 ErrCode SvLockBytes::WriteAt(sal_Size nPos, const void * pBuffer, sal_Size nCount,
193 sal_Size * pWritten)
195 if (!m_pStream)
197 DBG_ERROR("SvLockBytes::WriteAt(): Bad stream");
198 return ERRCODE_NONE;
201 m_pStream->Seek(nPos);
202 sal_Size nTheWritten = m_pStream->Write(pBuffer, nCount);
203 if (pWritten)
204 *pWritten = nTheWritten;
205 return m_pStream->GetErrorCode();
208 //============================================================================
209 // virtual
210 ErrCode SvLockBytes::Flush() const
212 if (!m_pStream)
214 DBG_ERROR("SvLockBytes::Flush(): Bad stream");
215 return ERRCODE_NONE;
218 m_pStream->Flush();
219 return m_pStream->GetErrorCode();
222 //============================================================================
223 // virtual
224 ErrCode SvLockBytes::SetSize(sal_Size nSize)
226 if (!m_pStream)
228 DBG_ERROR("SvLockBytes::SetSize(): Bad stream");
229 return ERRCODE_NONE;
232 m_pStream->SetStreamSize(nSize);
233 return m_pStream->GetErrorCode();
236 //============================================================================
237 ErrCode SvLockBytes::LockRegion(sal_Size, sal_Size, LockType)
239 DBG_ERROR("SvLockBytes::LockRegion(): Not implemented");
240 return ERRCODE_NONE;
243 //============================================================================
245 ErrCode SvLockBytes::UnlockRegion(sal_Size, sal_Size, LockType)
247 DBG_ERROR("SvLockBytes::UnlockRegion(): Not implemented");
248 return ERRCODE_NONE;
251 //============================================================================
252 ErrCode SvLockBytes::Stat(SvLockBytesStat * pStat, SvLockBytesStatFlag) const
254 if (!m_pStream)
256 DBG_ERROR("SvLockBytes::Stat(): Bad stream");
257 return ERRCODE_NONE;
260 if (pStat)
262 sal_Size nPos = m_pStream->Tell();
263 pStat->nSize = m_pStream->Seek(STREAM_SEEK_TO_END);
264 m_pStream->Seek(nPos);
266 return ERRCODE_NONE;
269 //============================================================================
271 // class SvOpenLockBytes
273 //============================================================================
275 TYPEINIT1(SvOpenLockBytes, SvLockBytes);
277 //============================================================================
279 // class SvAsyncLockBytes
281 //============================================================================
283 TYPEINIT1(SvAsyncLockBytes, SvOpenLockBytes);
285 //============================================================================
286 // virtual
287 ErrCode SvAsyncLockBytes::ReadAt(sal_Size nPos, void * pBuffer, sal_Size nCount,
288 sal_Size * pRead) const
290 if (m_bTerminated)
291 return SvOpenLockBytes::ReadAt(nPos, pBuffer, nCount, pRead);
292 else
294 sal_Size nTheCount = std::min(nPos < m_nSize ? m_nSize - nPos : 0, nCount);
295 ErrCode nError = SvOpenLockBytes::ReadAt(nPos, pBuffer, nTheCount,
296 pRead);
297 return !nCount || nTheCount == nCount || nError ? nError :
298 ERRCODE_IO_PENDING;
302 //============================================================================
303 // virtual
304 ErrCode SvAsyncLockBytes::WriteAt(sal_Size nPos, const void * pBuffer,
305 sal_Size nCount, sal_Size * pWritten)
307 if (m_bTerminated)
308 return SvOpenLockBytes::WriteAt(nPos, pBuffer, nCount, pWritten);
309 else
311 sal_Size nTheCount = std::min(nPos < m_nSize ? m_nSize - nPos : 0, nCount);
312 ErrCode nError = SvOpenLockBytes::WriteAt(nPos, pBuffer, nTheCount,
313 pWritten);
314 return !nCount || nTheCount == nCount || nError ? nError :
315 ERRCODE_IO_PENDING;
319 //============================================================================
320 // virtual
321 ErrCode SvAsyncLockBytes::FillAppend(const void * pBuffer, sal_Size nCount,
322 sal_Size * pWritten)
324 sal_Size nTheWritten;
325 ErrCode nError = SvOpenLockBytes::WriteAt(m_nSize, pBuffer, nCount,
326 &nTheWritten);
327 if (!nError)
328 m_nSize += nTheWritten;
329 if (pWritten)
330 *pWritten = nTheWritten;
331 return nError;
334 //============================================================================
335 // virtual
336 sal_Size SvAsyncLockBytes::Seek(sal_Size nPos)
338 if (nPos != STREAM_SEEK_TO_END)
339 m_nSize = nPos;
340 return m_nSize;
343 //============================================================================
345 // class SvStream
347 //============================================================================
349 sal_Size SvStream::GetData( void* pData, sal_Size nSize )
351 if( !GetError() )
353 DBG_ASSERT( xLockBytes.Is(), "pure virtual function" );
354 sal_Size nRet;
355 nError = xLockBytes->ReadAt( nActPos, pData, nSize, &nRet );
356 nActPos += nRet;
357 return nRet;
359 else return 0;
362 ErrCode SvStream::SetLockBytes( SvLockBytesRef& rLB )
364 xLockBytes = rLB;
365 RefreshBuffer();
366 return ERRCODE_NONE;
369 //========================================================================
371 sal_Size SvStream::PutData( const void* pData, sal_Size nSize )
373 if( !GetError() )
375 DBG_ASSERT( xLockBytes.Is(), "pure virtual function" );
376 sal_Size nRet;
377 nError = xLockBytes->WriteAt( nActPos, pData, nSize, &nRet );
378 nActPos += nRet;
379 return nRet;
381 else return 0;
384 //========================================================================
386 sal_Size SvStream::SeekPos( sal_Size nPos )
388 if( !GetError() && nPos == STREAM_SEEK_TO_END )
390 DBG_ASSERT( xLockBytes.Is(), "pure virtual function" );
391 SvLockBytesStat aStat;
392 xLockBytes->Stat( &aStat, SVSTATFLAG_DEFAULT );
393 nActPos = aStat.nSize;
395 else
396 nActPos = nPos;
397 return nActPos;
400 //========================================================================
402 void SvStream::FlushData()
404 if( !GetError() )
406 DBG_ASSERT( xLockBytes.Is(), "pure virtual function" );
407 nError = xLockBytes->Flush();
411 //========================================================================
413 void SvStream::SetSize( sal_Size nSize )
415 DBG_ASSERT( xLockBytes.Is(), "pure virtual function" );
416 nError = xLockBytes->SetSize( nSize );
419 void SvStream::ImpInit()
421 nActPos = 0;
422 nCompressMode = COMPRESSMODE_NONE;
423 eStreamCharSet = osl_getThreadTextEncoding();
424 // eTargetCharSet = osl_getThreadTextEncoding();
425 nCryptMask = 0;
426 bIsEof = FALSE;
427 #if defined UNX
428 eLineDelimiter = LINEEND_LF; // UNIX-Format
429 #else
430 eLineDelimiter = LINEEND_CRLF; // DOS-Format
431 #endif
433 SetNumberFormatInt( NUMBERFORMAT_INT_LITTLEENDIAN );
435 nBufFilePos = 0;
436 nBufActualPos = 0;
437 bIsDirty = FALSE;
438 bIsConsistent = TRUE;
439 bIsWritable = TRUE;
441 pRWBuf = 0;
442 pBufPos = 0;
443 nBufSize = 0;
444 nBufActualLen = 0;
445 eIOMode = STREAM_IO_DONTKNOW;
446 nBufFree = 0;
448 nRadix = 10;
449 nPrecision = 0; // all significant digits
450 nWidth = 0; // default width
451 cFiller = ' ';
452 nJustification = JUSTIFY_RIGHT;
453 eStreamMode = 0;
454 CreateFormatString();
456 nVersion = 0;
458 ClearError();
461 /*************************************************************************
463 |* Stream::Stream()
465 |* Beschreibung STREAM.SDW
466 |* Ersterstellung OV 08.06.94
467 |* Letzte Aenderung OV 08.06.94
469 *************************************************************************/
471 SvStream::SvStream( SvLockBytes* pLockBytesP )
473 DBG_CTOR( Stream, NULL );
475 ImpInit();
476 xLockBytes = pLockBytesP;
477 const SvStream* pStrm;
478 if( pLockBytesP ) {
479 pStrm = pLockBytesP->GetStream();
480 if( pStrm ) {
481 SetError( pStrm->GetErrorCode() );
484 SetBufferSize( 256 );
487 SvStream::SvStream()
489 DBG_CTOR( Stream, NULL );
491 ImpInit();
494 /*************************************************************************
496 |* Stream::~Stream()
498 |* Beschreibung STREAM.SDW
499 |* Ersterstellung OV 08.06.94
500 |* Letzte Aenderung OV 08.06.94
502 *************************************************************************/
504 SvStream::~SvStream()
506 DBG_DTOR( Stream, NULL );
508 if ( xLockBytes.Is() )
509 Flush();
511 if( pRWBuf )
512 delete[] pRWBuf;
515 /*************************************************************************
517 |* Stream::IsA()
519 |* Beschreibung STREAM.SDW
520 |* Ersterstellung OV 08.06.94
521 |* Letzte Aenderung OV 08.06.94
523 *************************************************************************/
525 sal_uInt16 SvStream::IsA() const
527 return (sal_uInt16)ID_STREAM;
530 /*************************************************************************
532 |* Stream::ClearError()
534 |* Beschreibung STREAM.SDW
535 |* Ersterstellung OV 08.06.94
536 |* Letzte Aenderung OV 08.06.94
538 *************************************************************************/
540 void SvStream::ClearError()
542 bIsEof = FALSE;
543 nError = SVSTREAM_OK;
546 /*************************************************************************
548 |* Stream::SetError()
550 |* Beschreibung STREAM.SDW
551 |* Ersterstellung OV 08.06.94
552 |* Letzte Aenderung OV 08.06.94
554 *************************************************************************/
556 void SvStream::SetError( sal_uInt32 nErrorCode )
558 if ( nError == SVSTREAM_OK )
559 nError = nErrorCode;
563 /*************************************************************************
565 |* Stream::SetNumberFormatInt()
567 |* Beschreibung STREAM.SDW
568 |* Ersterstellung OV 08.06.94
569 |* Letzte Aenderung OV 08.06.94
571 *************************************************************************/
573 void SvStream::SetNumberFormatInt( sal_uInt16 nNewFormat )
575 nNumberFormatInt = nNewFormat;
576 bSwap = FALSE;
577 #ifdef OSL_BIGENDIAN
578 if( nNumberFormatInt == NUMBERFORMAT_INT_LITTLEENDIAN )
579 bSwap = TRUE;
580 #else
581 if( nNumberFormatInt == NUMBERFORMAT_INT_BIGENDIAN )
582 bSwap = TRUE;
583 #endif
586 /*************************************************************************
588 |* Stream::SetBufferSize()
590 |* Beschreibung STREAM.SDW
591 |* Ersterstellung OV 08.06.94
592 |* Letzte Aenderung OV 08.06.94
594 *************************************************************************/
596 void SvStream::SetBufferSize( sal_uInt16 nBufferSize )
598 sal_Size nActualFilePos = Tell();
599 sal_Bool bDontSeek = (sal_Bool)(pRWBuf == 0);
601 if( bIsDirty && bIsConsistent && bIsWritable ) // wg. Windows NT: Access denied
602 Flush();
604 if( nBufSize )
606 delete[] pRWBuf;
607 nBufFilePos += nBufActualPos;
610 pRWBuf = 0;
611 nBufActualLen = 0;
612 nBufActualPos = 0;
613 nBufSize = nBufferSize;
614 if( nBufSize )
615 pRWBuf = new BYTE[ nBufSize ];
616 bIsConsistent = TRUE;
617 pBufPos = pRWBuf;
618 eIOMode = STREAM_IO_DONTKNOW;
619 if( !bDontSeek )
620 SeekPos( nActualFilePos );
623 /*************************************************************************
625 |* Stream::ClearBuffer()
627 |* Beschreibung STREAM.SDW
628 |* Ersterstellung OV 08.06.94
629 |* Letzte Aenderung OV 08.06.94
631 *************************************************************************/
633 void SvStream::ClearBuffer()
635 nBufActualLen = 0;
636 nBufActualPos = 0;
637 nBufFilePos = 0;
638 pBufPos = pRWBuf;
639 bIsDirty = FALSE;
640 bIsConsistent = TRUE;
641 eIOMode = STREAM_IO_DONTKNOW;
643 bIsEof = FALSE;
646 /*************************************************************************
648 |* Stream::ResetError()
650 |* Beschreibung STREAM.SDW
651 |* Ersterstellung OV 08.06.94
652 |* Letzte Aenderung OV 08.06.94
654 *************************************************************************/
656 void SvStream::ResetError()
658 ClearError();
661 /*************************************************************************
663 |* Stream::ReadLine()
665 |* Beschreibung STREAM.SDW
666 |* Ersterstellung OV 08.06.94
667 |* Letzte Aenderung OV 08.06.94
669 *************************************************************************/
671 sal_Bool SvStream::ReadByteStringLine( String& rStr, rtl_TextEncoding eSrcCharSet )
673 sal_Bool bRet;
674 ByteString aStr;
676 bRet = ReadLine(aStr);
677 rStr = UniString( aStr, eSrcCharSet );
678 return bRet;
681 sal_Bool SvStream::ReadLine( ByteString& rStr )
683 sal_Char buf[256+1];
684 sal_Bool bEnd = FALSE;
685 sal_Size nOldFilePos = Tell();
686 sal_Char c = 0;
687 sal_Size nTotalLen = 0;
689 rStr.Erase();
690 while( !bEnd && !GetError() ) // !!! nicht auf EOF testen,
691 // !!! weil wir blockweise
692 // !!! lesen
694 sal_uInt16 nLen = (sal_uInt16)Read( buf, sizeof(buf)-1 );
695 if ( !nLen )
697 if ( rStr.Len() == 0 )
699 // der allererste Blockread hat fehlgeschlagen -> Abflug
700 bIsEof = TRUE;
701 return FALSE;
703 else
704 break;
707 sal_uInt16 j, n;
708 for( j = n = 0; j < nLen ; ++j )
710 c = buf[j];
711 if ( c == '\n' || c == '\r' )
713 bEnd = TRUE;
714 break;
716 // erAck 26.02.01: Old behavior was no special treatment of '\0'
717 // character here, but a following rStr+=c did ignore it. Is this
718 // really intended? Or should a '\0' better terminate a line?
719 // The nOldFilePos stuff wasn't correct then anyways.
720 if ( c )
722 if ( n < j )
723 buf[n] = c;
724 ++n;
727 if ( n )
728 rStr.Append( buf, n );
729 nTotalLen += j;
732 if ( !bEnd && !GetError() && rStr.Len() )
733 bEnd = TRUE;
735 nOldFilePos += nTotalLen;
736 if( Tell() > nOldFilePos )
737 nOldFilePos++;
738 Seek( nOldFilePos ); // seeken wg. obigem BlockRead!
740 if ( bEnd && (c=='\r' || c=='\n') ) // Sonderbehandlung DOS-Dateien
742 char cTemp;
743 sal_Size nLen = Read((char*)&cTemp , sizeof(cTemp) );
744 if ( nLen ) {
745 if( cTemp == c || (cTemp != '\n' && cTemp != '\r') )
746 Seek( nOldFilePos );
750 if ( bEnd )
751 bIsEof = FALSE;
752 return bEnd;
755 sal_Bool SvStream::ReadUniStringLine( String& rStr )
757 sal_Unicode buf[256+1];
758 sal_Bool bEnd = FALSE;
759 sal_Size nOldFilePos = Tell();
760 sal_Unicode c = 0;
761 sal_Size nTotalLen = 0;
763 DBG_ASSERT( sizeof(sal_Unicode) == sizeof(sal_uInt16), "ReadUniStringLine: swapping sizeof(sal_Unicode) not implemented" );
765 rStr.Erase();
766 while( !bEnd && !GetError() ) // !!! nicht auf EOF testen,
767 // !!! weil wir blockweise
768 // !!! lesen
770 sal_uInt16 nLen = (sal_uInt16)Read( (char*)buf, sizeof(buf)-sizeof(sal_Unicode) );
771 nLen /= sizeof(sal_Unicode);
772 if ( !nLen )
774 if ( rStr.Len() == 0 )
776 // der allererste Blockread hat fehlgeschlagen -> Abflug
777 bIsEof = TRUE;
778 return FALSE;
780 else
781 break;
784 sal_uInt16 j, n;
785 for( j = n = 0; j < nLen ; ++j )
787 if ( bSwap )
788 SwapUShort( buf[n] );
789 c = buf[j];
790 if ( c == '\n' || c == '\r' )
792 bEnd = TRUE;
793 break;
795 // erAck 26.02.01: Old behavior was no special treatment of '\0'
796 // character here, but a following rStr+=c did ignore it. Is this
797 // really intended? Or should a '\0' better terminate a line?
798 // The nOldFilePos stuff wasn't correct then anyways.
799 if ( c )
801 if ( n < j )
802 buf[n] = c;
803 ++n;
806 if ( n )
807 rStr.Append( buf, n );
808 nTotalLen += j;
811 if ( !bEnd && !GetError() && rStr.Len() )
812 bEnd = TRUE;
814 nOldFilePos += nTotalLen * sizeof(sal_Unicode);
815 if( Tell() > nOldFilePos )
816 nOldFilePos += sizeof(sal_Unicode);
817 Seek( nOldFilePos ); // seeken wg. obigem BlockRead!
819 if ( bEnd && (c=='\r' || c=='\n') ) // Sonderbehandlung DOS-Dateien
821 sal_Unicode cTemp;
822 Read( (char*)&cTemp, sizeof(cTemp) );
823 if ( bSwap )
824 SwapUShort( cTemp );
825 if( cTemp == c || (cTemp != '\n' && cTemp != '\r') )
826 Seek( nOldFilePos );
829 if ( bEnd )
830 bIsEof = FALSE;
831 return bEnd;
834 sal_Bool SvStream::ReadUniOrByteStringLine( String& rStr, rtl_TextEncoding eSrcCharSet )
836 if ( eSrcCharSet == RTL_TEXTENCODING_UNICODE )
837 return ReadUniStringLine( rStr );
838 else
839 return ReadByteStringLine( rStr, eSrcCharSet );
842 /*************************************************************************
844 |* Stream::ReadCString
846 *************************************************************************/
848 sal_Bool SvStream::ReadCString( ByteString& rStr )
850 if( rStr.Len() )
851 rStr.Erase();
853 sal_Char buf[ 256 + 1 ];
854 sal_Bool bEnd = FALSE;
855 sal_Size nFilePos = Tell();
857 while( !bEnd && !GetError() )
859 sal_uInt16 nLen = (sal_uInt16)Read( buf, sizeof(buf)-1 );
860 sal_uInt16 nReallyRead = nLen;
861 if( !nLen )
862 break;
864 const sal_Char* pPtr = buf;
865 while( *pPtr && nLen )
866 ++pPtr, --nLen;
868 bEnd = ( nReallyRead < sizeof(buf)-1 ) // read less than attempted to read
869 || ( ( nLen > 0 ) // OR it is inside the block we read
870 && ( 0 == *pPtr ) // AND found a string terminator
873 rStr.Append( buf, ::sal::static_int_cast< xub_StrLen >( pPtr - buf ) );
876 nFilePos += rStr.Len();
877 if( Tell() > nFilePos )
878 nFilePos++;
879 Seek( nFilePos ); // seeken wg. obigem BlockRead!
880 return bEnd;
883 sal_Bool SvStream::ReadCString( String& rStr, rtl_TextEncoding eToEncode )
885 ByteString sStr;
886 sal_Bool bRet = ReadCString( sStr );
887 rStr = String( sStr, eToEncode );
888 return bRet;
892 /*************************************************************************
894 |* Stream::WriteUnicodeText()
896 *************************************************************************/
898 sal_Bool SvStream::WriteUnicodeText( const String& rStr )
900 DBG_ASSERT( sizeof(sal_Unicode) == sizeof(sal_uInt16), "WriteUnicodeText: swapping sizeof(sal_Unicode) not implemented" );
901 if ( bSwap )
903 xub_StrLen nLen = rStr.Len();
904 sal_Unicode aBuf[384];
905 sal_Unicode* const pTmp = ( nLen > 384 ? new sal_Unicode[nLen] : aBuf);
906 memcpy( pTmp, rStr.GetBuffer(), nLen * sizeof(sal_Unicode) );
907 sal_Unicode* p = pTmp;
908 const sal_Unicode* const pStop = pTmp + nLen;
909 while ( p < pStop )
911 SwapUShort( *p );
912 p++;
914 Write( (char*)pTmp, nLen * sizeof(sal_Unicode) );
915 if ( pTmp != aBuf )
916 delete [] pTmp;
918 else
919 Write( (char*)rStr.GetBuffer(), rStr.Len() * sizeof(sal_Unicode) );
920 return nError == SVSTREAM_OK;
923 sal_Bool SvStream::WriteUnicodeOrByteText( const String& rStr, rtl_TextEncoding eDestCharSet )
925 if ( eDestCharSet == RTL_TEXTENCODING_UNICODE )
926 return WriteUnicodeText( rStr );
927 else
929 ByteString aStr( rStr, eDestCharSet );
930 Write( aStr.GetBuffer(), aStr.Len() );
931 return nError == SVSTREAM_OK;
935 /*************************************************************************
937 |* Stream::WriteLine()
939 |* Beschreibung STREAM.SDW
940 |* Ersterstellung OV 08.06.94
941 |* Letzte Aenderung OV 08.06.94
943 *************************************************************************/
945 sal_Bool SvStream::WriteByteStringLine( const String& rStr, rtl_TextEncoding eDestCharSet )
947 return WriteLine( ByteString( rStr, eDestCharSet ) );
950 sal_Bool SvStream::WriteLine( const ByteString& rStr )
952 Write( rStr.GetBuffer(), rStr.Len() );
953 endl(*this);
954 return nError == SVSTREAM_OK;
957 sal_Bool SvStream::WriteUniStringLine( const String& rStr )
959 WriteUnicodeText( rStr );
960 endlu(*this);
961 return nError == SVSTREAM_OK;
964 sal_Bool SvStream::WriteUniOrByteStringLine( const String& rStr, rtl_TextEncoding eDestCharSet )
966 if ( eDestCharSet == RTL_TEXTENCODING_UNICODE )
967 return WriteUniStringLine( rStr );
968 else
969 return WriteByteStringLine( rStr, eDestCharSet );
972 /*************************************************************************
974 |* Stream::WriteLines()
976 |* Beschreibung STREAM.SDW
977 |* Ersterstellung OV 17.07.95
978 |* Letzte Aenderung OV 17.07.95
980 *************************************************************************/
982 sal_Bool SvStream::WriteByteStringLines( const String& rStr, rtl_TextEncoding eDestCharSet )
984 return WriteLines( ByteString( rStr, eDestCharSet ) );
987 sal_Bool SvStream::WriteLines( const ByteString& rStr )
989 ByteString aStr( rStr );
990 aStr.ConvertLineEnd( eLineDelimiter );
991 Write( aStr.GetBuffer(), aStr.Len() );
992 endl( *this );
993 return (sal_Bool)(nError == SVSTREAM_OK);
996 sal_Bool SvStream::WriteUniStringLines( const String& rStr )
998 String aStr( rStr );
999 aStr.ConvertLineEnd( eLineDelimiter );
1000 WriteUniStringLine( aStr );
1001 return nError == SVSTREAM_OK;
1004 sal_Bool SvStream::WriteUniOrByteStringLines( const String& rStr, rtl_TextEncoding eDestCharSet )
1006 if ( eDestCharSet == RTL_TEXTENCODING_UNICODE )
1007 return WriteUniStringLines( rStr );
1008 else
1009 return WriteByteStringLines( rStr, eDestCharSet );
1012 /*************************************************************************
1014 |* Stream::WriteUniOrByteChar()
1016 *************************************************************************/
1018 sal_Bool SvStream::WriteUniOrByteChar( sal_Unicode ch, rtl_TextEncoding eDestCharSet )
1020 if ( eDestCharSet == RTL_TEXTENCODING_UNICODE )
1021 *this << ch;
1022 else
1024 ByteString aStr( ch, eDestCharSet );
1025 Write( aStr.GetBuffer(), aStr.Len() );
1027 return nError == SVSTREAM_OK;
1030 /*************************************************************************
1032 |* Stream::StartWritingUnicodeText()
1034 *************************************************************************/
1036 sal_Bool SvStream::StartWritingUnicodeText()
1038 SetEndianSwap( FALSE ); // write native format
1039 // BOM, Byte Order Mark, U+FEFF, see
1040 // http://www.unicode.org/faq/utf_bom.html#BOM
1041 // Upon read: 0xfeff(-257) => no swap; 0xfffe(-2) => swap
1042 *this << sal_uInt16( 0xfeff );
1043 return nError == SVSTREAM_OK;
1046 /*************************************************************************
1048 |* Stream::StartReadingUnicodeText()
1050 *************************************************************************/
1052 sal_Bool SvStream::StartReadingUnicodeText()
1054 sal_uInt16 nFlag;
1055 *this >> nFlag;
1056 switch ( nFlag )
1058 case 0xfeff :
1059 // native
1060 break;
1061 case 0xfffe :
1062 SetEndianSwap( !bSwap );
1063 break;
1064 default:
1065 SeekRel( -((sal_sSize)sizeof(nFlag)) ); // no BOM, pure data
1067 return nError == SVSTREAM_OK;
1070 /*************************************************************************
1072 |* Stream::ReadCsvLine()
1074 *************************************************************************/
1076 // Precondition: pStr is guaranteed to be non-NULL and points to a 0-terminated
1077 // array.
1078 inline const sal_Unicode* lcl_UnicodeStrChr( const sal_Unicode* pStr,
1079 sal_Unicode c )
1081 while (*pStr)
1083 if (*pStr == c)
1084 return pStr;
1085 ++pStr;
1087 return 0;
1090 sal_Bool SvStream::ReadCsvLine( String& rStr, sal_Bool bEmbeddedLineBreak,
1091 const String& rFieldSeparators, sal_Unicode cFieldQuote,
1092 sal_Bool bAllowBackslashEscape)
1094 ReadUniOrByteStringLine( rStr);
1096 if (bEmbeddedLineBreak)
1098 const sal_Unicode* pSeps = rFieldSeparators.GetBuffer();
1099 xub_StrLen nLastOffset = 0;
1100 xub_StrLen nQuotes = 0;
1101 while (!IsEof() && rStr.Len() < STRING_MAXLEN)
1103 bool bBackslashEscaped = false;
1104 const sal_Unicode *p, *pStart;
1105 p = pStart = rStr.GetBuffer();
1106 p += nLastOffset;
1107 while (*p)
1109 if (nQuotes)
1111 if (*p == cFieldQuote && !bBackslashEscaped)
1112 ++nQuotes;
1113 else if (bAllowBackslashEscape)
1115 if (*p == '\\')
1116 bBackslashEscaped = !bBackslashEscaped;
1117 else
1118 bBackslashEscaped = false;
1121 else if (*p == cFieldQuote && (p == pStart ||
1122 lcl_UnicodeStrChr( pSeps, p[-1])))
1123 nQuotes = 1;
1124 // A quote character inside a field content does not start
1125 // a quote.
1126 ++p;
1129 if (nQuotes % 2 == 0)
1130 break;
1131 else
1133 nLastOffset = rStr.Len();
1134 String aNext;
1135 ReadUniOrByteStringLine( aNext);
1136 rStr += sal_Unicode(_LF);
1137 rStr += aNext;
1141 return nError == SVSTREAM_OK;
1144 /*************************************************************************
1146 |* Stream::SeekRel()
1148 |* Beschreibung STREAM.SDW
1149 |* Ersterstellung OV 08.06.94
1150 |* Letzte Aenderung OV 08.06.94
1152 *************************************************************************/
1154 sal_Size SvStream::SeekRel( sal_sSize nPos )
1156 sal_Size nActualPos = Tell();
1158 if ( nPos >= 0 )
1160 if ( SAL_MAX_SIZE - nActualPos > (sal_Size)nPos )
1161 nActualPos += nPos;
1163 else
1165 sal_Size nAbsPos = (sal_Size)-nPos;
1166 if ( nActualPos >= nAbsPos )
1167 nActualPos -= nAbsPos;
1170 pBufPos = pRWBuf + nActualPos;
1171 return Seek( nActualPos );
1174 /*************************************************************************
1176 |* Stream::operator>>()
1178 |* Beschreibung STREAM.SDW
1179 |* Ersterstellung OV 08.06.94
1180 |* Letzte Aenderung OV 08.06.94
1182 *************************************************************************/
1184 SvStream& SvStream::operator >> ( sal_uInt16& r )
1186 READNUMBER_WITHOUT_SWAP(sal_uInt16,r)
1187 if( bSwap )
1188 SwapUShort(r);
1189 return *this;
1192 SvStream& SvStream::operator>> ( sal_uInt32& r )
1194 READNUMBER_WITHOUT_SWAP(sal_uInt32,r)
1195 if( bSwap )
1196 SwapULong(r);
1197 return *this;
1200 SvStream& SvStream::operator >> ( long& r )
1202 #if(SAL_TYPES_SIZEOFLONG != 4)
1203 int tmp = r;
1204 *this >> tmp;
1205 r = tmp;
1206 #else
1207 READNUMBER_WITHOUT_SWAP(long,r)
1208 if( bSwap )
1209 SwapLong(r);
1210 #endif
1211 return *this;
1214 SvStream& SvStream::operator >> ( short& r )
1216 READNUMBER_WITHOUT_SWAP(short,r)
1217 if( bSwap )
1218 SwapShort(r);
1219 return *this;
1222 SvStream& SvStream::operator >> ( int& r )
1224 READNUMBER_WITHOUT_SWAP(int,r)
1225 if( bSwap )
1226 SwapLongInt(r);
1227 return *this;
1230 SvStream& SvStream::operator>>( signed char& r )
1232 if( (eIOMode == STREAM_IO_READ || !bIsConsistent) &&
1233 sizeof(signed char) <= nBufFree )
1235 r = *pBufPos;
1236 nBufActualPos += sizeof(signed char);
1237 pBufPos += sizeof(signed char);
1238 nBufFree -= sizeof(signed char);
1240 else
1241 Read( (char*)&r, sizeof(signed char) );
1242 return *this;
1245 // Sonderbehandlung fuer Chars wegen PutBack
1247 SvStream& SvStream::operator>>( char& r )
1249 if( (eIOMode == STREAM_IO_READ || !bIsConsistent) &&
1250 sizeof(char) <= nBufFree )
1252 r = *pBufPos;
1253 nBufActualPos += sizeof(char);
1254 pBufPos += sizeof(char);
1255 nBufFree -= sizeof(char);
1257 else
1258 Read( (char*)&r, sizeof(char) );
1259 return *this;
1262 SvStream& SvStream::operator>>( unsigned char& r )
1264 if( (eIOMode == STREAM_IO_READ || !bIsConsistent) &&
1265 sizeof(char) <= nBufFree )
1267 r = *pBufPos;
1268 nBufActualPos += sizeof(char);
1269 pBufPos += sizeof(char);
1270 nBufFree -= sizeof(char);
1272 else
1273 Read( (char*)&r, sizeof(char) );
1274 return *this;
1277 SvStream& SvStream::operator>>( float& r )
1279 // Read( (char*)&r, sizeof(float) );
1280 READNUMBER_WITHOUT_SWAP(float,r)
1281 #if defined UNX
1282 if( bSwap )
1283 SwapFloat(r);
1284 #endif
1285 return *this;
1288 SvStream& SvStream::operator>>( double& r )
1290 // Read( (char*)&r, sizeof(double) );
1291 READNUMBER_WITHOUT_SWAP(double,r)
1292 #if defined UNX
1293 if( bSwap )
1294 SwapDouble(r);
1295 #endif
1296 return *this;
1299 SvStream& SvStream::operator>> ( SvStream& rStream )
1301 const sal_uInt32 cBufLen = 0x8000;
1302 char* pBuf = new char[ cBufLen ];
1304 sal_uInt32 nCount;
1305 do {
1306 nCount = Read( pBuf, cBufLen );
1307 rStream.Write( pBuf, nCount );
1308 } while( nCount == cBufLen );
1310 delete[] pBuf;
1311 return *this;
1314 /*************************************************************************
1316 |* Stream::operator<<()
1318 |* Beschreibung STREAM.SDW
1319 |* Ersterstellung OV 08.06.94
1320 |* Letzte Aenderung OV 08.06.94
1322 *************************************************************************/
1324 SvStream& SvStream::operator<< ( sal_uInt16 v )
1326 if( bSwap )
1327 SwapUShort(v);
1328 WRITENUMBER_WITHOUT_SWAP(sal_uInt16,v)
1329 return *this;
1332 SvStream& SvStream::operator<< ( sal_uInt32 v )
1334 if( bSwap )
1335 SwapULong(v);
1336 WRITENUMBER_WITHOUT_SWAP(sal_uInt32,v)
1337 return *this;
1340 SvStream& SvStream::operator<< ( long v )
1342 #if(SAL_TYPES_SIZEOFLONG != 4)
1343 int tmp = v;
1344 *this << tmp;
1345 #else
1346 if( bSwap )
1347 SwapLong(v);
1348 WRITENUMBER_WITHOUT_SWAP(long,v)
1349 #endif
1350 return *this;
1353 SvStream& SvStream::operator<< ( short v )
1355 if( bSwap )
1356 SwapShort(v);
1357 WRITENUMBER_WITHOUT_SWAP(short,v)
1358 return *this;
1361 SvStream& SvStream::operator<<( int v )
1363 if( bSwap )
1364 SwapLongInt( v );
1365 WRITENUMBER_WITHOUT_SWAP(int,v)
1366 return *this;
1369 SvStream& SvStream::operator<< ( signed char v )
1371 //SDO
1372 int tmp = eIOMode;
1373 if(tmp == STREAM_IO_WRITE && sizeof(signed char) <= nBufFree )
1375 *pBufPos = v;
1376 pBufPos++; // sizeof(char);
1377 nBufActualPos++;
1378 if( nBufActualPos > nBufActualLen ) // Append ?
1379 nBufActualLen = nBufActualPos;
1380 nBufFree--; // = sizeof(char);
1381 bIsDirty = TRUE;
1383 else
1384 Write( (char*)&v, sizeof(signed char) );
1385 return *this;
1388 // Sonderbehandlung fuer chars wegen PutBack
1390 SvStream& SvStream::operator<< ( char v )
1392 //SDO
1393 int tmp = eIOMode;
1394 if(tmp == STREAM_IO_WRITE && sizeof(char) <= nBufFree )
1396 *pBufPos = v;
1397 pBufPos++; // sizeof(char);
1398 nBufActualPos++;
1399 if( nBufActualPos > nBufActualLen ) // Append ?
1400 nBufActualLen = nBufActualPos;
1401 nBufFree--; // = sizeof(char);
1402 bIsDirty = TRUE;
1404 else
1405 Write( (char*)&v, sizeof(char) );
1406 return *this;
1409 SvStream& SvStream::operator<< ( unsigned char v )
1411 //SDO
1412 int tmp = eIOMode;
1413 if(tmp == STREAM_IO_WRITE && sizeof(char) <= nBufFree )
1415 *(unsigned char*)pBufPos = v;
1416 pBufPos++; // = sizeof(char);
1417 nBufActualPos++; // = sizeof(char);
1418 if( nBufActualPos > nBufActualLen ) // Append ?
1419 nBufActualLen = nBufActualPos;
1420 nBufFree--;
1421 bIsDirty = TRUE;
1423 else
1424 Write( (char*)&v, sizeof(char) );
1425 return *this;
1428 SvStream& SvStream::operator<< ( float v )
1430 #ifdef UNX
1431 if( bSwap )
1432 SwapFloat(v);
1433 #endif
1434 WRITENUMBER_WITHOUT_SWAP(float,v)
1435 return *this;
1438 SvStream& SvStream::operator<< ( const double& r )
1440 // Write( (char*)&r, sizeof( double ) );
1441 #if defined UNX
1442 if( bSwap )
1444 double nHelp = r;
1445 SwapDouble(nHelp);
1446 WRITENUMBER_WITHOUT_SWAP(double,nHelp)
1447 return *this;
1449 else
1450 #endif
1451 WRITENUMBER_WITHOUT_SWAP(double,r)
1453 return *this;
1456 SvStream& SvStream::operator<< ( const char* pBuf )
1458 Write( pBuf, strlen( pBuf ) );
1459 return *this;
1462 SvStream& SvStream::operator<< ( const unsigned char* pBuf )
1464 Write( (char*)pBuf, strlen( (char*)pBuf ) );
1465 return *this;
1468 SvStream& SvStream::operator<< ( SvStream& rStream )
1470 const sal_uInt32 cBufLen = 0x8000;
1471 char* pBuf = new char[ cBufLen ];
1472 sal_uInt32 nCount;
1473 do {
1474 nCount = rStream.Read( pBuf, cBufLen );
1475 Write( pBuf, nCount );
1476 } while( nCount == cBufLen );
1478 delete[] pBuf;
1479 return *this;
1482 // -----------------------------------------------------------------------
1484 SvStream& SvStream::ReadByteString( UniString& rStr, rtl_TextEncoding eSrcCharSet )
1486 // read UTF-16 string directly from stream ?
1487 if (eSrcCharSet == RTL_TEXTENCODING_UNICODE)
1489 sal_uInt32 nLen;
1490 operator>> (nLen);
1491 if (nLen)
1493 if (nLen > STRING_MAXLEN) {
1494 SetError(SVSTREAM_GENERALERROR);
1495 return *this;
1497 sal_Unicode *pStr = rStr.AllocBuffer(
1498 static_cast< xub_StrLen >(nLen));
1499 BOOST_STATIC_ASSERT(STRING_MAXLEN <= SAL_MAX_SIZE / 2);
1500 Read( pStr, nLen << 1 );
1502 if (bSwap)
1503 for (sal_Unicode *pEnd = pStr + nLen; pStr < pEnd; pStr++)
1504 SwapUShort(*pStr);
1506 else
1507 rStr.Erase();
1509 return *this;
1512 ByteString aStr;
1513 ReadByteString( aStr );
1514 rStr = UniString( aStr, eSrcCharSet );
1515 return *this;
1518 // -----------------------------------------------------------------------
1520 SvStream& SvStream::ReadByteString( ByteString& rStr )
1522 sal_uInt16 nLen = 0;
1523 operator>>( nLen );
1524 if( nLen )
1526 char* pTmp = rStr.AllocBuffer( nLen );
1527 nLen = (sal_uInt16)Read( pTmp, nLen );
1529 else
1530 rStr.Erase();
1531 return *this;
1534 // -----------------------------------------------------------------------
1536 SvStream& SvStream::WriteByteString( const UniString& rStr, rtl_TextEncoding eDestCharSet )
1538 // write UTF-16 string directly into stream ?
1539 if (eDestCharSet == RTL_TEXTENCODING_UNICODE)
1541 sal_uInt32 nLen = rStr.Len();
1542 operator<< (nLen);
1543 if (nLen)
1545 if (bSwap)
1547 const sal_Unicode *pStr = rStr.GetBuffer();
1548 const sal_Unicode *pEnd = pStr + nLen;
1550 for (; pStr < pEnd; pStr++)
1552 sal_Unicode c = *pStr;
1553 SwapUShort(c);
1554 WRITENUMBER_WITHOUT_SWAP(sal_uInt16,c)
1557 else
1558 Write( rStr.GetBuffer(), nLen << 1 );
1561 return *this;
1564 return WriteByteString(ByteString( rStr, eDestCharSet ));
1567 // -----------------------------------------------------------------------
1569 SvStream& SvStream::WriteByteString( const ByteString& rStr)
1571 sal_uInt16 nLen = rStr.Len();
1572 operator<< ( nLen );
1573 if( nLen != 0 )
1574 Write( rStr.GetBuffer(), nLen );
1575 return *this;
1578 /*************************************************************************
1580 |* Stream::Read()
1582 |* Beschreibung STREAM.SDW
1583 |* Ersterstellung OV 08.06.94
1584 |* Letzte Aenderung OV 08.06.94
1586 *************************************************************************/
1588 sal_Size SvStream::Read( void* pData, sal_Size nCount )
1590 sal_Size nSaveCount = nCount;
1591 if( !bIsConsistent )
1592 RefreshBuffer();
1594 if( !pRWBuf )
1596 nCount = GetData( (char*)pData,nCount);
1597 if( nCryptMask )
1598 EncryptBuffer(pData, nCount);
1599 nBufFilePos += nCount;
1601 else
1603 // ist Block komplett im Puffer
1604 eIOMode = STREAM_IO_READ;
1605 if( nCount <= (sal_Size)(nBufActualLen - nBufActualPos ) )
1607 // Ja!
1608 memcpy(pData, pBufPos, (size_t) nCount);
1609 nBufActualPos = nBufActualPos + (sal_uInt16)nCount;
1610 pBufPos += nCount;
1611 nBufFree = nBufFree - (sal_uInt16)nCount;
1613 else
1615 if( bIsDirty ) // Flushen ?
1617 SeekPos( nBufFilePos );
1618 if( nCryptMask )
1619 CryptAndWriteBuffer(pRWBuf, nBufActualLen);
1620 else
1621 PutData( pRWBuf, nBufActualLen );
1622 bIsDirty = FALSE;
1625 // passt der Datenblock in den Puffer ?
1626 if( nCount > nBufSize )
1628 // Nein! Deshalb ohne Umweg ueber den Puffer direkt
1629 // in den Zielbereich einlesen
1631 eIOMode = STREAM_IO_DONTKNOW;
1633 SeekPos( nBufFilePos + nBufActualPos );
1634 nBufActualLen = 0;
1635 pBufPos = pRWBuf;
1636 nCount = GetData( (char*)pData, nCount );
1637 if( nCryptMask )
1638 EncryptBuffer(pData, nCount);
1639 nBufFilePos += nCount;
1640 nBufFilePos += nBufActualPos;
1641 nBufActualPos = 0;
1643 else
1645 // Der Datenblock passt komplett in den Puffer. Deshalb
1646 // Puffer fuellen und dann die angeforderten Daten in den
1647 // Zielbereich kopieren.
1649 nBufFilePos += nBufActualPos;
1650 SeekPos( nBufFilePos );
1652 // TODO: Typecast vor GetData, sal_uInt16 nCountTmp
1653 sal_Size nCountTmp = GetData( pRWBuf, nBufSize );
1654 if( nCryptMask )
1655 EncryptBuffer(pRWBuf, nCountTmp);
1656 nBufActualLen = (sal_uInt16)nCountTmp;
1657 if( nCount > nCountTmp )
1659 nCount = nCountTmp; // zurueckstutzen, Eof siehe unten
1661 memcpy( pData, pRWBuf, (size_t)nCount );
1662 nBufActualPos = (sal_uInt16)nCount;
1663 pBufPos = pRWBuf + nCount;
1667 bIsEof = FALSE;
1668 nBufFree = nBufActualLen - nBufActualPos;
1669 if( nCount != nSaveCount && nError != ERRCODE_IO_PENDING )
1670 bIsEof = TRUE;
1671 if( nCount == nSaveCount && nError == ERRCODE_IO_PENDING )
1672 nError = ERRCODE_NONE;
1673 return nCount;
1676 /*************************************************************************
1678 |* Stream::Write()
1680 |* Beschreibung STREAM.SDW
1681 |* Ersterstellung OV 08.06.94
1682 |* Letzte Aenderung OV 08.06.94
1684 *************************************************************************/
1686 sal_Size SvStream::Write( const void* pData, sal_Size nCount )
1688 if( !nCount )
1689 return 0;
1690 if( !bIsWritable )
1692 SetError( ERRCODE_IO_CANTWRITE );
1693 return 0;
1695 if( !bIsConsistent )
1696 RefreshBuffer(); // Aenderungen des Puffers durch PutBack loeschen
1698 if( !pRWBuf )
1700 if( nCryptMask )
1701 nCount = CryptAndWriteBuffer( pData, nCount );
1702 else
1703 nCount = PutData( (char*)pData, nCount );
1704 nBufFilePos += nCount;
1705 return nCount;
1708 eIOMode = STREAM_IO_WRITE;
1709 if( nCount <= (sal_Size)(nBufSize - nBufActualPos) )
1711 memcpy( pBufPos, pData, (size_t)nCount );
1712 nBufActualPos = nBufActualPos + (sal_uInt16)nCount;
1713 // wurde der Puffer erweitert ?
1714 if( nBufActualPos > nBufActualLen )
1715 nBufActualLen = nBufActualPos;
1717 pBufPos += nCount;
1718 bIsDirty = TRUE;
1720 else
1722 // Flushen ?
1723 if( bIsDirty )
1725 SeekPos( nBufFilePos );
1726 if( nCryptMask )
1727 CryptAndWriteBuffer( pRWBuf, (sal_Size)nBufActualLen );
1728 else
1729 PutData( pRWBuf, nBufActualLen );
1730 bIsDirty = FALSE;
1733 // passt der Block in den Puffer ?
1734 if( nCount > nBufSize )
1736 eIOMode = STREAM_IO_DONTKNOW;
1737 nBufFilePos += nBufActualPos;
1738 nBufActualLen = 0;
1739 nBufActualPos = 0;
1740 pBufPos = pRWBuf;
1741 SeekPos( nBufFilePos );
1742 if( nCryptMask )
1743 nCount = CryptAndWriteBuffer( pData, nCount );
1744 else
1745 nCount = PutData( (char*)pData, nCount );
1746 nBufFilePos += nCount;
1748 else
1750 // Block in Puffer stellen
1751 memcpy( pRWBuf, pData, (size_t)nCount );
1753 // Reihenfolge!
1754 nBufFilePos += nBufActualPos;
1755 nBufActualPos = (sal_uInt16)nCount;
1756 pBufPos = pRWBuf + nCount;
1757 nBufActualLen = (sal_uInt16)nCount;
1758 bIsDirty = TRUE;
1761 nBufFree = nBufSize - nBufActualPos;
1762 return nCount;
1766 /*************************************************************************
1768 |* Stream::Seek()
1770 |* Beschreibung STREAM.SDW
1771 |* Ersterstellung OV 08.06.94
1772 |* Letzte Aenderung OV 08.06.94
1774 *************************************************************************/
1776 sal_Size SvStream::Seek( sal_Size nFilePos )
1778 eIOMode = STREAM_IO_DONTKNOW;
1780 bIsEof = FALSE;
1781 if( !pRWBuf )
1783 nBufFilePos = SeekPos( nFilePos );
1784 DBG_ASSERT(Tell()==nBufFilePos,"Out Of Sync!");
1785 return nBufFilePos;
1788 // Ist Position im Puffer ?
1789 if( nFilePos >= nBufFilePos && nFilePos <= (nBufFilePos + nBufActualLen))
1791 nBufActualPos = (sal_uInt16)(nFilePos - nBufFilePos);
1792 pBufPos = pRWBuf + nBufActualPos;
1793 // nBufFree korrigieren, damit wir nicht von einem
1794 // PutBack (ignoriert den StreamMode) getoetet werden
1795 nBufFree = nBufActualLen - nBufActualPos;
1797 else
1799 if( bIsDirty && bIsConsistent)
1801 SeekPos( nBufFilePos );
1802 if( nCryptMask )
1803 CryptAndWriteBuffer( pRWBuf, nBufActualLen );
1804 else
1805 PutData( pRWBuf, nBufActualLen );
1806 bIsDirty = FALSE;
1808 nBufActualLen = 0;
1809 nBufActualPos = 0;
1810 pBufPos = pRWBuf;
1811 nBufFilePos = SeekPos( nFilePos );
1813 #ifdef OV_DEBUG
1815 sal_Size nDebugTemp = nBufFilePos + nBufActualPos;
1816 DBG_ASSERT(Tell()==nDebugTemp,"Sync?");
1818 #endif
1819 return nBufFilePos + nBufActualPos;
1822 /*************************************************************************
1824 |* Stream::Flush()
1826 |* Beschreibung STREAM.SDW
1827 |* Ersterstellung OV 08.06.94
1828 |* Letzte Aenderung OV 08.06.94
1830 *************************************************************************/
1832 void SvStream::Flush()
1834 if( bIsDirty && bIsConsistent )
1836 SeekPos( nBufFilePos );
1837 if( nCryptMask )
1838 CryptAndWriteBuffer( pRWBuf, (sal_Size)nBufActualLen );
1839 else
1840 if( PutData( pRWBuf, nBufActualLen ) != nBufActualLen )
1841 SetError( SVSTREAM_WRITE_ERROR );
1842 bIsDirty = FALSE;
1844 if( bIsWritable )
1845 FlushData();
1849 /*************************************************************************
1851 |* Stream::PutBack()
1853 |* Beschreibung STREAM.SDW
1854 |* Ersterstellung OV 01.08.94
1855 |* Letzte Aenderung OV 01.08.94
1857 *************************************************************************/
1860 4 Faelle :
1862 1. Datenzeiger steht mitten im Puffer (nBufActualPos >= 1)
1863 2. Datenzeiger auf Position 0, Puffer ist voll
1864 3. Datenzeiger auf Position 0, Puffer ist teilweise gefuellt
1865 4. Datenzeiger auf Position 0, Puffer ist leer -> Fehler!
1868 SvStream& SvStream::PutBack( char aCh )
1870 // wenn kein Buffer oder Zurueckscrollen nicht moeglich -> Fehler
1871 if( !pRWBuf || !nBufActualLen || ( !nBufActualPos && !nBufFilePos ) )
1873 // 4. Fall
1874 SetError( SVSTREAM_GENERALERROR );
1875 return *this;
1878 // Flush() (Phys. Flushen aber nicht notwendig, deshalb selbst schreiben)
1879 if( bIsConsistent && bIsDirty )
1881 SeekPos( nBufFilePos );
1882 if( nCryptMask )
1883 CryptAndWriteBuffer( pRWBuf, nBufActualLen );
1884 else
1885 PutData( pRWBuf, nBufActualLen );
1886 bIsDirty = FALSE;
1888 bIsConsistent = FALSE; // Puffer enthaelt jetzt TRASH
1889 if( nBufActualPos )
1891 // 1. Fall
1892 nBufActualPos--;
1893 pBufPos--;
1894 *pBufPos = aCh;
1895 nBufFree++;
1897 else // Puffer muss verschoben werden
1899 // Ist Puffer am Anschlag ?
1900 if( nBufSize == nBufActualLen )
1902 // 2. Fall
1903 memmove( pRWBuf+1, pRWBuf, nBufSize-1 );
1904 // nBufFree behaelt den Wert!
1906 else
1908 // 3. Fall -> Puffer vergroessern
1909 memmove( pRWBuf+1, pRWBuf, (sal_uInt16)nBufActualLen );
1910 nBufActualLen++;
1911 nBufFree++;
1913 nBufFilePos--;
1914 *pRWBuf = aCh;
1916 eIOMode = STREAM_IO_DONTKNOW;
1917 bIsEof = FALSE;
1918 return *this;
1921 /*************************************************************************
1923 |* Stream::EatWhite()
1925 |* Beschreibung STREAM.SDW
1926 |* Ersterstellung OV 01.08.94
1927 |* Letzte Aenderung OV 01.08.94
1929 *************************************************************************/
1931 void SvStream::EatWhite()
1933 char aCh;
1934 Read(&aCh, sizeof(char) );
1935 while( !bIsEof && isspace((int)aCh) ) //( aCh == ' ' || aCh == '\t' ) )
1936 Read(&aCh, sizeof(char) );
1937 if( !bIsEof ) // konnte das letzte Char gelesen werden ?
1938 SeekRel( -1L );
1941 /*************************************************************************
1943 |* Stream::RefreshBuffer()
1945 |* Beschreibung STREAM.SDW
1946 |* Ersterstellung OV 01.08.94
1947 |* Letzte Aenderung OV 01.08.94
1949 *************************************************************************/
1951 void SvStream::RefreshBuffer()
1953 if( bIsDirty && bIsConsistent )
1955 SeekPos( nBufFilePos );
1956 if( nCryptMask )
1957 CryptAndWriteBuffer( pRWBuf, (sal_Size)nBufActualLen );
1958 else
1959 PutData( pRWBuf, nBufActualLen );
1960 bIsDirty = FALSE;
1962 SeekPos( nBufFilePos );
1963 nBufActualLen = (sal_uInt16)GetData( pRWBuf, nBufSize );
1964 if( nBufActualLen && nError == ERRCODE_IO_PENDING )
1965 nError = ERRCODE_NONE;
1966 if( nCryptMask )
1967 EncryptBuffer(pRWBuf, (sal_Size)nBufActualLen);
1968 bIsConsistent = TRUE;
1969 eIOMode = STREAM_IO_DONTKNOW;
1973 /*************************************************************************
1975 |* Stream::CreateFormatString()
1977 |* Beschreibung Baut Formatstring zusammen
1978 |* Ersterstellung OV 08.06.94
1979 |* Letzte Aenderung OV 08.06.94
1981 *************************************************************************/
1983 void SvStream::CreateFormatString()
1985 aFormatString = '%';
1986 nPrintfParams = SPECIAL_PARAM_NONE;
1988 if( nJustification )
1990 aFormatString += '-';
1993 if( nWidth )
1995 if( cFiller != ' ' )
1996 aFormatString += '0';
1997 aFormatString += '*';
1998 nPrintfParams = SPECIAL_PARAM_WIDTH;
2001 if( nPrecision )
2003 aFormatString += ".*";
2004 if( nWidth )
2005 nPrintfParams = SPECIAL_PARAM_BOTH;
2006 else
2007 nPrintfParams = SPECIAL_PARAM_PRECISION;
2011 /*************************************************************************
2013 |* Stream::ReadNumber()
2015 |* Beschreibung STREAM.SDW
2016 |* Ersterstellung OV 08.06.94
2017 |* Letzte Aenderung OV 08.06.94
2019 *************************************************************************/
2021 #define BUFSIZE_LONG 21 // log( 2 hoch 64 ) + 1
2023 SvStream& SvStream::ReadNumber( long& rLong )
2025 EatWhite();
2026 if( bIsEof || nError )
2028 SetError( SVSTREAM_GENERALERROR );
2029 return *this;
2031 sal_Size nFPtr = Tell();
2032 char buf[ BUFSIZE_LONG ];
2033 memset( buf, 0, BUFSIZE_LONG );
2034 sal_Size nTemp = Read( buf, BUFSIZE_LONG-1 );
2035 if( !nTemp || nError )
2037 SetError( SVSTREAM_GENERALERROR );
2038 return *this;
2040 char *pEndPtr;
2041 rLong = strtol( buf, &pEndPtr, (int)nRadix );
2042 nFPtr += ( (sal_Size)pEndPtr - (sal_Size)(&(buf[0])) );
2043 Seek( nFPtr );
2044 bIsEof = FALSE;
2045 return *this;
2048 SvStream& SvStream::ReadNumber( sal_uInt32& rUInt32 )
2050 EatWhite();
2051 if( bIsEof || nError )
2053 SetError( SVSTREAM_GENERALERROR );
2054 return *this;
2056 sal_Size nFPtr = Tell();
2057 char buf[ BUFSIZE_LONG ];
2058 memset( buf, 0, BUFSIZE_LONG );
2059 sal_Size nTemp = Read( buf, BUFSIZE_LONG-1 );
2060 if( !nTemp || nError )
2062 SetError( SVSTREAM_GENERALERROR );
2063 return *this;
2065 char *pEndPtr;
2066 rUInt32 = strtoul( buf, &pEndPtr, (int)nRadix );
2067 nFPtr += ( (sal_uIntPtr)pEndPtr - (sal_uIntPtr)buf );
2068 Seek( nFPtr );
2069 bIsEof = FALSE;
2070 return *this;
2073 SvStream& SvStream::ReadNumber( double& rDouble )
2075 EatWhite();
2076 if( bIsEof || nError )
2078 SetError( SVSTREAM_GENERALERROR );
2079 return *this;
2081 sal_Size nFPtr = Tell();
2082 char buf[ BUFSIZE_LONG ];
2083 memset( buf, 0, BUFSIZE_LONG );
2084 sal_Size nTemp = Read( buf, BUFSIZE_LONG-1 );
2085 if( !nTemp || nError )
2087 SetError( SVSTREAM_GENERALERROR );
2088 return *this;
2090 char *pEndPtr;
2091 rDouble = strtod( buf, &pEndPtr );
2092 nFPtr += ( (sal_Size)pEndPtr - (sal_Size)buf );
2093 Seek( nFPtr );
2094 bIsEof = FALSE;
2095 return *this;
2099 /*************************************************************************
2101 |* Stream::WriteNumber()
2103 |* Beschreibung STREAM.SDW
2104 |* Ersterstellung OV 08.06.94
2105 |* Letzte Aenderung OV 08.06.94
2107 *************************************************************************/
2109 SvStream& SvStream::WriteNumber( long nLong )
2111 char buffer[256+12];
2112 char pType[] = "ld"; // Nicht static!
2113 if( nRadix == 16 )
2114 pType[1] = 'x';
2115 else if( nRadix == 8 )
2116 pType[1] = 'o';
2117 ByteString aFStr( aFormatString);
2118 aFStr += pType;
2119 int nLen;
2120 switch ( nPrintfParams )
2122 case SPECIAL_PARAM_NONE :
2123 nLen = sprintf( buffer, aFStr.GetBuffer(), nLong );
2124 break;
2125 case SPECIAL_PARAM_WIDTH :
2126 nLen = sprintf( buffer, aFStr.GetBuffer(), nWidth, nLong );
2127 break;
2128 case SPECIAL_PARAM_PRECISION :
2129 nLen = sprintf( buffer, aFStr.GetBuffer(), nPrecision,nLong);
2130 break;
2131 default:
2132 nLen=sprintf(buffer, aFStr.GetBuffer(),nWidth,nPrecision,nLong);
2134 Write( buffer, (long)nLen );
2135 return *this;
2138 SvStream& SvStream::WriteNumber( sal_uInt32 nUInt32 )
2140 char buffer[256+12];
2141 char pType[] = "lu"; // Nicht static!
2142 if( nRadix == 16 )
2143 pType[1] = 'x';
2144 else if( nRadix == 8 )
2145 pType[1] = 'o';
2146 ByteString aFStr( aFormatString);
2147 aFStr += pType;
2148 int nLen;
2149 switch ( nPrintfParams )
2151 case SPECIAL_PARAM_NONE :
2152 nLen = sprintf( buffer, aFStr.GetBuffer(), nUInt32 );
2153 break;
2154 case SPECIAL_PARAM_WIDTH :
2155 nLen = sprintf( buffer, aFStr.GetBuffer(), nWidth, nUInt32 );
2156 break;
2157 case SPECIAL_PARAM_PRECISION :
2158 nLen = sprintf( buffer, aFStr.GetBuffer(), nPrecision, nUInt32 );
2159 break;
2160 default:
2161 nLen=sprintf(buffer,aFStr.GetBuffer(),nWidth,nPrecision,nUInt32 );
2163 Write( buffer, (long)nLen );
2164 return *this;
2168 SvStream& SvStream::WriteNumber( const double& rDouble )
2170 char buffer[256+24];
2171 ByteString aFStr( aFormatString);
2172 aFStr += "lf";
2173 int nLen;
2174 switch ( nPrintfParams )
2176 case SPECIAL_PARAM_NONE :
2177 nLen = sprintf( buffer, aFStr.GetBuffer(), rDouble );
2178 break;
2179 case SPECIAL_PARAM_WIDTH :
2180 nLen = sprintf( buffer, aFStr.GetBuffer(), nWidth, rDouble );
2181 break;
2182 case SPECIAL_PARAM_PRECISION :
2183 nLen = sprintf( buffer, aFStr.GetBuffer(), nPrecision, rDouble);
2184 break;
2185 default:
2186 nLen=sprintf(buffer, aFStr.GetBuffer(),nWidth,nPrecision,rDouble);
2188 Write( buffer, (long)nLen );
2189 return *this;
2192 /*************************************************************************
2194 |* Stream::CryptAndWriteBuffer()
2196 |* Beschreibung Verschluesseln und Schreiben
2197 |* Ersterstellung OV 08.06.94
2198 |* Letzte Aenderung OV 08.06.94
2200 *************************************************************************/
2202 #define CRYPT_BUFSIZE 1024
2204 sal_Size SvStream::CryptAndWriteBuffer( const void* pStart, sal_Size nLen)
2206 unsigned char pTemp[CRYPT_BUFSIZE];
2207 unsigned char* pDataPtr = (unsigned char*)pStart;
2208 sal_Size nCount = 0;
2209 sal_Size nBufCount;
2210 unsigned char nMask = nCryptMask;
2213 if( nLen >= CRYPT_BUFSIZE )
2214 nBufCount = CRYPT_BUFSIZE;
2215 else
2216 nBufCount = nLen;
2217 nLen -= nBufCount;
2218 memcpy( pTemp, pDataPtr, (sal_uInt16)nBufCount );
2219 // **** Verschluesseln *****
2220 for ( sal_uInt16 n=0; n < CRYPT_BUFSIZE; n++ )
2222 unsigned char aCh = pTemp[n];
2223 aCh ^= nMask;
2224 SWAPNIBBLES(aCh)
2225 pTemp[n] = aCh;
2227 // *************************
2228 nCount += PutData( (char*)pTemp, nBufCount );
2229 pDataPtr += nBufCount;
2231 while ( nLen );
2232 return nCount;
2235 /*************************************************************************
2237 |* Stream::EncryptBuffer()
2239 |* Beschreibung Buffer entschluesseln
2240 |* Ersterstellung OV 08.06.94
2241 |* Letzte Aenderung OV 08.06.94
2243 *************************************************************************/
2245 sal_Bool SvStream::EncryptBuffer(void* pStart, sal_Size nLen)
2247 unsigned char* pTemp = (unsigned char*)pStart;
2248 unsigned char nMask = nCryptMask;
2250 for ( sal_Size n=0; n < nLen; n++, pTemp++ )
2252 unsigned char aCh = *pTemp;
2253 SWAPNIBBLES(aCh)
2254 aCh ^= nMask;
2255 *pTemp = aCh;
2257 return TRUE;
2260 /*************************************************************************
2262 |* Stream::SetKey()
2264 |* Beschreibung STREAM.SDW
2265 |* Ersterstellung OV 08.06.94
2266 |* Letzte Aenderung OV 08.06.94
2268 *************************************************************************/
2270 unsigned char implGetCryptMask(const sal_Char* pStr, sal_Int32 nLen, long nVersion)
2272 unsigned char nCryptMask = 0;
2274 if (!nLen)
2275 return nCryptMask;
2277 if( nVersion <= SOFFICE_FILEFORMAT_31 )
2279 while( nLen )
2281 nCryptMask ^= *pStr;
2282 pStr++;
2283 nLen--;
2286 else // BugFix #25888#
2288 for( sal_uInt16 i = 0; i < nLen; i++ ) {
2289 nCryptMask ^= pStr[i];
2290 if( nCryptMask & 0x80 ) {
2291 nCryptMask <<= 1;
2292 nCryptMask++;
2294 else
2295 nCryptMask <<= 1;
2299 if( !nCryptMask )
2300 nCryptMask = 67;
2302 return nCryptMask;
2305 void SvStream::SetKey( const ByteString& rKey )
2307 aKey = rKey;
2308 nCryptMask = implGetCryptMask( aKey.GetBuffer(), aKey.Len(), GetVersion() );
2311 /*************************************************************************
2313 |* Stream::SyncSvStream()
2315 |* Beschreibung STREAM.SDW
2316 |* Ersterstellung OV 08.06.94
2317 |* Letzte Aenderung OV 08.06.94
2319 *************************************************************************/
2321 void SvStream::SyncSvStream( sal_Size nNewStreamPos )
2323 ClearBuffer();
2324 SvStream::nBufFilePos = nNewStreamPos;
2327 /*************************************************************************
2329 |* Stream::SyncSysStream()
2331 |* Beschreibung STREAM.SDW
2332 |* Ersterstellung OV 08.06.94
2333 |* Letzte Aenderung OV 08.06.94
2335 *************************************************************************/
2337 void SvStream::SyncSysStream()
2339 Flush();
2340 SeekPos( Tell() );
2343 /*************************************************************************
2345 |* Stream::SetStreamSize()
2347 |* Beschreibung STREAM.SDW
2348 |* Ersterstellung OV 08.06.94
2349 |* Letzte Aenderung OV 08.06.94
2351 *************************************************************************/
2353 sal_Bool SvStream::SetStreamSize( sal_Size nSize )
2355 #ifdef DBG_UTIL
2356 sal_Size nFPos = Tell();
2357 #endif
2358 sal_uInt16 nBuf = nBufSize;
2359 SetBufferSize( 0 );
2360 SetSize( nSize );
2361 SetBufferSize( nBuf );
2362 DBG_ASSERT(Tell()==nFPos,"SetStreamSize failed");
2363 return (sal_Bool)(nError == 0);
2366 //============================================================================
2368 void SvStream::AddMark( sal_Size )
2372 //============================================================================
2374 void SvStream::RemoveMark( sal_Size )
2378 /*************************************************************************
2380 |* endl()
2382 |* Beschreibung STREAM.SDW
2383 |* Ersterstellung OV 08.06.94
2384 |* Letzte Aenderung TH 13.11.96
2386 *************************************************************************/
2388 SvStream& endl( SvStream& rStr )
2390 LineEnd eDelim = rStr.GetLineDelimiter();
2391 if ( eDelim == LINEEND_CR )
2392 rStr << _CR;
2393 else if( eDelim == LINEEND_LF )
2394 rStr << _LF;
2395 else
2396 rStr << _CR << _LF;
2397 return rStr;
2400 SvStream& endlu( SvStream& rStrm )
2402 switch ( rStrm.GetLineDelimiter() )
2404 case LINEEND_CR :
2405 rStrm << sal_Unicode(_CR);
2406 break;
2407 case LINEEND_LF :
2408 rStrm << sal_Unicode(_LF);
2409 break;
2410 default:
2411 rStrm << sal_Unicode(_CR) << sal_Unicode(_LF);
2413 return rStrm;
2416 SvStream& endlub( SvStream& rStrm )
2418 if ( rStrm.GetStreamCharSet() == RTL_TEXTENCODING_UNICODE )
2419 return endlu( rStrm );
2420 else
2421 return endl( rStrm );
2424 /*************************************************************************
2426 |* SvMemoryStream::SvMemoryStream()
2428 |* Beschreibung STREAM.SDW
2429 |* Ersterstellung OV 20.06.94
2430 |* Letzte Aenderung OV 20.06.94
2432 *************************************************************************/
2434 SvMemoryStream::SvMemoryStream( void* pBuffer, sal_Size bufSize,
2435 StreamMode eMode )
2437 if( eMode & STREAM_WRITE )
2438 bIsWritable = TRUE;
2439 else
2440 bIsWritable = FALSE;
2441 nEndOfData = bufSize;
2442 bOwnsData = FALSE;
2443 pBuf = (BYTE *) pBuffer;
2444 nResize = 0L;
2445 nSize = bufSize;
2446 nPos = 0L;
2447 SetBufferSize( 0 );
2450 /*************************************************************************
2452 |* SvMemoryStream::SvMemoryStream()
2454 |* Beschreibung STREAM.SDW
2455 |* Ersterstellung OV 20.06.94
2456 |* Letzte Aenderung OV 20.06.94
2458 *************************************************************************/
2460 SvMemoryStream::SvMemoryStream( sal_Size nInitSize, sal_Size nResizeOffset )
2462 bIsWritable = TRUE;
2463 bOwnsData = TRUE;
2464 nEndOfData = 0L;
2465 nResize = nResizeOffset;
2466 nPos = 0;
2467 pBuf = 0;
2468 if( nResize != 0 && nResize < 16 )
2469 nResize = 16;
2470 if( nInitSize && !AllocateMemory( nInitSize ) )
2472 SetError( SVSTREAM_OUTOFMEMORY );
2473 nSize = 0;
2475 else
2476 nSize = nInitSize;
2477 SetBufferSize( 64 );
2480 /*************************************************************************
2482 |* SvMemoryStream::~SvMemoryStream()
2484 |* Beschreibung STREAM.SDW
2485 |* Ersterstellung OV 20.06.94
2486 |* Letzte Aenderung OV 20.06.94
2488 *************************************************************************/
2490 SvMemoryStream::~SvMemoryStream()
2492 if( pBuf )
2494 if( bOwnsData )
2495 FreeMemory();
2496 else
2497 Flush();
2501 /*************************************************************************
2503 |* SvMemoryStream::IsA()
2505 |* Beschreibung STREAM.SDW
2506 |* Ersterstellung OV 20.06.94
2507 |* Letzte Aenderung OV 20.06.94
2509 *************************************************************************/
2511 sal_uInt16 SvMemoryStream::IsA() const
2513 return (sal_uInt16)ID_MEMORYSTREAM;
2516 /*************************************************************************
2518 |* SvMemoryStream::SetBuffer()
2520 |* Beschreibung STREAM.SDW
2521 |* Ersterstellung OV 20.06.94
2522 |* Letzte Aenderung OV 20.06.94
2524 *************************************************************************/
2526 void* SvMemoryStream::SetBuffer( void* pNewBuf, sal_Size nCount,
2527 sal_Bool bOwnsDat, sal_Size nEOF )
2529 void* pResult;
2530 SetBufferSize( 0 ); // Buffering in der Basisklasse initialisieren
2531 Seek( 0 );
2532 if( bOwnsData )
2534 pResult = 0;
2535 if( pNewBuf != pBuf )
2536 FreeMemory();
2538 else
2539 pResult = pBuf;
2541 pBuf = (BYTE *) pNewBuf;
2542 nPos = 0;
2543 nSize = nCount;
2544 nResize = 0;
2545 bOwnsData = bOwnsDat;
2547 if( nEOF > nCount )
2548 nEOF = nCount;
2549 nEndOfData = nEOF;
2551 ResetError();
2553 DBG_ASSERT( nEndOfData<STREAM_SEEK_TO_END,"Invalid EOF");
2554 return pResult;
2557 /*************************************************************************
2559 |* SvMemoryStream::GetData()
2561 |* Beschreibung STREAM.SDW
2562 |* Ersterstellung OV 20.06.94
2563 |* Letzte Aenderung OV 20.06.94
2565 *************************************************************************/
2567 sal_Size SvMemoryStream::GetData( void* pData, sal_Size nCount )
2569 sal_Size nMaxCount = nEndOfData-nPos;
2570 if( nCount > nMaxCount )
2571 nCount = nMaxCount;
2572 memcpy( pData, pBuf+nPos, (size_t)nCount );
2573 nPos += nCount;
2574 return nCount;
2577 /*************************************************************************
2579 |* SvMemoryStream::PutData()
2581 |* Beschreibung STREAM.SDW
2582 |* Ersterstellung OV 20.06.94
2583 |* Letzte Aenderung OV 20.06.94
2585 *************************************************************************/
2587 sal_Size SvMemoryStream::PutData( const void* pData, sal_Size nCount )
2589 if( GetError() )
2590 return 0L;
2592 sal_Size nMaxCount = nSize-nPos;
2594 // auf Ueberlauf testen
2595 if( nCount > nMaxCount )
2597 if( nResize == 0 )
2599 // soviel wie moeglich rueberschaufeln
2600 nCount = nMaxCount;
2601 SetError( SVSTREAM_OUTOFMEMORY );
2603 else
2605 long nNewResize;
2606 if( nSize && nSize > nResize )
2607 nNewResize = nSize;
2608 else
2609 nNewResize = nResize;
2611 if( (nCount-nMaxCount) < nResize )
2613 // fehlender Speicher ist kleiner als Resize-Offset,
2614 // deshalb um Resize-Offset vergroessern
2615 if( !ReAllocateMemory( nNewResize) )
2617 nCount = 0;
2618 SetError( SVSTREAM_WRITE_ERROR );
2621 else
2623 // fehlender Speicher ist groesser als Resize-Offset
2624 // deshalb um Differenz+ResizeOffset vergroessern
2625 if( !ReAllocateMemory( nCount-nMaxCount+nNewResize ) )
2627 nCount = 0;
2628 SetError( SVSTREAM_WRITE_ERROR );
2633 DBG_ASSERT(pBuf,"Possibly Reallocate failed");
2634 memcpy( pBuf+nPos, pData, (size_t)nCount);
2636 nPos += nCount;
2637 if( nPos > nEndOfData )
2638 nEndOfData = nPos;
2639 return nCount;
2642 /*************************************************************************
2644 |* SvMemoryStream::SeekPos()
2646 |* Beschreibung STREAM.SDW
2647 |* Ersterstellung OV 20.06.94
2648 |* Letzte Aenderung OV 20.06.94
2650 *************************************************************************/
2652 // nEndOfData: Erste Position im Stream, die nicht gelesen werden darf
2653 // nSize: Groesse des allozierten Speichers
2655 sal_Size SvMemoryStream::SeekPos( sal_Size nNewPos )
2657 if( nNewPos < nEndOfData )
2658 nPos = nNewPos;
2659 else if( nNewPos == STREAM_SEEK_TO_END )
2660 nPos = nEndOfData;
2661 else
2663 if( nNewPos >= nSize ) // muss Buffer vergroessert werden ?
2665 if( nResize ) // ist vergroeseern erlaubt ?
2667 long nDiff = (long)(nNewPos - nSize + 1);
2668 nDiff += (long)nResize;
2669 ReAllocateMemory( nDiff );
2670 nPos = nNewPos;
2671 nEndOfData = nNewPos;
2673 else // vergroessern ist nicht erlaubt -> ans Ende setzen
2675 // SetError( SVSTREAM_OUTOFMEMORY );
2676 nPos = nEndOfData;
2679 else // gueltigen Bereich innerhalb des Buffers vergroessern
2681 nPos = nNewPos;
2682 nEndOfData = nNewPos;
2685 return nPos;
2688 /*************************************************************************
2690 |* SvMemoryStream::FlushData()
2692 |* Beschreibung STREAM.SDW
2693 |* Ersterstellung OV 20.06.94
2694 |* Letzte Aenderung OV 20.06.94
2696 *************************************************************************/
2698 void SvMemoryStream::FlushData()
2702 /*************************************************************************
2704 |* SvMemoryStream::ResetError()
2706 |* Beschreibung STREAM.SDW
2707 |* Ersterstellung OV 20.06.94
2708 |* Letzte Aenderung OV 20.06.94
2710 *************************************************************************/
2712 void SvMemoryStream::ResetError()
2714 SvStream::ClearError();
2717 /*************************************************************************
2719 |* SvMemoryStream::AllocateMemory()
2721 |* Beschreibung STREAM.SDW
2722 |* Ersterstellung OV 20.06.94
2723 |* Letzte Aenderung OV 20.06.94
2725 *************************************************************************/
2727 sal_Bool SvMemoryStream::AllocateMemory( sal_Size nNewSize )
2729 pBuf = new BYTE[nNewSize];
2730 return( pBuf != 0 );
2733 /*************************************************************************
2735 |* SvMemoryStream::ReAllocateMemory() (Bozo-Algorithmus)
2737 |* Beschreibung STREAM.SDW
2738 |* Ersterstellung OV 20.06.94
2739 |* Letzte Aenderung OV 20.06.94
2741 *************************************************************************/
2743 sal_Bool SvMemoryStream::ReAllocateMemory( long nDiff )
2745 sal_Bool bRetVal = FALSE;
2746 long nTemp = (long)nSize;
2747 nTemp += nDiff;
2748 sal_Size nNewSize = (sal_Size)nTemp;
2750 if( nNewSize )
2752 BYTE* pNewBuf = new BYTE[nNewSize];
2754 if( pNewBuf )
2756 bRetVal = TRUE; // Success!
2757 if( nNewSize < nSize ) // Verkleinern ?
2759 memcpy( pNewBuf, pBuf, (size_t)nNewSize );
2760 if( nPos > nNewSize )
2761 nPos = 0L;
2762 if( nEndOfData >= nNewSize )
2763 nEndOfData = nNewSize-1L;
2765 else
2767 memcpy( pNewBuf, pBuf, (size_t)nSize );
2770 FreeMemory();
2772 pBuf = pNewBuf;
2773 nSize = nNewSize;
2776 else
2778 bRetVal = TRUE;
2779 FreeMemory();
2780 pBuf = 0;
2781 nSize = 0;
2782 nEndOfData = 0;
2783 nPos = 0;
2786 return bRetVal;
2789 void SvMemoryStream::FreeMemory()
2791 delete[] pBuf;
2794 /*************************************************************************
2796 |* SvMemoryStream::SwitchBuffer()
2798 |* Beschreibung STREAM.SDW
2799 |* Ersterstellung OV 26.07.94
2800 |* Letzte Aenderung OV 26.07.94
2802 *************************************************************************/
2804 void* SvMemoryStream::SwitchBuffer( sal_Size nInitSize, sal_Size nResizeOffset)
2806 Flush();
2807 if( !bOwnsData )
2808 return 0;
2809 Seek( STREAM_SEEK_TO_BEGIN );
2811 void* pRetVal = pBuf;
2812 pBuf = 0;
2813 nEndOfData = 0L;
2814 nResize = nResizeOffset;
2815 nPos = 0;
2817 if( nResize != 0 && nResize < 16 )
2818 nResize = 16;
2820 ResetError();
2822 if( nInitSize && !AllocateMemory(nInitSize) )
2824 SetError( SVSTREAM_OUTOFMEMORY );
2825 nSize = 0;
2827 else
2828 nSize = nInitSize;
2830 SetBufferSize( 64 );
2831 return pRetVal;
2834 void SvMemoryStream::SetSize( sal_Size nNewSize )
2836 long nDiff = (long)nNewSize - (long)nSize;
2837 ReAllocateMemory( nDiff );
2840 TYPEINIT0 ( SvDataCopyStream )
2842 void SvDataCopyStream::Assign( const SvDataCopyStream& )