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: biffoutputstream.cxx,v $
10 * $Revision: 1.3.22.1 $
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 #include "oox/xls/biffoutputstream.hxx"
36 // ============================================================================
40 BiffOutputRecordBuffer::BiffOutputRecordBuffer( BinaryOutputStream
& rOutStrm
, sal_uInt16 nMaxRecSize
) :
41 mrOutStrm( rOutStrm
),
42 mnMaxRecSize( nMaxRecSize
),
43 mnRecId( BIFF_ID_UNKNOWN
),
46 OSL_ENSURE( mrOutStrm
.isSeekable(), "BiffOutputRecordBuffer::BiffOutputRecordBuffer - stream must be seekable" );
47 maData
.reserve( SAL_MAX_UINT16
);
50 void BiffOutputRecordBuffer::startRecord( sal_uInt16 nRecId
)
52 OSL_ENSURE( !mbInRec
, "BiffOutputRecordBuffer::startRecord - another record still open" );
58 void BiffOutputRecordBuffer::endRecord()
60 OSL_ENSURE( mbInRec
, "BiffOutputRecordBuffer::endRecord - no record open" );
61 sal_uInt16 nRecSize
= getLimitedValue
< sal_uInt16
, size_t >( maData
.size(), 0, SAL_MAX_UINT16
);
62 mrOutStrm
.seekToEnd();
63 mrOutStrm
<< mnRecId
<< nRecSize
;
65 mrOutStrm
.writeMemory( &maData
.front(), nRecSize
);
69 void BiffOutputRecordBuffer::write( const void* pData
, sal_uInt16 nBytes
)
71 OSL_ENSURE( mbInRec
, "BiffOutputRecordBuffer::write - no record open" );
72 OSL_ENSURE( nBytes
> 0, "BiffOutputRecordBuffer::write - nothing to write" );
73 OSL_ENSURE( nBytes
<= getRecLeft(), "BiffOutputRecordBuffer::write - buffer overflow" );
74 maData
.resize( maData
.size() + nBytes
);
75 memcpy( &*(maData
.end() - nBytes
), pData
, nBytes
);
78 void BiffOutputRecordBuffer::fill( sal_uInt8 nValue
, sal_uInt16 nBytes
)
80 OSL_ENSURE( mbInRec
, "BiffOutputRecordBuffer::write - no record open" );
81 OSL_ENSURE( nBytes
> 0, "BiffOutputRecordBuffer::write - nothing to write" );
82 OSL_ENSURE( nBytes
<= getRecLeft(), "BiffOutputRecordBuffer::write - buffer overflow" );
83 maData
.resize( maData
.size() + nBytes
, nValue
);
88 // ============================================================================
90 BiffOutputStream::BiffOutputStream( BinaryOutputStream
& rOutStream
, sal_uInt16 nMaxRecSize
) :
91 maRecBuffer( rOutStream
, nMaxRecSize
),
97 // record control -------------------------------------------------------------
99 void BiffOutputStream::startRecord( sal_uInt16 nRecId
)
101 maRecBuffer
.startRecord( nRecId
);
105 void BiffOutputStream::endRecord()
108 maRecBuffer
.endRecord();
111 void BiffOutputStream::setPortionSize( sal_uInt16 nSize
)
113 OSL_ENSURE( mnPortionPos
== 0, "BiffOutputStream::setPortionSize - block operation inside portion" );
114 mnPortionSize
= nSize
;
118 // BinaryStreamBase interface (seeking) ---------------------------------------
120 sal_Int64
BiffOutputStream::tellBase() const
122 return maRecBuffer
.getBaseStream().tell();
125 sal_Int64
BiffOutputStream::getBaseLength() const
127 return maRecBuffer
.getBaseStream().getLength();
130 // BinaryOutputStream interface (stream write access) -------------------------
132 void BiffOutputStream::writeData( const StreamDataSequence
& rData
)
134 if( rData
.hasElements() )
135 writeMemory( rData
.getConstArray(), rData
.getLength() );
138 void BiffOutputStream::writeMemory( const void* pMem
, sal_Int32 nBytes
)
140 if( pMem
&& (nBytes
> 0) )
142 const sal_uInt8
* pnBuffer
= reinterpret_cast< const sal_uInt8
* >( pMem
);
143 sal_Int32 nBytesLeft
= nBytes
;
144 while( nBytesLeft
> 0 )
146 sal_uInt16 nBlockSize
= prepareRawBlock( nBytesLeft
);
147 maRecBuffer
.write( pnBuffer
, nBlockSize
);
148 pnBuffer
+= nBlockSize
;
149 nBytesLeft
-= nBlockSize
;
154 void BiffOutputStream::fill( sal_uInt8 nValue
, sal_Int32 nBytes
)
156 sal_Int32 nBytesLeft
= nBytes
;
157 while( nBytesLeft
> 0 )
159 sal_uInt16 nBlockSize
= prepareRawBlock( nBytesLeft
);
160 maRecBuffer
.fill( nValue
, nBlockSize
);
161 nBytesLeft
-= nBlockSize
;
165 void BiffOutputStream::writeBlock( const void* pMem
, sal_uInt16 nBytes
)
167 ensureRawBlock( nBytes
);
168 maRecBuffer
.write( pMem
, nBytes
);
171 // private --------------------------------------------------------------------
173 void BiffOutputStream::writeAtom( const void* pMem
, sal_uInt8 nSize
)
175 // byte swapping is done in calling BinaryOutputStream::writeValue() template function
176 writeBlock( pMem
, nSize
);
179 void BiffOutputStream::ensureRawBlock( sal_uInt16 nSize
)
181 if( (maRecBuffer
.getRecLeft() < nSize
) ||
182 ((mnPortionSize
> 0) && (mnPortionPos
== 0) && (maRecBuffer
.getRecLeft() < mnPortionSize
)) )
184 maRecBuffer
.endRecord();
185 maRecBuffer
.startRecord( BIFF_ID_CONT
);
187 if( mnPortionSize
> 0 )
189 OSL_ENSURE( mnPortionPos
+ nSize
<= mnPortionSize
, "BiffOutputStream::ensureRawBlock - portion overflow" );
190 mnPortionPos
= (mnPortionPos
+ nSize
) % mnPortionSize
; // prevent compiler warning, do not use operator+=, operator%=
194 sal_uInt16
BiffOutputStream::prepareRawBlock( sal_Int32 nTotalSize
)
196 sal_uInt16 nRecLeft
= maRecBuffer
.getRecLeft();
197 if( mnPortionSize
> 0 )
199 OSL_ENSURE( mnPortionPos
== 0, "BiffOutputStream::prepareRawBlock - block operation inside portion" );
200 OSL_ENSURE( nTotalSize
% mnPortionSize
== 0, "BiffOutputStream::prepareRawBlock - portion size does not match block size" );
201 nRecLeft
= (nRecLeft
/ mnPortionSize
) * mnPortionSize
;
203 sal_uInt16 nSize
= getLimitedValue
< sal_uInt16
, sal_Int32
>( nTotalSize
, 0, nRecLeft
);
204 ensureRawBlock( nSize
);
207 // ============================================================================