xref: /aoo42x/main/basic/source/sbx/sbxstr.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_basic.hxx"
30*cdf0e10cSrcweir #include <tools/errcode.hxx>
31*cdf0e10cSrcweir #include <basic/sbx.hxx>
32*cdf0e10cSrcweir #include "sbxconv.hxx"
33*cdf0e10cSrcweir #include "sbxres.hxx"
34*cdf0e10cSrcweir #include "runtime.hxx"
35*cdf0e10cSrcweir #ifndef _RTL_USTRBUF_HXX_
36*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
37*cdf0e10cSrcweir #endif
38*cdf0e10cSrcweir // AB 29.10.99 Unicode
39*cdf0e10cSrcweir #ifndef _USE_NO_NAMESPACE
40*cdf0e10cSrcweir using namespace rtl;
41*cdf0e10cSrcweir #endif
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir // Die Konversion eines Items auf String wird ueber die Put-Methoden
45*cdf0e10cSrcweir // der einzelnen Datentypen abgewickelt, um doppelten Code zu vermeiden.
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir ::rtl::OUString ImpGetString( const SbxValues* p )
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir 	SbxValues aTmp;
50*cdf0e10cSrcweir     ::rtl::OUString aRes;
51*cdf0e10cSrcweir 	aTmp.eType = SbxSTRING;
52*cdf0e10cSrcweir     aTmp.pOUString = &aRes;
53*cdf0e10cSrcweir 	switch( +p->eType )
54*cdf0e10cSrcweir 	{
55*cdf0e10cSrcweir 		case SbxNULL:
56*cdf0e10cSrcweir 			SbxBase::SetError( SbxERR_CONVERSION );
57*cdf0e10cSrcweir 		case SbxEMPTY:
58*cdf0e10cSrcweir 			break;
59*cdf0e10cSrcweir 		case SbxCHAR:
60*cdf0e10cSrcweir 			ImpPutChar( &aTmp, p->nChar ); break;
61*cdf0e10cSrcweir 		case SbxBYTE:
62*cdf0e10cSrcweir 			ImpPutByte( &aTmp, p->nByte ); break;
63*cdf0e10cSrcweir 		case SbxINTEGER:
64*cdf0e10cSrcweir 			ImpPutInteger( &aTmp, p->nInteger ); break;
65*cdf0e10cSrcweir 		case SbxBOOL:
66*cdf0e10cSrcweir 			ImpPutBool( &aTmp, p->nUShort ); break;
67*cdf0e10cSrcweir 		case SbxUSHORT:
68*cdf0e10cSrcweir 			ImpPutUShort( &aTmp, p->nUShort ); break;
69*cdf0e10cSrcweir 		case SbxLONG:
70*cdf0e10cSrcweir 			ImpPutLong( &aTmp, p->nLong ); break;
71*cdf0e10cSrcweir 		case SbxULONG:
72*cdf0e10cSrcweir 			ImpPutULong( &aTmp, p->nULong ); break;
73*cdf0e10cSrcweir 		case SbxSINGLE:
74*cdf0e10cSrcweir 			ImpPutSingle( &aTmp, p->nSingle ); break;
75*cdf0e10cSrcweir 		case SbxDOUBLE:
76*cdf0e10cSrcweir 			ImpPutDouble( &aTmp, p->nDouble ); break;
77*cdf0e10cSrcweir 		case SbxCURRENCY:
78*cdf0e10cSrcweir 			ImpPutCurrency( &aTmp, p->nLong64 ); break;
79*cdf0e10cSrcweir 		case SbxDECIMAL:
80*cdf0e10cSrcweir 		case SbxBYREF | SbxDECIMAL:
81*cdf0e10cSrcweir 			ImpPutDecimal( &aTmp, p->pDecimal ); break;
82*cdf0e10cSrcweir 		case SbxSALINT64:
83*cdf0e10cSrcweir 			ImpPutInt64( &aTmp, p->nInt64 ); break;
84*cdf0e10cSrcweir 		case SbxSALUINT64:
85*cdf0e10cSrcweir 			ImpPutUInt64( &aTmp, p->uInt64 ); break;
86*cdf0e10cSrcweir 		case SbxBYREF | SbxSTRING:
87*cdf0e10cSrcweir 		case SbxSTRING:
88*cdf0e10cSrcweir 		case SbxLPSTR:
89*cdf0e10cSrcweir             if ( p->pOUString )
90*cdf0e10cSrcweir                 *aTmp.pOUString = *p->pOUString;
91*cdf0e10cSrcweir 			break;
92*cdf0e10cSrcweir 		case SbxOBJECT:
93*cdf0e10cSrcweir 		{
94*cdf0e10cSrcweir 			SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
95*cdf0e10cSrcweir 			if( pVal )
96*cdf0e10cSrcweir 				aRes = pVal->GetString();
97*cdf0e10cSrcweir 			else if( p->pObj && p->pObj->IsFixed()
98*cdf0e10cSrcweir 					&& (p->pObj->GetType() == (SbxARRAY | SbxBYTE )) )
99*cdf0e10cSrcweir 			{
100*cdf0e10cSrcweir 				// convert byte array to string
101*cdf0e10cSrcweir 				SbxArray* pArr = PTR_CAST(SbxArray, p->pObj);
102*cdf0e10cSrcweir 				if( pArr )
103*cdf0e10cSrcweir 					aRes = ByteArrayToString( pArr );
104*cdf0e10cSrcweir 			}
105*cdf0e10cSrcweir 			else
106*cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_NO_OBJECT );
107*cdf0e10cSrcweir 			break;
108*cdf0e10cSrcweir 		}
109*cdf0e10cSrcweir 		case SbxERROR:
110*cdf0e10cSrcweir 			// Hier wird der String "Error n" erzeugt
111*cdf0e10cSrcweir 			aRes = SbxRes( STRING_ERRORMSG );
112*cdf0e10cSrcweir 			aRes += ::rtl::OUString( p->nUShort ); break;
113*cdf0e10cSrcweir 		case SbxDATE:
114*cdf0e10cSrcweir 			ImpPutDate( &aTmp, p->nDouble ); break;
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir 		case SbxBYREF | SbxCHAR:
117*cdf0e10cSrcweir 			ImpPutChar( &aTmp, *p->pChar ); break;
118*cdf0e10cSrcweir 		case SbxBYREF | SbxBYTE:
119*cdf0e10cSrcweir 			ImpPutByte( &aTmp, *p->pByte ); break;
120*cdf0e10cSrcweir 		case SbxBYREF | SbxINTEGER:
121*cdf0e10cSrcweir 		case SbxBYREF | SbxBOOL:
122*cdf0e10cSrcweir 			ImpPutInteger( &aTmp, *p->pInteger ); break;
123*cdf0e10cSrcweir 		case SbxBYREF | SbxLONG:
124*cdf0e10cSrcweir 			ImpPutLong( &aTmp, *p->pLong ); break;
125*cdf0e10cSrcweir 		case SbxBYREF | SbxULONG:
126*cdf0e10cSrcweir 			ImpPutULong( &aTmp, *p->pULong ); break;
127*cdf0e10cSrcweir 		case SbxBYREF | SbxERROR:
128*cdf0e10cSrcweir 		case SbxBYREF | SbxUSHORT:
129*cdf0e10cSrcweir 			ImpPutUShort( &aTmp, *p->pUShort ); break;
130*cdf0e10cSrcweir 		case SbxBYREF | SbxSINGLE:
131*cdf0e10cSrcweir 			ImpPutSingle( &aTmp, *p->pSingle ); break;
132*cdf0e10cSrcweir 		case SbxBYREF | SbxDATE:
133*cdf0e10cSrcweir 		case SbxBYREF | SbxDOUBLE:
134*cdf0e10cSrcweir 			ImpPutDouble( &aTmp, *p->pDouble ); break;
135*cdf0e10cSrcweir 		case SbxBYREF | SbxCURRENCY:
136*cdf0e10cSrcweir 			ImpPutCurrency( &aTmp, *p->pLong64 ); break;
137*cdf0e10cSrcweir 		case SbxBYREF | SbxSALINT64:
138*cdf0e10cSrcweir 			ImpPutInt64( &aTmp, *p->pnInt64 ); break;
139*cdf0e10cSrcweir 		case SbxBYREF | SbxSALUINT64:
140*cdf0e10cSrcweir 			ImpPutUInt64( &aTmp, *p->puInt64 ); break;
141*cdf0e10cSrcweir 		default:
142*cdf0e10cSrcweir 			SbxBase::SetError( SbxERR_CONVERSION );
143*cdf0e10cSrcweir 	}
144*cdf0e10cSrcweir 	return aRes;
145*cdf0e10cSrcweir }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir // AB 10.4.97, neue Funktion fuer SbxValue::GetCoreString()
148*cdf0e10cSrcweir ::rtl::OUString ImpGetCoreString( const SbxValues* p )
149*cdf0e10cSrcweir {
150*cdf0e10cSrcweir 	// Vorerst nur fuer double
151*cdf0e10cSrcweir 	if( ( p->eType & (~SbxBYREF) ) == SbxDOUBLE )
152*cdf0e10cSrcweir 	{
153*cdf0e10cSrcweir 		SbxValues aTmp;
154*cdf0e10cSrcweir 		XubString aRes;
155*cdf0e10cSrcweir 		aTmp.eType = SbxSTRING;
156*cdf0e10cSrcweir 		if( p->eType == SbxDOUBLE )
157*cdf0e10cSrcweir 			ImpPutDouble( &aTmp, p->nDouble, /*bCoreString=*/sal_True );
158*cdf0e10cSrcweir 		else
159*cdf0e10cSrcweir 			ImpPutDouble( &aTmp, *p->pDouble, /*bCoreString=*/sal_True );
160*cdf0e10cSrcweir 		return aRes;
161*cdf0e10cSrcweir 	}
162*cdf0e10cSrcweir 	else
163*cdf0e10cSrcweir 		return ImpGetString( p );
164*cdf0e10cSrcweir }
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir void ImpPutString( SbxValues* p, const ::rtl::OUString* n )
167*cdf0e10cSrcweir {
168*cdf0e10cSrcweir 	SbxValues aTmp;
169*cdf0e10cSrcweir 	aTmp.eType = SbxSTRING;
170*cdf0e10cSrcweir 	::rtl::OUString* pTmp = NULL;
171*cdf0e10cSrcweir 	// Sicherheitshalber, falls ein NULL-Ptr kommt
172*cdf0e10cSrcweir 	if( !n )
173*cdf0e10cSrcweir 		n = pTmp = new ::rtl::OUString;
174*cdf0e10cSrcweir 	aTmp.pOUString = (::rtl::OUString*)n;
175*cdf0e10cSrcweir 	switch( +p->eType )
176*cdf0e10cSrcweir 	{
177*cdf0e10cSrcweir 		case SbxCHAR:
178*cdf0e10cSrcweir 			p->nChar = ImpGetChar( &aTmp ); break;
179*cdf0e10cSrcweir 		case SbxBYTE:
180*cdf0e10cSrcweir 			p->nByte = ImpGetByte( &aTmp ); break;
181*cdf0e10cSrcweir 		case SbxINTEGER:
182*cdf0e10cSrcweir 		case SbxBOOL:
183*cdf0e10cSrcweir 			p->nInteger = ImpGetInteger( &aTmp ); break;
184*cdf0e10cSrcweir 		case SbxLONG:
185*cdf0e10cSrcweir 			p->nLong = ImpGetLong( &aTmp ); break;
186*cdf0e10cSrcweir 		case SbxULONG:
187*cdf0e10cSrcweir 			p->nULong = ImpGetULong( &aTmp ); break;
188*cdf0e10cSrcweir 		case SbxERROR:
189*cdf0e10cSrcweir 		case SbxUSHORT:
190*cdf0e10cSrcweir 			p->nUShort = ImpGetUShort( &aTmp ); break;
191*cdf0e10cSrcweir 		case SbxSINGLE:
192*cdf0e10cSrcweir 			p->nSingle = ImpGetSingle( &aTmp ); break;
193*cdf0e10cSrcweir 		case SbxDATE:
194*cdf0e10cSrcweir 			p->nDouble = ImpGetDate( &aTmp ); break;
195*cdf0e10cSrcweir 		case SbxDOUBLE:
196*cdf0e10cSrcweir 			p->nDouble = ImpGetDouble( &aTmp ); break;
197*cdf0e10cSrcweir 		case SbxULONG64:
198*cdf0e10cSrcweir 			p->nLong64 = ImpGetCurrency( &aTmp ); break;
199*cdf0e10cSrcweir 		case SbxDECIMAL:
200*cdf0e10cSrcweir 		case SbxBYREF | SbxDECIMAL:
201*cdf0e10cSrcweir 			releaseDecimalPtr( p->pDecimal );
202*cdf0e10cSrcweir 			p->pDecimal = ImpGetDecimal( &aTmp ); break;
203*cdf0e10cSrcweir 		case SbxSALINT64:
204*cdf0e10cSrcweir 			p->nInt64 = ImpGetInt64( &aTmp ); break;
205*cdf0e10cSrcweir 		case SbxSALUINT64:
206*cdf0e10cSrcweir 			p->uInt64 = ImpGetUInt64( &aTmp ); break;
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir 		case SbxBYREF | SbxSTRING:
209*cdf0e10cSrcweir 		case SbxSTRING:
210*cdf0e10cSrcweir 		case SbxLPSTR:
211*cdf0e10cSrcweir 			if( n->getLength() )
212*cdf0e10cSrcweir 			{
213*cdf0e10cSrcweir 				if( !p->pOUString )
214*cdf0e10cSrcweir 					p->pOUString = new ::rtl::OUString( *n );
215*cdf0e10cSrcweir                 else
216*cdf0e10cSrcweir                     *p->pOUString = *n;
217*cdf0e10cSrcweir 			}
218*cdf0e10cSrcweir 			else
219*cdf0e10cSrcweir 				delete p->pOUString, p->pOUString = NULL;
220*cdf0e10cSrcweir 			break;
221*cdf0e10cSrcweir 		case SbxOBJECT:
222*cdf0e10cSrcweir 		{
223*cdf0e10cSrcweir 			SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
224*cdf0e10cSrcweir 			if( pVal )
225*cdf0e10cSrcweir 				pVal->PutString( *n );
226*cdf0e10cSrcweir 			else
227*cdf0e10cSrcweir 				SbxBase::SetError( SbxERR_NO_OBJECT );
228*cdf0e10cSrcweir 			break;
229*cdf0e10cSrcweir 		}
230*cdf0e10cSrcweir 		case SbxBYREF | SbxCHAR:
231*cdf0e10cSrcweir 			*p->pChar = ImpGetChar( p ); break;
232*cdf0e10cSrcweir 		case SbxBYREF | SbxBYTE:
233*cdf0e10cSrcweir 			*p->pByte = ImpGetByte( p ); break;
234*cdf0e10cSrcweir 		case SbxBYREF | SbxINTEGER:
235*cdf0e10cSrcweir 			*p->pInteger = ImpGetInteger( p ); break;
236*cdf0e10cSrcweir 		case SbxBYREF | SbxBOOL:
237*cdf0e10cSrcweir 			*p->pUShort = sal::static_int_cast< sal_uInt16 >( ImpGetBool( p ) );
238*cdf0e10cSrcweir             break;
239*cdf0e10cSrcweir 		case SbxBYREF | SbxERROR:
240*cdf0e10cSrcweir 		case SbxBYREF | SbxUSHORT:
241*cdf0e10cSrcweir 			*p->pUShort = ImpGetUShort( p ); break;
242*cdf0e10cSrcweir 		case SbxBYREF | SbxLONG:
243*cdf0e10cSrcweir 			*p->pLong = ImpGetLong( p ); break;
244*cdf0e10cSrcweir 		case SbxBYREF | SbxULONG:
245*cdf0e10cSrcweir 			*p->pULong = ImpGetULong( p ); break;
246*cdf0e10cSrcweir 		case SbxBYREF | SbxSINGLE:
247*cdf0e10cSrcweir 			*p->pSingle = ImpGetSingle( p ); break;
248*cdf0e10cSrcweir 		case SbxBYREF | SbxDATE:
249*cdf0e10cSrcweir 			*p->pDouble = ImpGetDate( p ); break;
250*cdf0e10cSrcweir 		case SbxBYREF | SbxDOUBLE:
251*cdf0e10cSrcweir 			*p->pDouble = ImpGetDouble( p ); break;
252*cdf0e10cSrcweir 		case SbxBYREF | SbxCURRENCY:
253*cdf0e10cSrcweir 			*p->pLong64 = ImpGetCurrency( p ); break;
254*cdf0e10cSrcweir 		default:
255*cdf0e10cSrcweir 			SbxBase::SetError( SbxERR_CONVERSION );
256*cdf0e10cSrcweir 	}
257*cdf0e10cSrcweir 	delete pTmp;
258*cdf0e10cSrcweir }
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir // Convert string to an array of bytes, preserving unicode (2bytes per character)
261*cdf0e10cSrcweir SbxArray* StringToByteArray(const ::rtl::OUString& rStr)
262*cdf0e10cSrcweir {
263*cdf0e10cSrcweir 	sal_Int32 nArraySize = rStr.getLength() * 2;
264*cdf0e10cSrcweir 	const sal_Unicode* pSrc = rStr.getStr();
265*cdf0e10cSrcweir 	SbxDimArray* pArray = new SbxDimArray(SbxBYTE);
266*cdf0e10cSrcweir 	bool bIncIndex = ( IsBaseIndexOne() && SbiRuntime::isVBAEnabled() );
267*cdf0e10cSrcweir 	if( nArraySize )
268*cdf0e10cSrcweir 	{
269*cdf0e10cSrcweir 		if( bIncIndex )
270*cdf0e10cSrcweir 			pArray->AddDim32( 1, nArraySize );
271*cdf0e10cSrcweir 		else
272*cdf0e10cSrcweir 			pArray->AddDim32( 0, nArraySize-1 );
273*cdf0e10cSrcweir 	}
274*cdf0e10cSrcweir 	else
275*cdf0e10cSrcweir 	{
276*cdf0e10cSrcweir 		pArray->unoAddDim( 0, -1 );
277*cdf0e10cSrcweir 	}
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir 	for( sal_uInt16	i=0; i< nArraySize; i++)
280*cdf0e10cSrcweir 	{
281*cdf0e10cSrcweir 		SbxVariable* pNew = new SbxVariable( SbxBYTE );
282*cdf0e10cSrcweir 		sal_uInt8 aByte = static_cast< sal_uInt8 >( i%2 ? ((*pSrc) >> 8) & 0xff : (*pSrc) & 0xff );
283*cdf0e10cSrcweir 		pNew->PutByte( aByte );
284*cdf0e10cSrcweir 		pNew->SetFlag( SBX_WRITE );
285*cdf0e10cSrcweir 		pArray->Put( pNew, i );
286*cdf0e10cSrcweir 		if( i%2 )
287*cdf0e10cSrcweir 			pSrc++;
288*cdf0e10cSrcweir 	}
289*cdf0e10cSrcweir 	return pArray;
290*cdf0e10cSrcweir }
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir // Convert an array of bytes to string (2bytes per character)
293*cdf0e10cSrcweir ::rtl::OUString ByteArrayToString(SbxArray* pArr)
294*cdf0e10cSrcweir {
295*cdf0e10cSrcweir 	sal_uInt16 nCount = pArr->Count();
296*cdf0e10cSrcweir 	OUStringBuffer aStrBuf;
297*cdf0e10cSrcweir 	sal_Unicode aChar = 0;
298*cdf0e10cSrcweir 	for( sal_uInt16 i = 0 ; i < nCount ; i++ )
299*cdf0e10cSrcweir 	{
300*cdf0e10cSrcweir 		sal_Unicode aTempChar = pArr->Get(i)->GetByte();
301*cdf0e10cSrcweir 		if( i%2 )
302*cdf0e10cSrcweir 		{
303*cdf0e10cSrcweir 			aChar = (aTempChar << 8 ) | aChar;
304*cdf0e10cSrcweir 			aStrBuf.append(aChar);
305*cdf0e10cSrcweir 			aChar = 0;
306*cdf0e10cSrcweir 		}
307*cdf0e10cSrcweir 		else
308*cdf0e10cSrcweir 		{
309*cdf0e10cSrcweir 			aChar = aTempChar;
310*cdf0e10cSrcweir 		}
311*cdf0e10cSrcweir 	}
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir 	if( nCount%2 )
314*cdf0e10cSrcweir 	{
315*cdf0e10cSrcweir 		aStrBuf.append(aChar);
316*cdf0e10cSrcweir 	}
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir 	return aStrBuf.makeStringAndClear();
319*cdf0e10cSrcweir }
320