LCOV - code coverage report
Current view: top level - oox/source/helper - textinputstream.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 36 76 47.4 %
Date: 2014-11-03 Functions: 12 18 66.7 %
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/textinputstream.hxx"
      21             : 
      22             : #include <com/sun/star/io/XActiveDataSink.hpp>
      23             : #include <com/sun/star/io/TextInputStream.hpp>
      24             : #include <cppuhelper/implbase1.hxx>
      25             : #include <rtl/tencinfo.h>
      26             : #include "oox/helper/binaryinputstream.hxx"
      27             : 
      28             : namespace oox {
      29             : 
      30             : using namespace ::com::sun::star::io;
      31             : using namespace ::com::sun::star::lang;
      32             : using namespace ::com::sun::star::uno;
      33             : 
      34             : namespace {
      35             : 
      36             : typedef ::cppu::WeakImplHelper1< XInputStream > UnoBinaryInputStream_BASE;
      37             : 
      38             : /** Implementation of a UNO input stream wrapping a binary input stream.
      39             :  */
      40         736 : class UnoBinaryInputStream : public UnoBinaryInputStream_BASE
      41             : {
      42             : public:
      43             :     explicit            UnoBinaryInputStream( BinaryInputStream& rInStrm );
      44             : 
      45             :     virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& rData, sal_Int32 nBytesToRead )
      46             :                         throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
      47             :     virtual sal_Int32 SAL_CALL readSomeBytes( Sequence< sal_Int8 >& rData, sal_Int32 nMaxBytesToRead )
      48             :                         throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
      49             :     virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip )
      50             :                         throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
      51             :     virtual sal_Int32 SAL_CALL available()
      52             :                         throw (NotConnectedException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
      53             :     virtual void SAL_CALL closeInput()
      54             :                         throw (NotConnectedException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
      55             : 
      56             : private:
      57             :     void                ensureConnected() const throw (NotConnectedException);
      58             : 
      59             : private:
      60             :     BinaryInputStream*  mpInStrm;
      61             : };
      62             : 
      63         368 : UnoBinaryInputStream::UnoBinaryInputStream( BinaryInputStream& rInStrm ) :
      64         368 :     mpInStrm( &rInStrm )
      65             : {
      66         368 : }
      67             : 
      68           0 : sal_Int32 SAL_CALL UnoBinaryInputStream::readBytes( Sequence< sal_Int8 >& rData, sal_Int32 nBytesToRead )
      69             :         throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
      70             : {
      71           0 :     ensureConnected();
      72           0 :     return mpInStrm->readData( rData, nBytesToRead, 1 );
      73             : }
      74             : 
      75        2296 : sal_Int32 SAL_CALL UnoBinaryInputStream::readSomeBytes( Sequence< sal_Int8 >& rData, sal_Int32 nMaxBytesToRead )
      76             :         throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
      77             : {
      78        2296 :     ensureConnected();
      79        2296 :     return mpInStrm->readData( rData, nMaxBytesToRead, 1 );
      80             : }
      81             : 
      82           0 : void SAL_CALL UnoBinaryInputStream::skipBytes( sal_Int32 nBytesToSkip )
      83             :         throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
      84             : {
      85           0 :     ensureConnected();
      86           0 :     mpInStrm->skip( nBytesToSkip, 1 );
      87           0 : }
      88             : 
      89           0 : sal_Int32 SAL_CALL UnoBinaryInputStream::available() throw (NotConnectedException, IOException, RuntimeException, std::exception)
      90             : {
      91           0 :     ensureConnected();
      92           0 :     throw RuntimeException( "Functionality not supported", Reference< XInputStream >() );
      93             : }
      94             : 
      95           0 : void SAL_CALL UnoBinaryInputStream::closeInput() throw (NotConnectedException, IOException, RuntimeException, std::exception)
      96             : {
      97           0 :     ensureConnected();
      98           0 :     mpInStrm->close();
      99           0 :     mpInStrm = 0;
     100           0 : }
     101             : 
     102        2296 : void UnoBinaryInputStream::ensureConnected() const throw (NotConnectedException)
     103             : {
     104        2296 :     if( !mpInStrm )
     105           0 :         throw NotConnectedException( "Stream closed" );
     106        2296 : }
     107             : 
     108             : } // namespace
     109             : 
     110           0 : TextInputStream::TextInputStream( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm, rtl_TextEncoding eTextEnc )
     111             : {
     112           0 :     init( rxContext, rxInStrm, eTextEnc );
     113           0 : }
     114             : 
     115         368 : TextInputStream::TextInputStream( const Reference< XComponentContext >& rxContext, BinaryInputStream& rInStrm, rtl_TextEncoding eTextEnc )
     116             : {
     117         368 :     init( rxContext, new UnoBinaryInputStream( rInStrm ), eTextEnc );
     118         368 : }
     119             : 
     120         368 : TextInputStream::~TextInputStream()
     121             : {
     122         368 : }
     123             : 
     124       14628 : bool TextInputStream::isEof() const
     125             : {
     126       14628 :     if( mxTextStrm.is() ) try
     127             :     {
     128       14628 :         return mxTextStrm->isEOF();
     129             :     }
     130           0 :     catch (const Exception&)
     131             :     {
     132             :     }
     133           0 :     return true;
     134             : }
     135             : 
     136       14324 : OUString TextInputStream::readLine()
     137             : {
     138       14324 :     if( mxTextStrm.is() ) try
     139             :     {
     140             :         /*  The function createFinalString() adds a character that may have
     141             :             been buffered in the previous call of readToChar() (see below). */
     142       14324 :         return createFinalString( mxTextStrm->readLine() );
     143             :     }
     144           0 :     catch (const Exception&)
     145             :     {
     146           0 :         mxTextStrm.clear();
     147             :     }
     148           0 :     return OUString();
     149             : }
     150             : 
     151           0 : OUString TextInputStream::readToChar( sal_Unicode cChar, bool bIncludeChar )
     152             : {
     153           0 :     if( mxTextStrm.is() ) try
     154             :     {
     155           0 :         Sequence< sal_Unicode > aDelimiters( 1 );
     156           0 :         aDelimiters[ 0 ] = cChar;
     157             :         /*  Always get the delimiter character from the UNO text input stream.
     158             :             In difference to this implementation, it will not return it in the
     159             :             next call but silently skip it. If caller specifies to exclude the
     160             :             character in this call, it will be returned in the next call of one
     161             :             of the own member functions. The function createFinalString() adds
     162             :             a character that has been buffered in the previous call. */
     163           0 :         OUString aString = createFinalString( mxTextStrm->readString( aDelimiters, sal_False ) );
     164             :         // remove last character from string and remember it for next call
     165           0 :         if( !bIncludeChar && !aString.isEmpty() && (aString[ aString.getLength() - 1 ] == cChar) )
     166             :         {
     167           0 :             mcPendingChar = cChar;
     168           0 :             aString = aString.copy( 0, aString.getLength() - 1 );
     169             :         }
     170           0 :         return aString;
     171             :     }
     172           0 :     catch (const Exception&)
     173             :     {
     174           0 :         mxTextStrm.clear();
     175             :     }
     176           0 :     return OUString();
     177             : }
     178             : 
     179         376 : Reference< XTextInputStream2 > TextInputStream::createXTextInputStream(
     180             :         const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm, rtl_TextEncoding eTextEnc )
     181             : {
     182         376 :     Reference< XTextInputStream2 > xTextStrm;
     183         376 :     const char* pcCharset = rtl_getBestMimeCharsetFromTextEncoding( eTextEnc );
     184             :     OSL_ENSURE( pcCharset, "TextInputStream::createXTextInputStream - unsupported text encoding" );
     185         376 :     if( rxContext.is() && rxInStrm.is() && pcCharset ) try
     186             :     {
     187         376 :         xTextStrm = com::sun::star::io::TextInputStream::create( rxContext );
     188         376 :         xTextStrm->setInputStream( rxInStrm );
     189         376 :         xTextStrm->setEncoding( OUString::createFromAscii( pcCharset ) );
     190             :     }
     191           0 :     catch (const Exception&)
     192             :     {
     193             :     }
     194         376 :     return xTextStrm;
     195             : }
     196             : 
     197             : // private --------------------------------------------------------------------
     198             : 
     199       14324 : OUString TextInputStream::createFinalString( const OUString& rString )
     200             : {
     201       14324 :     if( mcPendingChar == 0 )
     202       14324 :         return rString;
     203             : 
     204           0 :     OUString aString = OUString( mcPendingChar ) + rString;
     205           0 :     mcPendingChar = 0;
     206           0 :     return aString;
     207             : }
     208             : 
     209         368 : void TextInputStream::init( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm, rtl_TextEncoding eTextEnc )
     210             : {
     211         368 :     mcPendingChar = 0;
     212         368 :     mxTextStrm = createXTextInputStream( rxContext, rxInStrm, eTextEnc );
     213         368 : }
     214             : 
     215             : } // namespace oox
     216             : 
     217             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10