LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/oox/source/helper - binaryoutputstream.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 49 66 74.2 %
Date: 2013-07-09 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         600 : BinaryXOutputStream::BinaryXOutputStream( const Reference< XOutputStream >& rxOutStrm, bool bAutoClose ) :
      43        1200 :     BinaryStreamBase( Reference< XSeekable >( rxOutStrm, UNO_QUERY ).is() ),
      44             :     BinaryXSeekableStream( Reference< XSeekable >( rxOutStrm, UNO_QUERY ) ),
      45             :     maBuffer( OUTPUTSTREAM_BUFFERSIZE ),
      46             :     mxOutStrm( rxOutStrm ),
      47        1200 :     mbAutoClose( bAutoClose && rxOutStrm.is() )
      48             : {
      49         600 :     mbEof = !mxOutStrm.is();
      50         600 : }
      51             : 
      52        1800 : BinaryXOutputStream::~BinaryXOutputStream()
      53             : {
      54         600 :     close();
      55        1200 : }
      56             : 
      57         600 : void BinaryXOutputStream::close()
      58             : {
      59             :     OSL_ENSURE( !mbAutoClose || mxOutStrm.is(), "BinaryXOutputStream::close - invalid call" );
      60         600 :     if( mxOutStrm.is() ) try
      61             :     {
      62         600 :         mxOutStrm->flush();
      63         600 :         if ( mbAutoClose )
      64         434 :             mxOutStrm->closeOutput();
      65             :     }
      66           0 :     catch( Exception& )
      67             :     {
      68             :         OSL_FAIL( "BinaryXOutputStream::close - closing output stream failed" );
      69             :     }
      70         600 :     mxOutStrm.clear();
      71         600 :     mbAutoClose = false;
      72         600 :     BinaryXSeekableStream::close();
      73         600 : }
      74             : 
      75         645 : void BinaryXOutputStream::writeData( const StreamDataSequence& rData, size_t /*nAtomSize*/ )
      76             : {
      77         645 :     if( mxOutStrm.is() ) try
      78             :     {
      79         645 :         mxOutStrm->writeBytes( rData );
      80             :     }
      81           0 :     catch( Exception& )
      82             :     {
      83             :         OSL_FAIL( "BinaryXOutputStream::writeData - stream read error" );
      84             :     }
      85         645 : }
      86             : 
      87          29 : void BinaryXOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize )
      88             : {
      89          29 :     if( mxOutStrm.is() && (nBytes > 0) )
      90             :     {
      91          29 :         sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, (OUTPUTSTREAM_BUFFERSIZE / nAtomSize) * nAtomSize );
      92          29 :         const sal_uInt8* pnMem = reinterpret_cast< const sal_uInt8* >( pMem );
      93          87 :         while( nBytes > 0 )
      94             :         {
      95          29 :             sal_Int32 nWriteSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, nBufferSize );
      96          29 :             maBuffer.realloc( nWriteSize );
      97          29 :             memcpy( maBuffer.getArray(), pnMem, static_cast< size_t >( nWriteSize ) );
      98          29 :             writeData( maBuffer, nAtomSize );
      99          29 :             pnMem += nWriteSize;
     100          29 :             nBytes -= nWriteSize;
     101             :         }
     102             :     }
     103          29 : }
     104             : 
     105             : void
     106           2 : BinaryOutputStream::writeCharArrayUC( const OUString& rString, rtl_TextEncoding eTextEnc, bool bAllowNulChars )
     107             : {
     108           2 :     OString sBuf( OUStringToOString( rString, eTextEnc ) );
     109           2 :     if( !bAllowNulChars )
     110           2 :         sBuf = sBuf.replace( '\0', '?' );
     111           2 :     writeMemory( static_cast< const void* >( sBuf.getStr() ), sBuf.getLength() );
     112           2 : }
     113             : 
     114             : void
     115           1 : BinaryOutputStream::writeUnicodeArray( const OUString& rString, bool bAllowNulChars )
     116             : {
     117           1 :     OUString sBuf( rString );
     118           1 :     if( !bAllowNulChars )
     119           1 :         sBuf = sBuf.replace( '\0', '?' );
     120             : #ifdef OSL_BIGENDIAN
     121             :     // need a non-const buffer for swapping byte order
     122             :     sal_Unicode notConst[sBuf.getLength()];
     123             :     memcpy( notConst, sBuf.getStr(), sizeof(sal_Unicode)*sBuf.getLength() );
     124             :     writeArray( notConst, sBuf.getLength() );
     125             : #else
     126           1 :     writeArray( sBuf.getStr(), sBuf.getLength() );
     127             : #endif
     128           1 : }
     129             : 
     130             : void
     131           2 : BinaryOutputStream::writeCompressedUnicodeArray( const OUString& rString, bool bCompressed, bool bAllowNulChars )
     132             : {
     133           2 :     if ( bCompressed )
     134             :          // ISO-8859-1 maps all byte values 0xHH to the same Unicode code point U+00HH
     135           2 :         writeCharArrayUC( rString, RTL_TEXTENCODING_ISO_8859_1, bAllowNulChars );
     136             :     else
     137           0 :         writeUnicodeArray( rString, bAllowNulChars );
     138           2 : }
     139             : 
     140             : // ============================================================================
     141             : 
     142           0 : SequenceOutputStream::SequenceOutputStream( StreamDataSequence& rData ) :
     143             :     BinaryStreamBase( true ),
     144           0 :     SequenceSeekableStream( rData )
     145             : {
     146           0 : }
     147             : 
     148           0 : void SequenceOutputStream::writeData( const StreamDataSequence& rData, size_t nAtomSize )
     149             : {
     150           0 :     if( mpData && rData.hasElements() )
     151           0 :         writeMemory( rData.getConstArray(), rData.getLength(), nAtomSize );
     152           0 : }
     153             : 
     154           0 : void SequenceOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes, size_t /*nAtomSize*/ )
     155             : {
     156           0 :     if( mpData && (nBytes > 0) )
     157             :     {
     158           0 :         if( mpData->getLength() - mnPos < nBytes )
     159           0 :             const_cast< StreamDataSequence* >( mpData )->realloc( mnPos + nBytes );
     160           0 :         memcpy( const_cast< StreamDataSequence* >( mpData )->getArray() + mnPos, pMem, static_cast< size_t >( nBytes ) );
     161           0 :         mnPos += nBytes;
     162             :     }
     163           0 : }
     164             : 
     165             : // ============================================================================
     166             : 
     167             : } // namespace oox
     168             : 
     169             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10