LCOV - code coverage report
Current view: top level - include/oox/helper - binaryoutputstream.hxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 19 19 100.0 %
Date: 2014-11-03 Functions: 17 21 81.0 %
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             : #ifndef INCLUDED_OOX_HELPER_BINARYOUTPUTSTREAM_HXX
      21             : #define INCLUDED_OOX_HELPER_BINARYOUTPUTSTREAM_HXX
      22             : 
      23             : #include <memory>
      24             : #include <boost/shared_array.hpp>
      25             : 
      26             : #include <oox/helper/binarystreambase.hxx>
      27             : 
      28             : namespace com { namespace sun { namespace star {
      29             :     namespace io { class XOutputStream; }
      30             : } } }
      31             : 
      32             : namespace oox {
      33             : 
      34             : 
      35             : 
      36             : /** Interface for binary output stream classes.
      37             : 
      38             :     The binary data in the stream is written in little-endian format.
      39             :  */
      40        3890 : class BinaryOutputStream : public virtual BinaryStreamBase
      41             : {
      42             : public:
      43             :     /** Derived classes implement writing the contents of the passed data
      44             :         sequence.
      45             : 
      46             :         @param nAtomSize
      47             :             The size of the elements in the memory block, if available. Derived
      48             :             classes may be interested in this information.
      49             :      */
      50             :     virtual void        writeData( const StreamDataSequence& rData, size_t nAtomSize = 1 ) = 0;
      51             : 
      52             :     /** Derived classes implement writing the contents of the (preallocated!)
      53             :         memory buffer pMem.
      54             : 
      55             :         @param nAtomSize
      56             :             The size of the elements in the memory block, if available. Derived
      57             :             classes may be interested in this information.
      58             :      */
      59             :     virtual void        writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize = 1 ) = 0;
      60             : 
      61             :     /** Writes a value to the stream and converts it to platform byte order.
      62             :         All data types supported by the ByteOrderConverter class can be used.
      63             :      */
      64             :     template< typename Type >
      65             :     void                writeValue( Type nValue );
      66             : 
      67             :     template< typename Type >
      68             :     void writeArray( Type* opnArray, sal_Int32 nElemCount );
      69             : 
      70             :     template< typename Type >
      71             :     void writeArray( const Type* opnArray, sal_Int32 nElemCount );
      72             : 
      73             :     /** Stream operator for all data types supported by the writeValue() function. */
      74             :     template< typename Type >
      75          24 :     BinaryOutputStream& operator<<( Type nValue ) { writeValue( nValue ); return *this; }
      76             : 
      77             :     void writeCompressedUnicodeArray( const OUString& rString, bool bCompressed, bool bAllowNulChars = false );
      78             : 
      79             :     void writeCharArrayUC( const OUString& rString, rtl_TextEncoding eTextEnc, bool bAllowNulChars = false );
      80             : 
      81             :     void writeUnicodeArray( const OUString& rString, bool bAllowNulChars = false );
      82             : 
      83             : protected:
      84             :     /** This dummy default c'tor will never call the c'tor of the virtual base
      85             :         class BinaryStreamBase as this class cannot be instantiated directly. */
      86        3890 :     BinaryOutputStream() : BinaryStreamBase( false ) {}
      87             : };
      88             : 
      89             : template< typename Type >
      90           2 : void BinaryOutputStream::writeArray( Type* opnArray, sal_Int32 nElemCount )
      91             : {
      92           2 :     sal_Int32 nWriteSize = getLimitedValue< sal_Int32, sal_Int32 >( nElemCount, 0, SAL_MAX_INT32 / sizeof( Type ) ) * sizeof( Type );
      93           2 :     ByteOrderConverter::convertLittleEndianArray( opnArray, static_cast< size_t >( nElemCount ) );
      94           2 :     writeMemory( opnArray, nWriteSize, sizeof( Type ) );
      95           2 : }
      96             : 
      97             : template< typename Type >
      98           2 : void BinaryOutputStream::writeArray( const Type* opnArray, sal_Int32 nElemCount )
      99             : {
     100           2 :     boost::shared_array<Type> pArray(new Type[nElemCount]);
     101           2 :     std::uninitialized_copy(opnArray, opnArray + nElemCount, pArray.get());
     102           2 :     writeArray(pArray.get(), nElemCount);
     103           2 : }
     104             : 
     105             : typedef ::boost::shared_ptr< BinaryOutputStream > BinaryOutputStreamRef;
     106             : 
     107             : 
     108             : 
     109             : template< typename Type >
     110          50 : void BinaryOutputStream::writeValue( Type nValue )
     111             : {
     112          50 :     ByteOrderConverter::convertLittleEndian( nValue );
     113          50 :     writeMemory( &nValue, static_cast< sal_Int32 >( sizeof( Type ) ), sizeof( Type ) );
     114          50 : }
     115             : 
     116             : 
     117             : 
     118             : /** Wraps a UNO output stream and provides convenient access functions.
     119             : 
     120             :     The binary data in the stream is written in little-endian format.
     121             :  */
     122             : class BinaryXOutputStream : public BinaryXSeekableStream, public BinaryOutputStream
     123             : {
     124             : public:
     125             :     /** Constructs the wrapper object for the passed output stream.
     126             : 
     127             :         @param rxOutStream
     128             :             The com.sun.star.io.XOutputStream interface of the output stream to
     129             :             be wrapped.
     130             : 
     131             :         @param bAutoClose
     132             :             True = automatically close the wrapped output stream on destruction
     133             :             of this wrapper or when close() is called.
     134             :      */
     135             :     explicit            BinaryXOutputStream(
     136             :                             const ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >& rxOutStrm,
     137             :                             bool bAutoClose );
     138             : 
     139             :     virtual             ~BinaryXOutputStream();
     140             : 
     141             :     /** Flushes and closes the output stream. Does also close the wrapped UNO
     142             :         output stream if bAutoClose has been set to true in the constructor. */
     143             :     void                close() SAL_OVERRIDE;
     144             : 
     145             :     /** Writes the passed data sequence. */
     146             :     virtual void        writeData( const StreamDataSequence& rData, size_t nAtomSize = 1 ) SAL_OVERRIDE;
     147             : 
     148             :     /** Write nBytes bytes from the (preallocated!) buffer pMem. */
     149             :     virtual void        writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize = 1 ) SAL_OVERRIDE;
     150             : 
     151             :     /** Stream operator for all data types supported by the writeValue() function. */
     152             :     template< typename Type >
     153           2 :     BinaryXOutputStream& operator<<( Type nValue ) { writeValue( nValue ); return *this; }
     154             : 
     155             :     /** Returns the XOutputStream interface of the wrapped output stream. */
     156             :     ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >
     157             :                         getXOutputStream() const { return mxOutStrm; }
     158             : 
     159             : private:
     160             :     StreamDataSequence  maBuffer;       ///< Data buffer used in writeMemory() function.
     161             :     ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >
     162             :                         mxOutStrm;      ///< Reference to the output stream.
     163             :     bool                mbAutoClose;    ///< True = automatically close stream on destruction.
     164             : };
     165             : 
     166             : 
     167             : 
     168             : /** Wraps a StreamDataSequence and provides convenient access functions.
     169             : 
     170             :     The binary data in the stream is written in little-endian format. After
     171             :     construction, the stream points to the beginning of the passed data
     172             :     sequence. The data sequence is expanded automatically while writing to it.
     173             :  */
     174         198 : class OOX_DLLPUBLIC SequenceOutputStream : public SequenceSeekableStream, public BinaryOutputStream
     175             : {
     176             : public:
     177             :     /** Constructs the wrapper object for the passed data sequence.
     178             : 
     179             :         @attention
     180             :             The passed data sequence MUST live at least as long as this stream
     181             :             wrapper. The data sequence MUST NOT be changed from outside as long
     182             :             as this stream wrapper is used to write to it.
     183             :      */
     184             :     explicit            SequenceOutputStream( StreamDataSequence& rData );
     185             : 
     186             :     /** Writes the passed data sequence. */
     187             :     virtual void        writeData( const StreamDataSequence& rData, size_t nAtomSize = 1 ) SAL_OVERRIDE;
     188             : 
     189             :     /** Write nBytes bytes from the (preallocated!) buffer pMem. */
     190             :     virtual void        writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize = 1 ) SAL_OVERRIDE;
     191             : 
     192             :     /** Stream operator for all data types supported by the writeValue() function. */
     193             :     template< typename Type >
     194             :     SequenceOutputStream& operator<<( Type nValue ) { writeValue( nValue ); return *this; }
     195             : };
     196             : 
     197             : 
     198             : 
     199             : } // namespace oox
     200             : 
     201             : #endif
     202             : 
     203             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10