1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
31 #include "comphelper/anytostring.hxx"
32 #include "osl/diagnose.h"
33 #include "rtl/ustrbuf.hxx"
34 #include "typelib/typedescription.h"
35 #include "com/sun/star/lang/XServiceInfo.hpp"
37 using namespace ::com::sun::star
;
39 namespace comphelper
{
43 rtl::OUStringBuffer
& buf
, typelib_TypeDescriptionReference
* typeRef
)
46 RTL_CONSTASCII_STRINGPARAM("<cannot get type description of type ") );
47 buf
.append( rtl::OUString::unacquired( &typeRef
->pTypeName
) );
48 buf
.append( static_cast< sal_Unicode
>('>') );
51 inline void appendChar( rtl::OUStringBuffer
& buf
, sal_Unicode c
)
53 if (c
< ' ' || c
> '~') {
54 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\X") );
55 rtl::OUString
const s(
56 rtl::OUString::valueOf( static_cast< sal_Int32
>(c
), 16 ) );
57 for ( sal_Int32 f
= 4 - s
.getLength(); f
> 0; --f
)
58 buf
.append( static_cast< sal_Unicode
>('0') );
66 //------------------------------------------------------------------------------
67 void appendValue( rtl::OUStringBuffer
& buf
,
68 void const * val
, typelib_TypeDescriptionReference
* typeRef
,
71 if (typeRef
->eTypeClass
== typelib_TypeClass_VOID
) {
72 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("void") );
75 OSL_ASSERT( val
!= 0 );
78 typeRef
->eTypeClass
!= typelib_TypeClass_STRING
&&
79 typeRef
->eTypeClass
!= typelib_TypeClass_CHAR
&&
80 typeRef
->eTypeClass
!= typelib_TypeClass_BOOLEAN
)
82 buf
.append( static_cast< sal_Unicode
>('(') );
83 buf
.append( rtl::OUString::unacquired( &typeRef
->pTypeName
) );
84 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM(") ") );
87 switch (typeRef
->eTypeClass
) {
88 case typelib_TypeClass_INTERFACE
: {
89 buf
.append( static_cast<sal_Unicode
>('@') );
90 buf
.append( reinterpret_cast< sal_Int64
>(
91 *static_cast< void * const * >(val
) ), 16 );
92 uno::Reference
< lang::XServiceInfo
> xServiceInfo(
93 *static_cast< uno::XInterface
* const * >(val
),
95 if (xServiceInfo
.is()) {
96 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM(
97 " (ImplementationName = \"") );
98 buf
.append( xServiceInfo
->getImplementationName() );
99 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("\")") );
103 case typelib_TypeClass_STRUCT
:
104 case typelib_TypeClass_EXCEPTION
: {
105 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") );
106 typelib_TypeDescription
* typeDescr
= 0;
107 typelib_typedescriptionreference_getDescription( &typeDescr
, typeRef
);
108 if (typeDescr
== 0 || !typelib_typedescription_complete( &typeDescr
)) {
109 appendTypeError( buf
, typeRef
);
112 typelib_CompoundTypeDescription
* compType
=
113 reinterpret_cast< typelib_CompoundTypeDescription
* >(
115 sal_Int32 nDescr
= compType
->nMembers
;
117 if (compType
->pBaseTypeDescription
) {
119 buf
, val
, reinterpret_cast<
120 typelib_TypeDescription
* >(
121 compType
->pBaseTypeDescription
)->pWeakRef
, false );
123 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM(", ") );
126 typelib_TypeDescriptionReference
** ppTypeRefs
=
127 compType
->ppTypeRefs
;
128 sal_Int32
* memberOffsets
= compType
->pMemberOffsets
;
129 rtl_uString
** ppMemberNames
= compType
->ppMemberNames
;
131 for ( sal_Int32 nPos
= 0; nPos
< nDescr
; ++nPos
)
133 buf
.append( ppMemberNames
[ nPos
] );
134 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM(" = ") );
135 typelib_TypeDescription
* memberType
= 0;
136 TYPELIB_DANGER_GET( &memberType
, ppTypeRefs
[ nPos
] );
137 if (memberType
== 0) {
138 appendTypeError( buf
, ppTypeRefs
[ nPos
] );
142 static_cast< char const * >(
143 val
) + memberOffsets
[ nPos
],
144 memberType
->pWeakRef
, true );
145 TYPELIB_DANGER_RELEASE( memberType
);
147 if (nPos
< (nDescr
- 1))
148 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM(", ") );
151 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") );
153 typelib_typedescription_release( typeDescr
);
156 case typelib_TypeClass_SEQUENCE
: {
157 typelib_TypeDescription
* typeDescr
= 0;
158 TYPELIB_DANGER_GET( &typeDescr
, typeRef
);
159 if (typeDescr
== 0) {
160 appendTypeError( buf
,typeRef
);
163 typelib_TypeDescriptionReference
* elementTypeRef
=
165 typelib_IndirectTypeDescription
* >(typeDescr
)->pType
;
166 typelib_TypeDescription
* elementTypeDescr
= 0;
167 TYPELIB_DANGER_GET( &elementTypeDescr
, elementTypeRef
);
168 if (elementTypeDescr
== 0)
170 appendTypeError( buf
, elementTypeRef
);
174 sal_Int32 nElementSize
= elementTypeDescr
->nSize
;
176 *static_cast< uno_Sequence
* const * >(val
);
177 sal_Int32 nElements
= seq
->nElements
;
181 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") );
182 char const * pElements
= seq
->elements
;
183 for ( sal_Int32 nPos
= 0; nPos
< nElements
; ++nPos
)
186 buf
, pElements
+ (nElementSize
* nPos
),
187 elementTypeDescr
->pWeakRef
, false );
188 if (nPos
< (nElements
- 1))
189 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM(", ") );
191 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") );
195 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("{}") );
197 TYPELIB_DANGER_RELEASE( elementTypeDescr
);
199 TYPELIB_DANGER_RELEASE( typeDescr
);
203 case typelib_TypeClass_ANY
: {
204 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") );
205 uno_Any
const * pAny
= static_cast< uno_Any
const * >(val
);
206 appendValue( buf
, pAny
->pData
, pAny
->pType
, true );
207 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") );
210 case typelib_TypeClass_TYPE
:
211 buf
.append( (*reinterpret_cast<
212 typelib_TypeDescriptionReference
* const * >(val
)
215 case typelib_TypeClass_STRING
: {
216 buf
.append( static_cast< sal_Unicode
>('\"') );
217 rtl::OUString
const & str
= rtl::OUString::unacquired(
218 static_cast< rtl_uString
* const * >(val
) );
219 sal_Int32 len
= str
.getLength();
220 for ( sal_Int32 pos
= 0; pos
< len
; ++pos
)
222 sal_Unicode c
= str
[ pos
];
224 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\\"") );
226 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\\\") );
228 appendChar( buf
, c
);
230 buf
.append( static_cast< sal_Unicode
>('\"') );
233 case typelib_TypeClass_ENUM
: {
234 typelib_TypeDescription
* typeDescr
= 0;
235 typelib_typedescriptionreference_getDescription( &typeDescr
, typeRef
);
236 if (typeDescr
== 0 || !typelib_typedescription_complete( &typeDescr
)) {
237 appendTypeError( buf
, typeRef
);
241 sal_Int32
* pValues
=
242 reinterpret_cast< typelib_EnumTypeDescription
* >(
243 typeDescr
)->pEnumValues
;
244 sal_Int32 nPos
= reinterpret_cast< typelib_EnumTypeDescription
* >(
245 typeDescr
)->nEnumValues
;
248 if (pValues
[ nPos
] == *static_cast< int const * >(val
))
253 buf
.append( reinterpret_cast< typelib_EnumTypeDescription
* >(
254 typeDescr
)->ppEnumNames
[ nPos
] );
259 RTL_CONSTASCII_STRINGPARAM("?unknown enum value?") );
263 typelib_typedescription_release( typeDescr
);
266 case typelib_TypeClass_BOOLEAN
:
267 if (*static_cast< sal_Bool
const * >(val
) != sal_False
)
268 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("true") );
270 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("false") );
272 case typelib_TypeClass_CHAR
: {
273 buf
.append( static_cast< sal_Unicode
>('\'') );
274 sal_Unicode c
= *static_cast< sal_Unicode
const * >(val
);
276 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\\'") );
278 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\\\") );
280 appendChar( buf
, c
);
281 buf
.append( static_cast< sal_Unicode
>('\'') );
284 case typelib_TypeClass_FLOAT
:
285 buf
.append( *static_cast< float const * >(val
) );
287 case typelib_TypeClass_DOUBLE
:
288 buf
.append( *static_cast< double const * >(val
) );
290 case typelib_TypeClass_BYTE
:
291 buf
.append( static_cast< sal_Int32
>(
292 *static_cast< sal_Int8
const * >(val
) ) );
294 case typelib_TypeClass_SHORT
:
295 buf
.append( static_cast< sal_Int32
>(
296 *static_cast< sal_Int16
const * >(val
) ) );
298 case typelib_TypeClass_UNSIGNED_SHORT
:
299 buf
.append( static_cast< sal_Int32
>(
300 *static_cast< sal_uInt16
const * >(val
) ) );
302 case typelib_TypeClass_LONG
:
303 buf
.append( *static_cast< sal_Int32
const * >(val
) );
305 case typelib_TypeClass_UNSIGNED_LONG
:
306 buf
.append( static_cast< sal_Int64
>(
307 *static_cast< sal_uInt32
const * >(val
) ) );
309 case typelib_TypeClass_HYPER
:
310 case typelib_TypeClass_UNSIGNED_HYPER
:
311 buf
.append( *static_cast< sal_Int64
const * >(val
) );
313 // case typelib_TypeClass_UNION:
314 // case typelib_TypeClass_ARRAY:
315 // case typelib_TypeClass_UNKNOWN:
316 // case typelib_TypeClass_SERVICE:
317 // case typelib_TypeClass_MODULE:
319 buf
.append( static_cast< sal_Unicode
>('?') );
326 //==============================================================================
327 rtl::OUString
anyToString( uno::Any
const & value
)
329 rtl::OUStringBuffer buf
;
330 appendValue( buf
, value
.getValue(), value
.getValueTypeRef(), true );
331 return buf
.makeStringAndClear();
334 } // namespace comphelper