xref: /trunk/main/tools/source/stream/stream.cxx (revision 7b2bc0e6)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_tools.hxx"
26 
27 // ToDo:
28 //  - Read->RefreshBuffer->Auf Aenderungen von nBufActualLen reagieren
29 
30 #include <cstddef>
31 
32 #include <string.h>
33 #include <stdio.h>
34 #include <ctype.h>  // isspace
35 #include <stdlib.h> // strtol, _crotl
36 
37 #include "boost/static_assert.hpp"
38 
39 /*
40 #if defined( DBG_UTIL ) && (OSL_DEBUG_LEVEL > 1)
41 // prueft Synchronisation des Buffers nach allen Read, Write, Seek
42 #define OV_DEBUG
43 #endif
44 */
45 
46 #include <tools/solar.h>
47 
48 #if defined(BLC)
49 #define SWAPNIBBLES(c) c=_crotl(c,4);
50 #else
51 #define SWAPNIBBLES(c)      \
52 unsigned char nSwapTmp=c;   \
53 nSwapTmp <<= 4;             \
54 c >>= 4;                    \
55 c |= nSwapTmp;
56 #endif
57 
58 #include <tools/debug.hxx>
59 #define ENABLE_BYTESTRING_STREAM_OPERATORS
60 #include <tools/stream.hxx>
61 #include <osl/thread.h>
62 #include <algorithm>
63 
64 // -----------------------------------------------------------------------
65 
DBG_NAME(Stream)66 DBG_NAME( Stream )
67 
68 // -----------------------------------------------------------------------
69 
70 // sprintf Param-Mode
71 #define SPECIAL_PARAM_NONE 0        // Format-Str, Number
72 #define SPECIAL_PARAM_WIDTH 1       // Format-Str, Width, Number
73 #define SPECIAL_PARAM_PRECISION 2   // Format-Str, Precision, Number
74 #define SPECIAL_PARAM_BOTH 3        // Format-Str, Width, Precision, Number
75 
76 // -----------------------------------------------------------------------
77 
78 // !!! Nicht inline, wenn Operatoren <<,>> inline sind
79 inline static void SwapUShort( sal_uInt16& r )
80     {   r = SWAPSHORT(r);   }
SwapShort(short & r)81 inline static void SwapShort( short& r )
82     {   r = SWAPSHORT(r);   }
SwapLong(long & r)83 inline static void SwapLong( long& r )
84     {   r = SWAPLONG(r);   }
SwapULong(sal_uInt32 & r)85 inline static void SwapULong( sal_uInt32& r )
86     {   r = SWAPLONG(r);   }
SwapLongInt(int & r)87 inline static void SwapLongInt( int& r )
88     {   r = SWAPLONG(r);   }
SwapLongUInt(unsigned int & r)89 inline static void SwapLongUInt( unsigned int& r )
90     {   r = SWAPLONG(r);   }
91 #ifdef UNX
SwapFloat(float & r)92 inline static void SwapFloat( float& r )
93     {
94           union
95           {
96               float f;
97               sal_uInt32 c;
98           } s;
99 
100           s.f = r;
101           s.c = SWAPLONG( s.c );
102           r = s.f;
103     }
SwapDouble(double & r)104 inline static void SwapDouble( double& r )
105     {
106         if( sizeof(double) != 8 )
107         {
108           DBG_ASSERT( sal_False, "Can only swap 8-Byte-doubles\n" );
109         }
110         else
111         {
112           union
113           {
114               double d;
115               sal_uInt32 c[2];
116           } s;
117 
118           s.d = r;
119           s.c[0] ^= s.c[1]; // zwei 32-Bit-Werte in situ vertauschen
120           s.c[1] ^= s.c[0];
121           s.c[0] ^= s.c[1];
122           s.c[0] = SWAPLONG(s.c[0]); // und die beiden 32-Bit-Werte selbst in situ drehen
123           s.c[1] = SWAPLONG(s.c[1]);
124           r = s.d;
125         }
126     }
127 #endif
128 
129 //SDO
130 
131 #define READNUMBER_WITHOUT_SWAP(datatype,value) \
132 {\
133 int tmp = eIOMode; \
134 if( (tmp == STREAM_IO_READ) && sizeof(datatype)<=nBufFree) \
135 {\
136     for (std::size_t i = 0; i < sizeof(datatype); i++)\
137         ((char *)&value)[i] = pBufPos[i];\
138     nBufActualPos += sizeof(datatype);\
139     pBufPos += sizeof(datatype);\
140     nBufFree -= sizeof(datatype);\
141 }\
142 else\
143     Read( (char*)&value, sizeof(datatype) );\
144 }
145 
146 #define WRITENUMBER_WITHOUT_SWAP(datatype,value) \
147 {\
148 int tmp = eIOMode; \
149 if( (tmp==STREAM_IO_WRITE) && sizeof(datatype) <= nBufFree)\
150 {\
151     for (std::size_t i = 0; i < sizeof(datatype); i++)\
152         pBufPos[i] = ((char *)&value)[i];\
153     nBufFree -= sizeof(datatype);\
154     nBufActualPos += sizeof(datatype);\
155     if( nBufActualPos > nBufActualLen )\
156         nBufActualLen = nBufActualPos;\
157     pBufPos += sizeof(datatype);\
158     bIsDirty = sal_True;\
159 }\
160 else\
161     Write( (char*)&value, sizeof(datatype) );\
162 }
163 
164 //============================================================================
165 //
166 //  class SvLockBytes
167 //
168 //============================================================================
169 
close()170 void SvLockBytes::close()
171 {
172     if (m_bOwner)
173         delete m_pStream;
174     m_pStream = 0;
175 }
176 
177 //============================================================================
178 TYPEINIT0(SvLockBytes);
179 
180 //============================================================================
181 // virtual
ReadAt(sal_Size nPos,void * pBuffer,sal_Size nCount,sal_Size * pRead) const182 ErrCode SvLockBytes::ReadAt(sal_Size nPos, void * pBuffer, sal_Size nCount,
183                             sal_Size * pRead) const
184 {
185     if (!m_pStream)
186     {
187         DBG_ERROR("SvLockBytes::ReadAt(): Bad stream");
188         return ERRCODE_NONE;
189     }
190 
191     m_pStream->Seek(nPos);
192     sal_Size nTheRead = m_pStream->Read(pBuffer, nCount);
193     if (pRead)
194         *pRead = nTheRead;
195     return m_pStream->GetErrorCode();
196 }
197 
198 //============================================================================
199 // virtual
WriteAt(sal_Size nPos,const void * pBuffer,sal_Size nCount,sal_Size * pWritten)200 ErrCode SvLockBytes::WriteAt(sal_Size nPos, const void * pBuffer, sal_Size nCount,
201                              sal_Size * pWritten)
202 {
203     if (!m_pStream)
204     {
205         DBG_ERROR("SvLockBytes::WriteAt(): Bad stream");
206         return ERRCODE_NONE;
207     }
208 
209     m_pStream->Seek(nPos);
210     sal_Size nTheWritten = m_pStream->Write(pBuffer, nCount);
211     if (pWritten)
212         *pWritten = nTheWritten;
213     return m_pStream->GetErrorCode();
214 }
215 
216 //============================================================================
217 // virtual
Flush() const218 ErrCode SvLockBytes::Flush() const
219 {
220     if (!m_pStream)
221     {
222         DBG_ERROR("SvLockBytes::Flush(): Bad stream");
223         return ERRCODE_NONE;
224     }
225 
226     m_pStream->Flush();
227     return m_pStream->GetErrorCode();
228 }
229 
230 //============================================================================
231 // virtual
SetSize(sal_Size nSize)232 ErrCode SvLockBytes::SetSize(sal_Size nSize)
233 {
234     if (!m_pStream)
235     {
236         DBG_ERROR("SvLockBytes::SetSize(): Bad stream");
237         return ERRCODE_NONE;
238     }
239 
240     m_pStream->SetStreamSize(nSize);
241     return m_pStream->GetErrorCode();
242 }
243 
244 //============================================================================
LockRegion(sal_Size,sal_Size,LockType)245 ErrCode SvLockBytes::LockRegion(sal_Size, sal_Size, LockType)
246 {
247     DBG_ERROR("SvLockBytes::LockRegion(): Not implemented");
248     return ERRCODE_NONE;
249 }
250 
251 //============================================================================
252 
UnlockRegion(sal_Size,sal_Size,LockType)253 ErrCode SvLockBytes::UnlockRegion(sal_Size, sal_Size, LockType)
254 {
255     DBG_ERROR("SvLockBytes::UnlockRegion(): Not implemented");
256     return ERRCODE_NONE;
257 }
258 
259 //============================================================================
Stat(SvLockBytesStat * pStat,SvLockBytesStatFlag) const260 ErrCode SvLockBytes::Stat(SvLockBytesStat * pStat, SvLockBytesStatFlag) const
261 {
262     if (!m_pStream)
263     {
264         DBG_ERROR("SvLockBytes::Stat(): Bad stream");
265         return ERRCODE_NONE;
266     }
267 
268     if (pStat)
269     {
270         sal_Size nPos = m_pStream->Tell();
271         pStat->nSize = m_pStream->Seek(STREAM_SEEK_TO_END);
272         m_pStream->Seek(nPos);
273     }
274     return ERRCODE_NONE;
275 }
276 
277 //============================================================================
278 //
279 //  class SvOpenLockBytes
280 //
281 //============================================================================
282 
283 TYPEINIT1(SvOpenLockBytes, SvLockBytes);
284 
285 //============================================================================
286 //
287 //  class SvAsyncLockBytes
288 //
289 //============================================================================
290 
291 TYPEINIT1(SvAsyncLockBytes, SvOpenLockBytes);
292 
293 //============================================================================
294 // virtual
ReadAt(sal_Size nPos,void * pBuffer,sal_Size nCount,sal_Size * pRead) const295 ErrCode SvAsyncLockBytes::ReadAt(sal_Size nPos, void * pBuffer, sal_Size nCount,
296                                  sal_Size * pRead) const
297 {
298     if (m_bTerminated)
299         return SvOpenLockBytes::ReadAt(nPos, pBuffer, nCount, pRead);
300     else
301     {
302         sal_Size nTheCount = std::min(nPos < m_nSize ? m_nSize - nPos : 0, nCount);
303         ErrCode nError = SvOpenLockBytes::ReadAt(nPos, pBuffer, nTheCount,
304                                                  pRead);
305         return !nCount || nTheCount == nCount || nError ? nError :
306                                                           ERRCODE_IO_PENDING;
307     }
308 }
309 
310 //============================================================================
311 // virtual
WriteAt(sal_Size nPos,const void * pBuffer,sal_Size nCount,sal_Size * pWritten)312 ErrCode SvAsyncLockBytes::WriteAt(sal_Size nPos, const void * pBuffer,
313                                   sal_Size nCount, sal_Size * pWritten)
314 {
315     if (m_bTerminated)
316         return SvOpenLockBytes::WriteAt(nPos, pBuffer, nCount, pWritten);
317     else
318     {
319         sal_Size nTheCount = std::min(nPos < m_nSize ? m_nSize - nPos : 0, nCount);
320         ErrCode nError = SvOpenLockBytes::WriteAt(nPos, pBuffer, nTheCount,
321                                                   pWritten);
322         return !nCount || nTheCount == nCount || nError ? nError :
323                                                           ERRCODE_IO_PENDING;
324     }
325 }
326 
327 //============================================================================
328 // virtual
FillAppend(const void * pBuffer,sal_Size nCount,sal_Size * pWritten)329 ErrCode SvAsyncLockBytes::FillAppend(const void * pBuffer, sal_Size nCount,
330                                      sal_Size * pWritten)
331 {
332     sal_Size nTheWritten;
333     ErrCode nError = SvOpenLockBytes::WriteAt(m_nSize, pBuffer, nCount,
334                                               &nTheWritten);
335     if (!nError)
336         m_nSize += nTheWritten;
337     if (pWritten)
338         *pWritten = nTheWritten;
339     return nError;
340 }
341 
342 //============================================================================
343 // virtual
Seek(sal_Size nPos)344 sal_Size SvAsyncLockBytes::Seek(sal_Size nPos)
345 {
346     if (nPos != STREAM_SEEK_TO_END)
347         m_nSize = nPos;
348     return m_nSize;
349 }
350 
351 //============================================================================
352 //
353 //  class SvStream
354 //
355 //============================================================================
356 
GetData(void * pData,sal_Size nSize)357 sal_Size SvStream::GetData( void* pData, sal_Size nSize )
358 {
359     if( !GetError() )
360     {
361         DBG_ASSERT( xLockBytes.Is(), "pure virtual function" );
362         sal_Size nRet;
363         nError = xLockBytes->ReadAt( nActPos, pData, nSize, &nRet );
364         nActPos += nRet;
365         return nRet;
366     }
367     else return 0;
368 }
369 
SetLockBytes(SvLockBytesRef & rLB)370 ErrCode SvStream::SetLockBytes( SvLockBytesRef& rLB )
371 {
372     xLockBytes = rLB;
373     RefreshBuffer();
374     return ERRCODE_NONE;
375 }
376 
377 //========================================================================
378 
PutData(const void * pData,sal_Size nSize)379 sal_Size SvStream::PutData( const void* pData, sal_Size nSize )
380 {
381     if( !GetError() )
382     {
383         DBG_ASSERT( xLockBytes.Is(), "pure virtual function" );
384         sal_Size nRet;
385         nError = xLockBytes->WriteAt( nActPos, pData, nSize, &nRet );
386         nActPos += nRet;
387         return nRet;
388     }
389     else return 0;
390 }
391 
392 //========================================================================
393 
SeekPos(sal_Size nPos)394 sal_Size SvStream::SeekPos( sal_Size nPos )
395 {
396     if( !GetError() && nPos == STREAM_SEEK_TO_END )
397     {
398         DBG_ASSERT( xLockBytes.Is(), "pure virtual function" );
399         SvLockBytesStat aStat;
400         xLockBytes->Stat( &aStat, SVSTATFLAG_DEFAULT );
401         nActPos = aStat.nSize;
402     }
403     else
404         nActPos = nPos;
405     return nActPos;
406 }
407 
408 //========================================================================
409 
FlushData()410 void SvStream::FlushData()
411 {
412     if( !GetError() )
413     {
414         DBG_ASSERT( xLockBytes.Is(), "pure virtual function" );
415         nError = xLockBytes->Flush();
416     }
417 }
418 
419 //========================================================================
420 
SetSize(sal_Size nSize)421 void SvStream::SetSize( sal_Size nSize )
422 {
423     DBG_ASSERT( xLockBytes.Is(), "pure virtual function" );
424     nError = xLockBytes->SetSize( nSize );
425 }
426 
ImpInit()427 void SvStream::ImpInit()
428 {
429     nActPos             = 0;
430     nCompressMode       = COMPRESSMODE_NONE;
431     eStreamCharSet      = osl_getThreadTextEncoding();
432 //  eTargetCharSet      = osl_getThreadTextEncoding();
433     nCryptMask          = 0;
434     bIsEof              = sal_False;
435 #if defined UNX
436     eLineDelimiter      = LINEEND_LF;   // UNIX-Format
437 #else
438     eLineDelimiter      = LINEEND_CRLF; // DOS-Format
439 #endif
440 
441     SetNumberFormatInt( NUMBERFORMAT_INT_LITTLEENDIAN );
442 
443     nBufFilePos         = 0;
444     nBufActualPos       = 0;
445     bIsDirty            = sal_False;
446     bIsConsistent       = sal_True;
447     bIsWritable         = sal_True;
448 
449     pRWBuf              = 0;
450     pBufPos             = 0;
451     nBufSize            = 0;
452     nBufActualLen       = 0;
453     eIOMode             = STREAM_IO_DONTKNOW;
454     nBufFree            = 0;
455 
456     nRadix              = 10;
457     nPrecision          = 0;  // all significant digits
458     nWidth              = 0; // default width
459     cFiller             = ' ';
460     nJustification      = JUSTIFY_RIGHT;
461     eStreamMode         = 0;
462     CreateFormatString();
463 
464     nVersion           = 0;
465 
466     ClearError();
467 }
468 
469 /*************************************************************************
470 |*
471 |*    Stream::Stream()
472 |*
473 |*    Beschreibung      STREAM.SDW
474 |*    Ersterstellung    OV 08.06.94
475 |*    Letzte Aenderung  OV 08.06.94
476 |*
477 *************************************************************************/
478 
SvStream(SvLockBytes * pLockBytesP)479 SvStream::SvStream( SvLockBytes* pLockBytesP )
480 {
481     DBG_CTOR( Stream, NULL );
482 
483     ImpInit();
484     xLockBytes = pLockBytesP;
485     const SvStream* pStrm;
486     if( pLockBytesP ) {
487         pStrm = pLockBytesP->GetStream();
488         if( pStrm ) {
489             SetError( pStrm->GetErrorCode() );
490         }
491     }
492     SetBufferSize( 256 );
493 }
494 
SvStream()495 SvStream::SvStream()
496 {
497     DBG_CTOR( Stream, NULL );
498 
499     ImpInit();
500 }
501 
502 /*************************************************************************
503 |*
504 |*    Stream::~Stream()
505 |*
506 |*    Beschreibung      STREAM.SDW
507 |*    Ersterstellung    OV 08.06.94
508 |*    Letzte Aenderung  OV 08.06.94
509 |*
510 *************************************************************************/
511 
~SvStream()512 SvStream::~SvStream()
513 {
514     DBG_DTOR( Stream, NULL );
515 
516     if ( xLockBytes.Is() )
517         Flush();
518 
519     if( pRWBuf )
520         delete[] pRWBuf;
521 }
522 
523 /*************************************************************************
524 |*
525 |*    Stream::IsA()
526 |*
527 |*    Beschreibung      STREAM.SDW
528 |*    Ersterstellung    OV 08.06.94
529 |*    Letzte Aenderung  OV 08.06.94
530 |*
531 *************************************************************************/
532 
IsA() const533 sal_uInt16 SvStream::IsA() const
534 {
535     return (sal_uInt16)ID_STREAM;
536 }
537 
538 /*************************************************************************
539 |*
540 |*    Stream::ClearError()
541 |*
542 |*    Beschreibung      STREAM.SDW
543 |*    Ersterstellung    OV 08.06.94
544 |*    Letzte Aenderung  OV 08.06.94
545 |*
546 *************************************************************************/
547 
ClearError()548 void SvStream::ClearError()
549 {
550     bIsEof = sal_False;
551     nError = SVSTREAM_OK;
552 }
553 
554 /*************************************************************************
555 |*
556 |*    Stream::SetError()
557 |*
558 |*    Beschreibung      STREAM.SDW
559 |*    Ersterstellung    OV 08.06.94
560 |*    Letzte Aenderung  OV 08.06.94
561 |*
562 *************************************************************************/
563 
SetError(sal_uInt32 nErrorCode)564 void SvStream::SetError( sal_uInt32 nErrorCode )
565 {
566     if ( nError == SVSTREAM_OK )
567         nError = nErrorCode;
568 }
569 
570 
571 /*************************************************************************
572 |*
573 |*    Stream::SetNumberFormatInt()
574 |*
575 |*    Beschreibung      STREAM.SDW
576 |*    Ersterstellung    OV 08.06.94
577 |*    Letzte Aenderung  OV 08.06.94
578 |*
579 *************************************************************************/
580 
SetNumberFormatInt(sal_uInt16 nNewFormat)581 void SvStream::SetNumberFormatInt( sal_uInt16 nNewFormat )
582 {
583     nNumberFormatInt = nNewFormat;
584     bSwap = sal_False;
585 #ifdef OSL_BIGENDIAN
586     if( nNumberFormatInt == NUMBERFORMAT_INT_LITTLEENDIAN )
587         bSwap = sal_True;
588 #else
589     if( nNumberFormatInt == NUMBERFORMAT_INT_BIGENDIAN )
590         bSwap = sal_True;
591 #endif
592 }
593 
594 /*************************************************************************
595 |*
596 |*    Stream::SetBufferSize()
597 |*
598 |*    Beschreibung      STREAM.SDW
599 |*    Ersterstellung    OV 08.06.94
600 |*    Letzte Aenderung  OV 08.06.94
601 |*
602 *************************************************************************/
603 
SetBufferSize(sal_uInt16 nBufferSize)604 void SvStream::SetBufferSize( sal_uInt16 nBufferSize )
605 {
606     sal_Size nActualFilePos = Tell();
607     sal_Bool bDontSeek = (sal_Bool)(pRWBuf == 0);
608 
609     if( bIsDirty && bIsConsistent && bIsWritable )  // wg. Windows NT: Access denied
610         Flush();
611 
612     if( nBufSize )
613     {
614         delete[] pRWBuf;
615         nBufFilePos += nBufActualPos;
616     }
617 
618     pRWBuf          = 0;
619     nBufActualLen   = 0;
620     nBufActualPos   = 0;
621     nBufSize        = nBufferSize;
622     if( nBufSize )
623         pRWBuf = new sal_uInt8[ nBufSize ];
624     bIsConsistent   = sal_True;
625     pBufPos         = pRWBuf;
626     eIOMode = STREAM_IO_DONTKNOW;
627     if( !bDontSeek )
628         SeekPos( nActualFilePos );
629 }
630 
631 /*************************************************************************
632 |*
633 |*    Stream::ClearBuffer()
634 |*
635 |*    Beschreibung      STREAM.SDW
636 |*    Ersterstellung    OV 08.06.94
637 |*    Letzte Aenderung  OV 08.06.94
638 |*
639 *************************************************************************/
640 
ClearBuffer()641 void SvStream::ClearBuffer()
642 {
643     nBufActualLen   = 0;
644     nBufActualPos   = 0;
645     nBufFilePos     = 0;
646     pBufPos         = pRWBuf;
647     bIsDirty        = sal_False;
648     bIsConsistent   = sal_True;
649     eIOMode         = STREAM_IO_DONTKNOW;
650 
651     bIsEof          = sal_False;
652 }
653 
654 /*************************************************************************
655 |*
656 |*    Stream::ResetError()
657 |*
658 |*    Beschreibung      STREAM.SDW
659 |*    Ersterstellung    OV 08.06.94
660 |*    Letzte Aenderung  OV 08.06.94
661 |*
662 *************************************************************************/
663 
ResetError()664 void SvStream::ResetError()
665 {
666     ClearError();
667 }
668 
669 /*************************************************************************
670 |*
671 |*    Stream::ReadLine()
672 |*
673 |*    Beschreibung      STREAM.SDW
674 |*    Ersterstellung    OV 08.06.94
675 |*    Letzte Aenderung  OV 08.06.94
676 |*
677 *************************************************************************/
678 
ReadByteStringLine(::rtl::OUString & rStr,rtl_TextEncoding eSrcCharSet)679 sal_Bool SvStream::ReadByteStringLine( ::rtl::OUString& rStr, rtl_TextEncoding eSrcCharSet )
680 {
681     sal_Bool bRet;
682     ::rtl::OStringBuffer stringBuffer;
683 
684     bRet = ReadLine( stringBuffer );
685     rStr = ::rtl::OStringToOUString( stringBuffer.makeStringAndClear(), eSrcCharSet );
686     return bRet;
687 }
688 
ReadByteStringLine(String & rStr,rtl_TextEncoding eSrcCharSet)689 sal_Bool SvStream::ReadByteStringLine( String& rStr, rtl_TextEncoding eSrcCharSet )
690 {
691     sal_Bool bRet;
692     ByteString aStr;
693 
694     bRet = ReadLine(aStr);
695     rStr = UniString( aStr, eSrcCharSet );
696     return bRet;
697 }
698 
ReadLine(::rtl::OStringBuffer & rStr)699 sal_Bool SvStream::ReadLine( ::rtl::OStringBuffer& rStr )
700 {
701     sal_Char    buf[256+1];
702     sal_Bool    bEnd = sal_False;
703     sal_Size    nOldFilePos = Tell();
704     sal_Char    c = 0;
705     sal_Size    nTotalLen = 0;
706 
707     rStr.setLength( 0 );
708     while( !bEnd && !GetError() )   // !!! do not test for EOF,
709                                     // !!! because we read in blocks
710     {
711         sal_uInt16 nLen = (sal_uInt16)Read( buf, sizeof(buf)-1 );
712         if ( !nLen )
713         {
714             if ( rStr.getLength() == 0 )
715             {
716                 // the very first block read failed -> abort
717                 bIsEof = sal_True;
718                 return sal_False;
719             }
720             else
721                 break;
722         }
723 
724         sal_uInt16 j, n;
725         for( j = n = 0; j < nLen ; ++j )
726         {
727             c = buf[j];
728             if ( c == '\n' || c == '\r' )
729             {
730                 bEnd = sal_True;
731                 break;
732             }
733             // erAck 26.02.01: Old behavior was no special treatment of '\0'
734             // character here, but a following rStr+=c did ignore it. Is this
735             // really intended? Or should a '\0' better terminate a line?
736             // The nOldFilePos stuff wasn't correct then anyways.
737             if ( c )
738             {
739                 if ( n < j )
740                     buf[n] = c;
741                 ++n;
742             }
743         }
744         if ( n )
745             rStr.append( buf, n );
746         nTotalLen += j;
747     }
748 
749     if ( !bEnd && !GetError() && rStr.getLength() )
750         bEnd = sal_True;
751 
752     nOldFilePos += nTotalLen;
753     if( Tell() > nOldFilePos )
754         nOldFilePos++;
755     Seek( nOldFilePos );  // seek because of the above BlockRead!
756 
757     if ( bEnd && (c=='\r' || c=='\n') )  // Special treatment of DOS files
758     {
759         char cTemp;
760         sal_Size nLen = Read((char*)&cTemp , sizeof(cTemp) );
761         if ( nLen ) {
762             if( cTemp == c || (cTemp != '\n' && cTemp != '\r') )
763                 Seek( nOldFilePos );
764         }
765     }
766 
767     if ( bEnd )
768         bIsEof = sal_False;
769     return bEnd;
770 }
771 
ReadLine(ByteString & rStr)772 sal_Bool SvStream::ReadLine( ByteString& rStr )
773 {
774     sal_Char    buf[256+1];
775     sal_Bool        bEnd        = sal_False;
776     sal_Size       nOldFilePos = Tell();
777     sal_Char    c           = 0;
778     sal_Size       nTotalLen   = 0;
779 
780     rStr.Erase();
781     while( !bEnd && !GetError() )   // !!! nicht auf EOF testen,
782                                     // !!! weil wir blockweise
783                                     // !!! lesen
784     {
785         sal_uInt16 nLen = (sal_uInt16)Read( buf, sizeof(buf)-1 );
786         if ( !nLen )
787         {
788             if ( rStr.Len() == 0 )
789             {
790                 // der allererste Blockread hat fehlgeschlagen -> Abflug
791                 bIsEof = sal_True;
792                 return sal_False;
793             }
794             else
795                 break;
796         }
797 
798         sal_uInt16 j, n;
799         for( j = n = 0; j < nLen ; ++j )
800         {
801             c = buf[j];
802             if ( c == '\n' || c == '\r' )
803             {
804                 bEnd = sal_True;
805                 break;
806             }
807             // erAck 26.02.01: Old behavior was no special treatment of '\0'
808             // character here, but a following rStr+=c did ignore it. Is this
809             // really intended? Or should a '\0' better terminate a line?
810             // The nOldFilePos stuff wasn't correct then anyways.
811             if ( c )
812             {
813                 if ( n < j )
814                     buf[n] = c;
815                 ++n;
816             }
817         }
818         if ( n )
819             rStr.Append( buf, n );
820         nTotalLen += j;
821     }
822 
823     if ( !bEnd && !GetError() && rStr.Len() )
824         bEnd = sal_True;
825 
826     nOldFilePos += nTotalLen;
827     if( Tell() > nOldFilePos )
828         nOldFilePos++;
829     Seek( nOldFilePos );  // seeken wg. obigem BlockRead!
830 
831     if ( bEnd && (c=='\r' || c=='\n') )  // Sonderbehandlung DOS-Dateien
832     {
833         char cTemp;
834         sal_Size nLen = Read((char*)&cTemp , sizeof(cTemp) );
835         if ( nLen ) {
836             if( cTemp == c || (cTemp != '\n' && cTemp != '\r') )
837                 Seek( nOldFilePos );
838         }
839     }
840 
841     if ( bEnd )
842         bIsEof = sal_False;
843     return bEnd;
844 }
845 
ReadUniStringLine(String & rStr)846 sal_Bool SvStream::ReadUniStringLine( String& rStr )
847 {
848     sal_Unicode buf[256+1];
849     sal_Bool        bEnd        = sal_False;
850     sal_Size       nOldFilePos = Tell();
851     sal_Unicode c           = 0;
852     sal_Size       nTotalLen   = 0;
853 
854     DBG_ASSERT( sizeof(sal_Unicode) == sizeof(sal_uInt16), "ReadUniStringLine: swapping sizeof(sal_Unicode) not implemented" );
855 
856     rStr.Erase();
857     while( !bEnd && !GetError() )   // !!! nicht auf EOF testen,
858                                     // !!! weil wir blockweise
859                                     // !!! lesen
860     {
861         sal_uInt16 nLen = (sal_uInt16)Read( (char*)buf, sizeof(buf)-sizeof(sal_Unicode) );
862         nLen /= sizeof(sal_Unicode);
863         if ( !nLen )
864         {
865             if ( rStr.Len() == 0 )
866             {
867                 // der allererste Blockread hat fehlgeschlagen -> Abflug
868                 bIsEof = sal_True;
869                 return sal_False;
870             }
871             else
872                 break;
873         }
874 
875         sal_uInt16 j, n;
876         for( j = n = 0; j < nLen ; ++j )
877         {
878             if ( bSwap )
879                 SwapUShort( buf[n] );
880             c = buf[j];
881             if ( c == '\n' || c == '\r' )
882             {
883                 bEnd = sal_True;
884                 break;
885             }
886             // erAck 26.02.01: Old behavior was no special treatment of '\0'
887             // character here, but a following rStr+=c did ignore it. Is this
888             // really intended? Or should a '\0' better terminate a line?
889             // The nOldFilePos stuff wasn't correct then anyways.
890             if ( c )
891             {
892                 if ( n < j )
893                     buf[n] = c;
894                 ++n;
895             }
896         }
897         if ( n )
898             rStr.Append( buf, n );
899         nTotalLen += j;
900     }
901 
902     if ( !bEnd && !GetError() && rStr.Len() )
903         bEnd = sal_True;
904 
905     nOldFilePos += nTotalLen * sizeof(sal_Unicode);
906     if( Tell() > nOldFilePos )
907         nOldFilePos += sizeof(sal_Unicode);
908     Seek( nOldFilePos );  // seeken wg. obigem BlockRead!
909 
910     if ( bEnd && (c=='\r' || c=='\n') )  // Sonderbehandlung DOS-Dateien
911     {
912         sal_Unicode cTemp;
913         Read( (char*)&cTemp, sizeof(cTemp) );
914         if ( bSwap )
915             SwapUShort( cTemp );
916         if( cTemp == c || (cTemp != '\n' && cTemp != '\r') )
917             Seek( nOldFilePos );
918     }
919 
920     if ( bEnd )
921         bIsEof = sal_False;
922     return bEnd;
923 }
924 
ReadUniOrByteStringLine(String & rStr,rtl_TextEncoding eSrcCharSet)925 sal_Bool SvStream::ReadUniOrByteStringLine( String& rStr, rtl_TextEncoding eSrcCharSet )
926 {
927     if ( eSrcCharSet == RTL_TEXTENCODING_UNICODE )
928         return ReadUniStringLine( rStr );
929     else
930         return ReadByteStringLine( rStr, eSrcCharSet );
931 }
932 
933 /*************************************************************************
934 |*
935 |*    Stream::ReadCString
936 |*
937 *************************************************************************/
938 
ReadCString(ByteString & rStr)939 sal_Bool SvStream::ReadCString( ByteString& rStr )
940 {
941     if( rStr.Len() )
942         rStr.Erase();
943 
944     sal_Char buf[ 256 + 1 ];
945     sal_Bool bEnd = sal_False;
946     sal_Size nFilePos = Tell();
947 
948     while( !bEnd && !GetError() )
949     {
950         sal_uInt16 nLen = (sal_uInt16)Read( buf, sizeof(buf)-1 );
951 		sal_uInt16 nReallyRead = nLen;
952         if( !nLen )
953             break;
954 
955         const sal_Char* pPtr = buf;
956         while( *pPtr && nLen )
957             ++pPtr, --nLen;
958 
959         bEnd =	( nReallyRead < sizeof(buf)-1 )			// read less than attempted to read
960                 ||  (  ( nLen > 0 )                    // OR it is inside the block we read
961 					&&	( 0 == *pPtr )					//    AND found a string terminator
962                     );
963 
964         rStr.Append( buf, ::sal::static_int_cast< xub_StrLen >( pPtr - buf ) );
965     }
966 
967     nFilePos += rStr.Len();
968     if( Tell() > nFilePos )
969         nFilePos++;
970     Seek( nFilePos );  // seeken wg. obigem BlockRead!
971     return bEnd;
972 }
973 
ReadCString(String & rStr,rtl_TextEncoding eToEncode)974 sal_Bool SvStream::ReadCString( String& rStr, rtl_TextEncoding eToEncode )
975 {
976     ByteString sStr;
977     sal_Bool bRet = ReadCString( sStr );
978     rStr = String( sStr, eToEncode );
979     return bRet;
980 }
981 
982 
983 /*************************************************************************
984 |*
985 |*    Stream::WriteUnicodeText()
986 |*
987 *************************************************************************/
988 
WriteUnicodeText(const String & rStr)989 sal_Bool SvStream::WriteUnicodeText( const String& rStr )
990 {
991     DBG_ASSERT( sizeof(sal_Unicode) == sizeof(sal_uInt16), "WriteUnicodeText: swapping sizeof(sal_Unicode) not implemented" );
992     if ( bSwap )
993     {
994         xub_StrLen nLen = rStr.Len();
995         sal_Unicode aBuf[384];
996         sal_Unicode* const pTmp = ( nLen > 384 ? new sal_Unicode[nLen] : aBuf);
997         memcpy( pTmp, rStr.GetBuffer(), nLen * sizeof(sal_Unicode) );
998         sal_Unicode* p = pTmp;
999         const sal_Unicode* const pStop = pTmp + nLen;
1000         while ( p < pStop )
1001         {
1002             SwapUShort( *p );
1003             p++;
1004         }
1005         Write( (char*)pTmp, nLen * sizeof(sal_Unicode) );
1006         if ( pTmp != aBuf )
1007             delete [] pTmp;
1008     }
1009     else
1010         Write( (char*)rStr.GetBuffer(), rStr.Len() * sizeof(sal_Unicode) );
1011     return nError == SVSTREAM_OK;
1012 }
1013 
WriteUnicodeOrByteText(const String & rStr,rtl_TextEncoding eDestCharSet)1014 sal_Bool SvStream::WriteUnicodeOrByteText( const String& rStr, rtl_TextEncoding eDestCharSet )
1015 {
1016     if ( eDestCharSet == RTL_TEXTENCODING_UNICODE )
1017         return WriteUnicodeText( rStr );
1018     else
1019     {
1020         ByteString aStr( rStr, eDestCharSet );
1021         Write( aStr.GetBuffer(), aStr.Len() );
1022         return nError == SVSTREAM_OK;
1023     }
1024 }
1025 
1026 /*************************************************************************
1027 |*
1028 |*    Stream::WriteLine()
1029 |*
1030 |*    Beschreibung      STREAM.SDW
1031 |*    Ersterstellung    OV 08.06.94
1032 |*    Letzte Aenderung  OV 08.06.94
1033 |*
1034 *************************************************************************/
1035 
WriteByteStringLine(const String & rStr,rtl_TextEncoding eDestCharSet)1036 sal_Bool SvStream::WriteByteStringLine( const String& rStr, rtl_TextEncoding eDestCharSet )
1037 {
1038     return WriteLine( ByteString( rStr, eDestCharSet ) );
1039 }
1040 
WriteLine(const ByteString & rStr)1041 sal_Bool SvStream::WriteLine( const ByteString& rStr )
1042 {
1043     Write( rStr.GetBuffer(), rStr.Len() );
1044     endl(*this);
1045     return nError == SVSTREAM_OK;
1046 }
1047 
WriteUniStringLine(const String & rStr)1048 sal_Bool SvStream::WriteUniStringLine( const String& rStr )
1049 {
1050     WriteUnicodeText( rStr );
1051     endlu(*this);
1052     return nError == SVSTREAM_OK;
1053 }
1054 
WriteUniOrByteStringLine(const String & rStr,rtl_TextEncoding eDestCharSet)1055 sal_Bool SvStream::WriteUniOrByteStringLine( const String& rStr, rtl_TextEncoding eDestCharSet )
1056 {
1057     if ( eDestCharSet == RTL_TEXTENCODING_UNICODE )
1058         return WriteUniStringLine( rStr );
1059     else
1060         return WriteByteStringLine( rStr, eDestCharSet );
1061 }
1062 
1063 /*************************************************************************
1064 |*
1065 |*    Stream::WriteLines()
1066 |*
1067 |*    Beschreibung      STREAM.SDW
1068 |*    Ersterstellung    OV 17.07.95
1069 |*    Letzte Aenderung  OV 17.07.95
1070 |*
1071 *************************************************************************/
1072 
WriteByteStringLines(const String & rStr,rtl_TextEncoding eDestCharSet)1073 sal_Bool SvStream::WriteByteStringLines( const String& rStr, rtl_TextEncoding eDestCharSet )
1074 {
1075     return WriteLines( ByteString( rStr, eDestCharSet ) );
1076 }
1077 
WriteLines(const ByteString & rStr)1078 sal_Bool SvStream::WriteLines( const ByteString& rStr )
1079 {
1080     ByteString aStr( rStr );
1081     aStr.ConvertLineEnd( eLineDelimiter );
1082     Write( aStr.GetBuffer(), aStr.Len() );
1083     endl( *this );
1084     return (sal_Bool)(nError == SVSTREAM_OK);
1085 }
1086 
WriteUniStringLines(const String & rStr)1087 sal_Bool SvStream::WriteUniStringLines( const String& rStr )
1088 {
1089     String aStr( rStr );
1090     aStr.ConvertLineEnd( eLineDelimiter );
1091     WriteUniStringLine( aStr );
1092     return nError == SVSTREAM_OK;
1093 }
1094 
WriteUniOrByteStringLines(const String & rStr,rtl_TextEncoding eDestCharSet)1095 sal_Bool SvStream::WriteUniOrByteStringLines( const String& rStr, rtl_TextEncoding eDestCharSet )
1096 {
1097     if ( eDestCharSet == RTL_TEXTENCODING_UNICODE )
1098         return WriteUniStringLines( rStr );
1099     else
1100         return WriteByteStringLines( rStr, eDestCharSet );
1101 }
1102 
1103 /*************************************************************************
1104 |*
1105 |*    Stream::WriteUniOrByteChar()
1106 |*
1107 *************************************************************************/
1108 
WriteUniOrByteChar(sal_Unicode ch,rtl_TextEncoding eDestCharSet)1109 sal_Bool SvStream::WriteUniOrByteChar( sal_Unicode ch, rtl_TextEncoding eDestCharSet )
1110 {
1111     if ( eDestCharSet == RTL_TEXTENCODING_UNICODE )
1112         *this << ch;
1113     else
1114     {
1115         ByteString aStr( ch, eDestCharSet );
1116         Write( aStr.GetBuffer(), aStr.Len() );
1117     }
1118     return nError == SVSTREAM_OK;
1119 }
1120 
1121 /*************************************************************************
1122 |*
1123 |*    Stream::StartWritingUnicodeText()
1124 |*
1125 *************************************************************************/
1126 
StartWritingUnicodeText()1127 sal_Bool SvStream::StartWritingUnicodeText()
1128 {
1129     SetEndianSwap( sal_False );     // write native format
1130     // BOM, Byte Order Mark, U+FEFF, see
1131     // http://www.unicode.org/faq/utf_bom.html#BOM
1132     // Upon read: 0xfeff(-257) => no swap; 0xfffe(-2) => swap
1133     *this << sal_uInt16( 0xfeff );
1134     return nError == SVSTREAM_OK;
1135 }
1136 
1137 /*************************************************************************
1138 |*
1139 |*    Stream::StartReadingUnicodeText()
1140 |*
1141 *************************************************************************/
1142 
StartReadingUnicodeText(rtl_TextEncoding eReadBomCharSet)1143 sal_Bool SvStream::StartReadingUnicodeText( rtl_TextEncoding eReadBomCharSet )
1144 {
1145     if (!(  eReadBomCharSet == RTL_TEXTENCODING_DONTKNOW ||
1146             eReadBomCharSet == RTL_TEXTENCODING_UNICODE ||
1147             eReadBomCharSet == RTL_TEXTENCODING_UTF8))
1148         return sal_True;    // nothing to read
1149 
1150     bool bTryUtf8 = false;
1151     sal_uInt16 nFlag;
1152     sal_sSize nBack = sizeof(nFlag);
1153     *this >> nFlag;
1154     switch ( nFlag )
1155     {
1156         case 0xfeff :
1157             // native UTF-16
1158             if (    eReadBomCharSet == RTL_TEXTENCODING_DONTKNOW ||
1159                     eReadBomCharSet == RTL_TEXTENCODING_UNICODE)
1160                 nBack = 0;
1161         break;
1162         case 0xfffe :
1163             // swapped UTF-16
1164             if (    eReadBomCharSet == RTL_TEXTENCODING_DONTKNOW ||
1165                     eReadBomCharSet == RTL_TEXTENCODING_UNICODE)
1166             {
1167                 SetEndianSwap( !bSwap );
1168                 nBack = 0;
1169             }
1170         break;
1171         case 0xefbb :
1172             if (nNumberFormatInt == NUMBERFORMAT_INT_BIGENDIAN &&
1173                     (eReadBomCharSet == RTL_TEXTENCODING_DONTKNOW ||
1174                      eReadBomCharSet == RTL_TEXTENCODING_UTF8))
1175                 bTryUtf8 = true;
1176         break;
1177         case 0xbbef :
1178             if (nNumberFormatInt == NUMBERFORMAT_INT_LITTLEENDIAN &&
1179                     (eReadBomCharSet == RTL_TEXTENCODING_DONTKNOW ||
1180                      eReadBomCharSet == RTL_TEXTENCODING_UTF8))
1181                 bTryUtf8 = true;
1182         break;
1183         default:
1184             ;   // nothing
1185     }
1186     if (bTryUtf8)
1187     {
1188         sal_uChar nChar;
1189         nBack += sizeof(nChar);
1190         *this >> nChar;
1191         if (nChar == 0xbf)
1192             nBack = 0;      // it is UTF-8
1193     }
1194     if (nBack)
1195         SeekRel( -nBack );      // no BOM, pure data
1196     return nError == SVSTREAM_OK;
1197 }
1198 
1199 /*************************************************************************
1200 |*
1201 |*    Stream::ReadCsvLine()
1202 |*
1203 *************************************************************************/
1204 
1205 // Precondition: pStr is guaranteed to be non-NULL and points to a 0-terminated
1206 // array.
lcl_UnicodeStrChr(const sal_Unicode * pStr,sal_Unicode c)1207 inline const sal_Unicode* lcl_UnicodeStrChr( const sal_Unicode* pStr,
1208         sal_Unicode c )
1209 {
1210     while (*pStr)
1211     {
1212         if (*pStr == c)
1213             return pStr;
1214         ++pStr;
1215     }
1216     return 0;
1217 }
1218 
ReadCsvLine(String & rStr,sal_Bool bEmbeddedLineBreak,const String & rFieldSeparators,sal_Unicode cFieldQuote,sal_Bool bAllowBackslashEscape)1219 sal_Bool SvStream::ReadCsvLine( String& rStr, sal_Bool bEmbeddedLineBreak,
1220         const String& rFieldSeparators, sal_Unicode cFieldQuote,
1221         sal_Bool bAllowBackslashEscape)
1222 {
1223     ReadUniOrByteStringLine( rStr);
1224 
1225     if (bEmbeddedLineBreak)
1226     {
1227         const sal_Unicode* pSeps = rFieldSeparators.GetBuffer();
1228         xub_StrLen nLastOffset = 0;
1229         bool isQuoted = false;
1230         bool isFieldStarting = true;
1231         while (!IsEof() && rStr.Len() < STRING_MAXLEN)
1232         {
1233             bool wasQuote = false;
1234             bool bBackslashEscaped = false;
1235             const sal_Unicode *p;
1236             p = rStr.GetBuffer();
1237             p += nLastOffset;
1238             while (*p)
1239             {
1240                 if (isQuoted)
1241                 {
1242                     if (*p == cFieldQuote && !bBackslashEscaped)
1243                         wasQuote = !wasQuote;
1244                     else
1245                     {
1246                         if (bAllowBackslashEscape)
1247                         {
1248                             if (*p == '\\')
1249                                 bBackslashEscaped = !bBackslashEscaped;
1250                             else
1251                                 bBackslashEscaped = false;
1252                         }
1253                         if (wasQuote)
1254                         {
1255                             wasQuote = false;
1256                             isQuoted = false;
1257                             if (lcl_UnicodeStrChr( pSeps, *p ))
1258                                 isFieldStarting = true;
1259                         }
1260                     }
1261                 }
1262                 else
1263                 {
1264                     if (isFieldStarting)
1265                     {
1266                         isFieldStarting = false;
1267                         if (*p == cFieldQuote)
1268                             isQuoted = true;
1269                         else if (lcl_UnicodeStrChr( pSeps, *p ))
1270                             isFieldStarting = true;
1271                     }
1272                     else if (lcl_UnicodeStrChr( pSeps, *p ))
1273                         isFieldStarting = true;
1274                 }
1275                 ++p;
1276             }
1277 
1278             if (wasQuote)
1279                 isQuoted = false;
1280 
1281             if (isQuoted)
1282             {
1283                 nLastOffset = rStr.Len();
1284                 String aNext;
1285                 ReadUniOrByteStringLine( aNext);
1286                 rStr += sal_Unicode(_LF);
1287                 rStr += aNext;
1288             }
1289             else
1290                 break;
1291         }
1292     }
1293     return nError == SVSTREAM_OK;
1294 }
1295 
1296 /*************************************************************************
1297 |*
1298 |*    Stream::SeekRel()
1299 |*
1300 |*    Beschreibung      STREAM.SDW
1301 |*    Ersterstellung    OV 08.06.94
1302 |*    Letzte Aenderung  OV 08.06.94
1303 |*
1304 *************************************************************************/
1305 
SeekRel(sal_sSize nPos)1306 sal_Size SvStream::SeekRel( sal_sSize nPos )
1307 {
1308     sal_Size nActualPos = Tell();
1309 
1310 	if ( nPos >= 0 )
1311 	{
1312 		if ( SAL_MAX_SIZE - nActualPos > (sal_Size)nPos )
1313     		nActualPos += nPos;
1314 	}
1315 	else
1316 	{
1317 		sal_Size nAbsPos = (sal_Size)-nPos;
1318 		if ( nActualPos >= nAbsPos )
1319 			nActualPos -= nAbsPos;
1320 	}
1321 
1322     pBufPos = pRWBuf + nActualPos;
1323     return Seek( nActualPos );
1324 }
1325 
1326 /*************************************************************************
1327 |*
1328 |*    Stream::operator>>()
1329 |*
1330 |*    Beschreibung      STREAM.SDW
1331 |*    Ersterstellung    OV 08.06.94
1332 |*    Letzte Aenderung  OV 08.06.94
1333 |*
1334 *************************************************************************/
1335 
operator >>(sal_uInt16 & r)1336 SvStream& SvStream::operator >> ( sal_uInt16& r )
1337 {
1338     READNUMBER_WITHOUT_SWAP(sal_uInt16,r)
1339     if( bSwap )
1340         SwapUShort(r);
1341     return *this;
1342 }
1343 
operator >>(sal_uInt32 & r)1344 SvStream& SvStream::operator>> ( sal_uInt32& r )
1345 {
1346     READNUMBER_WITHOUT_SWAP(sal_uInt32,r)
1347     if( bSwap )
1348         SwapULong(r);
1349     return *this;
1350 }
1351 
operator >>(long & r)1352 SvStream& SvStream::operator >> ( long& r )
1353 {
1354 #if(SAL_TYPES_SIZEOFLONG != 4)
1355     int tmp = r;
1356     *this >> tmp;
1357     r = tmp;
1358 #else
1359     READNUMBER_WITHOUT_SWAP(long,r)
1360     if( bSwap )
1361         SwapLong(r);
1362 #endif
1363     return *this;
1364 }
1365 
operator >>(short & r)1366 SvStream& SvStream::operator >> ( short& r )
1367 {
1368     READNUMBER_WITHOUT_SWAP(short,r)
1369     if( bSwap )
1370         SwapShort(r);
1371     return *this;
1372 }
1373 
operator >>(int & r)1374 SvStream& SvStream::operator >> ( int& r )
1375 {
1376     READNUMBER_WITHOUT_SWAP(int,r)
1377     if( bSwap )
1378         SwapLongInt(r);
1379     return *this;
1380 }
1381 
operator >>(signed char & r)1382 SvStream& SvStream::operator>>( signed char& r )
1383 {
1384     if( (eIOMode == STREAM_IO_READ || !bIsConsistent) &&
1385         sizeof(signed char) <= nBufFree )
1386     {
1387         r = *pBufPos;
1388         nBufActualPos += sizeof(signed char);
1389         pBufPos += sizeof(signed char);
1390         nBufFree -= sizeof(signed char);
1391     }
1392     else
1393         Read( (char*)&r, sizeof(signed char) );
1394     return *this;
1395 }
1396 
1397 // Sonderbehandlung fuer Chars wegen PutBack
1398 
operator >>(char & r)1399 SvStream& SvStream::operator>>( char& r )
1400 {
1401     if( (eIOMode == STREAM_IO_READ || !bIsConsistent) &&
1402         sizeof(char) <= nBufFree )
1403     {
1404         r = *pBufPos;
1405         nBufActualPos += sizeof(char);
1406         pBufPos += sizeof(char);
1407         nBufFree -= sizeof(char);
1408     }
1409     else
1410         Read( (char*)&r, sizeof(char) );
1411     return *this;
1412 }
1413 
operator >>(unsigned char & r)1414 SvStream& SvStream::operator>>( unsigned char& r )
1415 {
1416     if( (eIOMode == STREAM_IO_READ || !bIsConsistent) &&
1417         sizeof(char) <= nBufFree )
1418     {
1419         r = *pBufPos;
1420         nBufActualPos += sizeof(char);
1421         pBufPos += sizeof(char);
1422         nBufFree -= sizeof(char);
1423     }
1424     else
1425         Read( (char*)&r, sizeof(char) );
1426     return *this;
1427 }
1428 
operator >>(float & r)1429 SvStream& SvStream::operator>>( float& r )
1430 {
1431     // Read( (char*)&r, sizeof(float) );
1432     READNUMBER_WITHOUT_SWAP(float,r)
1433 #if defined UNX
1434     if( bSwap )
1435       SwapFloat(r);
1436 #endif
1437     return *this;
1438 }
1439 
operator >>(double & r)1440 SvStream& SvStream::operator>>( double& r )
1441 {
1442     // Read( (char*)&r, sizeof(double) );
1443     READNUMBER_WITHOUT_SWAP(double,r)
1444 #if defined UNX
1445     if( bSwap )
1446       SwapDouble(r);
1447 #endif
1448     return *this;
1449 }
1450 
operator >>(SvStream & rStream)1451 SvStream& SvStream::operator>> ( SvStream& rStream )
1452 {
1453     const sal_uInt32 cBufLen = 0x8000;
1454     char* pBuf = new char[ cBufLen ];
1455 
1456     sal_uInt32 nCount;
1457     do {
1458         nCount = Read( pBuf, cBufLen );
1459         rStream.Write( pBuf, nCount );
1460     } while( nCount == cBufLen );
1461 
1462     delete[] pBuf;
1463     return *this;
1464 }
1465 
1466 /*************************************************************************
1467 |*
1468 |*    Stream::operator<<()
1469 |*
1470 |*    Beschreibung      STREAM.SDW
1471 |*    Ersterstellung    OV 08.06.94
1472 |*    Letzte Aenderung  OV 08.06.94
1473 |*
1474 *************************************************************************/
1475 
operator <<(sal_uInt16 v)1476 SvStream& SvStream::operator<< ( sal_uInt16 v )
1477 {
1478     if( bSwap )
1479         SwapUShort(v);
1480     WRITENUMBER_WITHOUT_SWAP(sal_uInt16,v)
1481     return *this;
1482 }
1483 
operator <<(sal_uInt32 v)1484 SvStream& SvStream::operator<<  ( sal_uInt32 v )
1485 {
1486     if( bSwap )
1487         SwapULong(v);
1488     WRITENUMBER_WITHOUT_SWAP(sal_uInt32,v)
1489     return *this;
1490 }
1491 
operator <<(long v)1492 SvStream& SvStream::operator<< ( long v )
1493 {
1494 #if(SAL_TYPES_SIZEOFLONG != 4)
1495     int tmp = v;
1496     *this << tmp;
1497 #else
1498     if( bSwap )
1499         SwapLong(v);
1500     WRITENUMBER_WITHOUT_SWAP(long,v)
1501 #endif
1502     return *this;
1503 }
1504 
operator <<(short v)1505 SvStream& SvStream::operator<<  ( short v )
1506 {
1507     if( bSwap )
1508         SwapShort(v);
1509     WRITENUMBER_WITHOUT_SWAP(short,v)
1510     return *this;
1511 }
1512 
operator <<(int v)1513 SvStream& SvStream::operator<<( int v )
1514 {
1515     if( bSwap )
1516         SwapLongInt( v );
1517     WRITENUMBER_WITHOUT_SWAP(int,v)
1518     return *this;
1519 }
1520 
operator <<(signed char v)1521 SvStream& SvStream::operator<<  ( signed char v )
1522 {
1523     //SDO
1524     int tmp = eIOMode;
1525     if(tmp == STREAM_IO_WRITE && sizeof(signed char) <= nBufFree )
1526     {
1527         *pBufPos = v;
1528         pBufPos++; // sizeof(char);
1529         nBufActualPos++;
1530         if( nBufActualPos > nBufActualLen )  // Append ?
1531             nBufActualLen = nBufActualPos;
1532         nBufFree--; // = sizeof(char);
1533         bIsDirty = sal_True;
1534     }
1535     else
1536         Write( (char*)&v, sizeof(signed char) );
1537     return *this;
1538 }
1539 
1540 // Sonderbehandlung fuer chars wegen PutBack
1541 
operator <<(char v)1542 SvStream& SvStream::operator<<  ( char v )
1543 {
1544     //SDO
1545     int tmp = eIOMode;
1546     if(tmp == STREAM_IO_WRITE && sizeof(char) <= nBufFree )
1547     {
1548         *pBufPos = v;
1549         pBufPos++; // sizeof(char);
1550         nBufActualPos++;
1551         if( nBufActualPos > nBufActualLen )  // Append ?
1552             nBufActualLen = nBufActualPos;
1553         nBufFree--; // = sizeof(char);
1554         bIsDirty = sal_True;
1555     }
1556     else
1557         Write( (char*)&v, sizeof(char) );
1558     return *this;
1559 }
1560 
operator <<(unsigned char v)1561 SvStream& SvStream::operator<<  ( unsigned char v )
1562 {
1563 //SDO
1564     int tmp = eIOMode;
1565     if(tmp == STREAM_IO_WRITE && sizeof(char) <= nBufFree )
1566     {
1567         *(unsigned char*)pBufPos = v;
1568         pBufPos++; // = sizeof(char);
1569         nBufActualPos++; // = sizeof(char);
1570         if( nBufActualPos > nBufActualLen )  // Append ?
1571             nBufActualLen = nBufActualPos;
1572         nBufFree--;
1573         bIsDirty = sal_True;
1574     }
1575     else
1576         Write( (char*)&v, sizeof(char) );
1577     return *this;
1578 }
1579 
operator <<(float v)1580 SvStream& SvStream::operator<< ( float v )
1581 {
1582 #ifdef UNX
1583     if( bSwap )
1584       SwapFloat(v);
1585 #endif
1586     WRITENUMBER_WITHOUT_SWAP(float,v)
1587     return *this;
1588 }
1589 
operator <<(const double & r)1590 SvStream& SvStream::operator<< ( const double& r )
1591 {
1592 //    Write( (char*)&r, sizeof( double ) );
1593 #if defined UNX
1594     if( bSwap )
1595     {
1596       double nHelp = r;
1597       SwapDouble(nHelp);
1598       WRITENUMBER_WITHOUT_SWAP(double,nHelp)
1599       return *this;
1600     }
1601     else
1602 #endif
1603     WRITENUMBER_WITHOUT_SWAP(double,r)
1604 
1605     return *this;
1606 }
1607 
operator <<(const char * pBuf)1608 SvStream& SvStream::operator<<  ( const char* pBuf )
1609 {
1610     Write( pBuf, strlen( pBuf ) );
1611     return *this;
1612 }
1613 
operator <<(const unsigned char * pBuf)1614 SvStream& SvStream::operator<<  ( const unsigned char* pBuf )
1615 {
1616     Write( (char*)pBuf, strlen( (char*)pBuf ) );
1617     return *this;
1618 }
1619 
operator <<(SvStream & rStream)1620 SvStream& SvStream::operator<< ( SvStream& rStream )
1621 {
1622     const sal_uInt32 cBufLen = 0x8000;
1623     char* pBuf = new char[ cBufLen ];
1624     sal_uInt32 nCount;
1625     do {
1626         nCount = rStream.Read( pBuf, cBufLen );
1627         Write( pBuf, nCount );
1628     } while( nCount == cBufLen );
1629 
1630     delete[] pBuf;
1631     return *this;
1632 }
1633 
1634 // -----------------------------------------------------------------------
1635 
ReadByteString(UniString & rStr,rtl_TextEncoding eSrcCharSet)1636 SvStream& SvStream::ReadByteString( UniString& rStr, rtl_TextEncoding eSrcCharSet )
1637 {
1638     // read UTF-16 string directly from stream ?
1639     if (eSrcCharSet == RTL_TEXTENCODING_UNICODE)
1640     {
1641         sal_uInt32 nLen;
1642         operator>> (nLen);
1643         if (nLen)
1644         {
1645             if (nLen > STRING_MAXLEN) {
1646                 SetError(SVSTREAM_GENERALERROR);
1647                 return *this;
1648             }
1649             sal_Unicode *pStr = rStr.AllocBuffer(
1650                 static_cast< xub_StrLen >(nLen));
1651             BOOST_STATIC_ASSERT(STRING_MAXLEN <= SAL_MAX_SIZE / 2);
1652             Read( pStr, nLen << 1 );
1653 
1654             if (bSwap)
1655                 for (sal_Unicode *pEnd = pStr + nLen; pStr < pEnd; pStr++)
1656                     SwapUShort(*pStr);
1657         }
1658         else
1659             rStr.Erase();
1660 
1661         return *this;
1662     }
1663 
1664     ByteString aStr;
1665     ReadByteString( aStr );
1666     rStr = UniString( aStr, eSrcCharSet );
1667     return *this;
1668 }
1669 
1670 // -----------------------------------------------------------------------
1671 
ReadByteString(ByteString & rStr)1672 SvStream& SvStream::ReadByteString( ByteString& rStr )
1673 {
1674     sal_uInt16 nLen = 0;
1675     operator>>( nLen );
1676     if( nLen )
1677     {
1678         char* pTmp = rStr.AllocBuffer( nLen );
1679         nLen = (sal_uInt16)Read( pTmp, nLen );
1680     }
1681     else
1682         rStr.Erase();
1683     return *this;
1684 }
1685 
1686 // -----------------------------------------------------------------------
1687 
WriteByteString(const UniString & rStr,rtl_TextEncoding eDestCharSet)1688 SvStream& SvStream::WriteByteString( const UniString& rStr, rtl_TextEncoding eDestCharSet )
1689 {
1690     // write UTF-16 string directly into stream ?
1691     if (eDestCharSet == RTL_TEXTENCODING_UNICODE)
1692     {
1693         sal_uInt32 nLen = rStr.Len();
1694         operator<< (nLen);
1695         if (nLen)
1696         {
1697             if (bSwap)
1698             {
1699                 const sal_Unicode *pStr = rStr.GetBuffer();
1700                 const sal_Unicode *pEnd = pStr + nLen;
1701 
1702                 for (; pStr < pEnd; pStr++)
1703                 {
1704                     sal_Unicode c = *pStr;
1705                     SwapUShort(c);
1706                     WRITENUMBER_WITHOUT_SWAP(sal_uInt16,c)
1707                 }
1708             }
1709             else
1710                 Write( rStr.GetBuffer(), nLen << 1 );
1711         }
1712 
1713         return *this;
1714     }
1715 
1716     return WriteByteString(ByteString( rStr, eDestCharSet ));
1717 }
1718 
1719 // -----------------------------------------------------------------------
1720 
WriteByteString(const ByteString & rStr)1721 SvStream& SvStream::WriteByteString( const ByteString& rStr)
1722 {
1723     sal_uInt16 nLen = rStr.Len();
1724     operator<< ( nLen );
1725     if( nLen != 0 )
1726         Write( rStr.GetBuffer(), nLen );
1727     return *this;
1728 }
1729 
1730 /*************************************************************************
1731 |*
1732 |*    Stream::Read()
1733 |*
1734 |*    Beschreibung      STREAM.SDW
1735 |*    Ersterstellung    OV 08.06.94
1736 |*    Letzte Aenderung  OV 08.06.94
1737 |*
1738 *************************************************************************/
1739 
Read(void * pData,sal_Size nCount)1740 sal_Size SvStream::Read( void* pData, sal_Size nCount )
1741 {
1742     sal_Size nSaveCount = nCount;
1743     if( !bIsConsistent )
1744         RefreshBuffer();
1745 
1746     if( !pRWBuf )
1747     {
1748         nCount = GetData( (char*)pData,nCount);
1749         if( nCryptMask )
1750             EncryptBuffer(pData, nCount);
1751         nBufFilePos += nCount;
1752     }
1753     else
1754     {
1755         // ist Block komplett im Puffer
1756         eIOMode = STREAM_IO_READ;
1757         if( nCount <= (sal_Size)(nBufActualLen - nBufActualPos ) )
1758         {
1759             // Ja!
1760             memcpy(pData, pBufPos, (size_t) nCount);
1761             nBufActualPos = nBufActualPos + (sal_uInt16)nCount;
1762             pBufPos += nCount;
1763             nBufFree = nBufFree - (sal_uInt16)nCount;
1764         }
1765         else
1766         {
1767             if( bIsDirty ) // Flushen ?
1768             {
1769                 SeekPos( nBufFilePos );
1770                 if( nCryptMask )
1771                     CryptAndWriteBuffer(pRWBuf, nBufActualLen);
1772                 else
1773                     PutData( pRWBuf, nBufActualLen );
1774                 bIsDirty = sal_False;
1775             }
1776 
1777             // passt der Datenblock in den Puffer ?
1778             if( nCount > nBufSize )
1779             {
1780                 // Nein! Deshalb ohne Umweg ueber den Puffer direkt
1781                 // in den Zielbereich einlesen
1782 
1783                 eIOMode = STREAM_IO_DONTKNOW;
1784 
1785                 SeekPos( nBufFilePos + nBufActualPos );
1786                 nBufActualLen = 0;
1787                 pBufPos       = pRWBuf;
1788                 nCount = GetData( (char*)pData, nCount );
1789                 if( nCryptMask )
1790                     EncryptBuffer(pData, nCount);
1791                 nBufFilePos += nCount;
1792                 nBufFilePos += nBufActualPos;
1793                 nBufActualPos = 0;
1794             }
1795             else
1796             {
1797                 // Der Datenblock passt komplett in den Puffer. Deshalb
1798                 // Puffer fuellen und dann die angeforderten Daten in den
1799                 // Zielbereich kopieren.
1800 
1801                 nBufFilePos += nBufActualPos;
1802                 SeekPos( nBufFilePos );
1803 
1804                 // TODO: Typecast vor GetData, sal_uInt16 nCountTmp
1805                 sal_Size nCountTmp = GetData( pRWBuf, nBufSize );
1806                 if( nCryptMask )
1807                     EncryptBuffer(pRWBuf, nCountTmp);
1808                 nBufActualLen = (sal_uInt16)nCountTmp;
1809                 if( nCount > nCountTmp )
1810                 {
1811                     nCount = nCountTmp;  // zurueckstutzen, Eof siehe unten
1812                 }
1813                 memcpy( pData, pRWBuf, (size_t)nCount );
1814                 nBufActualPos = (sal_uInt16)nCount;
1815                 pBufPos = pRWBuf + nCount;
1816             }
1817         }
1818     }
1819     bIsEof = sal_False;
1820     nBufFree = nBufActualLen - nBufActualPos;
1821     if( nCount != nSaveCount && nError != ERRCODE_IO_PENDING )
1822         bIsEof = sal_True;
1823     if( nCount == nSaveCount && nError == ERRCODE_IO_PENDING )
1824         nError = ERRCODE_NONE;
1825     return nCount;
1826 }
1827 
1828 /*************************************************************************
1829 |*
1830 |*    Stream::Write()
1831 |*
1832 |*    Beschreibung      STREAM.SDW
1833 |*    Ersterstellung    OV 08.06.94
1834 |*    Letzte Aenderung  OV 08.06.94
1835 |*
1836 *************************************************************************/
1837 
Write(const void * pData,sal_Size nCount)1838 sal_Size SvStream::Write( const void* pData, sal_Size nCount )
1839 {
1840     if( !nCount )
1841         return 0;
1842     if( !bIsWritable )
1843     {
1844         SetError( ERRCODE_IO_CANTWRITE );
1845         return 0;
1846     }
1847     if( !bIsConsistent )
1848         RefreshBuffer();   // Aenderungen des Puffers durch PutBack loeschen
1849 
1850     if( !pRWBuf )
1851     {
1852         if( nCryptMask )
1853             nCount = CryptAndWriteBuffer( pData, nCount );
1854         else
1855             nCount = PutData( (char*)pData, nCount );
1856         nBufFilePos += nCount;
1857         return nCount;
1858     }
1859 
1860     eIOMode = STREAM_IO_WRITE;
1861     if( nCount <= (sal_Size)(nBufSize - nBufActualPos) )
1862     {
1863         memcpy( pBufPos, pData, (size_t)nCount );
1864         nBufActualPos = nBufActualPos + (sal_uInt16)nCount;
1865         // wurde der Puffer erweitert ?
1866         if( nBufActualPos > nBufActualLen )
1867             nBufActualLen = nBufActualPos;
1868 
1869         pBufPos += nCount;
1870         bIsDirty = sal_True;
1871     }
1872     else
1873     {
1874         // Flushen ?
1875         if( bIsDirty )
1876         {
1877             SeekPos( nBufFilePos );
1878             if( nCryptMask )
1879                 CryptAndWriteBuffer( pRWBuf, (sal_Size)nBufActualLen );
1880             else
1881                 PutData( pRWBuf, nBufActualLen );
1882             bIsDirty = sal_False;
1883         }
1884 
1885         // passt der Block in den Puffer ?
1886         if( nCount > nBufSize )
1887         {
1888             eIOMode = STREAM_IO_DONTKNOW;
1889             nBufFilePos += nBufActualPos;
1890             nBufActualLen = 0;
1891             nBufActualPos = 0;
1892             pBufPos       = pRWBuf;
1893             SeekPos( nBufFilePos );
1894             if( nCryptMask )
1895                 nCount = CryptAndWriteBuffer( pData, nCount );
1896             else
1897                 nCount = PutData( (char*)pData, nCount );
1898             nBufFilePos += nCount;
1899         }
1900         else
1901         {
1902             // Block in Puffer stellen
1903             memcpy( pRWBuf, pData, (size_t)nCount );
1904 
1905             // Reihenfolge!
1906             nBufFilePos += nBufActualPos;
1907             nBufActualPos = (sal_uInt16)nCount;
1908             pBufPos = pRWBuf + nCount;
1909             nBufActualLen = (sal_uInt16)nCount;
1910             bIsDirty = sal_True;
1911         }
1912     }
1913     nBufFree = nBufSize - nBufActualPos;
1914     return nCount;
1915 }
1916 
1917 
1918 /*************************************************************************
1919 |*
1920 |*    Stream::Seek()
1921 |*
1922 |*    Beschreibung      STREAM.SDW
1923 |*    Ersterstellung    OV 08.06.94
1924 |*    Letzte Aenderung  OV 08.06.94
1925 |*
1926 *************************************************************************/
1927 
Seek(sal_Size nFilePos)1928 sal_Size SvStream::Seek( sal_Size nFilePos )
1929 {
1930     eIOMode = STREAM_IO_DONTKNOW;
1931 
1932     bIsEof = sal_False;
1933     if( !pRWBuf )
1934     {
1935         nBufFilePos = SeekPos( nFilePos );
1936         DBG_ASSERT(Tell()==nBufFilePos,"Out Of Sync!");
1937         return nBufFilePos;
1938     }
1939 
1940     // Ist Position im Puffer ?
1941     if( nFilePos >= nBufFilePos && nFilePos <= (nBufFilePos + nBufActualLen))
1942     {
1943         nBufActualPos = (sal_uInt16)(nFilePos - nBufFilePos);
1944         pBufPos = pRWBuf + nBufActualPos;
1945         // nBufFree korrigieren, damit wir nicht von einem
1946         // PutBack (ignoriert den StreamMode) getoetet werden
1947         nBufFree = nBufActualLen - nBufActualPos;
1948     }
1949     else
1950     {
1951         if( bIsDirty && bIsConsistent)
1952         {
1953             SeekPos( nBufFilePos );
1954             if( nCryptMask )
1955                 CryptAndWriteBuffer( pRWBuf, nBufActualLen );
1956             else
1957                 PutData( pRWBuf, nBufActualLen );
1958             bIsDirty = sal_False;
1959         }
1960         nBufActualLen = 0;
1961         nBufActualPos = 0;
1962         pBufPos       = pRWBuf;
1963         nBufFilePos = SeekPos( nFilePos );
1964     }
1965 #ifdef OV_DEBUG
1966     {
1967         sal_Size nDebugTemp = nBufFilePos + nBufActualPos;
1968         DBG_ASSERT(Tell()==nDebugTemp,"Sync?");
1969     }
1970 #endif
1971     return nBufFilePos + nBufActualPos;
1972 }
1973 
1974 /*************************************************************************
1975 |*
1976 |*    Stream::Flush()
1977 |*
1978 |*    Beschreibung      STREAM.SDW
1979 |*    Ersterstellung    OV 08.06.94
1980 |*    Letzte Aenderung  OV 08.06.94
1981 |*
1982 *************************************************************************/
1983 
Flush()1984 void SvStream::Flush()
1985 {
1986     if( bIsDirty && bIsConsistent )
1987     {
1988         SeekPos( nBufFilePos );
1989         if( nCryptMask )
1990             CryptAndWriteBuffer( pRWBuf, (sal_Size)nBufActualLen );
1991         else
1992             if( PutData( pRWBuf, nBufActualLen ) != nBufActualLen )
1993                 SetError( SVSTREAM_WRITE_ERROR );
1994         bIsDirty = sal_False;
1995     }
1996     if( bIsWritable )
1997         FlushData();
1998 }
1999 
2000 
2001 /*************************************************************************
2002 |*
2003 |*    Stream::PutBack()
2004 |*
2005 |*    Beschreibung      STREAM.SDW
2006 |*    Ersterstellung    OV 01.08.94
2007 |*    Letzte Aenderung  OV 01.08.94
2008 |*
2009 *************************************************************************/
2010 
2011 /*
2012     4 Faelle :
2013 
2014     1. Datenzeiger steht mitten im Puffer (nBufActualPos >= 1)
2015     2. Datenzeiger auf Position 0, Puffer ist voll
2016     3. Datenzeiger auf Position 0, Puffer ist teilweise gefuellt
2017     4. Datenzeiger auf Position 0, Puffer ist leer -> Fehler!
2018 */
2019 
PutBack(char aCh)2020 SvStream& SvStream::PutBack( char aCh )
2021 {
2022     // wenn kein Buffer oder Zurueckscrollen nicht moeglich -> Fehler
2023     if( !pRWBuf || !nBufActualLen || ( !nBufActualPos && !nBufFilePos ) )
2024     {
2025         // 4. Fall
2026         SetError( SVSTREAM_GENERALERROR );
2027         return *this;
2028     }
2029 
2030     // Flush() (Phys. Flushen aber nicht notwendig, deshalb selbst schreiben)
2031     if( bIsConsistent && bIsDirty  )
2032     {
2033         SeekPos( nBufFilePos );
2034         if( nCryptMask )
2035             CryptAndWriteBuffer( pRWBuf, nBufActualLen );
2036         else
2037             PutData( pRWBuf, nBufActualLen );
2038         bIsDirty = sal_False;
2039     }
2040     bIsConsistent = sal_False;  // Puffer enthaelt jetzt TRASH
2041     if( nBufActualPos )
2042     {
2043         // 1. Fall
2044         nBufActualPos--;
2045         pBufPos--;
2046         *pBufPos = aCh;
2047         nBufFree++;
2048     }
2049     else  // Puffer muss verschoben werden
2050     {
2051         // Ist Puffer am Anschlag ?
2052         if( nBufSize == nBufActualLen )
2053         {
2054             // 2. Fall
2055             memmove( pRWBuf+1, pRWBuf, nBufSize-1 );
2056             // nBufFree behaelt den Wert!
2057         }
2058         else
2059         {
2060             // 3. Fall -> Puffer vergroessern
2061             memmove( pRWBuf+1, pRWBuf, (sal_uInt16)nBufActualLen );
2062             nBufActualLen++;
2063             nBufFree++;
2064         }
2065         nBufFilePos--;
2066         *pRWBuf = aCh;
2067     }
2068     eIOMode = STREAM_IO_DONTKNOW;
2069     bIsEof = sal_False;
2070     return *this;
2071 }
2072 
2073 /*************************************************************************
2074 |*
2075 |*    Stream::EatWhite()
2076 |*
2077 |*    Beschreibung      STREAM.SDW
2078 |*    Ersterstellung    OV 01.08.94
2079 |*    Letzte Aenderung  OV 01.08.94
2080 |*
2081 *************************************************************************/
2082 
EatWhite()2083 void SvStream::EatWhite()
2084 {
2085     char aCh;
2086     Read(&aCh, sizeof(char) );
2087     while( !bIsEof && isspace((int)aCh) )  //( aCh == ' ' || aCh == '\t' ) )
2088         Read(&aCh, sizeof(char) );
2089     if( !bIsEof ) // konnte das letzte Char gelesen werden ?
2090         SeekRel( -1L );
2091 }
2092 
2093 /*************************************************************************
2094 |*
2095 |*    Stream::RefreshBuffer()
2096 |*
2097 |*    Beschreibung      STREAM.SDW
2098 |*    Ersterstellung    OV 01.08.94
2099 |*    Letzte Aenderung  OV 01.08.94
2100 |*
2101 *************************************************************************/
2102 
RefreshBuffer()2103 void SvStream::RefreshBuffer()
2104 {
2105     if( bIsDirty && bIsConsistent )
2106     {
2107         SeekPos( nBufFilePos );
2108         if( nCryptMask )
2109             CryptAndWriteBuffer( pRWBuf, (sal_Size)nBufActualLen );
2110         else
2111             PutData( pRWBuf, nBufActualLen );
2112         bIsDirty = sal_False;
2113     }
2114     SeekPos( nBufFilePos );
2115     nBufActualLen = (sal_uInt16)GetData( pRWBuf, nBufSize );
2116     if( nBufActualLen && nError == ERRCODE_IO_PENDING )
2117         nError = ERRCODE_NONE;
2118     if( nCryptMask )
2119         EncryptBuffer(pRWBuf, (sal_Size)nBufActualLen);
2120     bIsConsistent = sal_True;
2121     eIOMode = STREAM_IO_DONTKNOW;
2122 }
2123 
2124 
2125 /*************************************************************************
2126 |*
2127 |*    Stream::CreateFormatString()
2128 |*
2129 |*    Beschreibung      Baut Formatstring zusammen
2130 |*    Ersterstellung    OV 08.06.94
2131 |*    Letzte Aenderung  OV 08.06.94
2132 |*
2133 *************************************************************************/
2134 
CreateFormatString()2135 void SvStream::CreateFormatString()
2136 {
2137     aFormatString = '%';
2138     nPrintfParams = SPECIAL_PARAM_NONE;
2139 
2140     if( nJustification )
2141     {
2142         aFormatString += '-';
2143     }
2144 
2145     if( nWidth )
2146     {
2147         if( cFiller != ' ' )
2148             aFormatString += '0';
2149         aFormatString += '*';
2150         nPrintfParams = SPECIAL_PARAM_WIDTH;
2151     }
2152 
2153     if( nPrecision )
2154     {
2155         aFormatString += ".*";
2156         if( nWidth )
2157             nPrintfParams = SPECIAL_PARAM_BOTH;
2158         else
2159             nPrintfParams = SPECIAL_PARAM_PRECISION;
2160     }
2161 }
2162 
2163 /*************************************************************************
2164 |*
2165 |*    Stream::ReadNumber()
2166 |*
2167 |*    Beschreibung      STREAM.SDW
2168 |*    Ersterstellung    OV 08.06.94
2169 |*    Letzte Aenderung  OV 08.06.94
2170 |*
2171 *************************************************************************/
2172 
2173 #define BUFSIZE_LONG 21  // log( 2 hoch 64 ) + 1
2174 
ReadNumber(long & rLong)2175 SvStream& SvStream::ReadNumber( long& rLong )
2176 {
2177     EatWhite();
2178     if( bIsEof || nError )
2179     {
2180         SetError( SVSTREAM_GENERALERROR );
2181         return *this;
2182     }
2183     sal_Size nFPtr = Tell();
2184     char buf[ BUFSIZE_LONG ];
2185     memset( buf, 0, BUFSIZE_LONG );
2186     sal_Size nTemp = Read( buf, BUFSIZE_LONG-1 );
2187     if( !nTemp || nError )
2188     {
2189         SetError( SVSTREAM_GENERALERROR );
2190         return *this;
2191     }
2192     char *pEndPtr;
2193     rLong = strtol( buf, &pEndPtr, (int)nRadix );
2194     nFPtr += ( (sal_Size)pEndPtr - (sal_Size)(&(buf[0])) );
2195     Seek( nFPtr );
2196     bIsEof = sal_False;
2197     return *this;
2198 }
2199 
ReadNumber(sal_uInt32 & rUInt32)2200 SvStream& SvStream::ReadNumber( sal_uInt32& rUInt32 )
2201 {
2202     EatWhite();
2203     if( bIsEof || nError )
2204     {
2205         SetError( SVSTREAM_GENERALERROR );
2206         return *this;
2207     }
2208     sal_Size nFPtr = Tell();
2209     char buf[ BUFSIZE_LONG ];
2210     memset( buf, 0, BUFSIZE_LONG );
2211     sal_Size nTemp = Read( buf, BUFSIZE_LONG-1 );
2212     if( !nTemp || nError )
2213     {
2214         SetError( SVSTREAM_GENERALERROR );
2215         return *this;
2216     }
2217     char *pEndPtr;
2218     rUInt32 = strtoul( buf, &pEndPtr, (int)nRadix );
2219     nFPtr += ( (sal_uIntPtr)pEndPtr - (sal_uIntPtr)buf );
2220     Seek( nFPtr );
2221     bIsEof = sal_False;
2222     return *this;
2223 }
2224 
ReadNumber(double & rDouble)2225 SvStream& SvStream::ReadNumber( double& rDouble )
2226 {
2227     EatWhite();
2228     if( bIsEof || nError )
2229     {
2230         SetError( SVSTREAM_GENERALERROR );
2231         return *this;
2232     }
2233     sal_Size nFPtr = Tell();
2234     char buf[ BUFSIZE_LONG ];
2235     memset( buf, 0, BUFSIZE_LONG );
2236     sal_Size nTemp = Read( buf, BUFSIZE_LONG-1 );
2237     if( !nTemp || nError )
2238     {
2239         SetError( SVSTREAM_GENERALERROR );
2240         return *this;
2241     }
2242     char *pEndPtr;
2243     rDouble = strtod( buf, &pEndPtr );
2244     nFPtr += ( (sal_Size)pEndPtr - (sal_Size)buf );
2245     Seek( nFPtr );
2246     bIsEof = sal_False;
2247     return *this;
2248 }
2249 
2250 
2251 /*************************************************************************
2252 |*
2253 |*    Stream::WriteNumber()
2254 |*
2255 |*    Beschreibung      STREAM.SDW
2256 |*    Ersterstellung    OV 08.06.94
2257 |*    Letzte Aenderung  OV 08.06.94
2258 |*
2259 *************************************************************************/
2260 
WriteNumber(long nLong)2261 SvStream& SvStream::WriteNumber( long nLong )
2262 {
2263     char buffer[256+12];
2264     char pType[] = "ld"; // Nicht static!
2265     if( nRadix == 16 )
2266         pType[1] = 'x';
2267     else if( nRadix == 8 )
2268         pType[1] = 'o';
2269     ByteString aFStr( aFormatString);
2270     aFStr += pType;
2271     int nLen;
2272     switch ( nPrintfParams )
2273     {
2274         case SPECIAL_PARAM_NONE :
2275             nLen = sprintf( buffer, aFStr.GetBuffer(), nLong );
2276             break;
2277         case SPECIAL_PARAM_WIDTH :
2278             nLen = sprintf( buffer, aFStr.GetBuffer(), nWidth, nLong );
2279             break;
2280         case SPECIAL_PARAM_PRECISION :
2281             nLen = sprintf( buffer, aFStr.GetBuffer(), nPrecision,nLong);
2282             break;
2283         default:
2284             nLen=sprintf(buffer, aFStr.GetBuffer(),nWidth,nPrecision,nLong);
2285     }
2286     Write( buffer, (long)nLen );
2287     return *this;
2288 }
2289 
WriteNumber(sal_uInt32 nUInt32)2290 SvStream& SvStream::WriteNumber( sal_uInt32 nUInt32 )
2291 {
2292     char buffer[256+12];
2293     char pType[] = "lu"; // Nicht static!
2294     if( nRadix == 16 )
2295         pType[1] = 'x';
2296     else if( nRadix == 8 )
2297         pType[1] = 'o';
2298     ByteString aFStr( aFormatString);
2299     aFStr += pType;
2300     int nLen;
2301     switch ( nPrintfParams )
2302     {
2303         case SPECIAL_PARAM_NONE :
2304             nLen = sprintf( buffer, aFStr.GetBuffer(), nUInt32 );
2305             break;
2306         case SPECIAL_PARAM_WIDTH :
2307             nLen = sprintf( buffer, aFStr.GetBuffer(), nWidth, nUInt32 );
2308             break;
2309         case SPECIAL_PARAM_PRECISION :
2310             nLen = sprintf( buffer, aFStr.GetBuffer(), nPrecision, nUInt32 );
2311             break;
2312         default:
2313             nLen=sprintf(buffer,aFStr.GetBuffer(),nWidth,nPrecision,nUInt32 );
2314     }
2315     Write( buffer, (long)nLen );
2316     return *this;
2317 }
2318 
2319 
WriteNumber(const double & rDouble)2320 SvStream& SvStream::WriteNumber( const double& rDouble )
2321 {
2322     char buffer[256+24];
2323     ByteString aFStr( aFormatString);
2324     aFStr += "lf";
2325     int nLen;
2326     switch ( nPrintfParams )
2327     {
2328         case SPECIAL_PARAM_NONE :
2329             nLen = sprintf( buffer, aFStr.GetBuffer(), rDouble );
2330             break;
2331         case SPECIAL_PARAM_WIDTH :
2332             nLen = sprintf( buffer, aFStr.GetBuffer(), nWidth, rDouble );
2333             break;
2334         case SPECIAL_PARAM_PRECISION :
2335             nLen = sprintf( buffer, aFStr.GetBuffer(), nPrecision, rDouble);
2336             break;
2337         default:
2338             nLen=sprintf(buffer, aFStr.GetBuffer(),nWidth,nPrecision,rDouble);
2339     }
2340     Write( buffer, (long)nLen );
2341     return *this;
2342 }
2343 
2344 /*************************************************************************
2345 |*
2346 |*    Stream::CryptAndWriteBuffer()
2347 |*
2348 |*    Beschreibung      Verschluesseln und Schreiben
2349 |*    Ersterstellung    OV 08.06.94
2350 |*    Letzte Aenderung  OV 08.06.94
2351 |*
2352 *************************************************************************/
2353 
2354 #define CRYPT_BUFSIZE 1024
2355 
CryptAndWriteBuffer(const void * pStart,sal_Size nLen)2356 sal_Size SvStream::CryptAndWriteBuffer( const void* pStart, sal_Size nLen)
2357 {
2358     unsigned char  pTemp[CRYPT_BUFSIZE];
2359     unsigned char* pDataPtr = (unsigned char*)pStart;
2360     sal_Size nCount = 0;
2361     sal_Size nBufCount;
2362     unsigned char nMask = nCryptMask;
2363     do
2364     {
2365         if( nLen >= CRYPT_BUFSIZE )
2366             nBufCount = CRYPT_BUFSIZE;
2367         else
2368             nBufCount = nLen;
2369         nLen -= nBufCount;
2370         memcpy( pTemp, pDataPtr, (sal_uInt16)nBufCount );
2371         // **** Verschluesseln *****
2372         for ( sal_uInt16 n=0; n < CRYPT_BUFSIZE; n++ )
2373         {
2374             unsigned char aCh = pTemp[n];
2375             aCh ^= nMask;
2376             SWAPNIBBLES(aCh)
2377             pTemp[n] = aCh;
2378         }
2379         // *************************
2380         nCount += PutData( (char*)pTemp, nBufCount );
2381         pDataPtr += nBufCount;
2382     }
2383     while ( nLen );
2384     return nCount;
2385 }
2386 
2387 /*************************************************************************
2388 |*
2389 |*    Stream::EncryptBuffer()
2390 |*
2391 |*    Beschreibung      Buffer entschluesseln
2392 |*    Ersterstellung    OV 08.06.94
2393 |*    Letzte Aenderung  OV 08.06.94
2394 |*
2395 *************************************************************************/
2396 
EncryptBuffer(void * pStart,sal_Size nLen)2397 sal_Bool SvStream::EncryptBuffer(void* pStart, sal_Size nLen)
2398 {
2399     unsigned char* pTemp = (unsigned char*)pStart;
2400     unsigned char nMask = nCryptMask;
2401 
2402     for ( sal_Size n=0; n < nLen; n++, pTemp++ )
2403     {
2404         unsigned char aCh = *pTemp;
2405         SWAPNIBBLES(aCh)
2406         aCh ^= nMask;
2407         *pTemp = aCh;
2408     }
2409     return sal_True;
2410 }
2411 
2412 /*************************************************************************
2413 |*
2414 |*    Stream::SetKey()
2415 |*
2416 |*    Beschreibung      STREAM.SDW
2417 |*    Ersterstellung    OV 08.06.94
2418 |*    Letzte Aenderung  OV 08.06.94
2419 |*
2420 *************************************************************************/
2421 
implGetCryptMask(const sal_Char * pStr,sal_Int32 nLen,long nVersion)2422 unsigned char implGetCryptMask(const sal_Char* pStr, sal_Int32 nLen, long nVersion)
2423 {
2424     unsigned char nCryptMask = 0;
2425 
2426     if (!nLen)
2427         return nCryptMask;
2428 
2429     if( nVersion <= SOFFICE_FILEFORMAT_31 )
2430     {
2431         while( nLen )
2432         {
2433             nCryptMask ^= *pStr;
2434             pStr++;
2435             nLen--;
2436         }
2437     }
2438     else // BugFix #25888#
2439     {
2440         for( sal_uInt16 i = 0; i < nLen; i++ ) {
2441             nCryptMask ^= pStr[i];
2442             if( nCryptMask & 0x80 ) {
2443                 nCryptMask <<= 1;
2444                 nCryptMask++;
2445             }
2446             else
2447                 nCryptMask <<= 1;
2448         }
2449     }
2450 
2451     if( !nCryptMask )
2452         nCryptMask = 67;
2453 
2454     return nCryptMask;
2455 }
2456 
SetKey(const ByteString & rKey)2457 void SvStream::SetKey( const ByteString& rKey )
2458 {
2459     aKey = rKey;
2460     nCryptMask = implGetCryptMask( aKey.GetBuffer(), aKey.Len(), GetVersion() );
2461 }
2462 
2463 /*************************************************************************
2464 |*
2465 |*    Stream::SyncSvStream()
2466 |*
2467 |*    Beschreibung      STREAM.SDW
2468 |*    Ersterstellung    OV 08.06.94
2469 |*    Letzte Aenderung  OV 08.06.94
2470 |*
2471 *************************************************************************/
2472 
SyncSvStream(sal_Size nNewStreamPos)2473 void SvStream::SyncSvStream( sal_Size nNewStreamPos )
2474 {
2475     ClearBuffer();
2476     SvStream::nBufFilePos = nNewStreamPos;
2477 }
2478 
2479 /*************************************************************************
2480 |*
2481 |*    Stream::SyncSysStream()
2482 |*
2483 |*    Beschreibung      STREAM.SDW
2484 |*    Ersterstellung    OV 08.06.94
2485 |*    Letzte Aenderung  OV 08.06.94
2486 |*
2487 *************************************************************************/
2488 
SyncSysStream()2489 void SvStream::SyncSysStream()
2490 {
2491     Flush();
2492     SeekPos( Tell() );
2493 }
2494 
2495 /*************************************************************************
2496 |*
2497 |*    Stream::SetStreamSize()
2498 |*
2499 |*    Beschreibung      STREAM.SDW
2500 |*    Ersterstellung    OV 08.06.94
2501 |*    Letzte Aenderung  OV 08.06.94
2502 |*
2503 *************************************************************************/
2504 
SetStreamSize(sal_Size nSize)2505 sal_Bool SvStream::SetStreamSize( sal_Size nSize )
2506 {
2507 #ifdef DBG_UTIL
2508     sal_Size nFPos = Tell();
2509 #endif
2510     sal_uInt16 nBuf = nBufSize;
2511     SetBufferSize( 0 );
2512     SetSize( nSize );
2513     SetBufferSize( nBuf );
2514     DBG_ASSERT(Tell()==nFPos,"SetStreamSize failed");
2515     return (sal_Bool)(nError == 0);
2516 }
2517 
2518 //============================================================================
2519 
AddMark(sal_Size)2520 void SvStream::AddMark( sal_Size )
2521 {
2522 }
2523 
2524 //============================================================================
2525 
RemoveMark(sal_Size)2526 void SvStream::RemoveMark( sal_Size )
2527 {
2528 }
2529 
2530 /*************************************************************************
2531 |*
2532 |*    endl()
2533 |*
2534 |*    Beschreibung      STREAM.SDW
2535 |*    Ersterstellung    OV 08.06.94
2536 |*    Letzte Aenderung  TH 13.11.96
2537 |*
2538 *************************************************************************/
2539 
endl(SvStream & rStr)2540 SvStream& endl( SvStream& rStr )
2541 {
2542     LineEnd eDelim = rStr.GetLineDelimiter();
2543     if ( eDelim == LINEEND_CR )
2544         rStr << _CR;
2545     else if( eDelim == LINEEND_LF )
2546         rStr << _LF;
2547     else
2548         rStr << _CR << _LF;
2549     return rStr;
2550 }
2551 
endlu(SvStream & rStrm)2552 SvStream& endlu( SvStream& rStrm )
2553 {
2554     switch ( rStrm.GetLineDelimiter() )
2555     {
2556         case LINEEND_CR :
2557             rStrm << sal_Unicode(_CR);
2558         break;
2559         case LINEEND_LF :
2560             rStrm << sal_Unicode(_LF);
2561         break;
2562         default:
2563             rStrm << sal_Unicode(_CR) << sal_Unicode(_LF);
2564     }
2565     return rStrm;
2566 }
2567 
endlub(SvStream & rStrm)2568 SvStream& endlub( SvStream& rStrm )
2569 {
2570     if ( rStrm.GetStreamCharSet() == RTL_TEXTENCODING_UNICODE )
2571         return endlu( rStrm );
2572     else
2573         return endl( rStrm );
2574 }
2575 
2576 /*************************************************************************
2577 |*
2578 |*    SvMemoryStream::SvMemoryStream()
2579 |*
2580 |*    Beschreibung      STREAM.SDW
2581 |*    Ersterstellung    OV 20.06.94
2582 |*    Letzte Aenderung  OV 20.06.94
2583 |*
2584 *************************************************************************/
2585 
SvMemoryStream(void * pBuffer,sal_Size bufSize,StreamMode eMode)2586 SvMemoryStream::SvMemoryStream( void* pBuffer, sal_Size bufSize,
2587                                 StreamMode eMode )
2588 {
2589     if( eMode & STREAM_WRITE )
2590         bIsWritable = sal_True;
2591     else
2592         bIsWritable = sal_False;
2593     nEndOfData  = bufSize;
2594     bOwnsData   = sal_False;
2595     pBuf        = (sal_uInt8 *) pBuffer;
2596     nResize     = 0L;
2597     nSize       = bufSize;
2598     nPos        = 0L;
2599     SetBufferSize( 0 );
2600 }
2601 
2602 /*************************************************************************
2603 |*
2604 |*    SvMemoryStream::SvMemoryStream()
2605 |*
2606 |*    Beschreibung      STREAM.SDW
2607 |*    Ersterstellung    OV 20.06.94
2608 |*    Letzte Aenderung  OV 20.06.94
2609 |*
2610 *************************************************************************/
2611 
SvMemoryStream(sal_Size nInitSize,sal_Size nResizeOffset)2612 SvMemoryStream::SvMemoryStream( sal_Size nInitSize, sal_Size nResizeOffset )
2613 {
2614     bIsWritable = sal_True;
2615     bOwnsData   = sal_True;
2616     nEndOfData  = 0L;
2617     nResize     = nResizeOffset;
2618     nPos        = 0;
2619     pBuf        = 0;
2620     if( nResize != 0 && nResize < 16 )
2621         nResize = 16;
2622     if( nInitSize && !AllocateMemory( nInitSize ) )
2623     {
2624         SetError( SVSTREAM_OUTOFMEMORY );
2625         nSize = 0;
2626     }
2627     else
2628         nSize = nInitSize;
2629     SetBufferSize( 64 );
2630 }
2631 
2632 /*************************************************************************
2633 |*
2634 |*    SvMemoryStream::~SvMemoryStream()
2635 |*
2636 |*    Beschreibung      STREAM.SDW
2637 |*    Ersterstellung    OV 20.06.94
2638 |*    Letzte Aenderung  OV 20.06.94
2639 |*
2640 *************************************************************************/
2641 
~SvMemoryStream()2642 SvMemoryStream::~SvMemoryStream()
2643 {
2644     if( pBuf )
2645     {
2646         if( bOwnsData )
2647             FreeMemory();
2648         else
2649             Flush();
2650     }
2651 }
2652 
2653 /*************************************************************************
2654 |*
2655 |*    SvMemoryStream::IsA()
2656 |*
2657 |*    Beschreibung      STREAM.SDW
2658 |*    Ersterstellung    OV 20.06.94
2659 |*    Letzte Aenderung  OV 20.06.94
2660 |*
2661 *************************************************************************/
2662 
IsA() const2663 sal_uInt16 SvMemoryStream::IsA() const
2664 {
2665     return (sal_uInt16)ID_MEMORYSTREAM;
2666 }
2667 
2668 /*************************************************************************
2669 |*
2670 |*    SvMemoryStream::SetBuffer()
2671 |*
2672 |*    Beschreibung      STREAM.SDW
2673 |*    Ersterstellung    OV 20.06.94
2674 |*    Letzte Aenderung  OV 20.06.94
2675 |*
2676 *************************************************************************/
2677 
SetBuffer(void * pNewBuf,sal_Size nCount,sal_Bool bOwnsDat,sal_Size nEOF)2678 void* SvMemoryStream::SetBuffer( void* pNewBuf, sal_Size nCount,
2679                                  sal_Bool bOwnsDat, sal_Size nEOF )
2680 {
2681     void* pResult;
2682     SetBufferSize( 0 ); // Buffering in der Basisklasse initialisieren
2683     Seek( 0 );
2684     if( bOwnsData )
2685     {
2686         pResult = 0;
2687         if( pNewBuf != pBuf )
2688             FreeMemory();
2689     }
2690     else
2691         pResult = pBuf;
2692 
2693     pBuf        = (sal_uInt8 *) pNewBuf;
2694     nPos        = 0;
2695     nSize       = nCount;
2696     nResize     = 0;
2697     bOwnsData   = bOwnsDat;
2698 
2699     if( nEOF > nCount )
2700         nEOF = nCount;
2701     nEndOfData = nEOF;
2702 
2703     ResetError();
2704 
2705     DBG_ASSERT( nEndOfData<STREAM_SEEK_TO_END,"Invalid EOF");
2706     return pResult;
2707 }
2708 
2709 /*************************************************************************
2710 |*
2711 |*    SvMemoryStream::GetData()
2712 |*
2713 |*    Beschreibung      STREAM.SDW
2714 |*    Ersterstellung    OV 20.06.94
2715 |*    Letzte Aenderung  OV 20.06.94
2716 |*
2717 *************************************************************************/
2718 
GetData(void * pData,sal_Size nCount)2719 sal_Size SvMemoryStream::GetData( void* pData, sal_Size nCount )
2720 {
2721     sal_Size nMaxCount = nEndOfData-nPos;
2722     if( nCount > nMaxCount )
2723         nCount = nMaxCount;
2724     memcpy( pData, pBuf+nPos, (size_t)nCount );
2725     nPos += nCount;
2726     return nCount;
2727 }
2728 
2729 /*************************************************************************
2730 |*
2731 |*    SvMemoryStream::PutData()
2732 |*
2733 |*    Beschreibung      STREAM.SDW
2734 |*    Ersterstellung    OV 20.06.94
2735 |*    Letzte Aenderung  OV 20.06.94
2736 |*
2737 *************************************************************************/
2738 
PutData(const void * pData,sal_Size nCount)2739 sal_Size SvMemoryStream::PutData( const void* pData, sal_Size nCount )
2740 {
2741     if( GetError() )
2742         return 0L;
2743 
2744     sal_Size nMaxCount = nSize-nPos;
2745 
2746     // auf Ueberlauf testen
2747     if( nCount > nMaxCount )
2748     {
2749         if( nResize == 0 )
2750         {
2751             // soviel wie moeglich rueberschaufeln
2752             nCount = nMaxCount;
2753             SetError( SVSTREAM_OUTOFMEMORY );
2754         }
2755         else
2756         {
2757             long nNewResize;
2758             if( nSize && nSize > nResize )
2759                 nNewResize = nSize;
2760             else
2761                 nNewResize = nResize;
2762 
2763             if( (nCount-nMaxCount) < nResize )
2764             {
2765                 // fehlender Speicher ist kleiner als Resize-Offset,
2766                 // deshalb um Resize-Offset vergroessern
2767                 if( !ReAllocateMemory( nNewResize) )
2768                 {
2769                     nCount = 0;
2770                     SetError( SVSTREAM_WRITE_ERROR );
2771                 }
2772             }
2773             else
2774             {
2775                 // fehlender Speicher ist groesser als Resize-Offset
2776                 // deshalb um Differenz+ResizeOffset vergroessern
2777                 if( !ReAllocateMemory( nCount-nMaxCount+nNewResize ) )
2778                 {
2779                     nCount = 0;
2780                     SetError( SVSTREAM_WRITE_ERROR );
2781                 }
2782             }
2783         }
2784     }
2785     DBG_ASSERT(pBuf,"Possibly Reallocate failed");
2786     memcpy( pBuf+nPos, pData, (size_t)nCount);
2787 
2788     nPos += nCount;
2789     if( nPos > nEndOfData )
2790         nEndOfData = nPos;
2791     return nCount;
2792 }
2793 
2794 /*************************************************************************
2795 |*
2796 |*    SvMemoryStream::SeekPos()
2797 |*
2798 |*    Beschreibung      STREAM.SDW
2799 |*    Ersterstellung    OV 20.06.94
2800 |*    Letzte Aenderung  OV 20.06.94
2801 |*
2802 *************************************************************************/
2803 
2804 // nEndOfData: Erste Position im Stream, die nicht gelesen werden darf
2805 // nSize: Groesse des allozierten Speichers
2806 
SeekPos(sal_Size nNewPos)2807 sal_Size SvMemoryStream::SeekPos( sal_Size nNewPos )
2808 {
2809     if( nNewPos < nEndOfData )
2810         nPos = nNewPos;
2811     else if( nNewPos == STREAM_SEEK_TO_END )
2812         nPos = nEndOfData;
2813     else
2814     {
2815         if( nNewPos >= nSize ) // muss Buffer vergroessert werden ?
2816         {
2817             if( nResize )  // ist vergroeseern erlaubt ?
2818             {
2819                 long nDiff = (long)(nNewPos - nSize + 1);
2820                 nDiff += (long)nResize;
2821                 ReAllocateMemory( nDiff );
2822                 nPos = nNewPos;
2823                 nEndOfData = nNewPos;
2824             }
2825             else  // vergroessern ist nicht erlaubt -> ans Ende setzen
2826             {
2827                 // SetError( SVSTREAM_OUTOFMEMORY );
2828                 nPos = nEndOfData;
2829             }
2830         }
2831         else  // gueltigen Bereich innerhalb des Buffers vergroessern
2832         {
2833             nPos = nNewPos;
2834             nEndOfData = nNewPos;
2835         }
2836     }
2837     return nPos;
2838 }
2839 
2840 /*************************************************************************
2841 |*
2842 |*    SvMemoryStream::FlushData()
2843 |*
2844 |*    Beschreibung      STREAM.SDW
2845 |*    Ersterstellung    OV 20.06.94
2846 |*    Letzte Aenderung  OV 20.06.94
2847 |*
2848 *************************************************************************/
2849 
FlushData()2850 void SvMemoryStream::FlushData()
2851 {
2852 }
2853 
2854 /*************************************************************************
2855 |*
2856 |*    SvMemoryStream::ResetError()
2857 |*
2858 |*    Beschreibung      STREAM.SDW
2859 |*    Ersterstellung    OV 20.06.94
2860 |*    Letzte Aenderung  OV 20.06.94
2861 |*
2862 *************************************************************************/
2863 
ResetError()2864 void SvMemoryStream::ResetError()
2865 {
2866     SvStream::ClearError();
2867 }
2868 
2869 /*************************************************************************
2870 |*
2871 |*    SvMemoryStream::AllocateMemory()
2872 |*
2873 |*    Beschreibung      STREAM.SDW
2874 |*    Ersterstellung    OV 20.06.94
2875 |*    Letzte Aenderung  OV 20.06.94
2876 |*
2877 *************************************************************************/
2878 
AllocateMemory(sal_Size nNewSize)2879 sal_Bool SvMemoryStream::AllocateMemory( sal_Size nNewSize )
2880 {
2881     pBuf = new sal_uInt8[nNewSize];
2882     return( pBuf != 0 );
2883 }
2884 
2885 /*************************************************************************
2886 |*
2887 |*    SvMemoryStream::ReAllocateMemory()   (Bozo-Algorithmus)
2888 |*
2889 |*    Beschreibung      STREAM.SDW
2890 |*    Ersterstellung    OV 20.06.94
2891 |*    Letzte Aenderung  OV 20.06.94
2892 |*
2893 *************************************************************************/
2894 
ReAllocateMemory(long nDiff)2895 sal_Bool SvMemoryStream::ReAllocateMemory( long nDiff )
2896 {
2897     sal_Bool bRetVal    = sal_False;
2898     long nTemp      = (long)nSize;
2899     nTemp           += nDiff;
2900     sal_Size nNewSize  = (sal_Size)nTemp;
2901 
2902     if( nNewSize )
2903     {
2904         sal_uInt8* pNewBuf   = new sal_uInt8[nNewSize];
2905 
2906         if( pNewBuf )
2907         {
2908             bRetVal = sal_True; // Success!
2909             if( nNewSize < nSize )      // Verkleinern ?
2910             {
2911                 memcpy( pNewBuf, pBuf, (size_t)nNewSize );
2912                 if( nPos > nNewSize )
2913                     nPos = 0L;
2914                 if( nEndOfData >= nNewSize )
2915                     nEndOfData = nNewSize-1L;
2916             }
2917             else
2918             {
2919                 memcpy( pNewBuf, pBuf, (size_t)nSize );
2920             }
2921 
2922             FreeMemory();
2923 
2924             pBuf  = pNewBuf;
2925             nSize = nNewSize;
2926         }
2927     }
2928     else
2929     {
2930         bRetVal = sal_True;
2931         FreeMemory();
2932         pBuf = 0;
2933         nSize = 0;
2934         nEndOfData = 0;
2935         nPos = 0;
2936     }
2937 
2938     return bRetVal;
2939 }
2940 
FreeMemory()2941 void SvMemoryStream::FreeMemory()
2942 {
2943     delete[] pBuf;
2944 }
2945 
2946 /*************************************************************************
2947 |*
2948 |*    SvMemoryStream::SwitchBuffer()
2949 |*
2950 |*    Beschreibung      STREAM.SDW
2951 |*    Ersterstellung    OV 26.07.94
2952 |*    Letzte Aenderung  OV 26.07.94
2953 |*
2954 *************************************************************************/
2955 
SwitchBuffer(sal_Size nInitSize,sal_Size nResizeOffset)2956 void* SvMemoryStream::SwitchBuffer( sal_Size nInitSize, sal_Size nResizeOffset)
2957 {
2958     Flush();
2959     if( !bOwnsData )
2960         return 0;
2961     Seek( STREAM_SEEK_TO_BEGIN );
2962 
2963     void* pRetVal = pBuf;
2964     pBuf          = 0;
2965     nEndOfData    = 0L;
2966     nResize       = nResizeOffset;
2967     nPos          = 0;
2968 
2969     if( nResize != 0 && nResize < 16 )
2970         nResize = 16;
2971 
2972     ResetError();
2973 
2974     if( nInitSize && !AllocateMemory(nInitSize) )
2975     {
2976         SetError( SVSTREAM_OUTOFMEMORY );
2977         nSize = 0;
2978     }
2979     else
2980         nSize = nInitSize;
2981 
2982     SetBufferSize( 64 );
2983     return pRetVal;
2984 }
2985 
SetSize(sal_Size nNewSize)2986 void SvMemoryStream::SetSize( sal_Size nNewSize )
2987 {
2988     long nDiff = (long)nNewSize - (long)nSize;
2989     ReAllocateMemory( nDiff );
2990 }
2991 
TYPEINIT0(SvDataCopyStream)2992 TYPEINIT0 ( SvDataCopyStream )
2993 
2994 void SvDataCopyStream::Assign( const SvDataCopyStream& )
2995 {
2996 }
2997