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