Debian Codegraph @

basic/source/sbx/sbxvalue.cxx

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