Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <config_features.h>
21 :
22 : #include <tools/errcode.hxx>
23 : #include <basic/sbx.hxx>
24 : #include "sbxconv.hxx"
25 : #include "sbxres.hxx"
26 : #include "runtime.hxx"
27 : #include <rtl/ustrbuf.hxx>
28 :
29 : // The conversion of an item onto String was handled via the Put-Methods
30 : // of the several data types to avoid duplicated code.
31 :
32 14966 : OUString ImpGetString( const SbxValues* p )
33 : {
34 14966 : SbxValues aTmp;
35 14966 : OUString aRes;
36 14966 : aTmp.eType = SbxSTRING;
37 14966 : aTmp.pOUString = &aRes;
38 14966 : switch( +p->eType )
39 : {
40 : case SbxNULL:
41 0 : SbxBase::SetError( SbxERR_CONVERSION );
42 : case SbxEMPTY:
43 32 : break;
44 : case SbxCHAR:
45 0 : ImpPutChar( &aTmp, p->nChar ); break;
46 : case SbxBYTE:
47 0 : ImpPutByte( &aTmp, p->nByte ); break;
48 : case SbxINTEGER:
49 288 : ImpPutInteger( &aTmp, p->nInteger ); break;
50 : case SbxBOOL:
51 220 : ImpPutBool( &aTmp, p->nUShort ); break;
52 : case SbxUSHORT:
53 0 : ImpPutUShort( &aTmp, p->nUShort ); break;
54 : case SbxLONG:
55 124 : ImpPutLong( &aTmp, p->nLong ); break;
56 : case SbxULONG:
57 0 : ImpPutULong( &aTmp, p->nULong ); break;
58 : case SbxSINGLE:
59 2 : ImpPutSingle( &aTmp, p->nSingle ); break;
60 : case SbxDOUBLE:
61 368 : ImpPutDouble( &aTmp, p->nDouble ); break;
62 : case SbxCURRENCY:
63 6 : ImpPutCurrency( &aTmp, p->nInt64 ); break;
64 : case SbxDECIMAL:
65 : case SbxBYREF | SbxDECIMAL:
66 0 : ImpPutDecimal( &aTmp, p->pDecimal ); break;
67 : case SbxSALINT64:
68 0 : ImpPutInt64( &aTmp, p->nInt64 ); break;
69 : case SbxSALUINT64:
70 0 : ImpPutUInt64( &aTmp, p->uInt64 ); break;
71 : case SbxBYREF | SbxSTRING:
72 : case SbxSTRING:
73 : case SbxLPSTR:
74 13916 : if ( p->pOUString )
75 : {
76 12662 : *aTmp.pOUString = *p->pOUString;
77 : }
78 13916 : break;
79 : case SbxOBJECT:
80 : {
81 0 : SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
82 0 : if( pVal )
83 : {
84 0 : aRes = pVal->GetOUString();
85 : }
86 0 : else if( p->pObj && p->pObj->IsFixed()
87 0 : && (p->pObj->GetType() == (SbxARRAY | SbxBYTE )) )
88 : {
89 : // convert byte array to string
90 0 : SbxArray* pArr = PTR_CAST(SbxArray, p->pObj);
91 0 : if( pArr )
92 : {
93 0 : aRes = ByteArrayToString( pArr );
94 : }
95 : }
96 : else
97 : {
98 0 : SbxBase::SetError( SbxERR_NO_OBJECT );
99 : }
100 0 : break;
101 : }
102 : case SbxERROR:
103 : // Here the String "Error n" is generated
104 0 : aRes = SbxRes( STRING_ERRORMSG );
105 0 : aRes += OUString( p->nUShort ); break;
106 : case SbxDATE:
107 10 : ImpPutDate( &aTmp, p->nDouble ); break;
108 :
109 : case SbxBYREF | SbxCHAR:
110 0 : ImpPutChar( &aTmp, *p->pChar ); break;
111 : case SbxBYREF | SbxBYTE:
112 0 : ImpPutByte( &aTmp, *p->pByte ); break;
113 : case SbxBYREF | SbxINTEGER:
114 : case SbxBYREF | SbxBOOL:
115 0 : ImpPutInteger( &aTmp, *p->pInteger ); break;
116 : case SbxBYREF | SbxLONG:
117 0 : ImpPutLong( &aTmp, *p->pLong ); break;
118 : case SbxBYREF | SbxULONG:
119 0 : ImpPutULong( &aTmp, *p->pULong ); break;
120 : case SbxBYREF | SbxERROR:
121 : case SbxBYREF | SbxUSHORT:
122 0 : ImpPutUShort( &aTmp, *p->pUShort ); break;
123 : case SbxBYREF | SbxSINGLE:
124 0 : ImpPutSingle( &aTmp, *p->pSingle ); break;
125 : case SbxBYREF | SbxDATE:
126 : case SbxBYREF | SbxDOUBLE:
127 0 : ImpPutDouble( &aTmp, *p->pDouble ); break;
128 : case SbxBYREF | SbxCURRENCY:
129 0 : ImpPutCurrency( &aTmp, *p->pnInt64 ); break;
130 : case SbxBYREF | SbxSALINT64:
131 0 : ImpPutInt64( &aTmp, *p->pnInt64 ); break;
132 : case SbxBYREF | SbxSALUINT64:
133 0 : ImpPutUInt64( &aTmp, *p->puInt64 ); break;
134 : default:
135 0 : SbxBase::SetError( SbxERR_CONVERSION );
136 : }
137 14966 : return aRes;
138 : }
139 :
140 : // From 1997-04-10, new function for SbxValue::GetCoreString()
141 0 : OUString ImpGetCoreString( const SbxValues* p )
142 : {
143 : // For now only for double
144 0 : if( ( p->eType & (~SbxBYREF) ) == SbxDOUBLE )
145 : {
146 0 : SbxValues aTmp;
147 0 : OUString aRes;
148 0 : aTmp.eType = SbxSTRING;
149 0 : aTmp.pOUString = &aRes;
150 0 : if( p->eType == SbxDOUBLE )
151 0 : ImpPutDouble( &aTmp, p->nDouble, true ); // true = bCoreString
152 : else
153 0 : ImpPutDouble( &aTmp, *p->pDouble, true ); // true = bCoreString
154 0 : return aRes;
155 : }
156 : else
157 0 : return ImpGetString( p );
158 : }
159 :
160 19872 : void ImpPutString( SbxValues* p, const OUString* n )
161 : {
162 19872 : SbxValues aTmp;
163 19872 : aTmp.eType = SbxSTRING;
164 19872 : OUString* pTmp = NULL;
165 : // as a precaution, if a NULL-Ptr appears
166 19872 : if( !n )
167 418 : n = pTmp = new OUString;
168 19872 : aTmp.pOUString = (OUString*)n;
169 19872 : switch( +p->eType )
170 : {
171 : case SbxCHAR:
172 0 : p->nChar = ImpGetChar( &aTmp ); break;
173 : case SbxBYTE:
174 0 : p->nByte = ImpGetByte( &aTmp ); break;
175 : case SbxINTEGER:
176 : case SbxBOOL:
177 0 : p->nInteger = ImpGetInteger( &aTmp ); break;
178 : case SbxLONG:
179 0 : p->nLong = ImpGetLong( &aTmp ); break;
180 : case SbxULONG:
181 0 : p->nULong = ImpGetULong( &aTmp ); break;
182 : case SbxERROR:
183 : case SbxUSHORT:
184 0 : p->nUShort = ImpGetUShort( &aTmp ); break;
185 : case SbxSINGLE:
186 0 : p->nSingle = ImpGetSingle( &aTmp ); break;
187 : case SbxDATE:
188 0 : p->nDouble = ImpGetDate( &aTmp ); break;
189 : case SbxDOUBLE:
190 0 : p->nDouble = ImpGetDouble( &aTmp ); break;
191 : case SbxCURRENCY:
192 0 : p->nInt64 = ImpGetCurrency( &aTmp ); break;
193 : case SbxDECIMAL:
194 : case SbxBYREF | SbxDECIMAL:
195 0 : releaseDecimalPtr( p->pDecimal );
196 0 : p->pDecimal = ImpGetDecimal( &aTmp ); break;
197 : case SbxSALINT64:
198 0 : p->nInt64 = ImpGetInt64( &aTmp ); break;
199 : case SbxSALUINT64:
200 0 : p->uInt64 = ImpGetUInt64( &aTmp ); break;
201 :
202 : case SbxBYREF | SbxSTRING:
203 : case SbxSTRING:
204 : case SbxLPSTR:
205 19872 : if( !n->isEmpty() )
206 : {
207 17756 : if( !p->pOUString )
208 11104 : p->pOUString = new OUString( *n );
209 : else
210 6652 : *p->pOUString = *n;
211 : }
212 : else
213 2116 : delete p->pOUString, p->pOUString = NULL;
214 19872 : break;
215 : case SbxOBJECT:
216 : {
217 0 : SbxValue* pVal = PTR_CAST(SbxValue,p->pObj);
218 0 : if( pVal )
219 0 : pVal->PutString( *n );
220 : else
221 0 : SbxBase::SetError( SbxERR_NO_OBJECT );
222 0 : break;
223 : }
224 : case SbxBYREF | SbxCHAR:
225 0 : *p->pChar = ImpGetChar( p ); break;
226 : case SbxBYREF | SbxBYTE:
227 0 : *p->pByte = ImpGetByte( p ); break;
228 : case SbxBYREF | SbxINTEGER:
229 0 : *p->pInteger = ImpGetInteger( p ); break;
230 : case SbxBYREF | SbxBOOL:
231 0 : *p->pUShort = sal::static_int_cast< sal_uInt16 >( ImpGetBool( p ) );
232 0 : break;
233 : case SbxBYREF | SbxERROR:
234 : case SbxBYREF | SbxUSHORT:
235 0 : *p->pUShort = ImpGetUShort( p ); break;
236 : case SbxBYREF | SbxLONG:
237 0 : *p->pLong = ImpGetLong( p ); break;
238 : case SbxBYREF | SbxULONG:
239 0 : *p->pULong = ImpGetULong( p ); break;
240 : case SbxBYREF | SbxSINGLE:
241 0 : *p->pSingle = ImpGetSingle( p ); break;
242 : case SbxBYREF | SbxDATE:
243 0 : *p->pDouble = ImpGetDate( p ); break;
244 : case SbxBYREF | SbxDOUBLE:
245 0 : *p->pDouble = ImpGetDouble( p ); break;
246 : case SbxBYREF | SbxCURRENCY:
247 0 : *p->pnInt64 = ImpGetCurrency( p ); break;
248 : case SbxBYREF | SbxSALINT64:
249 0 : *p->pnInt64 = ImpGetInt64( p ); break;
250 : case SbxBYREF | SbxSALUINT64:
251 0 : *p->puInt64 = ImpGetUInt64( p ); break;
252 : default:
253 0 : SbxBase::SetError( SbxERR_CONVERSION );
254 : }
255 19872 : delete pTmp;
256 19872 : }
257 :
258 :
259 : // Convert string to an array of bytes, preserving unicode (2bytes per character)
260 4 : SbxArray* StringToByteArray(const OUString& rStr)
261 : {
262 4 : sal_Int32 nArraySize = rStr.getLength() * 2;
263 4 : const sal_Unicode* pSrc = rStr.getStr();
264 4 : SbxDimArray* pArray = new SbxDimArray(SbxBYTE);
265 : #if !HAVE_FEATURE_SCRIPTING
266 : bool bIncIndex = false;
267 : #else
268 4 : bool bIncIndex = ( IsBaseIndexOne() && SbiRuntime::isVBAEnabled() );
269 : #endif
270 4 : if( nArraySize )
271 : {
272 4 : if( bIncIndex )
273 0 : pArray->AddDim32( 1, nArraySize );
274 : else
275 4 : pArray->AddDim32( 0, nArraySize-1 );
276 : }
277 : else
278 : {
279 0 : pArray->unoAddDim( 0, -1 );
280 : }
281 :
282 28 : for( sal_uInt16 i=0; i< nArraySize; i++)
283 : {
284 24 : SbxVariable* pNew = new SbxVariable( SbxBYTE );
285 24 : sal_uInt8 aByte = static_cast< sal_uInt8 >( (i%2) ? ((*pSrc) >> 8) & 0xff : (*pSrc) & 0xff );
286 24 : pNew->PutByte( aByte );
287 24 : pNew->SetFlag( SBX_WRITE );
288 24 : pArray->Put( pNew, i );
289 24 : if( i%2 )
290 12 : pSrc++;
291 : }
292 4 : return pArray;
293 : }
294 :
295 : // Convert an array of bytes to string (2bytes per character)
296 4 : OUString ByteArrayToString(SbxArray* pArr)
297 : {
298 4 : sal_uInt16 nCount = pArr->Count();
299 4 : OUStringBuffer aStrBuf;
300 4 : sal_Unicode aChar = 0;
301 28 : for( sal_uInt16 i = 0 ; i < nCount ; i++ )
302 : {
303 24 : sal_Unicode aTempChar = pArr->Get(i)->GetByte();
304 24 : if( i%2 )
305 : {
306 12 : aChar = (aTempChar << 8 ) | aChar;
307 12 : aStrBuf.append(aChar);
308 12 : aChar = 0;
309 : }
310 : else
311 : {
312 12 : aChar = aTempChar;
313 : }
314 : }
315 :
316 4 : if( nCount%2 )
317 : {
318 0 : aStrBuf.append(aChar);
319 : }
320 :
321 4 : return aStrBuf.makeStringAndClear();
322 : }
323 :
324 :
325 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|