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