LCOV - code coverage report
Current view: top level - io/source/TextOutputStream - TextOutputStream.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 18 79 22.8 %
Date: 2015-06-13 12:38:46 Functions: 8 18 44.4 %
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             : 
      21             : #include <osl/diagnose.h>
      22             : 
      23             : #include <uno/mapping.hxx>
      24             : 
      25             : #include <cppuhelper/factory.hxx>
      26             : #include <cppuhelper/implbase2.hxx>
      27             : #include <cppuhelper/implementationentry.hxx>
      28             : #include <cppuhelper/supportsservice.hxx>
      29             : 
      30             : #include <rtl/textenc.h>
      31             : #include <rtl/tencinfo.h>
      32             : 
      33             : #include <com/sun/star/io/XTextOutputStream2.hpp>
      34             : #include <com/sun/star/lang/XServiceInfo.hpp>
      35             : 
      36             : #include "services.hxx"
      37             : 
      38             : #define IMPLEMENTATION_NAME "com.sun.star.comp.io.TextOutputStream"
      39             : #define SERVICE_NAME "com.sun.star.io.TextOutputStream"
      40             : 
      41             : using namespace ::osl;
      42             : using namespace ::cppu;
      43             : using namespace ::com::sun::star::uno;
      44             : using namespace ::com::sun::star::lang;
      45             : using namespace ::com::sun::star::io;
      46             : using namespace ::com::sun::star::registry;
      47             : 
      48             : namespace io_TextOutputStream
      49             : {
      50             : 
      51             : // Implementation XTextOutputStream
      52             : 
      53             : typedef WeakImplHelper2< XTextOutputStream2, XServiceInfo > TextOutputStreamHelper;
      54             : 
      55             : class OTextOutputStream : public TextOutputStreamHelper
      56             : {
      57             :     Reference< XOutputStream > mxStream;
      58             : 
      59             :     // Encoding
      60             :     OUString mEncoding;
      61             :     bool mbEncodingInitialized;
      62             :     rtl_UnicodeToTextConverter  mConvUnicode2Text;
      63             :     rtl_UnicodeToTextContext    mContextUnicode2Text;
      64             : 
      65             :     Sequence<sal_Int8> implConvert( const OUString& rSource );
      66             :     void checkOutputStream() throw(IOException);
      67             : 
      68             : public:
      69             :     OTextOutputStream();
      70             :     virtual ~OTextOutputStream();
      71             : 
      72             :     // Methods XTextOutputStream
      73             :     virtual void SAL_CALL writeString( const OUString& aString )
      74             :         throw(IOException, RuntimeException, std::exception) SAL_OVERRIDE;
      75             :     virtual void SAL_CALL setEncoding( const OUString& Encoding )
      76             :         throw(RuntimeException, std::exception) SAL_OVERRIDE;
      77             : 
      78             :     // Methods XOutputStream
      79             :     virtual void SAL_CALL writeBytes( const Sequence< sal_Int8 >& aData )
      80             :         throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
      81             :     virtual void SAL_CALL flush(  )
      82             :         throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
      83             :     virtual void SAL_CALL closeOutput(  )
      84             :         throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
      85             : 
      86             :     // Methods XActiveDataSource
      87             :     virtual void SAL_CALL setOutputStream( const Reference< XOutputStream >& aStream )
      88             :         throw(RuntimeException, std::exception) SAL_OVERRIDE;
      89             :     virtual Reference< XOutputStream > SAL_CALL getOutputStream(  )
      90             :         throw(RuntimeException, std::exception) SAL_OVERRIDE;
      91             : 
      92             :     // Methods XServiceInfo
      93             :         virtual OUString              SAL_CALL getImplementationName() throw(std::exception) SAL_OVERRIDE;
      94             :         virtual Sequence< OUString >  SAL_CALL getSupportedServiceNames() throw(std::exception) SAL_OVERRIDE;
      95             :         virtual sal_Bool              SAL_CALL supportsService(const OUString& ServiceName) throw(std::exception) SAL_OVERRIDE;
      96             : };
      97             : 
      98           1 : OTextOutputStream::OTextOutputStream()
      99             :     : mbEncodingInitialized(false)
     100             :     , mConvUnicode2Text(NULL)
     101           1 :     , mContextUnicode2Text(NULL)
     102             : {
     103           1 : }
     104             : 
     105           3 : OTextOutputStream::~OTextOutputStream()
     106             : {
     107           1 :     if( mbEncodingInitialized )
     108             :     {
     109           0 :         rtl_destroyUnicodeToTextContext( mConvUnicode2Text, mContextUnicode2Text );
     110           0 :         rtl_destroyUnicodeToTextConverter( mConvUnicode2Text );
     111             :     }
     112           2 : }
     113             : 
     114           0 : Sequence<sal_Int8> OTextOutputStream::implConvert( const OUString& rSource )
     115             : {
     116           0 :     const sal_Unicode *puSource = rSource.getStr();
     117           0 :     sal_Int32 nSourceSize = rSource.getLength();
     118             : 
     119           0 :     sal_Size nTargetCount = 0;
     120           0 :     sal_Size nSourceCount = 0;
     121             : 
     122             :     sal_uInt32 uiInfo;
     123             :     sal_Size nSrcCvtChars;
     124             : 
     125             :     // take nSourceSize * 3 as preference
     126             :     // this is an upper boundary for converting to utf8,
     127             :     // which most often used as the target.
     128           0 :     sal_Int32 nSeqSize =  nSourceSize * 3;
     129             : 
     130           0 :     Sequence<sal_Int8> seqText( nSeqSize );
     131           0 :     sal_Char *pTarget = reinterpret_cast<char *>(seqText.getArray());
     132             :     while( true )
     133             :     {
     134             :         nTargetCount += rtl_convertUnicodeToText(
     135             :                                     mConvUnicode2Text,
     136             :                                     mContextUnicode2Text,
     137             :                                     &( puSource[nSourceCount] ),
     138             :                                     nSourceSize - nSourceCount ,
     139             :                                     &( pTarget[nTargetCount] ),
     140             :                                     nSeqSize - nTargetCount,
     141             :                                     RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT |
     142             :                                     RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT ,
     143             :                                     &uiInfo,
     144           0 :                                     &nSrcCvtChars);
     145           0 :         nSourceCount += nSrcCvtChars;
     146             : 
     147           0 :         if( uiInfo & RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL )
     148             :         {
     149           0 :             nSeqSize *= 2;
     150           0 :             seqText.realloc( nSeqSize );  // double array size
     151           0 :             pTarget = reinterpret_cast<char*>(seqText.getArray());
     152           0 :             continue;
     153             :         }
     154           0 :         break;
     155             :     }
     156             : 
     157             :     // reduce the size of the buffer (fast, no copy necessary)
     158           0 :     seqText.realloc( nTargetCount );
     159           0 :     return seqText;
     160             : }
     161             : 
     162             : 
     163             : 
     164             : // XTextOutputStream
     165             : 
     166           0 : void OTextOutputStream::writeString( const OUString& aString )
     167             :     throw(IOException, RuntimeException, std::exception)
     168             : {
     169           0 :     checkOutputStream();
     170           0 :     if( !mbEncodingInitialized )
     171             :     {
     172           0 :         OUString aUtf8Str("utf8");
     173           0 :         setEncoding( aUtf8Str );
     174             :     }
     175           0 :     if( !mbEncodingInitialized )
     176           0 :         return;
     177             : 
     178           0 :     Sequence<sal_Int8> aByteSeq = implConvert( aString );
     179           0 :     mxStream->writeBytes( aByteSeq );
     180             : }
     181             : 
     182           0 : void OTextOutputStream::setEncoding( const OUString& Encoding )
     183             :     throw(RuntimeException, std::exception)
     184             : {
     185           0 :     OString aOEncodingStr = OUStringToOString( Encoding, RTL_TEXTENCODING_ASCII_US );
     186           0 :     rtl_TextEncoding encoding = rtl_getTextEncodingFromMimeCharset( aOEncodingStr.getStr() );
     187           0 :     if( RTL_TEXTENCODING_DONTKNOW == encoding )
     188           0 :         return;
     189             : 
     190           0 :     mbEncodingInitialized = true;
     191           0 :     mConvUnicode2Text   = rtl_createUnicodeToTextConverter( encoding );
     192           0 :     mContextUnicode2Text = rtl_createUnicodeToTextContext( mConvUnicode2Text );
     193           0 :     mEncoding = Encoding;
     194             : }
     195             : 
     196             : 
     197             : // XOutputStream
     198           0 : void OTextOutputStream::writeBytes( const Sequence< sal_Int8 >& aData )
     199             :     throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
     200             : {
     201           0 :     checkOutputStream();
     202           0 :     mxStream->writeBytes( aData );
     203           0 : }
     204             : 
     205           0 : void OTextOutputStream::flush(  )
     206             :     throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
     207             : {
     208           0 :     checkOutputStream();
     209           0 :     mxStream->flush();
     210           0 : }
     211             : 
     212           0 : void OTextOutputStream::closeOutput(  )
     213             :     throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
     214             : {
     215           0 :     checkOutputStream();
     216           0 :     mxStream->closeOutput();
     217           0 : }
     218             : 
     219             : 
     220           0 : void OTextOutputStream::checkOutputStream()
     221             :     throw(IOException)
     222             : {
     223           0 :     if (! mxStream.is() )
     224           0 :         throw IOException("output stream is not initialized, you have to use setOutputStream first");
     225           0 : }
     226             : 
     227             : 
     228             : 
     229             : // XActiveDataSource
     230             : 
     231           0 : void OTextOutputStream::setOutputStream( const Reference< XOutputStream >& aStream )
     232             :     throw(RuntimeException, std::exception)
     233             : {
     234           0 :     mxStream = aStream;
     235           0 : }
     236             : 
     237           0 : Reference< XOutputStream > OTextOutputStream::getOutputStream()
     238             :     throw(RuntimeException, std::exception)
     239             : {
     240           0 :     return mxStream;
     241             : }
     242             : 
     243             : 
     244           1 : Reference< XInterface > SAL_CALL TextOutputStream_CreateInstance(
     245             :     SAL_UNUSED_PARAMETER const Reference< XComponentContext > &)
     246             : {
     247           1 :     return Reference < XInterface >( static_cast<OWeakObject *>(new OTextOutputStream()) );
     248             : }
     249             : 
     250         174 : OUString TextOutputStream_getImplementationName()
     251             : {
     252         174 :     return OUString( IMPLEMENTATION_NAME );
     253             : }
     254             : 
     255             : 
     256           2 : Sequence< OUString > TextOutputStream_getSupportedServiceNames()
     257             : {
     258           2 :     Sequence< OUString > seqNames(1);
     259           2 :     seqNames.getArray()[0] =  SERVICE_NAME;
     260           2 :     return seqNames;
     261             : }
     262             : 
     263           1 : OUString OTextOutputStream::getImplementationName() throw(std::exception)
     264             : {
     265           1 :     return TextOutputStream_getImplementationName();
     266             : }
     267             : 
     268           0 : sal_Bool OTextOutputStream::supportsService(const OUString& ServiceName) throw(std::exception)
     269             : {
     270           0 :     return cppu::supportsService(this, ServiceName);
     271             : }
     272             : 
     273           1 : Sequence< OUString > OTextOutputStream::getSupportedServiceNames() throw(std::exception)
     274             : {
     275           1 :     return TextOutputStream_getSupportedServiceNames();
     276             : }
     277             : 
     278             : 
     279             : }
     280             : 
     281             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11