1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: stream.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_tools.hxx"
35 // - Read->RefreshBuffer->Auf Aenderungen von nBufActualLen reagieren
41 #include <ctype.h> // isspace
42 #include <stdlib.h> // strtol, _crotl
44 #include "boost/static_assert.hpp"
47 #if defined( DBG_UTIL ) && (OSL_DEBUG_LEVEL > 1)
48 // prueft Synchronisation des Buffers nach allen Read, Write, Seek
53 #include <tools/solar.h>
56 #define SWAPNIBBLES(c) c=_crotl(c,4);
58 #define SWAPNIBBLES(c) \
59 unsigned char nSwapTmp=c; \
65 #include <tools/debug.hxx>
66 #define ENABLE_BYTESTRING_STREAM_OPERATORS
67 #include <tools/stream.hxx>
68 #include <osl/thread.h>
71 // -----------------------------------------------------------------------
75 // -----------------------------------------------------------------------
78 #define SPECIAL_PARAM_NONE 0 // Format-Str, Number
79 #define SPECIAL_PARAM_WIDTH 1 // Format-Str, Width, Number
80 #define SPECIAL_PARAM_PRECISION 2 // Format-Str, Precision, Number
81 #define SPECIAL_PARAM_BOTH 3 // Format-Str, Width, Precision, Number
83 // -----------------------------------------------------------------------
85 // !!! Nicht inline, wenn Operatoren <<,>> inline sind
86 inline static void SwapUShort( sal_uInt16
& r
)
88 inline static void SwapShort( short& r
)
90 inline static void SwapLong( long& r
)
92 inline static void SwapULong( sal_uInt32
& r
)
94 inline static void SwapLongInt( int& r
)
96 inline static void SwapLongUInt( unsigned int& r
)
99 inline static void SwapFloat( float& r
)
101 *((sal_uInt32
*)(void*)&r
) = SWAPLONG( *((sal_uInt32
*)(void*)&r
) );
103 inline static void SwapDouble( double& r
)
105 if( sizeof(double) != 8 )
107 DBG_ASSERT( FALSE
, "Can only swap 8-Byte-doubles\n" );
111 sal_uInt32
* c
= (sal_uInt32
*)(void*)&r
;
112 c
[0] ^= c
[1]; // zwei 32-Bit-Werte in situ vertauschen
115 c
[0] = SWAPLONG(c
[0]); // und die beiden 32-Bit-Werte selbst in situ drehen
116 c
[1] = SWAPLONG(c
[1]);
123 #define READNUMBER_WITHOUT_SWAP(datatype,value) \
126 if( (tmp == STREAM_IO_READ) && sizeof(datatype)<=nBufFree) \
128 for (std::size_t i = 0; i < sizeof(datatype); i++)\
129 ((char *)&value)[i] = pBufPos[i];\
130 nBufActualPos += sizeof(datatype);\
131 pBufPos += sizeof(datatype);\
132 nBufFree -= sizeof(datatype);\
135 Read( (char*)&value, sizeof(datatype) );\
138 #define WRITENUMBER_WITHOUT_SWAP(datatype,value) \
141 if( (tmp==STREAM_IO_WRITE) && sizeof(datatype) <= nBufFree)\
143 for (std::size_t i = 0; i < sizeof(datatype); i++)\
144 pBufPos[i] = ((char *)&value)[i];\
145 nBufFree -= sizeof(datatype);\
146 nBufActualPos += sizeof(datatype);\
147 if( nBufActualPos > nBufActualLen )\
148 nBufActualLen = nBufActualPos;\
149 pBufPos += sizeof(datatype);\
153 Write( (char*)&value, sizeof(datatype) );\
156 //============================================================================
160 //============================================================================
162 void SvLockBytes::close()
169 //============================================================================
170 TYPEINIT0(SvLockBytes
);
172 //============================================================================
174 ErrCode
SvLockBytes::ReadAt(sal_Size nPos
, void * pBuffer
, sal_Size nCount
,
175 sal_Size
* pRead
) const
179 DBG_ERROR("SvLockBytes::ReadAt(): Bad stream");
183 m_pStream
->Seek(nPos
);
184 sal_Size nTheRead
= m_pStream
->Read(pBuffer
, nCount
);
187 return m_pStream
->GetErrorCode();
190 //============================================================================
192 ErrCode
SvLockBytes::WriteAt(sal_Size nPos
, const void * pBuffer
, sal_Size nCount
,
197 DBG_ERROR("SvLockBytes::WriteAt(): Bad stream");
201 m_pStream
->Seek(nPos
);
202 sal_Size nTheWritten
= m_pStream
->Write(pBuffer
, nCount
);
204 *pWritten
= nTheWritten
;
205 return m_pStream
->GetErrorCode();
208 //============================================================================
210 ErrCode
SvLockBytes::Flush() const
214 DBG_ERROR("SvLockBytes::Flush(): Bad stream");
219 return m_pStream
->GetErrorCode();
222 //============================================================================
224 ErrCode
SvLockBytes::SetSize(sal_Size nSize
)
228 DBG_ERROR("SvLockBytes::SetSize(): Bad stream");
232 m_pStream
->SetStreamSize(nSize
);
233 return m_pStream
->GetErrorCode();
236 //============================================================================
237 ErrCode
SvLockBytes::LockRegion(sal_Size
, sal_Size
, LockType
)
239 DBG_ERROR("SvLockBytes::LockRegion(): Not implemented");
243 //============================================================================
245 ErrCode
SvLockBytes::UnlockRegion(sal_Size
, sal_Size
, LockType
)
247 DBG_ERROR("SvLockBytes::UnlockRegion(): Not implemented");
251 //============================================================================
252 ErrCode
SvLockBytes::Stat(SvLockBytesStat
* pStat
, SvLockBytesStatFlag
) const
256 DBG_ERROR("SvLockBytes::Stat(): Bad stream");
262 sal_Size nPos
= m_pStream
->Tell();
263 pStat
->nSize
= m_pStream
->Seek(STREAM_SEEK_TO_END
);
264 m_pStream
->Seek(nPos
);
269 //============================================================================
271 // class SvOpenLockBytes
273 //============================================================================
275 TYPEINIT1(SvOpenLockBytes
, SvLockBytes
);
277 //============================================================================
279 // class SvAsyncLockBytes
281 //============================================================================
283 TYPEINIT1(SvAsyncLockBytes
, SvOpenLockBytes
);
285 //============================================================================
287 ErrCode
SvAsyncLockBytes::ReadAt(sal_Size nPos
, void * pBuffer
, sal_Size nCount
,
288 sal_Size
* pRead
) const
291 return SvOpenLockBytes::ReadAt(nPos
, pBuffer
, nCount
, pRead
);
294 sal_Size nTheCount
= std::min(nPos
< m_nSize
? m_nSize
- nPos
: 0, nCount
);
295 ErrCode nError
= SvOpenLockBytes::ReadAt(nPos
, pBuffer
, nTheCount
,
297 return !nCount
|| nTheCount
== nCount
|| nError
? nError
:
302 //============================================================================
304 ErrCode
SvAsyncLockBytes::WriteAt(sal_Size nPos
, const void * pBuffer
,
305 sal_Size nCount
, sal_Size
* pWritten
)
308 return SvOpenLockBytes::WriteAt(nPos
, pBuffer
, nCount
, pWritten
);
311 sal_Size nTheCount
= std::min(nPos
< m_nSize
? m_nSize
- nPos
: 0, nCount
);
312 ErrCode nError
= SvOpenLockBytes::WriteAt(nPos
, pBuffer
, nTheCount
,
314 return !nCount
|| nTheCount
== nCount
|| nError
? nError
:
319 //============================================================================
321 ErrCode
SvAsyncLockBytes::FillAppend(const void * pBuffer
, sal_Size nCount
,
324 sal_Size nTheWritten
;
325 ErrCode nError
= SvOpenLockBytes::WriteAt(m_nSize
, pBuffer
, nCount
,
328 m_nSize
+= nTheWritten
;
330 *pWritten
= nTheWritten
;
334 //============================================================================
336 sal_Size
SvAsyncLockBytes::Seek(sal_Size nPos
)
338 if (nPos
!= STREAM_SEEK_TO_END
)
343 //============================================================================
347 //============================================================================
349 sal_Size
SvStream::GetData( void* pData
, sal_Size nSize
)
353 DBG_ASSERT( xLockBytes
.Is(), "pure virtual function" );
355 nError
= xLockBytes
->ReadAt( nActPos
, pData
, nSize
, &nRet
);
362 ErrCode
SvStream::SetLockBytes( SvLockBytesRef
& rLB
)
369 //========================================================================
371 sal_Size
SvStream::PutData( const void* pData
, sal_Size nSize
)
375 DBG_ASSERT( xLockBytes
.Is(), "pure virtual function" );
377 nError
= xLockBytes
->WriteAt( nActPos
, pData
, nSize
, &nRet
);
384 //========================================================================
386 sal_Size
SvStream::SeekPos( sal_Size nPos
)
388 if( !GetError() && nPos
== STREAM_SEEK_TO_END
)
390 DBG_ASSERT( xLockBytes
.Is(), "pure virtual function" );
391 SvLockBytesStat aStat
;
392 xLockBytes
->Stat( &aStat
, SVSTATFLAG_DEFAULT
);
393 nActPos
= aStat
.nSize
;
400 //========================================================================
402 void SvStream::FlushData()
406 DBG_ASSERT( xLockBytes
.Is(), "pure virtual function" );
407 nError
= xLockBytes
->Flush();
411 //========================================================================
413 void SvStream::SetSize( sal_Size nSize
)
415 DBG_ASSERT( xLockBytes
.Is(), "pure virtual function" );
416 nError
= xLockBytes
->SetSize( nSize
);
419 void SvStream::ImpInit()
422 nCompressMode
= COMPRESSMODE_NONE
;
423 eStreamCharSet
= osl_getThreadTextEncoding();
424 // eTargetCharSet = osl_getThreadTextEncoding();
428 eLineDelimiter
= LINEEND_LF
; // UNIX-Format
430 eLineDelimiter
= LINEEND_CRLF
; // DOS-Format
433 SetNumberFormatInt( NUMBERFORMAT_INT_LITTLEENDIAN
);
438 bIsConsistent
= TRUE
;
445 eIOMode
= STREAM_IO_DONTKNOW
;
449 nPrecision
= 0; // all significant digits
450 nWidth
= 0; // default width
452 nJustification
= JUSTIFY_RIGHT
;
454 CreateFormatString();
461 /*************************************************************************
465 |* Beschreibung STREAM.SDW
466 |* Ersterstellung OV 08.06.94
467 |* Letzte Aenderung OV 08.06.94
469 *************************************************************************/
471 SvStream::SvStream( SvLockBytes
* pLockBytesP
)
473 DBG_CTOR( Stream
, NULL
);
476 xLockBytes
= pLockBytesP
;
477 const SvStream
* pStrm
;
479 pStrm
= pLockBytesP
->GetStream();
481 SetError( pStrm
->GetErrorCode() );
484 SetBufferSize( 256 );
489 DBG_CTOR( Stream
, NULL
);
494 /*************************************************************************
498 |* Beschreibung STREAM.SDW
499 |* Ersterstellung OV 08.06.94
500 |* Letzte Aenderung OV 08.06.94
502 *************************************************************************/
504 SvStream::~SvStream()
506 DBG_DTOR( Stream
, NULL
);
508 if ( xLockBytes
.Is() )
515 /*************************************************************************
519 |* Beschreibung STREAM.SDW
520 |* Ersterstellung OV 08.06.94
521 |* Letzte Aenderung OV 08.06.94
523 *************************************************************************/
525 sal_uInt16
SvStream::IsA() const
527 return (sal_uInt16
)ID_STREAM
;
530 /*************************************************************************
532 |* Stream::ClearError()
534 |* Beschreibung STREAM.SDW
535 |* Ersterstellung OV 08.06.94
536 |* Letzte Aenderung OV 08.06.94
538 *************************************************************************/
540 void SvStream::ClearError()
543 nError
= SVSTREAM_OK
;
546 /*************************************************************************
548 |* Stream::SetError()
550 |* Beschreibung STREAM.SDW
551 |* Ersterstellung OV 08.06.94
552 |* Letzte Aenderung OV 08.06.94
554 *************************************************************************/
556 void SvStream::SetError( sal_uInt32 nErrorCode
)
558 if ( nError
== SVSTREAM_OK
)
563 /*************************************************************************
565 |* Stream::SetNumberFormatInt()
567 |* Beschreibung STREAM.SDW
568 |* Ersterstellung OV 08.06.94
569 |* Letzte Aenderung OV 08.06.94
571 *************************************************************************/
573 void SvStream::SetNumberFormatInt( sal_uInt16 nNewFormat
)
575 nNumberFormatInt
= nNewFormat
;
578 if( nNumberFormatInt
== NUMBERFORMAT_INT_LITTLEENDIAN
)
581 if( nNumberFormatInt
== NUMBERFORMAT_INT_BIGENDIAN
)
586 /*************************************************************************
588 |* Stream::SetBufferSize()
590 |* Beschreibung STREAM.SDW
591 |* Ersterstellung OV 08.06.94
592 |* Letzte Aenderung OV 08.06.94
594 *************************************************************************/
596 void SvStream::SetBufferSize( sal_uInt16 nBufferSize
)
598 sal_Size nActualFilePos
= Tell();
599 sal_Bool bDontSeek
= (sal_Bool
)(pRWBuf
== 0);
601 if( bIsDirty
&& bIsConsistent
&& bIsWritable
) // wg. Windows NT: Access denied
607 nBufFilePos
+= nBufActualPos
;
613 nBufSize
= nBufferSize
;
615 pRWBuf
= new BYTE
[ nBufSize
];
616 bIsConsistent
= TRUE
;
618 eIOMode
= STREAM_IO_DONTKNOW
;
620 SeekPos( nActualFilePos
);
623 /*************************************************************************
625 |* Stream::ClearBuffer()
627 |* Beschreibung STREAM.SDW
628 |* Ersterstellung OV 08.06.94
629 |* Letzte Aenderung OV 08.06.94
631 *************************************************************************/
633 void SvStream::ClearBuffer()
640 bIsConsistent
= TRUE
;
641 eIOMode
= STREAM_IO_DONTKNOW
;
646 /*************************************************************************
648 |* Stream::ResetError()
650 |* Beschreibung STREAM.SDW
651 |* Ersterstellung OV 08.06.94
652 |* Letzte Aenderung OV 08.06.94
654 *************************************************************************/
656 void SvStream::ResetError()
661 /*************************************************************************
663 |* Stream::ReadLine()
665 |* Beschreibung STREAM.SDW
666 |* Ersterstellung OV 08.06.94
667 |* Letzte Aenderung OV 08.06.94
669 *************************************************************************/
671 sal_Bool
SvStream::ReadByteStringLine( String
& rStr
, rtl_TextEncoding eSrcCharSet
)
676 bRet
= ReadLine(aStr
);
677 rStr
= UniString( aStr
, eSrcCharSet
);
681 sal_Bool
SvStream::ReadLine( ByteString
& rStr
)
684 sal_Bool bEnd
= FALSE
;
685 sal_Size nOldFilePos
= Tell();
687 sal_Size nTotalLen
= 0;
690 while( !bEnd
&& !GetError() ) // !!! nicht auf EOF testen,
691 // !!! weil wir blockweise
694 sal_uInt16 nLen
= (sal_uInt16
)Read( buf
, sizeof(buf
)-1 );
697 if ( rStr
.Len() == 0 )
699 // der allererste Blockread hat fehlgeschlagen -> Abflug
708 for( j
= n
= 0; j
< nLen
; ++j
)
711 if ( c
== '\n' || c
== '\r' )
716 // erAck 26.02.01: Old behavior was no special treatment of '\0'
717 // character here, but a following rStr+=c did ignore it. Is this
718 // really intended? Or should a '\0' better terminate a line?
719 // The nOldFilePos stuff wasn't correct then anyways.
728 rStr
.Append( buf
, n
);
732 if ( !bEnd
&& !GetError() && rStr
.Len() )
735 nOldFilePos
+= nTotalLen
;
736 if( Tell() > nOldFilePos
)
738 Seek( nOldFilePos
); // seeken wg. obigem BlockRead!
740 if ( bEnd
&& (c
=='\r' || c
=='\n') ) // Sonderbehandlung DOS-Dateien
743 sal_Size nLen
= Read((char*)&cTemp
, sizeof(cTemp
) );
745 if( cTemp
== c
|| (cTemp
!= '\n' && cTemp
!= '\r') )
755 sal_Bool
SvStream::ReadUniStringLine( String
& rStr
)
757 sal_Unicode buf
[256+1];
758 sal_Bool bEnd
= FALSE
;
759 sal_Size nOldFilePos
= Tell();
761 sal_Size nTotalLen
= 0;
763 DBG_ASSERT( sizeof(sal_Unicode
) == sizeof(sal_uInt16
), "ReadUniStringLine: swapping sizeof(sal_Unicode) not implemented" );
766 while( !bEnd
&& !GetError() ) // !!! nicht auf EOF testen,
767 // !!! weil wir blockweise
770 sal_uInt16 nLen
= (sal_uInt16
)Read( (char*)buf
, sizeof(buf
)-sizeof(sal_Unicode
) );
771 nLen
/= sizeof(sal_Unicode
);
774 if ( rStr
.Len() == 0 )
776 // der allererste Blockread hat fehlgeschlagen -> Abflug
785 for( j
= n
= 0; j
< nLen
; ++j
)
788 SwapUShort( buf
[n
] );
790 if ( c
== '\n' || c
== '\r' )
795 // erAck 26.02.01: Old behavior was no special treatment of '\0'
796 // character here, but a following rStr+=c did ignore it. Is this
797 // really intended? Or should a '\0' better terminate a line?
798 // The nOldFilePos stuff wasn't correct then anyways.
807 rStr
.Append( buf
, n
);
811 if ( !bEnd
&& !GetError() && rStr
.Len() )
814 nOldFilePos
+= nTotalLen
* sizeof(sal_Unicode
);
815 if( Tell() > nOldFilePos
)
816 nOldFilePos
+= sizeof(sal_Unicode
);
817 Seek( nOldFilePos
); // seeken wg. obigem BlockRead!
819 if ( bEnd
&& (c
=='\r' || c
=='\n') ) // Sonderbehandlung DOS-Dateien
822 Read( (char*)&cTemp
, sizeof(cTemp
) );
825 if( cTemp
== c
|| (cTemp
!= '\n' && cTemp
!= '\r') )
834 sal_Bool
SvStream::ReadUniOrByteStringLine( String
& rStr
, rtl_TextEncoding eSrcCharSet
)
836 if ( eSrcCharSet
== RTL_TEXTENCODING_UNICODE
)
837 return ReadUniStringLine( rStr
);
839 return ReadByteStringLine( rStr
, eSrcCharSet
);
842 /*************************************************************************
844 |* Stream::ReadCString
846 *************************************************************************/
848 sal_Bool
SvStream::ReadCString( ByteString
& rStr
)
853 sal_Char buf
[ 256 + 1 ];
854 sal_Bool bEnd
= FALSE
;
855 sal_Size nFilePos
= Tell();
857 while( !bEnd
&& !GetError() )
859 sal_uInt16 nLen
= (sal_uInt16
)Read( buf
, sizeof(buf
)-1 );
860 sal_uInt16 nReallyRead
= nLen
;
864 const sal_Char
* pPtr
= buf
;
865 while( *pPtr
&& nLen
)
868 bEnd
= ( nReallyRead
< sizeof(buf
)-1 ) // read less than attempted to read
869 || ( ( nLen
> 0 ) // OR it is inside the block we read
870 && ( 0 == *pPtr
) // AND found a string terminator
873 rStr
.Append( buf
, ::sal::static_int_cast
< xub_StrLen
>( pPtr
- buf
) );
876 nFilePos
+= rStr
.Len();
877 if( Tell() > nFilePos
)
879 Seek( nFilePos
); // seeken wg. obigem BlockRead!
883 sal_Bool
SvStream::ReadCString( String
& rStr
, rtl_TextEncoding eToEncode
)
886 sal_Bool bRet
= ReadCString( sStr
);
887 rStr
= String( sStr
, eToEncode
);
892 /*************************************************************************
894 |* Stream::WriteUnicodeText()
896 *************************************************************************/
898 sal_Bool
SvStream::WriteUnicodeText( const String
& rStr
)
900 DBG_ASSERT( sizeof(sal_Unicode
) == sizeof(sal_uInt16
), "WriteUnicodeText: swapping sizeof(sal_Unicode) not implemented" );
903 xub_StrLen nLen
= rStr
.Len();
904 sal_Unicode aBuf
[384];
905 sal_Unicode
* const pTmp
= ( nLen
> 384 ? new sal_Unicode
[nLen
] : aBuf
);
906 memcpy( pTmp
, rStr
.GetBuffer(), nLen
* sizeof(sal_Unicode
) );
907 sal_Unicode
* p
= pTmp
;
908 const sal_Unicode
* const pStop
= pTmp
+ nLen
;
914 Write( (char*)pTmp
, nLen
* sizeof(sal_Unicode
) );
919 Write( (char*)rStr
.GetBuffer(), rStr
.Len() * sizeof(sal_Unicode
) );
920 return nError
== SVSTREAM_OK
;
923 sal_Bool
SvStream::WriteUnicodeOrByteText( const String
& rStr
, rtl_TextEncoding eDestCharSet
)
925 if ( eDestCharSet
== RTL_TEXTENCODING_UNICODE
)
926 return WriteUnicodeText( rStr
);
929 ByteString
aStr( rStr
, eDestCharSet
);
930 Write( aStr
.GetBuffer(), aStr
.Len() );
931 return nError
== SVSTREAM_OK
;
935 /*************************************************************************
937 |* Stream::WriteLine()
939 |* Beschreibung STREAM.SDW
940 |* Ersterstellung OV 08.06.94
941 |* Letzte Aenderung OV 08.06.94
943 *************************************************************************/
945 sal_Bool
SvStream::WriteByteStringLine( const String
& rStr
, rtl_TextEncoding eDestCharSet
)
947 return WriteLine( ByteString( rStr
, eDestCharSet
) );
950 sal_Bool
SvStream::WriteLine( const ByteString
& rStr
)
952 Write( rStr
.GetBuffer(), rStr
.Len() );
954 return nError
== SVSTREAM_OK
;
957 sal_Bool
SvStream::WriteUniStringLine( const String
& rStr
)
959 WriteUnicodeText( rStr
);
961 return nError
== SVSTREAM_OK
;
964 sal_Bool
SvStream::WriteUniOrByteStringLine( const String
& rStr
, rtl_TextEncoding eDestCharSet
)
966 if ( eDestCharSet
== RTL_TEXTENCODING_UNICODE
)
967 return WriteUniStringLine( rStr
);
969 return WriteByteStringLine( rStr
, eDestCharSet
);
972 /*************************************************************************
974 |* Stream::WriteLines()
976 |* Beschreibung STREAM.SDW
977 |* Ersterstellung OV 17.07.95
978 |* Letzte Aenderung OV 17.07.95
980 *************************************************************************/
982 sal_Bool
SvStream::WriteByteStringLines( const String
& rStr
, rtl_TextEncoding eDestCharSet
)
984 return WriteLines( ByteString( rStr
, eDestCharSet
) );
987 sal_Bool
SvStream::WriteLines( const ByteString
& rStr
)
989 ByteString
aStr( rStr
);
990 aStr
.ConvertLineEnd( eLineDelimiter
);
991 Write( aStr
.GetBuffer(), aStr
.Len() );
993 return (sal_Bool
)(nError
== SVSTREAM_OK
);
996 sal_Bool
SvStream::WriteUniStringLines( const String
& rStr
)
999 aStr
.ConvertLineEnd( eLineDelimiter
);
1000 WriteUniStringLine( aStr
);
1001 return nError
== SVSTREAM_OK
;
1004 sal_Bool
SvStream::WriteUniOrByteStringLines( const String
& rStr
, rtl_TextEncoding eDestCharSet
)
1006 if ( eDestCharSet
== RTL_TEXTENCODING_UNICODE
)
1007 return WriteUniStringLines( rStr
);
1009 return WriteByteStringLines( rStr
, eDestCharSet
);
1012 /*************************************************************************
1014 |* Stream::WriteUniOrByteChar()
1016 *************************************************************************/
1018 sal_Bool
SvStream::WriteUniOrByteChar( sal_Unicode ch
, rtl_TextEncoding eDestCharSet
)
1020 if ( eDestCharSet
== RTL_TEXTENCODING_UNICODE
)
1024 ByteString
aStr( ch
, eDestCharSet
);
1025 Write( aStr
.GetBuffer(), aStr
.Len() );
1027 return nError
== SVSTREAM_OK
;
1030 /*************************************************************************
1032 |* Stream::StartWritingUnicodeText()
1034 *************************************************************************/
1036 sal_Bool
SvStream::StartWritingUnicodeText()
1038 SetEndianSwap( FALSE
); // write native format
1039 // BOM, Byte Order Mark, U+FEFF, see
1040 // http://www.unicode.org/faq/utf_bom.html#BOM
1041 // Upon read: 0xfeff(-257) => no swap; 0xfffe(-2) => swap
1042 *this << sal_uInt16( 0xfeff );
1043 return nError
== SVSTREAM_OK
;
1046 /*************************************************************************
1048 |* Stream::StartReadingUnicodeText()
1050 *************************************************************************/
1052 sal_Bool
SvStream::StartReadingUnicodeText()
1062 SetEndianSwap( !bSwap
);
1065 SeekRel( -((sal_sSize
)sizeof(nFlag
)) ); // no BOM, pure data
1067 return nError
== SVSTREAM_OK
;
1070 /*************************************************************************
1072 |* Stream::ReadCsvLine()
1074 *************************************************************************/
1076 // Precondition: pStr is guaranteed to be non-NULL and points to a 0-terminated
1078 inline const sal_Unicode
* lcl_UnicodeStrChr( const sal_Unicode
* pStr
,
1090 sal_Bool
SvStream::ReadCsvLine( String
& rStr
, sal_Bool bEmbeddedLineBreak
,
1091 const String
& rFieldSeparators
, sal_Unicode cFieldQuote
,
1092 sal_Bool bAllowBackslashEscape
)
1094 ReadUniOrByteStringLine( rStr
);
1096 if (bEmbeddedLineBreak
)
1098 const sal_Unicode
* pSeps
= rFieldSeparators
.GetBuffer();
1099 xub_StrLen nLastOffset
= 0;
1100 xub_StrLen nQuotes
= 0;
1101 while (!IsEof() && rStr
.Len() < STRING_MAXLEN
)
1103 bool bBackslashEscaped
= false;
1104 const sal_Unicode
*p
, *pStart
;
1105 p
= pStart
= rStr
.GetBuffer();
1111 if (*p
== cFieldQuote
&& !bBackslashEscaped
)
1113 else if (bAllowBackslashEscape
)
1116 bBackslashEscaped
= !bBackslashEscaped
;
1118 bBackslashEscaped
= false;
1121 else if (*p
== cFieldQuote
&& (p
== pStart
||
1122 lcl_UnicodeStrChr( pSeps
, p
[-1])))
1124 // A quote character inside a field content does not start
1129 if (nQuotes
% 2 == 0)
1133 nLastOffset
= rStr
.Len();
1135 ReadUniOrByteStringLine( aNext
);
1136 rStr
+= sal_Unicode(_LF
);
1141 return nError
== SVSTREAM_OK
;
1144 /*************************************************************************
1146 |* Stream::SeekRel()
1148 |* Beschreibung STREAM.SDW
1149 |* Ersterstellung OV 08.06.94
1150 |* Letzte Aenderung OV 08.06.94
1152 *************************************************************************/
1154 sal_Size
SvStream::SeekRel( sal_sSize nPos
)
1156 sal_Size nActualPos
= Tell();
1160 if ( SAL_MAX_SIZE
- nActualPos
> (sal_Size
)nPos
)
1165 sal_Size nAbsPos
= (sal_Size
)-nPos
;
1166 if ( nActualPos
>= nAbsPos
)
1167 nActualPos
-= nAbsPos
;
1170 pBufPos
= pRWBuf
+ nActualPos
;
1171 return Seek( nActualPos
);
1174 /*************************************************************************
1176 |* Stream::operator>>()
1178 |* Beschreibung STREAM.SDW
1179 |* Ersterstellung OV 08.06.94
1180 |* Letzte Aenderung OV 08.06.94
1182 *************************************************************************/
1184 SvStream
& SvStream::operator >> ( sal_uInt16
& r
)
1186 READNUMBER_WITHOUT_SWAP(sal_uInt16
,r
)
1192 SvStream
& SvStream::operator>> ( sal_uInt32
& r
)
1194 READNUMBER_WITHOUT_SWAP(sal_uInt32
,r
)
1200 SvStream
& SvStream::operator >> ( long& r
)
1202 #if(SAL_TYPES_SIZEOFLONG != 4)
1207 READNUMBER_WITHOUT_SWAP(long,r
)
1214 SvStream
& SvStream::operator >> ( short& r
)
1216 READNUMBER_WITHOUT_SWAP(short,r
)
1222 SvStream
& SvStream::operator >> ( int& r
)
1224 READNUMBER_WITHOUT_SWAP(int,r
)
1230 SvStream
& SvStream::operator>>( signed char& r
)
1232 if( (eIOMode
== STREAM_IO_READ
|| !bIsConsistent
) &&
1233 sizeof(signed char) <= nBufFree
)
1236 nBufActualPos
+= sizeof(signed char);
1237 pBufPos
+= sizeof(signed char);
1238 nBufFree
-= sizeof(signed char);
1241 Read( (char*)&r
, sizeof(signed char) );
1245 // Sonderbehandlung fuer Chars wegen PutBack
1247 SvStream
& SvStream::operator>>( char& r
)
1249 if( (eIOMode
== STREAM_IO_READ
|| !bIsConsistent
) &&
1250 sizeof(char) <= nBufFree
)
1253 nBufActualPos
+= sizeof(char);
1254 pBufPos
+= sizeof(char);
1255 nBufFree
-= sizeof(char);
1258 Read( (char*)&r
, sizeof(char) );
1262 SvStream
& SvStream::operator>>( unsigned char& r
)
1264 if( (eIOMode
== STREAM_IO_READ
|| !bIsConsistent
) &&
1265 sizeof(char) <= nBufFree
)
1268 nBufActualPos
+= sizeof(char);
1269 pBufPos
+= sizeof(char);
1270 nBufFree
-= sizeof(char);
1273 Read( (char*)&r
, sizeof(char) );
1277 SvStream
& SvStream::operator>>( float& r
)
1279 // Read( (char*)&r, sizeof(float) );
1280 READNUMBER_WITHOUT_SWAP(float,r
)
1288 SvStream
& SvStream::operator>>( double& r
)
1290 // Read( (char*)&r, sizeof(double) );
1291 READNUMBER_WITHOUT_SWAP(double,r
)
1299 SvStream
& SvStream::operator>> ( SvStream
& rStream
)
1301 const sal_uInt32 cBufLen
= 0x8000;
1302 char* pBuf
= new char[ cBufLen
];
1306 nCount
= Read( pBuf
, cBufLen
);
1307 rStream
.Write( pBuf
, nCount
);
1308 } while( nCount
== cBufLen
);
1314 /*************************************************************************
1316 |* Stream::operator<<()
1318 |* Beschreibung STREAM.SDW
1319 |* Ersterstellung OV 08.06.94
1320 |* Letzte Aenderung OV 08.06.94
1322 *************************************************************************/
1324 SvStream
& SvStream::operator<< ( sal_uInt16 v
)
1328 WRITENUMBER_WITHOUT_SWAP(sal_uInt16
,v
)
1332 SvStream
& SvStream::operator<< ( sal_uInt32 v
)
1336 WRITENUMBER_WITHOUT_SWAP(sal_uInt32
,v
)
1340 SvStream
& SvStream::operator<< ( long v
)
1342 #if(SAL_TYPES_SIZEOFLONG != 4)
1348 WRITENUMBER_WITHOUT_SWAP(long,v
)
1353 SvStream
& SvStream::operator<< ( short v
)
1357 WRITENUMBER_WITHOUT_SWAP(short,v
)
1361 SvStream
& SvStream::operator<<( int v
)
1365 WRITENUMBER_WITHOUT_SWAP(int,v
)
1369 SvStream
& SvStream::operator<< ( signed char v
)
1373 if(tmp
== STREAM_IO_WRITE
&& sizeof(signed char) <= nBufFree
)
1376 pBufPos
++; // sizeof(char);
1378 if( nBufActualPos
> nBufActualLen
) // Append ?
1379 nBufActualLen
= nBufActualPos
;
1380 nBufFree
--; // = sizeof(char);
1384 Write( (char*)&v
, sizeof(signed char) );
1388 // Sonderbehandlung fuer chars wegen PutBack
1390 SvStream
& SvStream::operator<< ( char v
)
1394 if(tmp
== STREAM_IO_WRITE
&& sizeof(char) <= nBufFree
)
1397 pBufPos
++; // sizeof(char);
1399 if( nBufActualPos
> nBufActualLen
) // Append ?
1400 nBufActualLen
= nBufActualPos
;
1401 nBufFree
--; // = sizeof(char);
1405 Write( (char*)&v
, sizeof(char) );
1409 SvStream
& SvStream::operator<< ( unsigned char v
)
1413 if(tmp
== STREAM_IO_WRITE
&& sizeof(char) <= nBufFree
)
1415 *(unsigned char*)pBufPos
= v
;
1416 pBufPos
++; // = sizeof(char);
1417 nBufActualPos
++; // = sizeof(char);
1418 if( nBufActualPos
> nBufActualLen
) // Append ?
1419 nBufActualLen
= nBufActualPos
;
1424 Write( (char*)&v
, sizeof(char) );
1428 SvStream
& SvStream::operator<< ( float v
)
1434 WRITENUMBER_WITHOUT_SWAP(float,v
)
1438 SvStream
& SvStream::operator<< ( const double& r
)
1440 // Write( (char*)&r, sizeof( double ) );
1446 WRITENUMBER_WITHOUT_SWAP(double,nHelp
)
1451 WRITENUMBER_WITHOUT_SWAP(double,r
)
1456 SvStream
& SvStream::operator<< ( const char* pBuf
)
1458 Write( pBuf
, strlen( pBuf
) );
1462 SvStream
& SvStream::operator<< ( const unsigned char* pBuf
)
1464 Write( (char*)pBuf
, strlen( (char*)pBuf
) );
1468 SvStream
& SvStream::operator<< ( SvStream
& rStream
)
1470 const sal_uInt32 cBufLen
= 0x8000;
1471 char* pBuf
= new char[ cBufLen
];
1474 nCount
= rStream
.Read( pBuf
, cBufLen
);
1475 Write( pBuf
, nCount
);
1476 } while( nCount
== cBufLen
);
1482 // -----------------------------------------------------------------------
1484 SvStream
& SvStream::ReadByteString( UniString
& rStr
, rtl_TextEncoding eSrcCharSet
)
1486 // read UTF-16 string directly from stream ?
1487 if (eSrcCharSet
== RTL_TEXTENCODING_UNICODE
)
1493 if (nLen
> STRING_MAXLEN
) {
1494 SetError(SVSTREAM_GENERALERROR
);
1497 sal_Unicode
*pStr
= rStr
.AllocBuffer(
1498 static_cast< xub_StrLen
>(nLen
));
1499 BOOST_STATIC_ASSERT(STRING_MAXLEN
<= SAL_MAX_SIZE
/ 2);
1500 Read( pStr
, nLen
<< 1 );
1503 for (sal_Unicode
*pEnd
= pStr
+ nLen
; pStr
< pEnd
; pStr
++)
1513 ReadByteString( aStr
);
1514 rStr
= UniString( aStr
, eSrcCharSet
);
1518 // -----------------------------------------------------------------------
1520 SvStream
& SvStream::ReadByteString( ByteString
& rStr
)
1522 sal_uInt16 nLen
= 0;
1526 char* pTmp
= rStr
.AllocBuffer( nLen
);
1527 nLen
= (sal_uInt16
)Read( pTmp
, nLen
);
1534 // -----------------------------------------------------------------------
1536 SvStream
& SvStream::WriteByteString( const UniString
& rStr
, rtl_TextEncoding eDestCharSet
)
1538 // write UTF-16 string directly into stream ?
1539 if (eDestCharSet
== RTL_TEXTENCODING_UNICODE
)
1541 sal_uInt32 nLen
= rStr
.Len();
1547 const sal_Unicode
*pStr
= rStr
.GetBuffer();
1548 const sal_Unicode
*pEnd
= pStr
+ nLen
;
1550 for (; pStr
< pEnd
; pStr
++)
1552 sal_Unicode c
= *pStr
;
1554 WRITENUMBER_WITHOUT_SWAP(sal_uInt16
,c
)
1558 Write( rStr
.GetBuffer(), nLen
<< 1 );
1564 return WriteByteString(ByteString( rStr
, eDestCharSet
));
1567 // -----------------------------------------------------------------------
1569 SvStream
& SvStream::WriteByteString( const ByteString
& rStr
)
1571 sal_uInt16 nLen
= rStr
.Len();
1572 operator<< ( nLen
);
1574 Write( rStr
.GetBuffer(), nLen
);
1578 /*************************************************************************
1582 |* Beschreibung STREAM.SDW
1583 |* Ersterstellung OV 08.06.94
1584 |* Letzte Aenderung OV 08.06.94
1586 *************************************************************************/
1588 sal_Size
SvStream::Read( void* pData
, sal_Size nCount
)
1590 sal_Size nSaveCount
= nCount
;
1591 if( !bIsConsistent
)
1596 nCount
= GetData( (char*)pData
,nCount
);
1598 EncryptBuffer(pData
, nCount
);
1599 nBufFilePos
+= nCount
;
1603 // ist Block komplett im Puffer
1604 eIOMode
= STREAM_IO_READ
;
1605 if( nCount
<= (sal_Size
)(nBufActualLen
- nBufActualPos
) )
1608 memcpy(pData
, pBufPos
, (size_t) nCount
);
1609 nBufActualPos
= nBufActualPos
+ (sal_uInt16
)nCount
;
1611 nBufFree
= nBufFree
- (sal_uInt16
)nCount
;
1615 if( bIsDirty
) // Flushen ?
1617 SeekPos( nBufFilePos
);
1619 CryptAndWriteBuffer(pRWBuf
, nBufActualLen
);
1621 PutData( pRWBuf
, nBufActualLen
);
1625 // passt der Datenblock in den Puffer ?
1626 if( nCount
> nBufSize
)
1628 // Nein! Deshalb ohne Umweg ueber den Puffer direkt
1629 // in den Zielbereich einlesen
1631 eIOMode
= STREAM_IO_DONTKNOW
;
1633 SeekPos( nBufFilePos
+ nBufActualPos
);
1636 nCount
= GetData( (char*)pData
, nCount
);
1638 EncryptBuffer(pData
, nCount
);
1639 nBufFilePos
+= nCount
;
1640 nBufFilePos
+= nBufActualPos
;
1645 // Der Datenblock passt komplett in den Puffer. Deshalb
1646 // Puffer fuellen und dann die angeforderten Daten in den
1647 // Zielbereich kopieren.
1649 nBufFilePos
+= nBufActualPos
;
1650 SeekPos( nBufFilePos
);
1652 // TODO: Typecast vor GetData, sal_uInt16 nCountTmp
1653 sal_Size nCountTmp
= GetData( pRWBuf
, nBufSize
);
1655 EncryptBuffer(pRWBuf
, nCountTmp
);
1656 nBufActualLen
= (sal_uInt16
)nCountTmp
;
1657 if( nCount
> nCountTmp
)
1659 nCount
= nCountTmp
; // zurueckstutzen, Eof siehe unten
1661 memcpy( pData
, pRWBuf
, (size_t)nCount
);
1662 nBufActualPos
= (sal_uInt16
)nCount
;
1663 pBufPos
= pRWBuf
+ nCount
;
1668 nBufFree
= nBufActualLen
- nBufActualPos
;
1669 if( nCount
!= nSaveCount
&& nError
!= ERRCODE_IO_PENDING
)
1671 if( nCount
== nSaveCount
&& nError
== ERRCODE_IO_PENDING
)
1672 nError
= ERRCODE_NONE
;
1676 /*************************************************************************
1680 |* Beschreibung STREAM.SDW
1681 |* Ersterstellung OV 08.06.94
1682 |* Letzte Aenderung OV 08.06.94
1684 *************************************************************************/
1686 sal_Size
SvStream::Write( const void* pData
, sal_Size nCount
)
1692 SetError( ERRCODE_IO_CANTWRITE
);
1695 if( !bIsConsistent
)
1696 RefreshBuffer(); // Aenderungen des Puffers durch PutBack loeschen
1701 nCount
= CryptAndWriteBuffer( pData
, nCount
);
1703 nCount
= PutData( (char*)pData
, nCount
);
1704 nBufFilePos
+= nCount
;
1708 eIOMode
= STREAM_IO_WRITE
;
1709 if( nCount
<= (sal_Size
)(nBufSize
- nBufActualPos
) )
1711 memcpy( pBufPos
, pData
, (size_t)nCount
);
1712 nBufActualPos
= nBufActualPos
+ (sal_uInt16
)nCount
;
1713 // wurde der Puffer erweitert ?
1714 if( nBufActualPos
> nBufActualLen
)
1715 nBufActualLen
= nBufActualPos
;
1725 SeekPos( nBufFilePos
);
1727 CryptAndWriteBuffer( pRWBuf
, (sal_Size
)nBufActualLen
);
1729 PutData( pRWBuf
, nBufActualLen
);
1733 // passt der Block in den Puffer ?
1734 if( nCount
> nBufSize
)
1736 eIOMode
= STREAM_IO_DONTKNOW
;
1737 nBufFilePos
+= nBufActualPos
;
1741 SeekPos( nBufFilePos
);
1743 nCount
= CryptAndWriteBuffer( pData
, nCount
);
1745 nCount
= PutData( (char*)pData
, nCount
);
1746 nBufFilePos
+= nCount
;
1750 // Block in Puffer stellen
1751 memcpy( pRWBuf
, pData
, (size_t)nCount
);
1754 nBufFilePos
+= nBufActualPos
;
1755 nBufActualPos
= (sal_uInt16
)nCount
;
1756 pBufPos
= pRWBuf
+ nCount
;
1757 nBufActualLen
= (sal_uInt16
)nCount
;
1761 nBufFree
= nBufSize
- nBufActualPos
;
1766 /*************************************************************************
1770 |* Beschreibung STREAM.SDW
1771 |* Ersterstellung OV 08.06.94
1772 |* Letzte Aenderung OV 08.06.94
1774 *************************************************************************/
1776 sal_Size
SvStream::Seek( sal_Size nFilePos
)
1778 eIOMode
= STREAM_IO_DONTKNOW
;
1783 nBufFilePos
= SeekPos( nFilePos
);
1784 DBG_ASSERT(Tell()==nBufFilePos
,"Out Of Sync!");
1788 // Ist Position im Puffer ?
1789 if( nFilePos
>= nBufFilePos
&& nFilePos
<= (nBufFilePos
+ nBufActualLen
))
1791 nBufActualPos
= (sal_uInt16
)(nFilePos
- nBufFilePos
);
1792 pBufPos
= pRWBuf
+ nBufActualPos
;
1793 // nBufFree korrigieren, damit wir nicht von einem
1794 // PutBack (ignoriert den StreamMode) getoetet werden
1795 nBufFree
= nBufActualLen
- nBufActualPos
;
1799 if( bIsDirty
&& bIsConsistent
)
1801 SeekPos( nBufFilePos
);
1803 CryptAndWriteBuffer( pRWBuf
, nBufActualLen
);
1805 PutData( pRWBuf
, nBufActualLen
);
1811 nBufFilePos
= SeekPos( nFilePos
);
1815 sal_Size nDebugTemp
= nBufFilePos
+ nBufActualPos
;
1816 DBG_ASSERT(Tell()==nDebugTemp
,"Sync?");
1819 return nBufFilePos
+ nBufActualPos
;
1822 /*************************************************************************
1826 |* Beschreibung STREAM.SDW
1827 |* Ersterstellung OV 08.06.94
1828 |* Letzte Aenderung OV 08.06.94
1830 *************************************************************************/
1832 void SvStream::Flush()
1834 if( bIsDirty
&& bIsConsistent
)
1836 SeekPos( nBufFilePos
);
1838 CryptAndWriteBuffer( pRWBuf
, (sal_Size
)nBufActualLen
);
1840 if( PutData( pRWBuf
, nBufActualLen
) != nBufActualLen
)
1841 SetError( SVSTREAM_WRITE_ERROR
);
1849 /*************************************************************************
1851 |* Stream::PutBack()
1853 |* Beschreibung STREAM.SDW
1854 |* Ersterstellung OV 01.08.94
1855 |* Letzte Aenderung OV 01.08.94
1857 *************************************************************************/
1862 1. Datenzeiger steht mitten im Puffer (nBufActualPos >= 1)
1863 2. Datenzeiger auf Position 0, Puffer ist voll
1864 3. Datenzeiger auf Position 0, Puffer ist teilweise gefuellt
1865 4. Datenzeiger auf Position 0, Puffer ist leer -> Fehler!
1868 SvStream
& SvStream::PutBack( char aCh
)
1870 // wenn kein Buffer oder Zurueckscrollen nicht moeglich -> Fehler
1871 if( !pRWBuf
|| !nBufActualLen
|| ( !nBufActualPos
&& !nBufFilePos
) )
1874 SetError( SVSTREAM_GENERALERROR
);
1878 // Flush() (Phys. Flushen aber nicht notwendig, deshalb selbst schreiben)
1879 if( bIsConsistent
&& bIsDirty
)
1881 SeekPos( nBufFilePos
);
1883 CryptAndWriteBuffer( pRWBuf
, nBufActualLen
);
1885 PutData( pRWBuf
, nBufActualLen
);
1888 bIsConsistent
= FALSE
; // Puffer enthaelt jetzt TRASH
1897 else // Puffer muss verschoben werden
1899 // Ist Puffer am Anschlag ?
1900 if( nBufSize
== nBufActualLen
)
1903 memmove( pRWBuf
+1, pRWBuf
, nBufSize
-1 );
1904 // nBufFree behaelt den Wert!
1908 // 3. Fall -> Puffer vergroessern
1909 memmove( pRWBuf
+1, pRWBuf
, (sal_uInt16
)nBufActualLen
);
1916 eIOMode
= STREAM_IO_DONTKNOW
;
1921 /*************************************************************************
1923 |* Stream::EatWhite()
1925 |* Beschreibung STREAM.SDW
1926 |* Ersterstellung OV 01.08.94
1927 |* Letzte Aenderung OV 01.08.94
1929 *************************************************************************/
1931 void SvStream::EatWhite()
1934 Read(&aCh
, sizeof(char) );
1935 while( !bIsEof
&& isspace((int)aCh
) ) //( aCh == ' ' || aCh == '\t' ) )
1936 Read(&aCh
, sizeof(char) );
1937 if( !bIsEof
) // konnte das letzte Char gelesen werden ?
1941 /*************************************************************************
1943 |* Stream::RefreshBuffer()
1945 |* Beschreibung STREAM.SDW
1946 |* Ersterstellung OV 01.08.94
1947 |* Letzte Aenderung OV 01.08.94
1949 *************************************************************************/
1951 void SvStream::RefreshBuffer()
1953 if( bIsDirty
&& bIsConsistent
)
1955 SeekPos( nBufFilePos
);
1957 CryptAndWriteBuffer( pRWBuf
, (sal_Size
)nBufActualLen
);
1959 PutData( pRWBuf
, nBufActualLen
);
1962 SeekPos( nBufFilePos
);
1963 nBufActualLen
= (sal_uInt16
)GetData( pRWBuf
, nBufSize
);
1964 if( nBufActualLen
&& nError
== ERRCODE_IO_PENDING
)
1965 nError
= ERRCODE_NONE
;
1967 EncryptBuffer(pRWBuf
, (sal_Size
)nBufActualLen
);
1968 bIsConsistent
= TRUE
;
1969 eIOMode
= STREAM_IO_DONTKNOW
;
1973 /*************************************************************************
1975 |* Stream::CreateFormatString()
1977 |* Beschreibung Baut Formatstring zusammen
1978 |* Ersterstellung OV 08.06.94
1979 |* Letzte Aenderung OV 08.06.94
1981 *************************************************************************/
1983 void SvStream::CreateFormatString()
1985 aFormatString
= '%';
1986 nPrintfParams
= SPECIAL_PARAM_NONE
;
1988 if( nJustification
)
1990 aFormatString
+= '-';
1995 if( cFiller
!= ' ' )
1996 aFormatString
+= '0';
1997 aFormatString
+= '*';
1998 nPrintfParams
= SPECIAL_PARAM_WIDTH
;
2003 aFormatString
+= ".*";
2005 nPrintfParams
= SPECIAL_PARAM_BOTH
;
2007 nPrintfParams
= SPECIAL_PARAM_PRECISION
;
2011 /*************************************************************************
2013 |* Stream::ReadNumber()
2015 |* Beschreibung STREAM.SDW
2016 |* Ersterstellung OV 08.06.94
2017 |* Letzte Aenderung OV 08.06.94
2019 *************************************************************************/
2021 #define BUFSIZE_LONG 21 // log( 2 hoch 64 ) + 1
2023 SvStream
& SvStream::ReadNumber( long& rLong
)
2026 if( bIsEof
|| nError
)
2028 SetError( SVSTREAM_GENERALERROR
);
2031 sal_Size nFPtr
= Tell();
2032 char buf
[ BUFSIZE_LONG
];
2033 memset( buf
, 0, BUFSIZE_LONG
);
2034 sal_Size nTemp
= Read( buf
, BUFSIZE_LONG
-1 );
2035 if( !nTemp
|| nError
)
2037 SetError( SVSTREAM_GENERALERROR
);
2041 rLong
= strtol( buf
, &pEndPtr
, (int)nRadix
);
2042 nFPtr
+= ( (sal_Size
)pEndPtr
- (sal_Size
)(&(buf
[0])) );
2048 SvStream
& SvStream::ReadNumber( sal_uInt32
& rUInt32
)
2051 if( bIsEof
|| nError
)
2053 SetError( SVSTREAM_GENERALERROR
);
2056 sal_Size nFPtr
= Tell();
2057 char buf
[ BUFSIZE_LONG
];
2058 memset( buf
, 0, BUFSIZE_LONG
);
2059 sal_Size nTemp
= Read( buf
, BUFSIZE_LONG
-1 );
2060 if( !nTemp
|| nError
)
2062 SetError( SVSTREAM_GENERALERROR
);
2066 rUInt32
= strtoul( buf
, &pEndPtr
, (int)nRadix
);
2067 nFPtr
+= ( (sal_uIntPtr
)pEndPtr
- (sal_uIntPtr
)buf
);
2073 SvStream
& SvStream::ReadNumber( double& rDouble
)
2076 if( bIsEof
|| nError
)
2078 SetError( SVSTREAM_GENERALERROR
);
2081 sal_Size nFPtr
= Tell();
2082 char buf
[ BUFSIZE_LONG
];
2083 memset( buf
, 0, BUFSIZE_LONG
);
2084 sal_Size nTemp
= Read( buf
, BUFSIZE_LONG
-1 );
2085 if( !nTemp
|| nError
)
2087 SetError( SVSTREAM_GENERALERROR
);
2091 rDouble
= strtod( buf
, &pEndPtr
);
2092 nFPtr
+= ( (sal_Size
)pEndPtr
- (sal_Size
)buf
);
2099 /*************************************************************************
2101 |* Stream::WriteNumber()
2103 |* Beschreibung STREAM.SDW
2104 |* Ersterstellung OV 08.06.94
2105 |* Letzte Aenderung OV 08.06.94
2107 *************************************************************************/
2109 SvStream
& SvStream::WriteNumber( long nLong
)
2111 char buffer
[256+12];
2112 char pType
[] = "ld"; // Nicht static!
2115 else if( nRadix
== 8 )
2117 ByteString
aFStr( aFormatString
);
2120 switch ( nPrintfParams
)
2122 case SPECIAL_PARAM_NONE
:
2123 nLen
= sprintf( buffer
, aFStr
.GetBuffer(), nLong
);
2125 case SPECIAL_PARAM_WIDTH
:
2126 nLen
= sprintf( buffer
, aFStr
.GetBuffer(), nWidth
, nLong
);
2128 case SPECIAL_PARAM_PRECISION
:
2129 nLen
= sprintf( buffer
, aFStr
.GetBuffer(), nPrecision
,nLong
);
2132 nLen
=sprintf(buffer
, aFStr
.GetBuffer(),nWidth
,nPrecision
,nLong
);
2134 Write( buffer
, (long)nLen
);
2138 SvStream
& SvStream::WriteNumber( sal_uInt32 nUInt32
)
2140 char buffer
[256+12];
2141 char pType
[] = "lu"; // Nicht static!
2144 else if( nRadix
== 8 )
2146 ByteString
aFStr( aFormatString
);
2149 switch ( nPrintfParams
)
2151 case SPECIAL_PARAM_NONE
:
2152 nLen
= sprintf( buffer
, aFStr
.GetBuffer(), nUInt32
);
2154 case SPECIAL_PARAM_WIDTH
:
2155 nLen
= sprintf( buffer
, aFStr
.GetBuffer(), nWidth
, nUInt32
);
2157 case SPECIAL_PARAM_PRECISION
:
2158 nLen
= sprintf( buffer
, aFStr
.GetBuffer(), nPrecision
, nUInt32
);
2161 nLen
=sprintf(buffer
,aFStr
.GetBuffer(),nWidth
,nPrecision
,nUInt32
);
2163 Write( buffer
, (long)nLen
);
2168 SvStream
& SvStream::WriteNumber( const double& rDouble
)
2170 char buffer
[256+24];
2171 ByteString
aFStr( aFormatString
);
2174 switch ( nPrintfParams
)
2176 case SPECIAL_PARAM_NONE
:
2177 nLen
= sprintf( buffer
, aFStr
.GetBuffer(), rDouble
);
2179 case SPECIAL_PARAM_WIDTH
:
2180 nLen
= sprintf( buffer
, aFStr
.GetBuffer(), nWidth
, rDouble
);
2182 case SPECIAL_PARAM_PRECISION
:
2183 nLen
= sprintf( buffer
, aFStr
.GetBuffer(), nPrecision
, rDouble
);
2186 nLen
=sprintf(buffer
, aFStr
.GetBuffer(),nWidth
,nPrecision
,rDouble
);
2188 Write( buffer
, (long)nLen
);
2192 /*************************************************************************
2194 |* Stream::CryptAndWriteBuffer()
2196 |* Beschreibung Verschluesseln und Schreiben
2197 |* Ersterstellung OV 08.06.94
2198 |* Letzte Aenderung OV 08.06.94
2200 *************************************************************************/
2202 #define CRYPT_BUFSIZE 1024
2204 sal_Size
SvStream::CryptAndWriteBuffer( const void* pStart
, sal_Size nLen
)
2206 unsigned char pTemp
[CRYPT_BUFSIZE
];
2207 unsigned char* pDataPtr
= (unsigned char*)pStart
;
2208 sal_Size nCount
= 0;
2210 unsigned char nMask
= nCryptMask
;
2213 if( nLen
>= CRYPT_BUFSIZE
)
2214 nBufCount
= CRYPT_BUFSIZE
;
2218 memcpy( pTemp
, pDataPtr
, (sal_uInt16
)nBufCount
);
2219 // **** Verschluesseln *****
2220 for ( sal_uInt16 n
=0; n
< CRYPT_BUFSIZE
; n
++ )
2222 unsigned char aCh
= pTemp
[n
];
2227 // *************************
2228 nCount
+= PutData( (char*)pTemp
, nBufCount
);
2229 pDataPtr
+= nBufCount
;
2235 /*************************************************************************
2237 |* Stream::EncryptBuffer()
2239 |* Beschreibung Buffer entschluesseln
2240 |* Ersterstellung OV 08.06.94
2241 |* Letzte Aenderung OV 08.06.94
2243 *************************************************************************/
2245 sal_Bool
SvStream::EncryptBuffer(void* pStart
, sal_Size nLen
)
2247 unsigned char* pTemp
= (unsigned char*)pStart
;
2248 unsigned char nMask
= nCryptMask
;
2250 for ( sal_Size n
=0; n
< nLen
; n
++, pTemp
++ )
2252 unsigned char aCh
= *pTemp
;
2260 /*************************************************************************
2264 |* Beschreibung STREAM.SDW
2265 |* Ersterstellung OV 08.06.94
2266 |* Letzte Aenderung OV 08.06.94
2268 *************************************************************************/
2270 unsigned char implGetCryptMask(const sal_Char
* pStr
, sal_Int32 nLen
, long nVersion
)
2272 unsigned char nCryptMask
= 0;
2277 if( nVersion
<= SOFFICE_FILEFORMAT_31
)
2281 nCryptMask
^= *pStr
;
2286 else // BugFix #25888#
2288 for( sal_uInt16 i
= 0; i
< nLen
; i
++ ) {
2289 nCryptMask
^= pStr
[i
];
2290 if( nCryptMask
& 0x80 ) {
2305 void SvStream::SetKey( const ByteString
& rKey
)
2308 nCryptMask
= implGetCryptMask( aKey
.GetBuffer(), aKey
.Len(), GetVersion() );
2311 /*************************************************************************
2313 |* Stream::SyncSvStream()
2315 |* Beschreibung STREAM.SDW
2316 |* Ersterstellung OV 08.06.94
2317 |* Letzte Aenderung OV 08.06.94
2319 *************************************************************************/
2321 void SvStream::SyncSvStream( sal_Size nNewStreamPos
)
2324 SvStream::nBufFilePos
= nNewStreamPos
;
2327 /*************************************************************************
2329 |* Stream::SyncSysStream()
2331 |* Beschreibung STREAM.SDW
2332 |* Ersterstellung OV 08.06.94
2333 |* Letzte Aenderung OV 08.06.94
2335 *************************************************************************/
2337 void SvStream::SyncSysStream()
2343 /*************************************************************************
2345 |* Stream::SetStreamSize()
2347 |* Beschreibung STREAM.SDW
2348 |* Ersterstellung OV 08.06.94
2349 |* Letzte Aenderung OV 08.06.94
2351 *************************************************************************/
2353 sal_Bool
SvStream::SetStreamSize( sal_Size nSize
)
2356 sal_Size nFPos
= Tell();
2358 sal_uInt16 nBuf
= nBufSize
;
2361 SetBufferSize( nBuf
);
2362 DBG_ASSERT(Tell()==nFPos
,"SetStreamSize failed");
2363 return (sal_Bool
)(nError
== 0);
2366 //============================================================================
2368 void SvStream::AddMark( sal_Size
)
2372 //============================================================================
2374 void SvStream::RemoveMark( sal_Size
)
2378 /*************************************************************************
2382 |* Beschreibung STREAM.SDW
2383 |* Ersterstellung OV 08.06.94
2384 |* Letzte Aenderung TH 13.11.96
2386 *************************************************************************/
2388 SvStream
& endl( SvStream
& rStr
)
2390 LineEnd eDelim
= rStr
.GetLineDelimiter();
2391 if ( eDelim
== LINEEND_CR
)
2393 else if( eDelim
== LINEEND_LF
)
2400 SvStream
& endlu( SvStream
& rStrm
)
2402 switch ( rStrm
.GetLineDelimiter() )
2405 rStrm
<< sal_Unicode(_CR
);
2408 rStrm
<< sal_Unicode(_LF
);
2411 rStrm
<< sal_Unicode(_CR
) << sal_Unicode(_LF
);
2416 SvStream
& endlub( SvStream
& rStrm
)
2418 if ( rStrm
.GetStreamCharSet() == RTL_TEXTENCODING_UNICODE
)
2419 return endlu( rStrm
);
2421 return endl( rStrm
);
2424 /*************************************************************************
2426 |* SvMemoryStream::SvMemoryStream()
2428 |* Beschreibung STREAM.SDW
2429 |* Ersterstellung OV 20.06.94
2430 |* Letzte Aenderung OV 20.06.94
2432 *************************************************************************/
2434 SvMemoryStream::SvMemoryStream( void* pBuffer
, sal_Size bufSize
,
2437 if( eMode
& STREAM_WRITE
)
2440 bIsWritable
= FALSE
;
2441 nEndOfData
= bufSize
;
2443 pBuf
= (BYTE
*) pBuffer
;
2450 /*************************************************************************
2452 |* SvMemoryStream::SvMemoryStream()
2454 |* Beschreibung STREAM.SDW
2455 |* Ersterstellung OV 20.06.94
2456 |* Letzte Aenderung OV 20.06.94
2458 *************************************************************************/
2460 SvMemoryStream::SvMemoryStream( sal_Size nInitSize
, sal_Size nResizeOffset
)
2465 nResize
= nResizeOffset
;
2468 if( nResize
!= 0 && nResize
< 16 )
2470 if( nInitSize
&& !AllocateMemory( nInitSize
) )
2472 SetError( SVSTREAM_OUTOFMEMORY
);
2477 SetBufferSize( 64 );
2480 /*************************************************************************
2482 |* SvMemoryStream::~SvMemoryStream()
2484 |* Beschreibung STREAM.SDW
2485 |* Ersterstellung OV 20.06.94
2486 |* Letzte Aenderung OV 20.06.94
2488 *************************************************************************/
2490 SvMemoryStream::~SvMemoryStream()
2501 /*************************************************************************
2503 |* SvMemoryStream::IsA()
2505 |* Beschreibung STREAM.SDW
2506 |* Ersterstellung OV 20.06.94
2507 |* Letzte Aenderung OV 20.06.94
2509 *************************************************************************/
2511 sal_uInt16
SvMemoryStream::IsA() const
2513 return (sal_uInt16
)ID_MEMORYSTREAM
;
2516 /*************************************************************************
2518 |* SvMemoryStream::SetBuffer()
2520 |* Beschreibung STREAM.SDW
2521 |* Ersterstellung OV 20.06.94
2522 |* Letzte Aenderung OV 20.06.94
2524 *************************************************************************/
2526 void* SvMemoryStream::SetBuffer( void* pNewBuf
, sal_Size nCount
,
2527 sal_Bool bOwnsDat
, sal_Size nEOF
)
2530 SetBufferSize( 0 ); // Buffering in der Basisklasse initialisieren
2535 if( pNewBuf
!= pBuf
)
2541 pBuf
= (BYTE
*) pNewBuf
;
2545 bOwnsData
= bOwnsDat
;
2553 DBG_ASSERT( nEndOfData
<STREAM_SEEK_TO_END
,"Invalid EOF");
2557 /*************************************************************************
2559 |* SvMemoryStream::GetData()
2561 |* Beschreibung STREAM.SDW
2562 |* Ersterstellung OV 20.06.94
2563 |* Letzte Aenderung OV 20.06.94
2565 *************************************************************************/
2567 sal_Size
SvMemoryStream::GetData( void* pData
, sal_Size nCount
)
2569 sal_Size nMaxCount
= nEndOfData
-nPos
;
2570 if( nCount
> nMaxCount
)
2572 memcpy( pData
, pBuf
+nPos
, (size_t)nCount
);
2577 /*************************************************************************
2579 |* SvMemoryStream::PutData()
2581 |* Beschreibung STREAM.SDW
2582 |* Ersterstellung OV 20.06.94
2583 |* Letzte Aenderung OV 20.06.94
2585 *************************************************************************/
2587 sal_Size
SvMemoryStream::PutData( const void* pData
, sal_Size nCount
)
2592 sal_Size nMaxCount
= nSize
-nPos
;
2594 // auf Ueberlauf testen
2595 if( nCount
> nMaxCount
)
2599 // soviel wie moeglich rueberschaufeln
2601 SetError( SVSTREAM_OUTOFMEMORY
);
2606 if( nSize
&& nSize
> nResize
)
2609 nNewResize
= nResize
;
2611 if( (nCount
-nMaxCount
) < nResize
)
2613 // fehlender Speicher ist kleiner als Resize-Offset,
2614 // deshalb um Resize-Offset vergroessern
2615 if( !ReAllocateMemory( nNewResize
) )
2618 SetError( SVSTREAM_WRITE_ERROR
);
2623 // fehlender Speicher ist groesser als Resize-Offset
2624 // deshalb um Differenz+ResizeOffset vergroessern
2625 if( !ReAllocateMemory( nCount
-nMaxCount
+nNewResize
) )
2628 SetError( SVSTREAM_WRITE_ERROR
);
2633 DBG_ASSERT(pBuf
,"Possibly Reallocate failed");
2634 memcpy( pBuf
+nPos
, pData
, (size_t)nCount
);
2637 if( nPos
> nEndOfData
)
2642 /*************************************************************************
2644 |* SvMemoryStream::SeekPos()
2646 |* Beschreibung STREAM.SDW
2647 |* Ersterstellung OV 20.06.94
2648 |* Letzte Aenderung OV 20.06.94
2650 *************************************************************************/
2652 // nEndOfData: Erste Position im Stream, die nicht gelesen werden darf
2653 // nSize: Groesse des allozierten Speichers
2655 sal_Size
SvMemoryStream::SeekPos( sal_Size nNewPos
)
2657 if( nNewPos
< nEndOfData
)
2659 else if( nNewPos
== STREAM_SEEK_TO_END
)
2663 if( nNewPos
>= nSize
) // muss Buffer vergroessert werden ?
2665 if( nResize
) // ist vergroeseern erlaubt ?
2667 long nDiff
= (long)(nNewPos
- nSize
+ 1);
2668 nDiff
+= (long)nResize
;
2669 ReAllocateMemory( nDiff
);
2671 nEndOfData
= nNewPos
;
2673 else // vergroessern ist nicht erlaubt -> ans Ende setzen
2675 // SetError( SVSTREAM_OUTOFMEMORY );
2679 else // gueltigen Bereich innerhalb des Buffers vergroessern
2682 nEndOfData
= nNewPos
;
2688 /*************************************************************************
2690 |* SvMemoryStream::FlushData()
2692 |* Beschreibung STREAM.SDW
2693 |* Ersterstellung OV 20.06.94
2694 |* Letzte Aenderung OV 20.06.94
2696 *************************************************************************/
2698 void SvMemoryStream::FlushData()
2702 /*************************************************************************
2704 |* SvMemoryStream::ResetError()
2706 |* Beschreibung STREAM.SDW
2707 |* Ersterstellung OV 20.06.94
2708 |* Letzte Aenderung OV 20.06.94
2710 *************************************************************************/
2712 void SvMemoryStream::ResetError()
2714 SvStream::ClearError();
2717 /*************************************************************************
2719 |* SvMemoryStream::AllocateMemory()
2721 |* Beschreibung STREAM.SDW
2722 |* Ersterstellung OV 20.06.94
2723 |* Letzte Aenderung OV 20.06.94
2725 *************************************************************************/
2727 sal_Bool
SvMemoryStream::AllocateMemory( sal_Size nNewSize
)
2729 pBuf
= new BYTE
[nNewSize
];
2730 return( pBuf
!= 0 );
2733 /*************************************************************************
2735 |* SvMemoryStream::ReAllocateMemory() (Bozo-Algorithmus)
2737 |* Beschreibung STREAM.SDW
2738 |* Ersterstellung OV 20.06.94
2739 |* Letzte Aenderung OV 20.06.94
2741 *************************************************************************/
2743 sal_Bool
SvMemoryStream::ReAllocateMemory( long nDiff
)
2745 sal_Bool bRetVal
= FALSE
;
2746 long nTemp
= (long)nSize
;
2748 sal_Size nNewSize
= (sal_Size
)nTemp
;
2752 BYTE
* pNewBuf
= new BYTE
[nNewSize
];
2756 bRetVal
= TRUE
; // Success!
2757 if( nNewSize
< nSize
) // Verkleinern ?
2759 memcpy( pNewBuf
, pBuf
, (size_t)nNewSize
);
2760 if( nPos
> nNewSize
)
2762 if( nEndOfData
>= nNewSize
)
2763 nEndOfData
= nNewSize
-1L;
2767 memcpy( pNewBuf
, pBuf
, (size_t)nSize
);
2789 void SvMemoryStream::FreeMemory()
2794 /*************************************************************************
2796 |* SvMemoryStream::SwitchBuffer()
2798 |* Beschreibung STREAM.SDW
2799 |* Ersterstellung OV 26.07.94
2800 |* Letzte Aenderung OV 26.07.94
2802 *************************************************************************/
2804 void* SvMemoryStream::SwitchBuffer( sal_Size nInitSize
, sal_Size nResizeOffset
)
2809 Seek( STREAM_SEEK_TO_BEGIN
);
2811 void* pRetVal
= pBuf
;
2814 nResize
= nResizeOffset
;
2817 if( nResize
!= 0 && nResize
< 16 )
2822 if( nInitSize
&& !AllocateMemory(nInitSize
) )
2824 SetError( SVSTREAM_OUTOFMEMORY
);
2830 SetBufferSize( 64 );
2834 void SvMemoryStream::SetSize( sal_Size nNewSize
)
2836 long nDiff
= (long)nNewSize
- (long)nSize
;
2837 ReAllocateMemory( nDiff
);
2840 TYPEINIT0 ( SvDataCopyStream
)
2842 void SvDataCopyStream::Assign( const SvDataCopyStream
& )