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 :
21 : #include <math.h>
22 : #include <tools/stream.hxx>
23 :
24 : #include <basic/sbx.hxx>
25 : #include "sbxconv.hxx"
26 : #include "runtime.hxx"
27 :
28 231410 : TYPEINIT1(SbxValue,SbxBase)
29 :
30 : ///////////////////////////// constructors //////////////////////////////
31 :
32 6742 : SbxValue::SbxValue() : SbxBase()
33 : {
34 6742 : aData.eType = SbxEMPTY;
35 6742 : }
36 :
37 42600 : SbxValue::SbxValue( SbxDataType t, void* p ) : SbxBase()
38 : {
39 42600 : int n = t & 0x0FFF;
40 42600 : if( p )
41 0 : n |= SbxBYREF;
42 42600 : if( n == SbxVARIANT )
43 5089 : n = SbxEMPTY;
44 : else
45 37511 : SetFlag( SBX_FIXED );
46 42600 : if( p )
47 : {
48 0 : switch( t & 0x0FFF )
49 : {
50 0 : case SbxINTEGER: n |= SbxBYREF; aData.pInteger = (sal_Int16*) p; break;
51 0 : case SbxSALUINT64: n |= SbxBYREF; aData.puInt64 = (sal_uInt64*) p; break;
52 : case SbxSALINT64:
53 0 : case SbxCURRENCY: n |= SbxBYREF; aData.pnInt64 = (sal_Int64*) p; break;
54 0 : case SbxLONG: n |= SbxBYREF; aData.pLong = (sal_Int32*) p; break;
55 0 : case SbxSINGLE: n |= SbxBYREF; aData.pSingle = (float*) p; break;
56 : case SbxDATE:
57 0 : case SbxDOUBLE: n |= SbxBYREF; aData.pDouble = (double*) p; break;
58 0 : case SbxSTRING: n |= SbxBYREF; aData.pOUString = (OUString*) p; break;
59 : case SbxERROR:
60 : case SbxUSHORT:
61 0 : case SbxBOOL: n |= SbxBYREF; aData.pUShort = (sal_uInt16*) p; break;
62 0 : case SbxULONG: n |= SbxBYREF; aData.pULong = (sal_uInt32*) p; break;
63 0 : case SbxCHAR: n |= SbxBYREF; aData.pChar = (sal_Unicode*) p; break;
64 0 : case SbxBYTE: n |= SbxBYREF; aData.pByte = (sal_uInt8*) p; break;
65 0 : case SbxINT: n |= SbxBYREF; aData.pInt = (int*) p; break;
66 : case SbxOBJECT:
67 0 : aData.pObj = (SbxBase*) p;
68 0 : if( p )
69 0 : aData.pObj->AddRef();
70 0 : break;
71 : case SbxDECIMAL:
72 0 : aData.pDecimal = (SbxDecimal*) p;
73 0 : if( p )
74 0 : aData.pDecimal->addRef();
75 0 : break;
76 : default:
77 : DBG_ASSERT( !this, "Improper pointer argument" );
78 0 : n = SbxNULL;
79 : }
80 : }
81 : else
82 42600 : memset( &aData, 0, sizeof( SbxValues ) );
83 42600 : aData.eType = SbxDataType( n );
84 42600 : }
85 :
86 8457 : SbxValue::SbxValue( const SbxValue& r )
87 8457 : : SvRefBase( r ), SbxBase( r )
88 : {
89 8457 : if( !r.CanRead() )
90 : {
91 0 : SetError( SbxERR_PROP_WRITEONLY );
92 0 : if( !IsFixed() )
93 0 : aData.eType = SbxNULL;
94 : }
95 : else
96 : {
97 8457 : ((SbxValue*) &r)->Broadcast( SBX_HINT_DATAWANTED );
98 8457 : aData = r.aData;
99 : // Copy pointer, increment references
100 8457 : switch( aData.eType )
101 : {
102 : case SbxSTRING:
103 1792 : if( aData.pOUString )
104 1226 : aData.pOUString = new OUString( *aData.pOUString );
105 1792 : break;
106 : case SbxOBJECT:
107 1829 : if( aData.pObj )
108 1790 : aData.pObj->AddRef();
109 1829 : break;
110 : case SbxDECIMAL:
111 0 : if( aData.pDecimal )
112 0 : aData.pDecimal->addRef();
113 0 : break;
114 4836 : default: break;
115 : }
116 : }
117 8457 : }
118 :
119 5934 : SbxValue& SbxValue::operator=( const SbxValue& r )
120 : {
121 5934 : if( &r != this )
122 : {
123 5934 : if( !CanWrite() )
124 0 : SetError( SbxERR_PROP_READONLY );
125 : else
126 : {
127 : // string -> byte array
128 16918 : if( IsFixed() && (aData.eType == SbxOBJECT)
129 430 : && aData.pObj && ( aData.pObj->GetType() == (SbxARRAY | SbxBYTE) )
130 5936 : && (r.aData.eType == SbxSTRING) )
131 : {
132 2 : OUString aStr = r.GetOUString();
133 2 : SbxArray* pArr = StringToByteArray(aStr);
134 2 : PutObject(pArr);
135 2 : return *this;
136 : }
137 : // byte array -> string
138 12959 : if( r.IsFixed() && (r.aData.eType == SbxOBJECT)
139 304 : && r.aData.pObj && ( r.aData.pObj->GetType() == (SbxARRAY | SbxBYTE) )
140 5934 : && (aData.eType == SbxSTRING) )
141 : {
142 2 : SbxBase* pObj = r.GetObject();
143 2 : SbxArray* pArr = PTR_CAST(SbxArray, pObj);
144 2 : if( pArr )
145 : {
146 2 : OUString aStr = ByteArrayToString( pArr );
147 2 : PutString(aStr);
148 2 : return *this;
149 : }
150 : }
151 : // Readout the content of the variables
152 5930 : SbxValues aNew;
153 5930 : if( IsFixed() )
154 : // firm: then the type had to match
155 5046 : aNew.eType = aData.eType;
156 884 : else if( r.IsFixed() )
157 : // Source firm: take over the type
158 155 : aNew.eType = SbxDataType( r.aData.eType & 0x0FFF );
159 : else
160 : // both variant: then it is equal
161 729 : aNew.eType = SbxVARIANT;
162 5930 : if( r.Get( aNew ) )
163 5920 : Put( aNew );
164 : }
165 : }
166 5930 : return *this;
167 : }
168 :
169 96786 : SbxValue::~SbxValue()
170 : {
171 48393 : Broadcast( SBX_HINT_DYING );
172 48393 : SetFlag( SBX_WRITE );
173 48393 : SbxValue::Clear();
174 48393 : }
175 :
176 56741 : void SbxValue::Clear()
177 : {
178 56741 : switch( aData.eType )
179 : {
180 : case SbxNULL:
181 : case SbxEMPTY:
182 : case SbxVOID:
183 6720 : break;
184 : case SbxSTRING:
185 16219 : delete aData.pOUString; aData.pOUString = NULL;
186 16219 : break;
187 : case SbxOBJECT:
188 23051 : if( aData.pObj )
189 : {
190 10654 : if( aData.pObj != this )
191 : {
192 : SAL_WARN("basic.sbx", "Not at Parent-Prop - otherwise CyclicRef");
193 4496 : SbxVariable *pThisVar = PTR_CAST(SbxVariable, this);
194 6119 : sal_Bool bParentProp = pThisVar && 5345 ==
195 6119 : ( (sal_Int16) ( pThisVar->GetUserData() & 0xFFFF ) );
196 4496 : if ( !bParentProp )
197 4496 : aData.pObj->ReleaseRef();
198 : }
199 10654 : aData.pObj = NULL;
200 : }
201 23051 : break;
202 : case SbxDECIMAL:
203 166 : if( aData.eType == SbxDECIMAL )
204 166 : releaseDecimalPtr( aData.pDecimal );
205 166 : break;
206 : case SbxDATAOBJECT:
207 0 : aData.pData = NULL; break;
208 : default:
209 : {
210 10585 : SbxValues aEmpty;
211 10585 : memset( &aEmpty, 0, sizeof( SbxValues ) );
212 10585 : aEmpty.eType = GetType();
213 10585 : Put( aEmpty );
214 : }
215 : }
216 56741 : }
217 :
218 : // Dummy
219 :
220 59914 : void SbxValue::Broadcast( sal_uIntPtr )
221 59914 : {}
222 :
223 : //////////////////////////// Readout data //////////////////////////////
224 :
225 : // Detect the "right" variables. If it is an object, will be addressed either
226 : // the object itself or its default property.
227 : // If the variable contain a variable or an object, this will be
228 : // addressed.
229 :
230 26302 : SbxValue* SbxValue::TheRealValue() const
231 : {
232 26302 : return TheRealValue( sal_True );
233 : }
234 :
235 : // #55226 ship additional information
236 : bool handleToStringForCOMObjects( SbxObject* pObj, SbxValue* pVal ); // sbunoobj.cxx
237 :
238 61336 : SbxValue* SbxValue::TheRealValue( sal_Bool bObjInObjError ) const
239 : {
240 61336 : SbxValue* p = (SbxValue*) this;
241 : for( ;; )
242 : {
243 61336 : SbxDataType t = SbxDataType( p->aData.eType & 0x0FFF );
244 61336 : if( t == SbxOBJECT )
245 : {
246 : // The block contains an object or a variable
247 25 : SbxObject* pObj = PTR_CAST(SbxObject,p->aData.pObj);
248 25 : if( pObj )
249 : {
250 : // Has the object a default property?
251 4 : SbxVariable* pDflt = pObj->GetDfltProperty();
252 :
253 : // If this is an object and contains itself,
254 : // we cannot access on it
255 : // The old condition to set an error
256 : // is not correct, because e.g. a regular variant variable with an object
257 : // could be affected thereof, if another value should be assigned.
258 : // Therefore with flag.
259 4 : if( bObjInObjError && !pDflt &&
260 0 : ((SbxValue*) pObj)->aData.eType == SbxOBJECT &&
261 0 : ((SbxValue*) pObj)->aData.pObj == pObj )
262 : {
263 : #ifdef DISABLE_SCRIPTING // No sbunoobj
264 : const bool bSuccess = false;
265 : #else
266 0 : bool bSuccess = handleToStringForCOMObjects( pObj, p );
267 : #endif
268 0 : if( !bSuccess )
269 : {
270 0 : SetError( SbxERR_BAD_PROP_VALUE );
271 0 : p = NULL;
272 0 : }
273 : }
274 4 : else if( pDflt )
275 4 : p = pDflt;
276 4 : break;
277 : }
278 : // Did we have an array?
279 21 : SbxArray* pArray = PTR_CAST(SbxArray,p->aData.pObj);
280 21 : if( pArray )
281 : {
282 : // When indicated get the parameter
283 14 : SbxArray* pPar = NULL;
284 14 : SbxVariable* pVar = PTR_CAST(SbxVariable,p);
285 14 : if( pVar )
286 14 : pPar = pVar->GetParameters();
287 14 : if( pPar )
288 : {
289 : // Did we have a dimensioned array?
290 0 : SbxDimArray* pDimArray = PTR_CAST(SbxDimArray,p->aData.pObj);
291 0 : if( pDimArray )
292 0 : p = pDimArray->Get( pPar );
293 : else
294 0 : p = pArray->Get( pPar->Get( 1 )->GetInteger() );
295 0 : break;
296 : }
297 : }
298 : // Elsewise guess a SbxValue
299 21 : SbxValue* pVal = PTR_CAST(SbxValue,p->aData.pObj);
300 21 : if( pVal )
301 0 : p = pVal;
302 : else
303 21 : break;
304 : }
305 : else
306 61311 : break;
307 0 : }
308 61336 : return p;
309 : }
310 :
311 51894 : sal_Bool SbxValue::Get( SbxValues& rRes ) const
312 : {
313 51894 : sal_Bool bRes = sal_False;
314 51894 : SbxError eOld = GetError();
315 51894 : if( eOld != SbxERR_OK )
316 0 : ResetError();
317 51894 : if( !CanRead() )
318 : {
319 0 : SetError( SbxERR_PROP_WRITEONLY );
320 0 : rRes.pObj = NULL;
321 : }
322 : else
323 : {
324 : // If there was asked for an object or a VARIANT, don't search
325 : // the real values
326 51894 : SbxValue* p = (SbxValue*) this;
327 51894 : if( rRes.eType != SbxOBJECT && rRes.eType != SbxVARIANT )
328 26302 : p = TheRealValue();
329 51894 : if( p )
330 : {
331 51894 : p->Broadcast( SBX_HINT_DATAWANTED );
332 51894 : switch( rRes.eType )
333 : {
334 : case SbxEMPTY:
335 : case SbxVOID:
336 15 : case SbxNULL: break;
337 805 : case SbxVARIANT: rRes = p->aData; break;
338 2755 : case SbxINTEGER: rRes.nInteger = ImpGetInteger( &p->aData ); break;
339 2974 : case SbxLONG: rRes.nLong = ImpGetLong( &p->aData ); break;
340 0 : case SbxSALINT64: rRes.nInt64 = ImpGetInt64( &p->aData ); break;
341 0 : case SbxSALUINT64: rRes.uInt64 = ImpGetUInt64( &p->aData ); break;
342 1 : case SbxSINGLE: rRes.nSingle = ImpGetSingle( &p->aData ); break;
343 9845 : case SbxDOUBLE: rRes.nDouble = ImpGetDouble( &p->aData ); break;
344 0 : case SbxCURRENCY:rRes.nInt64 = ImpGetCurrency( &p->aData ); break;
345 0 : case SbxDECIMAL: rRes.pDecimal = ImpGetDecimal( &p->aData ); break;
346 3 : case SbxDATE: rRes.nDouble = ImpGetDate( &p->aData ); break;
347 : case SbxBOOL:
348 : rRes.nUShort = sal::static_int_cast< sal_uInt16 >(
349 2872 : ImpGetBool( &p->aData ) );
350 2872 : break;
351 0 : case SbxCHAR: rRes.nChar = ImpGetChar( &p->aData ); break;
352 170 : case SbxBYTE: rRes.nByte = ImpGetByte( &p->aData ); break;
353 0 : case SbxUSHORT: rRes.nUShort = ImpGetUShort( &p->aData ); break;
354 0 : case SbxULONG: rRes.nULong = ImpGetULong( &p->aData ); break;
355 : case SbxLPSTR:
356 7667 : case SbxSTRING: p->aPic = ImpGetString( &p->aData );
357 7667 : rRes.pOUString = &p->aPic; break;
358 0 : case SbxCoreSTRING: p->aPic = ImpGetCoreString( &p->aData );
359 0 : rRes.pOUString = &p->aPic; break;
360 : case SbxINT:
361 : #if SAL_TYPES_SIZEOFINT == 2
362 : rRes.nInt = (int) ImpGetInteger( &p->aData );
363 : #else
364 0 : rRes.nInt = (int) ImpGetLong( &p->aData );
365 : #endif
366 0 : break;
367 : case SbxUINT:
368 : #if SAL_TYPES_SIZEOFINT == 2
369 : rRes.nUInt = (int) ImpGetUShort( &p->aData );
370 : #else
371 0 : rRes.nUInt = (int) ImpGetULong( &p->aData );
372 : #endif
373 0 : break;
374 : case SbxOBJECT:
375 24787 : if( p->aData.eType == SbxOBJECT )
376 24784 : rRes.pObj = p->aData.pObj;
377 : else
378 : {
379 3 : SetError( SbxERR_NO_OBJECT );
380 3 : rRes.pObj = NULL;
381 : }
382 24787 : break;
383 : default:
384 0 : if( p->aData.eType == rRes.eType )
385 0 : rRes = p->aData;
386 : else
387 : {
388 0 : SetError( SbxERR_CONVERSION );
389 0 : rRes.pObj = NULL;
390 : }
391 : }
392 : }
393 : else
394 : {
395 : // Object contained itself
396 0 : SbxDataType eTemp = rRes.eType;
397 0 : memset( &rRes, 0, sizeof( SbxValues ) );
398 0 : rRes.eType = eTemp;
399 : }
400 : }
401 51894 : if( !IsError() )
402 : {
403 51875 : bRes = sal_True;
404 51875 : if( eOld != SbxERR_OK )
405 0 : SetError( eOld );
406 : }
407 51894 : return bRes;
408 : }
409 :
410 0 : const OUString& SbxValue::GetCoreString() const
411 : {
412 0 : SbxValues aRes;
413 0 : aRes.eType = SbxCoreSTRING;
414 0 : if( Get( aRes ) )
415 : {
416 0 : ((SbxValue*) this)->aToolString = *aRes.pOUString;
417 : }
418 : else
419 : {
420 0 : ((SbxValue*) this)->aToolString = "";
421 : }
422 0 : return aToolString;
423 : }
424 :
425 1554 : OUString SbxValue::GetOUString() const
426 : {
427 1554 : OUString aResult;
428 1554 : SbxValues aRes;
429 1554 : aRes.eType = SbxSTRING;
430 1554 : if( Get( aRes ) )
431 : {
432 1554 : aResult = *aRes.pOUString;
433 : }
434 1554 : return aResult;
435 : }
436 :
437 1879 : sal_Bool SbxValue::GetBool() const
438 : {
439 1879 : SbxValues aRes;
440 1879 : aRes.eType = SbxBOOL;
441 1879 : Get( aRes );
442 1879 : return sal_Bool( aRes.nUShort != 0 );
443 : }
444 :
445 : #define GET( g, e, t, m ) \
446 : t SbxValue::g() const { SbxValues aRes(e); Get( aRes ); return aRes.m; }
447 :
448 170 : GET( GetByte, SbxBYTE, sal_uInt8, nByte )
449 0 : GET( GetChar, SbxCHAR, sal_Unicode, nChar )
450 0 : GET( GetCurrency, SbxCURRENCY, sal_Int64, nInt64 )
451 0 : GET( GetDate, SbxDATE, double, nDouble )
452 1493 : GET( GetDouble, SbxDOUBLE, double, nDouble )
453 1241 : GET( GetInteger, SbxINTEGER, sal_Int16, nInteger )
454 1479 : GET( GetLong, SbxLONG, sal_Int32, nLong )
455 24341 : GET( GetObject, SbxOBJECT, SbxBase*, pObj )
456 0 : GET( GetSingle, SbxSINGLE, float, nSingle )
457 0 : GET( GetULong, SbxULONG, sal_uInt32, nULong )
458 0 : GET( GetUShort, SbxUSHORT, sal_uInt16, nUShort )
459 0 : GET( GetInt64, SbxSALINT64, sal_Int64, nInt64 )
460 0 : GET( GetUInt64, SbxSALUINT64, sal_uInt64, uInt64 )
461 0 : GET( GetDecimal, SbxDECIMAL, SbxDecimal*, pDecimal )
462 :
463 :
464 : //////////////////////////// Write data /////////////////////////////
465 :
466 38105 : sal_Bool SbxValue::Put( const SbxValues& rVal )
467 : {
468 38105 : sal_Bool bRes = sal_False;
469 38105 : SbxError eOld = GetError();
470 38105 : if( eOld != SbxERR_OK )
471 0 : ResetError();
472 38105 : if( !CanWrite() )
473 0 : SetError( SbxERR_PROP_READONLY );
474 38105 : else if( rVal.eType & 0xF000 )
475 0 : SetError( SbxERR_NOTIMP );
476 : else
477 : {
478 : // If there was asked for an object, don't search
479 : // the real values
480 38105 : SbxValue* p = this;
481 38105 : if( rVal.eType != SbxOBJECT )
482 35034 : p = TheRealValue( sal_False ); // Don't allow an error here
483 38105 : if( p )
484 : {
485 38105 : if( !p->CanWrite() )
486 0 : SetError( SbxERR_PROP_READONLY );
487 38105 : else if( p->IsFixed() || p->SetType( (SbxDataType) ( rVal.eType & 0x0FFF ) ) )
488 38105 : switch( rVal.eType & 0x0FFF )
489 : {
490 : case SbxEMPTY:
491 : case SbxVOID:
492 910 : case SbxNULL: break;
493 8730 : case SbxINTEGER: ImpPutInteger( &p->aData, rVal.nInteger ); break;
494 3201 : case SbxLONG: ImpPutLong( &p->aData, rVal.nLong ); break;
495 0 : case SbxSALINT64: ImpPutInt64( &p->aData, rVal.nInt64 ); break;
496 0 : case SbxSALUINT64: ImpPutUInt64( &p->aData, rVal.uInt64 ); break;
497 6 : case SbxSINGLE: ImpPutSingle( &p->aData, rVal.nSingle ); break;
498 7785 : case SbxDOUBLE: ImpPutDouble( &p->aData, rVal.nDouble ); break;
499 0 : case SbxCURRENCY: ImpPutCurrency( &p->aData, rVal.nInt64 ); break;
500 0 : case SbxDECIMAL: ImpPutDecimal( &p->aData, rVal.pDecimal ); break;
501 31 : case SbxDATE: ImpPutDate( &p->aData, rVal.nDouble ); break;
502 4263 : case SbxBOOL: ImpPutBool( &p->aData, rVal.nInteger ); break;
503 0 : case SbxCHAR: ImpPutChar( &p->aData, rVal.nChar ); break;
504 24 : case SbxBYTE: ImpPutByte( &p->aData, rVal.nByte ); break;
505 0 : case SbxUSHORT: ImpPutUShort( &p->aData, rVal.nUShort ); break;
506 0 : case SbxULONG: ImpPutULong( &p->aData, rVal.nULong ); break;
507 : case SbxLPSTR:
508 10069 : case SbxSTRING: ImpPutString( &p->aData, rVal.pOUString ); break;
509 : case SbxINT:
510 : #if SAL_TYPES_SIZEOFINT == 2
511 : ImpPutInteger( &p->aData, (sal_Int16) rVal.nInt );
512 : #else
513 0 : ImpPutLong( &p->aData, (sal_Int32) rVal.nInt );
514 : #endif
515 0 : break;
516 : case SbxUINT:
517 : #if SAL_TYPES_SIZEOFINT == 2
518 : ImpPutUShort( &p->aData, (sal_uInt16) rVal.nUInt );
519 : #else
520 0 : ImpPutULong( &p->aData, (sal_uInt32) rVal.nUInt );
521 : #endif
522 0 : break;
523 : case SbxOBJECT:
524 3071 : if( !p->IsFixed() || p->aData.eType == SbxOBJECT )
525 : {
526 : // is already inside
527 3071 : if( p->aData.eType == SbxOBJECT && p->aData.pObj == rVal.pObj )
528 298 : break;
529 :
530 : // Delete only the value part!
531 2773 : p->SbxValue::Clear();
532 :
533 : // real allocation
534 2773 : p->aData.pObj = rVal.pObj;
535 :
536 : // if necessary cont in Ref-Count
537 2773 : if( p->aData.pObj && p->aData.pObj != p )
538 : {
539 : if ( p != this )
540 : {
541 : OSL_FAIL( "TheRealValue" );
542 : }
543 : SAL_WARN("basic.sbx", "Not at Parent-Prop - otherwise CyclicRef");
544 2771 : SbxVariable *pThisVar = PTR_CAST(SbxVariable, this);
545 5542 : sal_Bool bParentProp = pThisVar && 5345 ==
546 5542 : ( (sal_Int16) ( pThisVar->GetUserData() & 0xFFFF ) );
547 2771 : if ( !bParentProp )
548 2771 : p->aData.pObj->AddRef();
549 : }
550 : }
551 : else
552 0 : SetError( SbxERR_CONVERSION );
553 2773 : break;
554 : default:
555 15 : if( p->aData.eType == rVal.eType )
556 15 : p->aData = rVal;
557 : else
558 : {
559 0 : SetError( SbxERR_CONVERSION );
560 0 : if( !p->IsFixed() )
561 0 : p->aData.eType = SbxNULL;
562 : }
563 : }
564 38105 : if( !IsError() )
565 : {
566 38105 : p->SetModified( sal_True );
567 38105 : p->Broadcast( SBX_HINT_DATACHANGED );
568 38105 : if( eOld != SbxERR_OK )
569 0 : SetError( eOld );
570 38105 : bRes = sal_True;
571 : }
572 : }
573 : }
574 38105 : return bRes;
575 : }
576 :
577 : // From 1996-03-28:
578 : // Method to execute a pretreatment of the strings at special types.
579 : // In particular necessary for BASIC-IDE, so that
580 : // the output in the Watch-Window can be writen back with PutStringExt,
581 : // if Float were declared with ',' as the decimal separator or BOOl
582 : // explicit with "TRUE" or "FALSE".
583 : // Implementation in ImpConvStringExt (SBXSCAN.CXX)
584 0 : sal_Bool SbxValue::PutStringExt( const OUString& r )
585 : {
586 : // Copy; if it is Unicode convert it immediately
587 0 : OUString aStr( r );
588 :
589 : // Identify the own type (not as in Put() with TheRealValue(),
590 : // Objects are not handled anyway)
591 0 : SbxDataType eTargetType = SbxDataType( aData.eType & 0x0FFF );
592 :
593 : // tinker a Source-Value
594 0 : SbxValues aRes;
595 0 : aRes.eType = SbxSTRING;
596 :
597 : // Only if really something was converted, take the copy,
598 : // elsewise take the original (Unicode remain)
599 : sal_Bool bRet;
600 0 : if( ImpConvStringExt( aStr, eTargetType ) )
601 0 : aRes.pOUString = (OUString*)&aStr;
602 : else
603 0 : aRes.pOUString = (OUString*)&r;
604 :
605 : // #34939: Set a Fixed-Flag at Strings. which contain a number, and
606 : // if this has a Num-Type, so that the type will not be changed
607 0 : sal_uInt16 nFlags_ = GetFlags();
608 0 : if( ( eTargetType >= SbxINTEGER && eTargetType <= SbxCURRENCY ) ||
609 0 : ( eTargetType >= SbxCHAR && eTargetType <= SbxUINT ) ||
610 : eTargetType == SbxBOOL )
611 : {
612 0 : SbxValue aVal;
613 0 : aVal.Put( aRes );
614 0 : if( aVal.IsNumeric() )
615 0 : SetFlag( SBX_FIXED );
616 : }
617 :
618 0 : Put( aRes );
619 0 : bRet = sal_Bool( !IsError() );
620 :
621 : // If it throwed an error with FIXED, set it back
622 : // (UI-Action should not cast an error, but only fail)
623 0 : if( !bRet )
624 0 : ResetError();
625 :
626 0 : SetFlags( nFlags_ );
627 0 : return bRet;
628 : }
629 :
630 1439 : sal_Bool SbxValue::PutBool( sal_Bool b )
631 : {
632 1439 : SbxValues aRes;
633 1439 : aRes.eType = SbxBOOL;
634 1439 : aRes.nUShort = sal::static_int_cast< sal_uInt16 >(b ? SbxTRUE : SbxFALSE);
635 1439 : Put( aRes );
636 1439 : return sal_Bool( !IsError() );
637 : }
638 :
639 1538 : sal_Bool SbxValue::PutEmpty()
640 : {
641 1538 : sal_Bool bRet = SetType( SbxEMPTY );
642 1538 : SetModified( sal_True );
643 1538 : return bRet;
644 : }
645 :
646 0 : sal_Bool SbxValue::PutNull()
647 : {
648 0 : sal_Bool bRet = SetType( SbxNULL );
649 0 : if( bRet )
650 0 : SetModified( sal_True );
651 0 : return bRet;
652 : }
653 :
654 :
655 : // Special decimal methods
656 0 : sal_Bool SbxValue::PutDecimal( com::sun::star::bridge::oleautomation::Decimal& rAutomationDec )
657 : {
658 0 : SbxValue::Clear();
659 0 : aData.pDecimal = new SbxDecimal( rAutomationDec );
660 0 : aData.pDecimal->addRef();
661 0 : aData.eType = SbxDECIMAL;
662 0 : return sal_True;
663 : }
664 :
665 0 : sal_Bool SbxValue::fillAutomationDecimal
666 : ( com::sun::star::bridge::oleautomation::Decimal& rAutomationDec )
667 : {
668 0 : SbxDecimal* pDecimal = GetDecimal();
669 0 : if( pDecimal != NULL )
670 : {
671 0 : pDecimal->fillAutomationDecimal( rAutomationDec );
672 0 : return sal_True;
673 : }
674 0 : return sal_False;
675 : }
676 :
677 :
678 4455 : sal_Bool SbxValue::PutString( const OUString& r )
679 : {
680 4455 : SbxValues aRes;
681 4455 : aRes.eType = SbxSTRING;
682 4455 : aRes.pOUString = (OUString*) &r;
683 4455 : Put( aRes );
684 4455 : return sal_Bool( !IsError() );
685 : }
686 :
687 :
688 : #define PUT( p, e, t, m ) \
689 : sal_Bool SbxValue::p( t n ) \
690 : { SbxValues aRes(e); aRes.m = n; Put( aRes ); return sal_Bool( !IsError() ); }
691 :
692 12 : PUT( PutByte, SbxBYTE, sal_uInt8, nByte )
693 0 : PUT( PutChar, SbxCHAR, sal_Unicode, nChar )
694 0 : PUT( PutCurrency, SbxCURRENCY, const sal_Int64&, nInt64 )
695 5 : PUT( PutDate, SbxDATE, double, nDouble )
696 2627 : PUT( PutDouble, SbxDOUBLE, double, nDouble )
697 15 : PUT( PutErr, SbxERROR, sal_uInt16, nUShort )
698 3176 : PUT( PutInteger, SbxINTEGER, sal_Int16, nInteger )
699 809 : PUT( PutLong, SbxLONG, sal_Int32, nLong )
700 2543 : PUT( PutObject, SbxOBJECT, SbxBase*, pObj )
701 2 : PUT( PutSingle, SbxSINGLE, float, nSingle )
702 0 : PUT( PutULong, SbxULONG, sal_uInt32, nULong )
703 0 : PUT( PutUShort, SbxUSHORT, sal_uInt16, nUShort )
704 0 : PUT( PutInt64, SbxSALINT64, sal_Int64, nInt64 )
705 0 : PUT( PutUInt64, SbxSALUINT64, sal_uInt64, uInt64 )
706 0 : PUT( PutDecimal, SbxDECIMAL, SbxDecimal*, pDecimal )
707 :
708 : ////////////////////////// Setting of the data type ///////////////////////////
709 :
710 82909 : sal_Bool SbxValue::IsFixed() const
711 : {
712 82909 : return ( (GetFlags() & SBX_FIXED) | (aData.eType & SbxBYREF) ) != 0;
713 : }
714 :
715 : // A variable is numeric, if it is EMPTY or realy numeric
716 : // or if it contains a complete convertible String
717 :
718 : // #41692, implement it for RTL and Basic-Core separately
719 15 : sal_Bool SbxValue::IsNumeric() const
720 : {
721 15 : return ImpIsNumeric( /*bOnlyIntntl*/false );
722 : }
723 :
724 4 : sal_Bool SbxValue::IsNumericRTL() const
725 : {
726 4 : return ImpIsNumeric( /*bOnlyIntntl*/true );
727 : }
728 :
729 19 : sal_Bool SbxValue::ImpIsNumeric( bool bOnlyIntntl ) const
730 : {
731 :
732 19 : if( !CanRead() )
733 : {
734 0 : SetError( SbxERR_PROP_WRITEONLY ); return sal_False;
735 : }
736 : // Test downcast!!!
737 19 : if( this->ISA(SbxVariable) )
738 18 : ((SbxVariable*)this)->Broadcast( SBX_HINT_DATAWANTED );
739 19 : SbxDataType t = GetType();
740 19 : if( t == SbxSTRING )
741 : {
742 1 : if( aData.pOUString )
743 : {
744 0 : OUString s( *aData.pOUString );
745 : double n;
746 : SbxDataType t2;
747 0 : sal_uInt16 nLen = 0;
748 0 : if( ImpScan( s, n, t2, &nLen, /*bAllowIntntl*/false, bOnlyIntntl ) == SbxERR_OK )
749 0 : return sal_Bool( nLen == s.getLength() );
750 : }
751 1 : return sal_False;
752 : }
753 : else
754 : return sal_Bool( t == SbxEMPTY
755 11 : || ( t >= SbxINTEGER && t <= SbxCURRENCY )
756 18 : || ( t >= SbxCHAR && t <= SbxUINT ) );
757 : }
758 :
759 0 : SbxClassType SbxValue::GetClass() const
760 : {
761 0 : return SbxCLASS_VALUE;
762 : }
763 :
764 33199 : SbxDataType SbxValue::GetType() const
765 : {
766 33199 : return SbxDataType( aData.eType & 0x0FFF );
767 : }
768 :
769 0 : SbxDataType SbxValue::GetFullType() const
770 : {
771 0 : return aData.eType;
772 : }
773 :
774 30426 : sal_Bool SbxValue::SetType( SbxDataType t )
775 : {
776 : DBG_ASSERT( !( t & 0xF000 ), "SetType of BYREF|ARRAY is forbidden!" );
777 30426 : if( ( t == SbxEMPTY && aData.eType == SbxVOID )
778 29917 : || ( aData.eType == SbxEMPTY && t == SbxVOID ) )
779 563 : return sal_True;
780 29863 : if( ( t & 0x0FFF ) == SbxVARIANT )
781 : {
782 : // Trial to set the data type to Variant
783 146 : ResetFlag( SBX_FIXED );
784 146 : if( IsFixed() )
785 : {
786 0 : SetError( SbxERR_CONVERSION ); return sal_False;
787 : }
788 146 : t = SbxEMPTY;
789 : }
790 29863 : if( ( t & 0x0FFF ) != ( aData.eType & 0x0FFF ) )
791 : {
792 12683 : if( !CanWrite() || IsFixed() )
793 : {
794 0 : SetError( SbxERR_CONVERSION ); return sal_False;
795 : }
796 : else
797 : {
798 : // De-allocate potential objects
799 12683 : switch( aData.eType )
800 : {
801 : case SbxSTRING:
802 18 : delete aData.pOUString;
803 18 : break;
804 : case SbxOBJECT:
805 25 : if( aData.pObj && aData.pObj != this )
806 : {
807 : SAL_WARN("basic.sbx", "Not at Parent-Prop - otherwise CyclicRef");
808 18 : SbxVariable *pThisVar = PTR_CAST(SbxVariable, this);
809 : sal_uInt16 nSlotId = pThisVar
810 18 : ? ( (sal_Int16) ( pThisVar->GetUserData() & 0xFFFF ) )
811 36 : : 0;
812 : DBG_ASSERT( nSlotId != 5345 || pThisVar->GetName().equalsAscii("Parent"),
813 : "SID_PARENTOBJECT is not named 'Parent'" );
814 18 : sal_Bool bParentProp = 5345 == nSlotId;
815 18 : if ( !bParentProp )
816 18 : aData.pObj->ReleaseRef();
817 : }
818 25 : break;
819 12640 : default: break;
820 : }
821 : // This works always, because the Float representations are 0 as well.
822 12683 : memset( &aData, 0, sizeof( SbxValues ) );
823 12683 : aData.eType = t;
824 : }
825 : }
826 29863 : return sal_True;
827 : }
828 :
829 19 : sal_Bool SbxValue::Convert( SbxDataType eTo )
830 : {
831 19 : eTo = SbxDataType( eTo & 0x0FFF );
832 19 : if( ( aData.eType & 0x0FFF ) == eTo )
833 19 : return sal_True;
834 0 : if( !CanWrite() )
835 0 : return sal_False;
836 0 : if( eTo == SbxVARIANT )
837 : {
838 : // Trial to set the data type to Variant
839 0 : ResetFlag( SBX_FIXED );
840 0 : if( IsFixed() )
841 : {
842 0 : SetError( SbxERR_CONVERSION ); return sal_False;
843 : }
844 : else
845 0 : return sal_True;
846 : }
847 : // Converting from zero doesn't work. Once zero, always zero!
848 0 : if( aData.eType == SbxNULL )
849 : {
850 0 : SetError( SbxERR_CONVERSION ); return sal_False;
851 : }
852 :
853 : // Conversion of the data:
854 0 : SbxValues aNew;
855 0 : aNew.eType = eTo;
856 0 : if( Get( aNew ) )
857 : {
858 : // The data type could be converted. It ends here with fixed elements,
859 : // because the data had not to be taken over
860 0 : if( !IsFixed() )
861 : {
862 0 : SetType( eTo );
863 0 : Put( aNew );
864 0 : SetModified( sal_True );
865 : }
866 0 : Broadcast( SBX_HINT_CONVERTED );
867 0 : return sal_True;
868 : }
869 : else
870 0 : return sal_False;
871 : }
872 : ////////////////////////////////// Calculating /////////////////////////////////
873 :
874 3613 : sal_Bool SbxValue::Compute( SbxOperator eOp, const SbxValue& rOp )
875 : {
876 : #ifdef DISABLE_SCRIPTING
877 : bool bVBAInterop = false;
878 : #else
879 3613 : bool bVBAInterop = SbiRuntime::isVBAEnabled();
880 : #endif
881 3613 : SbxDataType eThisType = GetType();
882 3613 : SbxDataType eOpType = rOp.GetType();
883 3613 : SbxError eOld = GetError();
884 3613 : if( eOld != SbxERR_OK )
885 0 : ResetError();
886 3613 : if( !CanWrite() )
887 0 : SetError( SbxERR_PROP_READONLY );
888 3613 : else if( !rOp.CanRead() )
889 0 : SetError( SbxERR_PROP_WRITEONLY );
890 : // Special rule 1: If one operand is zero, the result is zero
891 3613 : else if( eThisType == SbxNULL || eOpType == SbxNULL )
892 0 : SetType( SbxNULL );
893 : // Special rule 2: If the operand is Empty, the result is the 2. operand
894 3613 : else if( eThisType == SbxEMPTY
895 9 : && !bVBAInterop
896 : )
897 0 : *this = rOp;
898 : // 1996-2-13: Don't test already before Get upon SbxEMPTY
899 : else
900 : {
901 3613 : SbxValues aL, aR;
902 3613 : bool bDecimal = false;
903 3613 : if( bVBAInterop && ( ( eThisType == SbxSTRING && eOpType != SbxSTRING && eOpType != SbxEMPTY ) ||
904 1188 : ( eThisType != SbxSTRING && eThisType != SbxEMPTY && eOpType == SbxSTRING ) ) &&
905 358 : ( eOp == SbxMUL || eOp == SbxDIV || eOp == SbxPLUS || eOp == SbxMINUS ) )
906 : {
907 : goto Lbl_OpIsDouble;
908 : }
909 3601 : else if( eThisType == SbxSTRING || eOp == SbxCAT || ( bVBAInterop && ( eOpType == SbxSTRING ) && ( eOp == SbxPLUS ) ) )
910 : {
911 2891 : if( eOp == SbxCAT || eOp == SbxPLUS )
912 : {
913 : // From 1999-11-5, keep OUString in mind
914 1446 : aL.eType = aR.eType = SbxSTRING;
915 1446 : rOp.Get( aR );
916 : // From 1999-12-8, #70399: Here call GetType() again, Get() can change the type!
917 1446 : if( rOp.GetType() == SbxEMPTY )
918 2 : goto Lbl_OpIsEmpty;
919 1445 : Get( aL );
920 :
921 : // #30576: To begin with test, if the conversion worked
922 1445 : if( aL.pOUString != NULL && aR.pOUString != NULL )
923 : {
924 1445 : *aL.pOUString += *aR.pOUString;
925 : }
926 : // Not even Left OK?
927 0 : else if( aL.pOUString == NULL )
928 : {
929 0 : aL.pOUString = new OUString();
930 : }
931 1445 : Put( aL );
932 : }
933 : else
934 0 : SetError( SbxERR_CONVERSION );
935 : }
936 2155 : else if( eOpType == SbxSTRING && rOp.IsFixed() )
937 : { // Numeric: there is no String allowed on the right side
938 0 : SetError( SbxERR_CONVERSION );
939 : // falls all the way out
940 : }
941 2155 : else if( ( eOp >= SbxIDIV && eOp <= SbxNOT ) || eOp == SbxMOD )
942 : {
943 792 : if( GetType() == eOpType )
944 : {
945 2364 : if( GetType() == SbxSALUINT64 || GetType() == SbxSALINT64
946 1576 : || GetType() == SbxCURRENCY || GetType() == SbxULONG )
947 0 : aL.eType = aR.eType = GetType();
948 788 : else if ( bVBAInterop && eOpType == SbxBOOL )
949 68 : aL.eType = aR.eType = SbxBOOL;
950 : else
951 720 : aL.eType = aR.eType = SbxLONG;
952 : }
953 : else
954 4 : aL.eType = aR.eType = SbxLONG;
955 :
956 1584 : if( rOp.Get( aR ) ) // re-do Get after type assigns above
957 : {
958 792 : if( rOp.GetType() == SbxEMPTY )
959 : {
960 0 : if ( !bVBAInterop || ( bVBAInterop && ( eOp != SbxNOT ) ) )
961 : goto Lbl_OpIsEmpty;
962 : }
963 792 : if( Get( aL ) ) switch( eOp )
964 : {
965 : case SbxIDIV:
966 0 : if( aL.eType == SbxCURRENCY )
967 0 : if( !aR.nInt64 ) SetError( SbxERR_ZERODIV );
968 : else {
969 0 : aL.nInt64 /= aR.nInt64;
970 0 : aL.nInt64 *= CURRENCY_FACTOR;
971 : }
972 0 : else if( aL.eType == SbxSALUINT64 )
973 0 : if( !aR.uInt64 ) SetError( SbxERR_ZERODIV );
974 0 : else aL.uInt64 /= aR.uInt64;
975 0 : else if( aL.eType == SbxSALINT64 )
976 0 : if( !aR.nInt64 ) SetError( SbxERR_ZERODIV );
977 0 : else aL.nInt64 /= aR.nInt64;
978 0 : else if( aL.eType == SbxLONG )
979 0 : if( !aR.nLong ) SetError( SbxERR_ZERODIV );
980 0 : else aL.nLong /= aR.nLong;
981 : else
982 0 : if( !aR.nULong ) SetError( SbxERR_ZERODIV );
983 0 : else aL.nULong /= aR.nULong;
984 0 : break;
985 : case SbxMOD:
986 0 : if( aL.eType == SbxCURRENCY || aL.eType == SbxSALINT64 )
987 0 : if( !aR.nInt64 ) SetError( SbxERR_ZERODIV );
988 0 : else aL.nInt64 %= aR.nInt64;
989 0 : else if( aL.eType == SbxSALUINT64 )
990 0 : if( !aR.uInt64 ) SetError( SbxERR_ZERODIV );
991 0 : else aL.uInt64 %= aR.uInt64;
992 0 : else if( aL.eType == SbxLONG )
993 0 : if( !aR.nLong ) SetError( SbxERR_ZERODIV );
994 0 : else aL.nLong %= aR.nLong;
995 : else
996 0 : if( !aR.nULong ) SetError( SbxERR_ZERODIV );
997 0 : else aL.nULong %= aR.nULong;
998 0 : break;
999 : case SbxAND:
1000 108 : if( aL.eType != SbxLONG && aL.eType != SbxULONG )
1001 43 : aL.nInt64 &= aR.nInt64;
1002 : else
1003 65 : aL.nLong &= aR.nLong;
1004 108 : break;
1005 : case SbxOR:
1006 613 : if( aL.eType != SbxLONG && aL.eType != SbxULONG )
1007 7 : aL.nInt64 |= aR.nInt64;
1008 : else
1009 606 : aL.nLong |= aR.nLong;
1010 613 : break;
1011 : case SbxXOR:
1012 0 : if( aL.eType != SbxLONG && aL.eType != SbxULONG )
1013 0 : aL.nInt64 ^= aR.nInt64;
1014 : else
1015 0 : aL.nLong ^= aR.nLong;
1016 0 : break;
1017 : case SbxEQV:
1018 0 : if( aL.eType != SbxLONG && aL.eType != SbxULONG )
1019 0 : aL.nInt64 = (aL.nInt64 & aR.nInt64) | (~aL.nInt64 & ~aR.nInt64);
1020 : else
1021 0 : aL.nLong = (aL.nLong & aR.nLong) | (~aL.nLong & ~aR.nLong);
1022 0 : break;
1023 : case SbxIMP:
1024 0 : if( aL.eType != SbxLONG && aL.eType != SbxULONG )
1025 0 : aL.nInt64 = ~aL.nInt64 | aR.nInt64;
1026 : else
1027 0 : aL.nLong = ~aL.nLong | aR.nLong;
1028 0 : break;
1029 : case SbxNOT:
1030 71 : if( aL.eType != SbxLONG && aL.eType != SbxULONG )
1031 : {
1032 36 : if ( aL.eType != SbxBOOL )
1033 0 : aL.nInt64 = ~aL.nInt64;
1034 : else
1035 18 : aL.nLong = ~aL.nLong;
1036 : }
1037 : else
1038 53 : aL.nLong = ~aL.nLong;
1039 71 : break;
1040 0 : default: break;
1041 : }
1042 : }
1043 : }
1044 4089 : else if( ( GetType() == SbxDECIMAL || rOp.GetType() == SbxDECIMAL )
1045 1363 : && ( eOp == SbxMUL || eOp == SbxDIV || eOp == SbxPLUS || eOp == SbxMINUS || eOp == SbxNEG ) )
1046 : {
1047 0 : aL.eType = aR.eType = SbxDECIMAL;
1048 0 : bDecimal = true;
1049 0 : if( rOp.Get( aR ) )
1050 : {
1051 0 : if( rOp.GetType() == SbxEMPTY )
1052 : {
1053 0 : releaseDecimalPtr( aL.pDecimal );
1054 0 : goto Lbl_OpIsEmpty;
1055 : }
1056 0 : if( Get( aL ) )
1057 : {
1058 0 : if( aL.pDecimal && aR.pDecimal )
1059 : {
1060 0 : bool bOk = true;
1061 0 : switch( eOp )
1062 : {
1063 : case SbxMUL:
1064 0 : bOk = ( *(aL.pDecimal) *= *(aR.pDecimal) );
1065 0 : break;
1066 : case SbxDIV:
1067 0 : if( aR.pDecimal->isZero() )
1068 0 : SetError( SbxERR_ZERODIV );
1069 : else
1070 0 : bOk = ( *(aL.pDecimal) /= *(aR.pDecimal) );
1071 0 : break;
1072 : case SbxPLUS:
1073 0 : bOk = ( *(aL.pDecimal) += *(aR.pDecimal) );
1074 0 : break;
1075 : case SbxMINUS:
1076 0 : bOk = ( *(aL.pDecimal) -= *(aR.pDecimal) );
1077 0 : break;
1078 : case SbxNEG:
1079 0 : bOk = ( aL.pDecimal->neg() );
1080 0 : break;
1081 : default:
1082 0 : SetError( SbxERR_NOTIMP );
1083 : }
1084 0 : if( !bOk )
1085 0 : SetError( SbxERR_OVERFLOW );
1086 : }
1087 : else
1088 : {
1089 0 : SetError( SbxERR_CONVERSION );
1090 : }
1091 : }
1092 : }
1093 : }
1094 1363 : else if( GetType() == SbxCURRENCY || rOp.GetType() == SbxCURRENCY )
1095 : {
1096 0 : aL.eType = SbxCURRENCY;
1097 0 : aR.eType = SbxCURRENCY;
1098 :
1099 0 : if( rOp.Get( aR ) )
1100 : {
1101 0 : if( rOp.GetType() == SbxEMPTY )
1102 0 : goto Lbl_OpIsEmpty;
1103 :
1104 0 : if( Get( aL ) ) switch( eOp )
1105 : {
1106 : double dTest;
1107 : case SbxMUL:
1108 : // first overflow check: see if product will fit - test real value of product (hence 2 curr factors)
1109 0 : dTest = (double)aL.nInt64 * (double)aR.nInt64 / (double)CURRENCY_FACTOR_SQUARE;
1110 0 : if( dTest < SbxMINCURR || SbxMAXCURR < dTest)
1111 : {
1112 0 : aL.nInt64 = SAL_MAX_INT64;
1113 0 : if( dTest < SbxMINCURR ) aL.nInt64 = SAL_MIN_INT64;
1114 0 : SetError( SbxERR_OVERFLOW );
1115 0 : break;
1116 : }
1117 : // second overflow check: see if unscaled product overflows - if so use doubles
1118 0 : dTest = (double)aL.nInt64 * (double)aR.nInt64;
1119 0 : if( dTest < SAL_MIN_INT64 || SAL_MAX_INT64 < dTest)
1120 : {
1121 0 : aL.nInt64 = (sal_Int64)( dTest / (double)CURRENCY_FACTOR );
1122 0 : break;
1123 : }
1124 : // precise calc: multiply then scale back (move decimal pt)
1125 0 : aL.nInt64 *= aR.nInt64;
1126 0 : aL.nInt64 /= CURRENCY_FACTOR;
1127 0 : break;
1128 :
1129 : case SbxDIV:
1130 0 : if( !aR.nInt64 )
1131 : {
1132 0 : SetError( SbxERR_ZERODIV );
1133 0 : break;
1134 : }
1135 : // first overflow check: see if quotient will fit - calc real value of quotient (curr factors cancel)
1136 0 : dTest = (double)aL.nInt64 / (double)aR.nInt64;
1137 0 : if( dTest < SbxMINCURR || SbxMAXCURR < dTest)
1138 : {
1139 0 : SetError( SbxERR_OVERFLOW );
1140 0 : break;
1141 : }
1142 : // second overflow check: see if scaled dividend overflows - if so use doubles
1143 0 : dTest = (double)aL.nInt64 * (double)CURRENCY_FACTOR;
1144 0 : if( dTest < SAL_MIN_INT64 || SAL_MAX_INT64 < dTest)
1145 : {
1146 0 : aL.nInt64 = (sal_Int64)(dTest / (double)aR.nInt64);
1147 0 : break;
1148 : }
1149 : // precise calc: scale (move decimal pt) then divide
1150 0 : aL.nInt64 *= CURRENCY_FACTOR;
1151 0 : aL.nInt64 /= aR.nInt64;
1152 0 : break;
1153 :
1154 : case SbxPLUS:
1155 0 : dTest = ( (double)aL.nInt64 + (double)aR.nInt64 ) / (double)CURRENCY_FACTOR;
1156 0 : if( dTest < SbxMINCURR || SbxMAXCURR < dTest)
1157 : {
1158 0 : SetError( SbxERR_OVERFLOW );
1159 0 : break;
1160 : }
1161 0 : aL.nInt64 += aR.nInt64;
1162 0 : break;
1163 :
1164 : case SbxMINUS:
1165 0 : dTest = ( (double)aL.nInt64 - (double)aR.nInt64 ) / (double)CURRENCY_FACTOR;
1166 0 : if( dTest < SbxMINCURR || SbxMAXCURR < dTest)
1167 : {
1168 0 : SetError( SbxERR_OVERFLOW );
1169 0 : break;
1170 : }
1171 0 : aL.nInt64 -= aR.nInt64;
1172 0 : break;
1173 : case SbxNEG:
1174 0 : aL.nInt64 = -aL.nInt64;
1175 0 : break;
1176 : default:
1177 0 : SetError( SbxERR_NOTIMP );
1178 : }
1179 : }
1180 : }
1181 : else
1182 : Lbl_OpIsDouble:
1183 : { // other types and operators including Date, Double and Single
1184 1375 : aL.eType = aR.eType = SbxDOUBLE;
1185 1375 : if( rOp.Get( aR ) )
1186 : {
1187 1375 : if( rOp.GetType() == SbxEMPTY )
1188 : {
1189 0 : if ( !bVBAInterop || ( bVBAInterop && ( eOp != SbxNEG ) ) )
1190 : goto Lbl_OpIsEmpty;
1191 : }
1192 1375 : if( Get( aL ) )
1193 : {
1194 1367 : switch( eOp )
1195 : {
1196 : case SbxEXP:
1197 0 : aL.nDouble = pow( aL.nDouble, aR.nDouble );
1198 0 : break;
1199 : case SbxMUL:
1200 14 : aL.nDouble *= aR.nDouble; break;
1201 : case SbxDIV:
1202 0 : if( !aR.nDouble ) SetError( SbxERR_ZERODIV );
1203 0 : else aL.nDouble /= aR.nDouble; break;
1204 : case SbxPLUS:
1205 1305 : aL.nDouble += aR.nDouble; break;
1206 : case SbxMINUS:
1207 37 : aL.nDouble -= aR.nDouble; break;
1208 : case SbxNEG:
1209 11 : aL.nDouble = -aL.nDouble; break;
1210 : default:
1211 0 : SetError( SbxERR_NOTIMP );
1212 : }
1213 : // Date with "+" or "-" needs special handling that
1214 : // forces the Date type. If the operation is '+' the
1215 : // result is always a Date, if '-' the result is only
1216 : // a Date if one of lhs or rhs ( but not both ) is already
1217 : // a Date
1218 1367 : if( ( GetType() == SbxDATE || rOp.GetType() == SbxDATE ) )
1219 : {
1220 0 : if( eOp == SbxPLUS || ( ( eOp == SbxMINUS ) && ( GetType() != rOp.GetType() ) ) )
1221 0 : aL.eType = SbxDATE;
1222 : }
1223 :
1224 : }
1225 : }
1226 :
1227 : }
1228 3612 : if( !IsError() )
1229 3604 : Put( aL );
1230 3612 : if( bDecimal )
1231 : {
1232 0 : releaseDecimalPtr( aL.pDecimal );
1233 0 : releaseDecimalPtr( aR.pDecimal );
1234 : }
1235 : }
1236 : Lbl_OpIsEmpty:
1237 :
1238 3613 : sal_Bool bRes = sal_Bool( !IsError() );
1239 3613 : if( bRes && eOld != SbxERR_OK )
1240 0 : SetError( eOld );
1241 3613 : return bRes;
1242 : }
1243 :
1244 : // The comparison routine deliver TRUE or FALSE.
1245 :
1246 3257 : sal_Bool SbxValue::Compare( SbxOperator eOp, const SbxValue& rOp ) const
1247 : {
1248 : #ifdef DISABLE_SCRIPTING
1249 : bool bVBAInterop = false;
1250 : #else
1251 3257 : bool bVBAInterop = SbiRuntime::isVBAEnabled();
1252 : #endif
1253 :
1254 3257 : sal_Bool bRes = sal_False;
1255 3257 : SbxError eOld = GetError();
1256 3257 : if( eOld != SbxERR_OK )
1257 0 : ResetError();
1258 3257 : if( !CanRead() || !rOp.CanRead() )
1259 0 : SetError( SbxERR_PROP_WRITEONLY );
1260 3257 : else if( GetType() == SbxNULL && rOp.GetType() == SbxNULL && !bVBAInterop )
1261 : {
1262 0 : bRes = sal_True;
1263 : }
1264 3257 : else if( GetType() == SbxEMPTY && rOp.GetType() == SbxEMPTY )
1265 3 : bRes = !bVBAInterop ? sal_True : ( eOp == SbxEQ ? sal_True : sal_False );
1266 : // Special rule 1: If an operand is zero, the result is FALSE
1267 3254 : else if( GetType() == SbxNULL || rOp.GetType() == SbxNULL )
1268 0 : bRes = sal_False;
1269 : // Special rule 2: If both are variant and one is numeric
1270 : // and the other is a String, num is < str
1271 7202 : else if( !IsFixed() && !rOp.IsFixed()
1272 3840 : && ( rOp.GetType() == SbxSTRING && GetType() != SbxSTRING && IsNumeric() ) && !bVBAInterop
1273 : )
1274 1 : bRes = sal_Bool( eOp == SbxLT || eOp == SbxLE || eOp == SbxNE );
1275 7199 : else if( !IsFixed() && !rOp.IsFixed()
1276 585 : && ( GetType() == SbxSTRING && rOp.GetType() != SbxSTRING && rOp.IsNumeric() )
1277 3253 : && !bVBAInterop
1278 : )
1279 0 : bRes = sal_Bool( eOp == SbxGT || eOp == SbxGE || eOp == SbxNE );
1280 : else
1281 : {
1282 3253 : SbxValues aL, aR;
1283 : // If one of the operands is a String,
1284 : // a String comparing take place
1285 3253 : if( GetType() == SbxSTRING || rOp.GetType() == SbxSTRING )
1286 : {
1287 504 : aL.eType = aR.eType = SbxSTRING;
1288 504 : if( Get( aL ) && rOp.Get( aR ) ) switch( eOp )
1289 : {
1290 : case SbxEQ:
1291 255 : bRes = sal_Bool( *aL.pOUString == *aR.pOUString ); break;
1292 : case SbxNE:
1293 249 : bRes = sal_Bool( *aL.pOUString != *aR.pOUString ); break;
1294 : case SbxLT:
1295 0 : bRes = sal_Bool( *aL.pOUString < *aR.pOUString ); break;
1296 : case SbxGT:
1297 0 : bRes = sal_Bool( *aL.pOUString > *aR.pOUString ); break;
1298 : case SbxLE:
1299 0 : bRes = sal_Bool( *aL.pOUString <= *aR.pOUString ); break;
1300 : case SbxGE:
1301 0 : bRes = sal_Bool( *aL.pOUString >= *aR.pOUString ); break;
1302 : default:
1303 0 : SetError( SbxERR_NOTIMP );
1304 : }
1305 : }
1306 : // From 1995-12-19: If SbxSINGLE participate, then convert to SINGLE,
1307 : // elsewise it shows a numeric error
1308 2749 : else if( GetType() == SbxSINGLE || rOp.GetType() == SbxSINGLE )
1309 : {
1310 0 : aL.eType = aR.eType = SbxSINGLE;
1311 0 : if( Get( aL ) && rOp.Get( aR ) )
1312 0 : switch( eOp )
1313 : {
1314 : case SbxEQ:
1315 0 : bRes = sal_Bool( aL.nSingle == aR.nSingle ); break;
1316 : case SbxNE:
1317 0 : bRes = sal_Bool( aL.nSingle != aR.nSingle ); break;
1318 : case SbxLT:
1319 0 : bRes = sal_Bool( aL.nSingle < aR.nSingle ); break;
1320 : case SbxGT:
1321 0 : bRes = sal_Bool( aL.nSingle > aR.nSingle ); break;
1322 : case SbxLE:
1323 0 : bRes = sal_Bool( aL.nSingle <= aR.nSingle ); break;
1324 : case SbxGE:
1325 0 : bRes = sal_Bool( aL.nSingle >= aR.nSingle ); break;
1326 : default:
1327 0 : SetError( SbxERR_NOTIMP );
1328 : }
1329 : }
1330 2749 : else if( GetType() == SbxDECIMAL && rOp.GetType() == SbxDECIMAL )
1331 : {
1332 0 : aL.eType = aR.eType = SbxDECIMAL;
1333 0 : Get( aL );
1334 0 : rOp.Get( aR );
1335 0 : if( aL.pDecimal && aR.pDecimal )
1336 : {
1337 0 : SbxDecimal::CmpResult eRes = compare( *aL.pDecimal, *aR.pDecimal );
1338 0 : switch( eOp )
1339 : {
1340 : case SbxEQ:
1341 0 : bRes = sal_Bool( eRes == SbxDecimal::EQ ); break;
1342 : case SbxNE:
1343 0 : bRes = sal_Bool( eRes != SbxDecimal::EQ ); break;
1344 : case SbxLT:
1345 0 : bRes = sal_Bool( eRes == SbxDecimal::LT ); break;
1346 : case SbxGT:
1347 0 : bRes = sal_Bool( eRes == SbxDecimal::GT ); break;
1348 : case SbxLE:
1349 0 : bRes = sal_Bool( eRes != SbxDecimal::GT ); break;
1350 : case SbxGE:
1351 0 : bRes = sal_Bool( eRes != SbxDecimal::LT ); break;
1352 : default:
1353 0 : SetError( SbxERR_NOTIMP );
1354 0 : }
1355 : }
1356 : else
1357 : {
1358 0 : SetError( SbxERR_CONVERSION );
1359 : }
1360 0 : releaseDecimalPtr( aL.pDecimal );
1361 0 : releaseDecimalPtr( aR.pDecimal );
1362 : }
1363 : // Everything else comparing on a SbxDOUBLE-Basis
1364 : else
1365 : {
1366 2749 : aL.eType = aR.eType = SbxDOUBLE;
1367 2749 : bool bGetL = Get( aL );
1368 2749 : bool bGetR = rOp.Get( aR );
1369 5498 : if( bGetL && bGetR )
1370 2749 : switch( eOp )
1371 : {
1372 : case SbxEQ:
1373 1901 : bRes = sal_Bool( aL.nDouble == aR.nDouble ); break;
1374 : case SbxNE:
1375 231 : bRes = sal_Bool( aL.nDouble != aR.nDouble ); break;
1376 : case SbxLT:
1377 40 : bRes = sal_Bool( aL.nDouble < aR.nDouble ); break;
1378 : case SbxGT:
1379 573 : bRes = sal_Bool( aL.nDouble > aR.nDouble ); break;
1380 : case SbxLE:
1381 0 : bRes = sal_Bool( aL.nDouble <= aR.nDouble ); break;
1382 : case SbxGE:
1383 4 : bRes = sal_Bool( aL.nDouble >= aR.nDouble ); break;
1384 : default:
1385 0 : SetError( SbxERR_NOTIMP );
1386 : }
1387 : // at least one value was got
1388 : // if this is VBA then a conversion error for one
1389 : // side will yield a false result of an equality test
1390 0 : else if ( bGetR || bGetL )
1391 : {
1392 0 : if ( bVBAInterop && eOp == SbxEQ && GetError() == SbxERR_CONVERSION )
1393 : {
1394 0 : ResetError();
1395 0 : bRes = sal_False;
1396 : }
1397 : }
1398 : }
1399 : }
1400 3257 : if( eOld != SbxERR_OK )
1401 0 : SetError( eOld );
1402 3257 : return bRes;
1403 : }
1404 :
1405 : ///////////////////////////// Reading/Writing ////////////////////////////
1406 :
1407 0 : sal_Bool SbxValue::LoadData( SvStream& r, sal_uInt16 )
1408 : {
1409 : // #TODO see if these types are really dumped to any stream
1410 : // more than likely this is functionality used in the binfilter alone
1411 0 : SbxValue::Clear();
1412 : sal_uInt16 nType;
1413 0 : r >> nType;
1414 0 : aData.eType = SbxDataType( nType );
1415 0 : switch( nType )
1416 : {
1417 : case SbxBOOL:
1418 : case SbxINTEGER:
1419 0 : r >> aData.nInteger; break;
1420 : case SbxLONG:
1421 0 : r >> aData.nLong; break;
1422 : case SbxSINGLE:
1423 : {
1424 : // Floats as ASCII
1425 : OUString aVal = read_lenPrefixed_uInt8s_ToOUString<sal_uInt16>(r,
1426 0 : RTL_TEXTENCODING_ASCII_US);
1427 : double d;
1428 : SbxDataType t;
1429 0 : if( ImpScan( aVal, d, t, NULL ) != SbxERR_OK || t == SbxDOUBLE )
1430 : {
1431 0 : aData.nSingle = 0.0F;
1432 0 : return sal_False;
1433 : }
1434 0 : aData.nSingle = (float) d;
1435 0 : break;
1436 : }
1437 : case SbxDATE:
1438 : case SbxDOUBLE:
1439 : {
1440 : // Floats as ASCII
1441 : OUString aVal = read_lenPrefixed_uInt8s_ToOUString<sal_uInt16>(r,
1442 0 : RTL_TEXTENCODING_ASCII_US);
1443 : SbxDataType t;
1444 0 : if( ImpScan( aVal, aData.nDouble, t, NULL ) != SbxERR_OK )
1445 : {
1446 0 : aData.nDouble = 0.0;
1447 0 : return sal_False;
1448 : }
1449 0 : break;
1450 : }
1451 : //#fdo39428 SvStream no longer supports operator>>(long&)
1452 : case SbxSALINT64:
1453 0 : r.ReadInt64(aData.nInt64);
1454 0 : break;
1455 : case SbxSALUINT64:
1456 0 : r >> aData.uInt64;
1457 0 : break;
1458 : case SbxCURRENCY:
1459 : {
1460 0 : sal_uInt32 tmpHi = 0;
1461 0 : sal_uInt32 tmpLo = 0;
1462 0 : r >> tmpHi >> tmpLo;
1463 0 : aData.nInt64 = ((sal_Int64)tmpHi << 32);
1464 0 : aData.nInt64 |= (sal_Int64)tmpLo;
1465 0 : break;
1466 : }
1467 : case SbxSTRING:
1468 : {
1469 : OUString aVal = read_lenPrefixed_uInt8s_ToOUString<sal_uInt16>(r,
1470 0 : RTL_TEXTENCODING_ASCII_US);
1471 0 : if( !aVal.isEmpty() )
1472 0 : aData.pOUString = new OUString( aVal );
1473 : else
1474 0 : aData.pOUString = NULL; // JSM 1995-09-22
1475 0 : break;
1476 : }
1477 : case SbxERROR:
1478 : case SbxUSHORT:
1479 0 : r >> aData.nUShort; break;
1480 : case SbxOBJECT:
1481 : {
1482 : sal_uInt8 nMode;
1483 0 : r >> nMode;
1484 0 : switch( nMode )
1485 : {
1486 : case 0:
1487 0 : aData.pObj = NULL;
1488 0 : break;
1489 : case 1:
1490 0 : aData.pObj = SbxBase::Load( r );
1491 0 : return sal_Bool( aData.pObj != NULL );
1492 : case 2:
1493 0 : aData.pObj = this;
1494 0 : break;
1495 : }
1496 0 : break;
1497 : }
1498 : case SbxCHAR:
1499 : {
1500 : char c;
1501 0 : r >> c;
1502 0 : aData.nChar = c;
1503 0 : break;
1504 : }
1505 : case SbxBYTE:
1506 0 : r >> aData.nByte; break;
1507 : case SbxULONG:
1508 0 : r >> aData.nULong; break;
1509 : case SbxINT:
1510 : {
1511 : sal_uInt8 n;
1512 0 : r >> n;
1513 : // Match the Int on this system?
1514 0 : if( n > SAL_TYPES_SIZEOFINT )
1515 0 : r >> aData.nLong, aData.eType = SbxLONG;
1516 : else {
1517 : sal_Int32 nInt;
1518 0 : r >> nInt;
1519 0 : aData.nInt = nInt;
1520 : }
1521 0 : break;
1522 : }
1523 : case SbxUINT:
1524 : {
1525 : sal_uInt8 n;
1526 0 : r >> n;
1527 : // Match the UInt on this system?
1528 0 : if( n > SAL_TYPES_SIZEOFINT )
1529 0 : r >> aData.nULong, aData.eType = SbxULONG;
1530 : else {
1531 : sal_uInt32 nUInt;
1532 0 : r >> nUInt;
1533 0 : aData.nUInt = nUInt;
1534 : }
1535 0 : break;
1536 : }
1537 : case SbxEMPTY:
1538 : case SbxNULL:
1539 : case SbxVOID:
1540 0 : break;
1541 : case SbxDATAOBJECT:
1542 0 : r >> aData.nLong;
1543 0 : break;
1544 : // #78919 For backwards compatibility
1545 : case SbxWSTRING:
1546 : case SbxWCHAR:
1547 0 : break;
1548 : default:
1549 0 : memset (&aData,0,sizeof(aData));
1550 0 : ResetFlag(SBX_FIXED);
1551 0 : aData.eType = SbxNULL;
1552 : DBG_ASSERT( !this, "Loaded a non-supported data type" );
1553 :
1554 0 : return sal_False;
1555 : }
1556 0 : return sal_True;
1557 : }
1558 :
1559 0 : sal_Bool SbxValue::StoreData( SvStream& r ) const
1560 : {
1561 0 : sal_uInt16 nType = sal::static_int_cast< sal_uInt16 >(aData.eType);
1562 0 : r << nType;
1563 0 : switch( nType & 0x0FFF )
1564 : {
1565 : case SbxBOOL:
1566 : case SbxINTEGER:
1567 0 : r << aData.nInteger; break;
1568 : case SbxLONG:
1569 0 : r << aData.nLong; break;
1570 : case SbxDATE:
1571 : // #49935: Save as double, elsewise an error during the read in
1572 0 : ((SbxValue*)this)->aData.eType = (SbxDataType)( ( nType & 0xF000 ) | SbxDOUBLE );
1573 0 : write_lenPrefixed_uInt8s_FromOUString<sal_uInt16>(r, GetCoreString(), RTL_TEXTENCODING_ASCII_US);
1574 0 : ((SbxValue*)this)->aData.eType = (SbxDataType)nType;
1575 0 : break;
1576 : case SbxSINGLE:
1577 : case SbxDOUBLE:
1578 0 : write_lenPrefixed_uInt8s_FromOUString<sal_uInt16>(r, GetCoreString(), RTL_TEXTENCODING_ASCII_US);
1579 0 : break;
1580 : case SbxSALUINT64:
1581 : case SbxSALINT64:
1582 : // see comment in SbxValue::StoreData
1583 0 : r << aData.uInt64;
1584 0 : break;
1585 : case SbxCURRENCY:
1586 : {
1587 0 : sal_Int32 tmpHi = ( (aData.nInt64 >> 32) & 0xFFFFFFFF );
1588 0 : sal_Int32 tmpLo = ( sal_Int32 )aData.nInt64;
1589 0 : r << tmpHi << tmpLo;
1590 0 : break;
1591 : }
1592 : case SbxSTRING:
1593 0 : if( aData.pOUString )
1594 : {
1595 0 : write_lenPrefixed_uInt8s_FromOUString<sal_uInt16>(r, *aData.pOUString, RTL_TEXTENCODING_ASCII_US);
1596 : }
1597 : else
1598 : {
1599 0 : write_lenPrefixed_uInt8s_FromOUString<sal_uInt16>(r, OUString(), RTL_TEXTENCODING_ASCII_US);
1600 : }
1601 0 : break;
1602 : case SbxERROR:
1603 : case SbxUSHORT:
1604 0 : r << aData.nUShort; break;
1605 : case SbxOBJECT:
1606 : // to save itself as Objectptr does not work!
1607 0 : if( aData.pObj )
1608 : {
1609 0 : if( PTR_CAST(SbxValue,aData.pObj) != this )
1610 : {
1611 0 : r << (sal_uInt8) 1;
1612 0 : return aData.pObj->Store( r );
1613 : }
1614 : else
1615 0 : r << (sal_uInt8) 2;
1616 : }
1617 : else
1618 0 : r << (sal_uInt8) 0;
1619 0 : break;
1620 : case SbxCHAR:
1621 : {
1622 0 : char c = sal::static_int_cast< char >(aData.nChar);
1623 0 : r << c;
1624 0 : break;
1625 : }
1626 : case SbxBYTE:
1627 0 : r << aData.nByte; break;
1628 : case SbxULONG:
1629 0 : r << aData.nULong; break;
1630 : case SbxINT:
1631 : {
1632 0 : sal_uInt8 n = SAL_TYPES_SIZEOFINT;
1633 0 : r << n << (sal_Int32)aData.nInt;
1634 0 : break;
1635 : }
1636 : case SbxUINT:
1637 : {
1638 0 : sal_uInt8 n = SAL_TYPES_SIZEOFINT;
1639 0 : r << n << (sal_uInt32)aData.nUInt;
1640 0 : break;
1641 : }
1642 : case SbxEMPTY:
1643 : case SbxNULL:
1644 : case SbxVOID:
1645 0 : break;
1646 : case SbxDATAOBJECT:
1647 0 : r << aData.nLong;
1648 0 : break;
1649 : // #78919 For backwards compatibility
1650 : case SbxWSTRING:
1651 : case SbxWCHAR:
1652 0 : break;
1653 : default:
1654 : DBG_ASSERT( !this, "Saving a non-supported data type" );
1655 0 : return sal_False;
1656 : }
1657 0 : return sal_True;
1658 : }
1659 :
1660 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|