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

Generated by: LCOV version 1.10