fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / basic / source / sbx / sbxstr.cxx
blob24175fb1138d932ddf09a1ca28669cd5bcab2f5d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <tools/errcode.hxx>
21 #include <basic/sbx.hxx>
22 #include "sbxconv.hxx"
23 #include "sbxres.hxx"
24 #include "runtime.hxx"
25 #include <rtl/ustrbuf.hxx>
27 // The conversion of an item onto String was handled via the Put-Methods
28 // of the several data types to avoid double code.
30 OUString ImpGetString( const SbxValues* p )
32 SbxValues aTmp;
33 OUString aRes;
34 aTmp.eType = SbxSTRING;
35 aTmp.pOUString = &aRes;
36 switch( +p->eType )
38 case SbxNULL:
39 SbxBase::SetError( SbxERR_CONVERSION );
40 case SbxEMPTY:
41 break;
42 case SbxCHAR:
43 ImpPutChar( &aTmp, p->nChar ); break;
44 case SbxBYTE:
45 ImpPutByte( &aTmp, p->nByte ); break;
46 case SbxINTEGER:
47 ImpPutInteger( &aTmp, p->nInteger ); break;
48 case SbxBOOL:
49 ImpPutBool( &aTmp, p->nUShort ); break;
50 case SbxUSHORT:
51 ImpPutUShort( &aTmp, p->nUShort ); break;
52 case SbxLONG:
53 ImpPutLong( &aTmp, p->nLong ); break;
54 case SbxULONG:
55 ImpPutULong( &aTmp, p->nULong ); break;
56 case SbxSINGLE:
57 ImpPutSingle( &aTmp, p->nSingle ); break;
58 case SbxDOUBLE:
59 ImpPutDouble( &aTmp, p->nDouble ); break;
60 case SbxCURRENCY:
61 ImpPutCurrency( &aTmp, p->nInt64 ); break;
62 case SbxDECIMAL:
63 case SbxBYREF | SbxDECIMAL:
64 ImpPutDecimal( &aTmp, p->pDecimal ); break;
65 case SbxSALINT64:
66 ImpPutInt64( &aTmp, p->nInt64 ); break;
67 case SbxSALUINT64:
68 ImpPutUInt64( &aTmp, p->uInt64 ); break;
69 case SbxBYREF | SbxSTRING:
70 case SbxSTRING:
71 case SbxLPSTR:
72 if ( p->pOUString )
74 *aTmp.pOUString = *p->pOUString;
76 break;
77 case SbxOBJECT:
79 SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
80 if( pVal )
82 aRes = pVal->GetOUString();
84 else if( p->pObj && p->pObj->IsFixed()
85 && (p->pObj->GetType() == (SbxARRAY | SbxBYTE )) )
87 // convert byte array to string
88 SbxArray* pArr = PTR_CAST(SbxArray, p->pObj);
89 if( pArr )
91 aRes = ByteArrayToString( pArr );
94 else
96 SbxBase::SetError( SbxERR_NO_OBJECT );
98 break;
100 case SbxERROR:
101 // Here will be created the String "Error n"
102 aRes = SbxRes( STRING_ERRORMSG );
103 aRes += OUString( p->nUShort ); break;
104 case SbxDATE:
105 ImpPutDate( &aTmp, p->nDouble ); break;
107 case SbxBYREF | SbxCHAR:
108 ImpPutChar( &aTmp, *p->pChar ); break;
109 case SbxBYREF | SbxBYTE:
110 ImpPutByte( &aTmp, *p->pByte ); break;
111 case SbxBYREF | SbxINTEGER:
112 case SbxBYREF | SbxBOOL:
113 ImpPutInteger( &aTmp, *p->pInteger ); break;
114 case SbxBYREF | SbxLONG:
115 ImpPutLong( &aTmp, *p->pLong ); break;
116 case SbxBYREF | SbxULONG:
117 ImpPutULong( &aTmp, *p->pULong ); break;
118 case SbxBYREF | SbxERROR:
119 case SbxBYREF | SbxUSHORT:
120 ImpPutUShort( &aTmp, *p->pUShort ); break;
121 case SbxBYREF | SbxSINGLE:
122 ImpPutSingle( &aTmp, *p->pSingle ); break;
123 case SbxBYREF | SbxDATE:
124 case SbxBYREF | SbxDOUBLE:
125 ImpPutDouble( &aTmp, *p->pDouble ); break;
126 case SbxBYREF | SbxCURRENCY:
127 ImpPutCurrency( &aTmp, *p->pnInt64 ); break;
128 case SbxBYREF | SbxSALINT64:
129 ImpPutInt64( &aTmp, *p->pnInt64 ); break;
130 case SbxBYREF | SbxSALUINT64:
131 ImpPutUInt64( &aTmp, *p->puInt64 ); break;
132 default:
133 SbxBase::SetError( SbxERR_CONVERSION );
135 return aRes;
138 // From 1997-04-10, new function for SbxValue::GetCoreString()
139 OUString ImpGetCoreString( const SbxValues* p )
141 // For now only for double
142 if( ( p->eType & (~SbxBYREF) ) == SbxDOUBLE )
144 SbxValues aTmp;
145 OUString aRes;
146 aTmp.eType = SbxSTRING;
147 aTmp.pOUString = &aRes;
148 if( p->eType == SbxDOUBLE )
149 ImpPutDouble( &aTmp, p->nDouble, true ); // true = bCoreString
150 else
151 ImpPutDouble( &aTmp, *p->pDouble, true ); // true = bCoreString
152 return aRes;
154 else
155 return ImpGetString( p );
158 void ImpPutString( SbxValues* p, const OUString* n )
160 SbxValues aTmp;
161 aTmp.eType = SbxSTRING;
162 OUString* pTmp = NULL;
163 // as a precaution, if a NULL-Ptr appears
164 if( !n )
165 n = pTmp = new OUString;
166 aTmp.pOUString = (OUString*)n;
167 switch( +p->eType )
169 case SbxCHAR:
170 p->nChar = ImpGetChar( &aTmp ); break;
171 case SbxBYTE:
172 p->nByte = ImpGetByte( &aTmp ); break;
173 case SbxINTEGER:
174 case SbxBOOL:
175 p->nInteger = ImpGetInteger( &aTmp ); break;
176 case SbxLONG:
177 p->nLong = ImpGetLong( &aTmp ); break;
178 case SbxULONG:
179 p->nULong = ImpGetULong( &aTmp ); break;
180 case SbxERROR:
181 case SbxUSHORT:
182 p->nUShort = ImpGetUShort( &aTmp ); break;
183 case SbxSINGLE:
184 p->nSingle = ImpGetSingle( &aTmp ); break;
185 case SbxDATE:
186 p->nDouble = ImpGetDate( &aTmp ); break;
187 case SbxDOUBLE:
188 p->nDouble = ImpGetDouble( &aTmp ); break;
189 case SbxCURRENCY:
190 p->nInt64 = ImpGetCurrency( &aTmp ); break;
191 case SbxDECIMAL:
192 case SbxBYREF | SbxDECIMAL:
193 releaseDecimalPtr( p->pDecimal );
194 p->pDecimal = ImpGetDecimal( &aTmp ); break;
195 case SbxSALINT64:
196 p->nInt64 = ImpGetInt64( &aTmp ); break;
197 case SbxSALUINT64:
198 p->uInt64 = ImpGetUInt64( &aTmp ); break;
200 case SbxBYREF | SbxSTRING:
201 case SbxSTRING:
202 case SbxLPSTR:
203 if( !n->isEmpty() )
205 if( !p->pOUString )
206 p->pOUString = new OUString( *n );
207 else
208 *p->pOUString = *n;
210 else
211 delete p->pOUString, p->pOUString = NULL;
212 break;
213 case SbxOBJECT:
215 SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
216 if( pVal )
217 pVal->PutString( *n );
218 else
219 SbxBase::SetError( SbxERR_NO_OBJECT );
220 break;
222 case SbxBYREF | SbxCHAR:
223 *p->pChar = ImpGetChar( p ); break;
224 case SbxBYREF | SbxBYTE:
225 *p->pByte = ImpGetByte( p ); break;
226 case SbxBYREF | SbxINTEGER:
227 *p->pInteger = ImpGetInteger( p ); break;
228 case SbxBYREF | SbxBOOL:
229 *p->pUShort = sal::static_int_cast< sal_uInt16 >( ImpGetBool( p ) );
230 break;
231 case SbxBYREF | SbxERROR:
232 case SbxBYREF | SbxUSHORT:
233 *p->pUShort = ImpGetUShort( p ); break;
234 case SbxBYREF | SbxLONG:
235 *p->pLong = ImpGetLong( p ); break;
236 case SbxBYREF | SbxULONG:
237 *p->pULong = ImpGetULong( p ); break;
238 case SbxBYREF | SbxSINGLE:
239 *p->pSingle = ImpGetSingle( p ); break;
240 case SbxBYREF | SbxDATE:
241 *p->pDouble = ImpGetDate( p ); break;
242 case SbxBYREF | SbxDOUBLE:
243 *p->pDouble = ImpGetDouble( p ); break;
244 case SbxBYREF | SbxCURRENCY:
245 *p->pnInt64 = ImpGetCurrency( p ); break;
246 case SbxBYREF | SbxSALINT64:
247 *p->pnInt64 = ImpGetInt64( p ); break;
248 case SbxBYREF | SbxSALUINT64:
249 *p->puInt64 = ImpGetUInt64( p ); break;
250 default:
251 SbxBase::SetError( SbxERR_CONVERSION );
253 delete pTmp;
257 // Convert string to an array of bytes, preserving unicode (2bytes per character)
258 SbxArray* StringToByteArray(const OUString& rStr)
260 sal_Int32 nArraySize = rStr.getLength() * 2;
261 const sal_Unicode* pSrc = rStr.getStr();
262 SbxDimArray* pArray = new SbxDimArray(SbxBYTE);
263 #ifdef DISABLE_SCRIPTING
264 bool bIncIndex = false;
265 #else
266 bool bIncIndex = ( IsBaseIndexOne() && SbiRuntime::isVBAEnabled() );
267 #endif
268 if( nArraySize )
270 if( bIncIndex )
271 pArray->AddDim32( 1, nArraySize );
272 else
273 pArray->AddDim32( 0, nArraySize-1 );
275 else
277 pArray->unoAddDim( 0, -1 );
280 for( sal_uInt16 i=0; i< nArraySize; i++)
282 SbxVariable* pNew = new SbxVariable( SbxBYTE );
283 sal_uInt8 aByte = static_cast< sal_uInt8 >( (i%2) ? ((*pSrc) >> 8) & 0xff : (*pSrc) & 0xff );
284 pNew->PutByte( aByte );
285 pNew->SetFlag( SBX_WRITE );
286 pArray->Put( pNew, i );
287 if( i%2 )
288 pSrc++;
290 return pArray;
293 // Convert an array of bytes to string (2bytes per character)
294 OUString ByteArrayToString(SbxArray* pArr)
296 sal_uInt16 nCount = pArr->Count();
297 OUStringBuffer aStrBuf;
298 sal_Unicode aChar = 0;
299 for( sal_uInt16 i = 0 ; i < nCount ; i++ )
301 sal_Unicode aTempChar = pArr->Get(i)->GetByte();
302 if( i%2 )
304 aChar = (aTempChar << 8 ) | aChar;
305 aStrBuf.append(aChar);
306 aChar = 0;
308 else
310 aChar = aTempChar;
314 if( nCount%2 )
316 aStrBuf.append(aChar);
319 return aStrBuf.makeStringAndClear();
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */