LCOV - code coverage report
Current view: top level - libreoffice/comphelper/source/misc - anytostring.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 152 0.0 %
Date: 2012-12-27 Functions: 0 4 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 "comphelper/anytostring.hxx"
      22             : #include "osl/diagnose.h"
      23             : #include "rtl/ustrbuf.hxx"
      24             : #include "typelib/typedescription.h"
      25             : #include "com/sun/star/lang/XServiceInfo.hpp"
      26             : 
      27             : using namespace ::com::sun::star;
      28             : 
      29             : namespace comphelper {
      30             : namespace {
      31             : 
      32           0 : void appendTypeError(
      33             :     rtl::OUStringBuffer & buf, typelib_TypeDescriptionReference * typeRef )
      34             : {
      35             :     buf.appendAscii(
      36           0 :         RTL_CONSTASCII_STRINGPARAM("<cannot get type description of type ") );
      37           0 :     buf.append( rtl::OUString::unacquired( &typeRef->pTypeName ) );
      38           0 :     buf.append( static_cast< sal_Unicode >('>') );
      39           0 : }
      40             : 
      41           0 : inline void appendChar( rtl::OUStringBuffer & buf, sal_Unicode c )
      42             : {
      43           0 :     if (c < ' ' || c > '~') {
      44           0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\X") );
      45             :         rtl::OUString const s(
      46           0 :             rtl::OUString::valueOf( static_cast< sal_Int32 >(c), 16 ) );
      47           0 :         for ( sal_Int32 f = 4 - s.getLength(); f > 0; --f )
      48           0 :             buf.append( static_cast< sal_Unicode >('0') );
      49           0 :         buf.append( s );
      50             :     }
      51             :     else {
      52           0 :         buf.append( c );
      53             :     }
      54           0 : }
      55             : 
      56             : //------------------------------------------------------------------------------
      57           0 : void appendValue( rtl::OUStringBuffer & buf,
      58             :                   void const * val, typelib_TypeDescriptionReference * typeRef,
      59             :                   bool prependType )
      60             : {
      61           0 :     if (typeRef->eTypeClass == typelib_TypeClass_VOID) {
      62           0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("void") );
      63           0 :         return;
      64             :     }
      65             :     OSL_ASSERT( val != 0 );
      66             : 
      67           0 :     if (prependType &&
      68             :         typeRef->eTypeClass != typelib_TypeClass_STRING &&
      69             :         typeRef->eTypeClass != typelib_TypeClass_CHAR &&
      70             :         typeRef->eTypeClass != typelib_TypeClass_BOOLEAN)
      71             :     {
      72           0 :         buf.append( static_cast< sal_Unicode >('(') );
      73           0 :         buf.append( rtl::OUString::unacquired( &typeRef->pTypeName ) );
      74           0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(") ") );
      75             :     }
      76             : 
      77           0 :     switch (typeRef->eTypeClass) {
      78             :     case typelib_TypeClass_INTERFACE: {
      79           0 :         buf.append( static_cast<sal_Unicode>('@') );
      80             :         buf.append( reinterpret_cast< sal_Int64 >(
      81           0 :                         *static_cast< void * const * >(val) ), 16 );
      82             :         uno::Reference< lang::XServiceInfo > xServiceInfo(
      83             :             *static_cast< uno::XInterface * const * >(val),
      84           0 :             uno::UNO_QUERY );
      85           0 :         if (xServiceInfo.is()) {
      86             :             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(
      87           0 :                                  " (ImplementationName = \"") );
      88           0 :             buf.append( xServiceInfo->getImplementationName() );
      89           0 :             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\")") );
      90             :         }
      91           0 :         break;
      92             :     }
      93             :     case typelib_TypeClass_STRUCT:
      94             :     case typelib_TypeClass_EXCEPTION: {
      95           0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") );
      96           0 :         typelib_TypeDescription * typeDescr = 0;
      97           0 :         typelib_typedescriptionreference_getDescription( &typeDescr, typeRef );
      98           0 :         if (typeDescr == 0 || !typelib_typedescription_complete( &typeDescr )) {
      99           0 :             appendTypeError( buf, typeRef );
     100             :         }
     101             :         else {
     102             :             typelib_CompoundTypeDescription * compType =
     103             :                 reinterpret_cast< typelib_CompoundTypeDescription * >(
     104           0 :                     typeDescr );
     105           0 :             sal_Int32 nDescr = compType->nMembers;
     106             : 
     107           0 :             if (compType->pBaseTypeDescription) {
     108             :                 appendValue(
     109             :                     buf, val, reinterpret_cast<
     110             :                     typelib_TypeDescription * >(
     111           0 :                         compType->pBaseTypeDescription)->pWeakRef, false );
     112           0 :                 if (nDescr > 0)
     113           0 :                     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", ") );
     114             :             }
     115             : 
     116             :             typelib_TypeDescriptionReference ** ppTypeRefs =
     117           0 :                 compType->ppTypeRefs;
     118           0 :             sal_Int32 * memberOffsets = compType->pMemberOffsets;
     119           0 :             rtl_uString ** ppMemberNames = compType->ppMemberNames;
     120             : 
     121           0 :             for ( sal_Int32 nPos = 0; nPos < nDescr; ++nPos )
     122             :             {
     123           0 :                 buf.append( ppMemberNames[ nPos ] );
     124           0 :                 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" = ") );
     125           0 :                 typelib_TypeDescription * memberType = 0;
     126           0 :                 TYPELIB_DANGER_GET( &memberType, ppTypeRefs[ nPos ] );
     127           0 :                 if (memberType == 0) {
     128           0 :                     appendTypeError( buf, ppTypeRefs[ nPos ] );
     129             :                 }
     130             :                 else {
     131             :                     appendValue( buf,
     132             :                                  static_cast< char const * >(
     133           0 :                                      val ) + memberOffsets[ nPos ],
     134           0 :                                  memberType->pWeakRef, true );
     135           0 :                     TYPELIB_DANGER_RELEASE( memberType );
     136             :                 }
     137           0 :                 if (nPos < (nDescr - 1))
     138           0 :                     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", ") );
     139             :             }
     140             :         }
     141           0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") );
     142           0 :         if (typeDescr != 0)
     143           0 :             typelib_typedescription_release( typeDescr );
     144             :         break;
     145             :     }
     146             :     case typelib_TypeClass_SEQUENCE: {
     147           0 :         typelib_TypeDescription * typeDescr = 0;
     148           0 :         TYPELIB_DANGER_GET( &typeDescr, typeRef );
     149           0 :         if (typeDescr == 0) {
     150           0 :             appendTypeError( buf,typeRef );
     151             :         }
     152             :         else {
     153             :             typelib_TypeDescriptionReference * elementTypeRef =
     154             :                 reinterpret_cast<
     155           0 :                 typelib_IndirectTypeDescription * >(typeDescr)->pType;
     156           0 :             typelib_TypeDescription * elementTypeDescr = 0;
     157           0 :             TYPELIB_DANGER_GET( &elementTypeDescr, elementTypeRef );
     158           0 :             if (elementTypeDescr == 0)
     159             :             {
     160           0 :                 appendTypeError( buf, elementTypeRef );
     161             :             }
     162             :             else
     163             :             {
     164           0 :                 sal_Int32 nElementSize = elementTypeDescr->nSize;
     165             :                 uno_Sequence * seq =
     166           0 :                     *static_cast< uno_Sequence * const * >(val);
     167           0 :                 sal_Int32 nElements = seq->nElements;
     168             : 
     169           0 :                 if (nElements > 0)
     170             :                 {
     171           0 :                     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") );
     172           0 :                     char const * pElements = seq->elements;
     173           0 :                     for ( sal_Int32 nPos = 0; nPos < nElements; ++nPos )
     174             :                     {
     175             :                         appendValue(
     176           0 :                             buf, pElements + (nElementSize * nPos),
     177           0 :                             elementTypeDescr->pWeakRef, false );
     178           0 :                         if (nPos < (nElements - 1))
     179           0 :                             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(", ") );
     180             :                     }
     181           0 :                     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") );
     182             :                 }
     183             :                 else
     184             :                 {
     185           0 :                     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{}") );
     186             :                 }
     187           0 :                 TYPELIB_DANGER_RELEASE( elementTypeDescr );
     188             :             }
     189           0 :             TYPELIB_DANGER_RELEASE( typeDescr );
     190             :         }
     191             :         break;
     192             :     }
     193             :     case typelib_TypeClass_ANY: {
     194           0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") );
     195           0 :         uno_Any const * pAny = static_cast< uno_Any const * >(val);
     196           0 :         appendValue( buf, pAny->pData, pAny->pType, true );
     197           0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") );
     198           0 :         break;
     199             :     }
     200             :     case typelib_TypeClass_TYPE:
     201             :         buf.append( (*reinterpret_cast<
     202             :                      typelib_TypeDescriptionReference * const * >(val)
     203           0 :                         )->pTypeName );
     204           0 :         break;
     205             :     case typelib_TypeClass_STRING: {
     206           0 :         buf.append( static_cast< sal_Unicode >('\"') );
     207             :         rtl::OUString const & str = rtl::OUString::unacquired(
     208           0 :             static_cast< rtl_uString * const * >(val) );
     209           0 :         sal_Int32 len = str.getLength();
     210           0 :         for ( sal_Int32 pos = 0; pos < len; ++pos )
     211             :         {
     212           0 :             sal_Unicode c = str[ pos ];
     213           0 :             if (c == '\"')
     214           0 :                 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\\"") );
     215           0 :             else if (c == '\\')
     216           0 :                 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\\\") );
     217             :             else
     218           0 :                 appendChar( buf, c );
     219             :         }
     220           0 :         buf.append( static_cast< sal_Unicode >('\"') );
     221           0 :         break;
     222             :     }
     223             :     case typelib_TypeClass_ENUM: {
     224           0 :         typelib_TypeDescription * typeDescr = 0;
     225           0 :         typelib_typedescriptionreference_getDescription( &typeDescr, typeRef );
     226           0 :         if (typeDescr == 0 || !typelib_typedescription_complete( &typeDescr )) {
     227           0 :             appendTypeError( buf, typeRef );
     228             :         }
     229             :         else
     230             :         {
     231             :             sal_Int32 * pValues =
     232             :                 reinterpret_cast< typelib_EnumTypeDescription * >(
     233           0 :                     typeDescr )->pEnumValues;
     234             :             sal_Int32 nPos = reinterpret_cast< typelib_EnumTypeDescription * >(
     235           0 :                 typeDescr )->nEnumValues;
     236           0 :             while (nPos--)
     237             :             {
     238           0 :                 if (pValues[ nPos ] == *static_cast< int const * >(val))
     239           0 :                     break;
     240             :             }
     241           0 :             if (nPos >= 0)
     242             :             {
     243             :                 buf.append( reinterpret_cast< typelib_EnumTypeDescription * >(
     244           0 :                                 typeDescr )->ppEnumNames[ nPos ] );
     245             :             }
     246             :             else
     247             :             {
     248             :                 buf.appendAscii(
     249           0 :                     RTL_CONSTASCII_STRINGPARAM("?unknown enum value?") );
     250             :             }
     251             :         }
     252           0 :         if (typeDescr != 0)
     253           0 :             typelib_typedescription_release( typeDescr );
     254             :         break;
     255             :     }
     256             :     case typelib_TypeClass_BOOLEAN:
     257           0 :         if (*static_cast< sal_Bool const * >(val) != sal_False)
     258           0 :             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("true") );
     259             :         else
     260           0 :             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("false") );
     261           0 :         break;
     262             :     case typelib_TypeClass_CHAR: {
     263           0 :         buf.append( static_cast< sal_Unicode >('\'') );
     264           0 :         sal_Unicode c = *static_cast< sal_Unicode const * >(val);
     265           0 :         if (c == '\'')
     266           0 :             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\\'") );
     267           0 :         else if (c == '\\')
     268           0 :             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\\\") );
     269             :         else
     270           0 :             appendChar( buf, c );
     271           0 :         buf.append( static_cast< sal_Unicode >('\'') );
     272           0 :         break;
     273             :     }
     274             :     case typelib_TypeClass_FLOAT:
     275           0 :         buf.append( *static_cast< float const * >(val) );
     276           0 :         break;
     277             :     case typelib_TypeClass_DOUBLE:
     278           0 :         buf.append( *static_cast< double const * >(val) );
     279           0 :         break;
     280             :     case typelib_TypeClass_BYTE:
     281             :         buf.append( static_cast< sal_Int32 >(
     282           0 :                         *static_cast< sal_Int8 const * >(val) ) );
     283           0 :         break;
     284             :     case typelib_TypeClass_SHORT:
     285             :         buf.append( static_cast< sal_Int32 >(
     286           0 :                         *static_cast< sal_Int16 const * >(val) ) );
     287           0 :         break;
     288             :     case typelib_TypeClass_UNSIGNED_SHORT:
     289             :         buf.append( static_cast< sal_Int32 >(
     290           0 :                         *static_cast< sal_uInt16 const * >(val) ) );
     291           0 :         break;
     292             :     case typelib_TypeClass_LONG:
     293           0 :         buf.append( *static_cast< sal_Int32 const * >(val) );
     294           0 :         break;
     295             :     case typelib_TypeClass_UNSIGNED_LONG:
     296             :         buf.append( static_cast< sal_Int64 >(
     297           0 :                         *static_cast< sal_uInt32 const * >(val) ) );
     298           0 :         break;
     299             :     case typelib_TypeClass_HYPER:
     300             :     case typelib_TypeClass_UNSIGNED_HYPER:
     301           0 :         buf.append( *static_cast< sal_Int64 const * >(val) );
     302           0 :         break;
     303             : //     case typelib_TypeClass_UNION:
     304             : //     case typelib_TypeClass_ARRAY:
     305             : //     case typelib_TypeClass_UNKNOWN:
     306             : //     case typelib_TypeClass_SERVICE:
     307             : //     case typelib_TypeClass_MODULE:
     308             :     default:
     309           0 :         buf.append( static_cast< sal_Unicode >('?') );
     310           0 :         break;
     311             :     }
     312             : }
     313             : 
     314             : } // anon namespace
     315             : 
     316             : //==============================================================================
     317           0 : rtl::OUString anyToString( uno::Any const & value )
     318             : {
     319           0 :     rtl::OUStringBuffer buf;
     320           0 :     appendValue( buf, value.getValue(), value.getValueTypeRef(), true );
     321           0 :     return buf.makeStringAndClear();
     322             : }
     323             : 
     324             : } // namespace comphelper
     325             : 
     326             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10