1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: anytostring.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
34 #include "comphelper/anytostring.hxx"
35 #include "osl/diagnose.h"
36 #include "rtl/ustrbuf.hxx"
37 #include "typelib/typedescription.h"
38 #include "com/sun/star/lang/XServiceInfo.hpp"
40 using namespace ::com::sun::star
;
42 namespace comphelper
{
46 rtl::OUStringBuffer
& buf
, typelib_TypeDescriptionReference
* typeRef
)
49 RTL_CONSTASCII_STRINGPARAM("<cannot get type description of type ") );
50 buf
.append( rtl::OUString::unacquired( &typeRef
->pTypeName
) );
51 buf
.append( static_cast< sal_Unicode
>('>') );
54 inline void appendChar( rtl::OUStringBuffer
& buf
, sal_Unicode c
)
56 if (c
< ' ' || c
> '~') {
57 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\X") );
58 rtl::OUString
const s(
59 rtl::OUString::valueOf( static_cast< sal_Int32
>(c
), 16 ) );
60 for ( sal_Int32 f
= 4 - s
.getLength(); f
> 0; --f
)
61 buf
.append( static_cast< sal_Unicode
>('0') );
69 //------------------------------------------------------------------------------
70 void appendValue( rtl::OUStringBuffer
& buf
,
71 void const * val
, typelib_TypeDescriptionReference
* typeRef
,
74 if (typeRef
->eTypeClass
== typelib_TypeClass_VOID
) {
75 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("void") );
78 OSL_ASSERT( val
!= 0 );
81 typeRef
->eTypeClass
!= typelib_TypeClass_STRING
&&
82 typeRef
->eTypeClass
!= typelib_TypeClass_CHAR
&&
83 typeRef
->eTypeClass
!= typelib_TypeClass_BOOLEAN
)
85 buf
.append( static_cast< sal_Unicode
>('(') );
86 buf
.append( rtl::OUString::unacquired( &typeRef
->pTypeName
) );
87 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM(") ") );
90 switch (typeRef
->eTypeClass
) {
91 case typelib_TypeClass_INTERFACE
: {
92 buf
.append( static_cast<sal_Unicode
>('@') );
93 buf
.append( reinterpret_cast< sal_Int64
>(
94 *static_cast< void * const * >(val
) ), 16 );
95 uno::Reference
< lang::XServiceInfo
> xServiceInfo(
96 *static_cast< uno::XInterface
* const * >(val
),
98 if (xServiceInfo
.is()) {
99 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM(
100 " (ImplementationName = \"") );
101 buf
.append( xServiceInfo
->getImplementationName() );
102 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("\")") );
106 case typelib_TypeClass_STRUCT
:
107 case typelib_TypeClass_EXCEPTION
: {
108 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") );
109 typelib_TypeDescription
* typeDescr
= 0;
110 typelib_typedescriptionreference_getDescription( &typeDescr
, typeRef
);
111 if (typeDescr
== 0 || !typelib_typedescription_complete( &typeDescr
)) {
112 appendTypeError( buf
, typeRef
);
115 typelib_CompoundTypeDescription
* compType
=
116 reinterpret_cast< typelib_CompoundTypeDescription
* >(
118 sal_Int32 nDescr
= compType
->nMembers
;
120 if (compType
->pBaseTypeDescription
) {
122 buf
, val
, reinterpret_cast<
123 typelib_TypeDescription
* >(
124 compType
->pBaseTypeDescription
)->pWeakRef
, false );
126 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM(", ") );
129 typelib_TypeDescriptionReference
** ppTypeRefs
=
130 compType
->ppTypeRefs
;
131 sal_Int32
* memberOffsets
= compType
->pMemberOffsets
;
132 rtl_uString
** ppMemberNames
= compType
->ppMemberNames
;
134 for ( sal_Int32 nPos
= 0; nPos
< nDescr
; ++nPos
)
136 buf
.append( ppMemberNames
[ nPos
] );
137 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM(" = ") );
138 typelib_TypeDescription
* memberType
= 0;
139 TYPELIB_DANGER_GET( &memberType
, ppTypeRefs
[ nPos
] );
140 if (memberType
== 0) {
141 appendTypeError( buf
, ppTypeRefs
[ nPos
] );
145 static_cast< char const * >(
146 val
) + memberOffsets
[ nPos
],
147 memberType
->pWeakRef
, true );
148 TYPELIB_DANGER_RELEASE( memberType
);
150 if (nPos
< (nDescr
- 1))
151 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM(", ") );
154 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") );
156 typelib_typedescription_release( typeDescr
);
159 case typelib_TypeClass_SEQUENCE
: {
160 typelib_TypeDescription
* typeDescr
= 0;
161 TYPELIB_DANGER_GET( &typeDescr
, typeRef
);
162 if (typeDescr
== 0) {
163 appendTypeError( buf
,typeRef
);
166 typelib_TypeDescriptionReference
* elementTypeRef
=
168 typelib_IndirectTypeDescription
* >(typeDescr
)->pType
;
169 typelib_TypeDescription
* elementTypeDescr
= 0;
170 TYPELIB_DANGER_GET( &elementTypeDescr
, elementTypeRef
);
171 if (elementTypeDescr
== 0)
173 appendTypeError( buf
, elementTypeRef
);
177 sal_Int32 nElementSize
= elementTypeDescr
->nSize
;
179 *static_cast< uno_Sequence
* const * >(val
);
180 sal_Int32 nElements
= seq
->nElements
;
184 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") );
185 char const * pElements
= seq
->elements
;
186 for ( sal_Int32 nPos
= 0; nPos
< nElements
; ++nPos
)
189 buf
, pElements
+ (nElementSize
* nPos
),
190 elementTypeDescr
->pWeakRef
, false );
191 if (nPos
< (nElements
- 1))
192 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM(", ") );
194 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") );
198 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("{}") );
200 TYPELIB_DANGER_RELEASE( elementTypeDescr
);
202 TYPELIB_DANGER_RELEASE( typeDescr
);
206 case typelib_TypeClass_ANY
: {
207 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") );
208 uno_Any
const * pAny
= static_cast< uno_Any
const * >(val
);
209 appendValue( buf
, pAny
->pData
, pAny
->pType
, true );
210 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM(" }") );
213 case typelib_TypeClass_TYPE
:
214 buf
.append( (*reinterpret_cast<
215 typelib_TypeDescriptionReference
* const * >(val
)
218 case typelib_TypeClass_STRING
: {
219 buf
.append( static_cast< sal_Unicode
>('\"') );
220 rtl::OUString
const & str
= rtl::OUString::unacquired(
221 static_cast< rtl_uString
* const * >(val
) );
222 sal_Int32 len
= str
.getLength();
223 for ( sal_Int32 pos
= 0; pos
< len
; ++pos
)
225 sal_Unicode c
= str
[ pos
];
227 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\\"") );
229 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\\\") );
231 appendChar( buf
, c
);
233 buf
.append( static_cast< sal_Unicode
>('\"') );
236 case typelib_TypeClass_ENUM
: {
237 typelib_TypeDescription
* typeDescr
= 0;
238 typelib_typedescriptionreference_getDescription( &typeDescr
, typeRef
);
239 if (typeDescr
== 0 || !typelib_typedescription_complete( &typeDescr
)) {
240 appendTypeError( buf
, typeRef
);
244 sal_Int32
* pValues
=
245 reinterpret_cast< typelib_EnumTypeDescription
* >(
246 typeDescr
)->pEnumValues
;
247 sal_Int32 nPos
= reinterpret_cast< typelib_EnumTypeDescription
* >(
248 typeDescr
)->nEnumValues
;
251 if (pValues
[ nPos
] == *static_cast< int const * >(val
))
256 buf
.append( reinterpret_cast< typelib_EnumTypeDescription
* >(
257 typeDescr
)->ppEnumNames
[ nPos
] );
262 RTL_CONSTASCII_STRINGPARAM("?unknown enum value?") );
266 typelib_typedescription_release( typeDescr
);
269 case typelib_TypeClass_BOOLEAN
:
270 if (*static_cast< sal_Bool
const * >(val
) != sal_False
)
271 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("true") );
273 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("false") );
275 case typelib_TypeClass_CHAR
: {
276 buf
.append( static_cast< sal_Unicode
>('\'') );
277 sal_Unicode c
= *static_cast< sal_Unicode
const * >(val
);
279 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\\'") );
281 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\\\") );
283 appendChar( buf
, c
);
284 buf
.append( static_cast< sal_Unicode
>('\'') );
287 case typelib_TypeClass_FLOAT
:
288 buf
.append( *static_cast< float const * >(val
) );
290 case typelib_TypeClass_DOUBLE
:
291 buf
.append( *static_cast< double const * >(val
) );
293 case typelib_TypeClass_BYTE
:
294 buf
.append( static_cast< sal_Int32
>(
295 *static_cast< sal_Int8
const * >(val
) ) );
297 case typelib_TypeClass_SHORT
:
298 buf
.append( static_cast< sal_Int32
>(
299 *static_cast< sal_Int16
const * >(val
) ) );
301 case typelib_TypeClass_UNSIGNED_SHORT
:
302 buf
.append( static_cast< sal_Int32
>(
303 *static_cast< sal_uInt16
const * >(val
) ) );
305 case typelib_TypeClass_LONG
:
306 buf
.append( *static_cast< sal_Int32
const * >(val
) );
308 case typelib_TypeClass_UNSIGNED_LONG
:
309 buf
.append( static_cast< sal_Int64
>(
310 *static_cast< sal_uInt32
const * >(val
) ) );
312 case typelib_TypeClass_HYPER
:
313 case typelib_TypeClass_UNSIGNED_HYPER
:
314 buf
.append( *static_cast< sal_Int64
const * >(val
) );
316 // case typelib_TypeClass_UNION:
317 // case typelib_TypeClass_ARRAY:
318 // case typelib_TypeClass_UNKNOWN:
319 // case typelib_TypeClass_SERVICE:
320 // case typelib_TypeClass_MODULE:
322 buf
.append( static_cast< sal_Unicode
>('?') );
329 //==============================================================================
330 rtl::OUString
anyToString( uno::Any
const & value
)
332 rtl::OUStringBuffer buf
;
333 appendValue( buf
, value
.getValue(), value
.getValueTypeRef(), true );
334 return buf
.makeStringAndClear();
337 } // namespace comphelper