LCOV - code coverage report
Current view: top level - libreoffice/oox/source/helper - binaryoutputstream.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 48 65 73.8 %
Date: 2012-12-27 Functions: 8 15 53.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "oox/helper/binaryoutputstream.hxx"
      21             : 
      22             : #include <com/sun/star/io/XOutputStream.hpp>
      23             : #include <com/sun/star/io/XSeekable.hpp>
      24             : #include <osl/diagnose.h>
      25             : #include <string.h>
      26             : 
      27             : namespace oox {
      28             : 
      29             : // ============================================================================
      30             : 
      31             : using namespace ::com::sun::star::io;
      32             : using namespace ::com::sun::star::uno;
      33             : 
      34             : namespace {
      35             : 
      36             : const sal_Int32 OUTPUTSTREAM_BUFFERSIZE     = 0x8000;
      37             : 
      38             : } // namespace
      39             : 
      40             : // ============================================================================
      41             : 
      42         152 : BinaryXOutputStream::BinaryXOutputStream( const Reference< XOutputStream >& rxOutStrm, bool bAutoClose ) :
      43         304 :     BinaryStreamBase( Reference< XSeekable >( rxOutStrm, UNO_QUERY ).is() ),
      44             :     BinaryXSeekableStream( Reference< XSeekable >( rxOutStrm, UNO_QUERY ) ),
      45             :     maBuffer( OUTPUTSTREAM_BUFFERSIZE ),
      46             :     mxOutStrm( rxOutStrm ),
      47         304 :     mbAutoClose( bAutoClose && rxOutStrm.is() )
      48             : {
      49         152 :     mbEof = !mxOutStrm.is();
      50         152 : }
      51             : 
      52         456 : BinaryXOutputStream::~BinaryXOutputStream()
      53             : {
      54         152 :     close();
      55         304 : }
      56             : 
      57         152 : void BinaryXOutputStream::close()
      58             : {
      59             :     OSL_ENSURE( !mbAutoClose || mxOutStrm.is(), "BinaryXOutputStream::close - invalid call" );
      60         152 :     if( mxOutStrm.is() ) try
      61             :     {
      62         152 :         mxOutStrm->flush();
      63         152 :         mxOutStrm->closeOutput();
      64             :     }
      65           0 :     catch( Exception& )
      66             :     {
      67             :         OSL_FAIL( "BinaryXOutputStream::close - closing output stream failed" );
      68             :     }
      69         152 :     mxOutStrm.clear();
      70         152 :     mbAutoClose = false;
      71         152 :     BinaryXSeekableStream::close();
      72         152 : }
      73             : 
      74         210 : void BinaryXOutputStream::writeData( const StreamDataSequence& rData, size_t /*nAtomSize*/ )
      75             : {
      76         210 :     if( mxOutStrm.is() ) try
      77             :     {
      78         210 :         mxOutStrm->writeBytes( rData );
      79             :     }
      80           0 :     catch( Exception& )
      81             :     {
      82             :         OSL_FAIL( "BinaryXOutputStream::writeData - stream read error" );
      83             :     }
      84         210 : }
      85             : 
      86          37 : void BinaryXOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize )
      87             : {
      88          37 :     if( mxOutStrm.is() && (nBytes > 0) )
      89             :     {
      90          35 :         sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, (OUTPUTSTREAM_BUFFERSIZE / nAtomSize) * nAtomSize );
      91          35 :         const sal_uInt8* pnMem = reinterpret_cast< const sal_uInt8* >( pMem );
      92         105 :         while( nBytes > 0 )
      93             :         {
      94          35 :             sal_Int32 nWriteSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, nBufferSize );
      95          35 :             maBuffer.realloc( nWriteSize );
      96          35 :             memcpy( maBuffer.getArray(), pnMem, static_cast< size_t >( nWriteSize ) );
      97          35 :             writeData( maBuffer, nAtomSize );
      98          35 :             pnMem += nWriteSize;
      99          35 :             nBytes -= nWriteSize;
     100             :         }
     101             :     }
     102          37 : }
     103             : 
     104             : void
     105           4 : BinaryOutputStream::writeCharArrayUC( const OUString& rString, rtl_TextEncoding eTextEnc, bool bAllowNulChars )
     106             : {
     107           4 :     OString sBuf( OUStringToOString( rString, eTextEnc ) );
     108           4 :     if( !bAllowNulChars )
     109           4 :         sBuf = sBuf.replace( '\0', '?' );
     110           4 :     writeMemory( static_cast< const void* >( sBuf.getStr() ), sBuf.getLength() );
     111           4 : }
     112             : 
     113             : void
     114           1 : BinaryOutputStream::writeUnicodeArray( const ::rtl::OUString& rString, bool bAllowNulChars )
     115             : {
     116           1 :     OUString sBuf( rString );
     117           1 :     if( !bAllowNulChars )
     118           1 :         sBuf = sBuf.replace( '\0', '?' );
     119             : #ifdef OSL_BIGENDIAN
     120             :     // need a non-const buffer for swapping byte order
     121             :     sal_Unicode notConst[sBuf.getLength()];
     122             :     memcpy( notConst, sBuf.getStr(), sizeof(sal_Unicode)*sBuf.getLength() );
     123             :     writeArray( notConst, sBuf.getLength() );
     124             : #else
     125           1 :     writeArray( sBuf.getStr(), sBuf.getLength() );
     126             : #endif
     127           1 : }
     128             : 
     129             : void
     130           4 : BinaryOutputStream::writeCompressedUnicodeArray( const rtl::OUString& rString, bool bCompressed, bool bAllowNulChars )
     131             : {
     132           4 :     if ( bCompressed )
     133             :          // ISO-8859-1 maps all byte values 0xHH to the same Unicode code point U+00HH
     134           4 :         writeCharArrayUC( rString, RTL_TEXTENCODING_ISO_8859_1, bAllowNulChars );
     135             :     else
     136           0 :         writeUnicodeArray( rString, bAllowNulChars );
     137           4 : }
     138             : 
     139             : // ============================================================================
     140             : 
     141           0 : SequenceOutputStream::SequenceOutputStream( StreamDataSequence& rData ) :
     142             :     BinaryStreamBase( true ),
     143           0 :     SequenceSeekableStream( rData )
     144             : {
     145           0 : }
     146             : 
     147           0 : void SequenceOutputStream::writeData( const StreamDataSequence& rData, size_t nAtomSize )
     148             : {
     149           0 :     if( mpData && rData.hasElements() )
     150           0 :         writeMemory( rData.getConstArray(), rData.getLength(), nAtomSize );
     151           0 : }
     152             : 
     153           0 : void SequenceOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes, size_t /*nAtomSize*/ )
     154             : {
     155           0 :     if( mpData && (nBytes > 0) )
     156             :     {
     157           0 :         if( mpData->getLength() - mnPos < nBytes )
     158           0 :             const_cast< StreamDataSequence* >( mpData )->realloc( mnPos + nBytes );
     159           0 :         memcpy( const_cast< StreamDataSequence* >( mpData )->getArray() + mnPos, pMem, static_cast< size_t >( nBytes ) );
     160           0 :         mnPos += nBytes;
     161             :     }
     162           0 : }
     163             : 
     164             : // ============================================================================
     165             : 
     166             : } // namespace oox
     167             : 
     168             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10