Update ooo320-m1
[ooovba.git] / comphelper / source / misc / anytostring.cxx
blobd98e640ccb7a14b1365f6a7d52181d61d7eca085
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: anytostring.cxx,v $
10 * $Revision: 1.8 $
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 {
43 namespace {
45 void appendTypeError(
46 rtl::OUStringBuffer & buf, typelib_TypeDescriptionReference * typeRef )
48 buf.appendAscii(
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') );
62 buf.append( s );
64 else {
65 buf.append( c );
69 //------------------------------------------------------------------------------
70 void appendValue( rtl::OUStringBuffer & buf,
71 void const * val, typelib_TypeDescriptionReference * typeRef,
72 bool prependType )
74 if (typeRef->eTypeClass == typelib_TypeClass_VOID) {
75 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("void") );
76 return;
78 OSL_ASSERT( val != 0 );
80 if (prependType &&
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),
97 uno::UNO_QUERY );
98 if (xServiceInfo.is()) {
99 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(
100 " (ImplementationName = \"") );
101 buf.append( xServiceInfo->getImplementationName() );
102 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\")") );
104 break;
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 );
114 else {
115 typelib_CompoundTypeDescription * compType =
116 reinterpret_cast< typelib_CompoundTypeDescription * >(
117 typeDescr );
118 sal_Int32 nDescr = compType->nMembers;
120 if (compType->pBaseTypeDescription) {
121 appendValue(
122 buf, val, reinterpret_cast<
123 typelib_TypeDescription * >(
124 compType->pBaseTypeDescription)->pWeakRef, false );
125 if (nDescr > 0)
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 ] );
143 else {
144 appendValue( buf,
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(" }") );
155 if (typeDescr != 0)
156 typelib_typedescription_release( typeDescr );
157 break;
159 case typelib_TypeClass_SEQUENCE: {
160 typelib_TypeDescription * typeDescr = 0;
161 TYPELIB_DANGER_GET( &typeDescr, typeRef );
162 if (typeDescr == 0) {
163 appendTypeError( buf,typeRef );
165 else {
166 typelib_TypeDescriptionReference * elementTypeRef =
167 reinterpret_cast<
168 typelib_IndirectTypeDescription * >(typeDescr)->pType;
169 typelib_TypeDescription * elementTypeDescr = 0;
170 TYPELIB_DANGER_GET( &elementTypeDescr, elementTypeRef );
171 if (elementTypeDescr == 0)
173 appendTypeError( buf, elementTypeRef );
175 else
177 sal_Int32 nElementSize = elementTypeDescr->nSize;
178 uno_Sequence * seq =
179 *static_cast< uno_Sequence * const * >(val);
180 sal_Int32 nElements = seq->nElements;
182 if (nElements > 0)
184 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{ ") );
185 char const * pElements = seq->elements;
186 for ( sal_Int32 nPos = 0; nPos < nElements; ++nPos )
188 appendValue(
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(" }") );
196 else
198 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("{}") );
200 TYPELIB_DANGER_RELEASE( elementTypeDescr );
202 TYPELIB_DANGER_RELEASE( typeDescr );
204 break;
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(" }") );
211 break;
213 case typelib_TypeClass_TYPE:
214 buf.append( (*reinterpret_cast<
215 typelib_TypeDescriptionReference * const * >(val)
216 )->pTypeName );
217 break;
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 ];
226 if (c == '\"')
227 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\\"") );
228 else if (c == '\\')
229 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\\\") );
230 else
231 appendChar( buf, c );
233 buf.append( static_cast< sal_Unicode >('\"') );
234 break;
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 );
242 else
244 sal_Int32 * pValues =
245 reinterpret_cast< typelib_EnumTypeDescription * >(
246 typeDescr )->pEnumValues;
247 sal_Int32 nPos = reinterpret_cast< typelib_EnumTypeDescription * >(
248 typeDescr )->nEnumValues;
249 while (nPos--)
251 if (pValues[ nPos ] == *static_cast< int const * >(val))
252 break;
254 if (nPos >= 0)
256 buf.append( reinterpret_cast< typelib_EnumTypeDescription * >(
257 typeDescr )->ppEnumNames[ nPos ] );
259 else
261 buf.appendAscii(
262 RTL_CONSTASCII_STRINGPARAM("?unknown enum value?") );
265 if (typeDescr != 0)
266 typelib_typedescription_release( typeDescr );
267 break;
269 case typelib_TypeClass_BOOLEAN:
270 if (*static_cast< sal_Bool const * >(val) != sal_False)
271 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("true") );
272 else
273 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("false") );
274 break;
275 case typelib_TypeClass_CHAR: {
276 buf.append( static_cast< sal_Unicode >('\'') );
277 sal_Unicode c = *static_cast< sal_Unicode const * >(val);
278 if (c == '\'')
279 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\\'") );
280 else if (c == '\\')
281 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\\\\") );
282 else
283 appendChar( buf, c );
284 buf.append( static_cast< sal_Unicode >('\'') );
285 break;
287 case typelib_TypeClass_FLOAT:
288 buf.append( *static_cast< float const * >(val) );
289 break;
290 case typelib_TypeClass_DOUBLE:
291 buf.append( *static_cast< double const * >(val) );
292 break;
293 case typelib_TypeClass_BYTE:
294 buf.append( static_cast< sal_Int32 >(
295 *static_cast< sal_Int8 const * >(val) ) );
296 break;
297 case typelib_TypeClass_SHORT:
298 buf.append( static_cast< sal_Int32 >(
299 *static_cast< sal_Int16 const * >(val) ) );
300 break;
301 case typelib_TypeClass_UNSIGNED_SHORT:
302 buf.append( static_cast< sal_Int32 >(
303 *static_cast< sal_uInt16 const * >(val) ) );
304 break;
305 case typelib_TypeClass_LONG:
306 buf.append( *static_cast< sal_Int32 const * >(val) );
307 break;
308 case typelib_TypeClass_UNSIGNED_LONG:
309 buf.append( static_cast< sal_Int64 >(
310 *static_cast< sal_uInt32 const * >(val) ) );
311 break;
312 case typelib_TypeClass_HYPER:
313 case typelib_TypeClass_UNSIGNED_HYPER:
314 buf.append( *static_cast< sal_Int64 const * >(val) );
315 break;
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:
321 default:
322 buf.append( static_cast< sal_Unicode >('?') );
323 break;
327 } // anon namespace
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