1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <vcl/errcode.hxx>
#include <basic/sbx.hxx>
#include <basic/sberrors.hxx>
#include "sbxconv.hxx"
#include <com/sun/star/bridge/oleautomation/Decimal.hpp>
#include <memory>
// Implementation SbxDecimal
SbxDecimal::SbxDecimal()
{
setInt( 0 );
mnRefCount = 0;
}
SbxDecimal::SbxDecimal( const SbxDecimal& rDec )
{
#ifdef _WIN32
maDec = rDec.maDec;
#else
(void)rDec;
#endif
mnRefCount = 0;
}
SbxDecimal::SbxDecimal
( const css::bridge::oleautomation::Decimal& rAutomationDec )
{
#ifdef _WIN32
maDec.scale = rAutomationDec.Scale;
maDec.sign = rAutomationDec.Sign;
maDec.Lo32 = rAutomationDec.LowValue;
maDec.Mid32 = rAutomationDec.MiddleValue;
maDec.Hi32 = rAutomationDec.HighValue;
#else
(void)rAutomationDec;
#endif
mnRefCount = 0;
}
void SbxDecimal::fillAutomationDecimal
( css::bridge::oleautomation::Decimal& rAutomationDec )
{
#ifdef _WIN32
rAutomationDec.Scale = maDec.scale;
rAutomationDec.Sign = maDec.sign;
rAutomationDec.LowValue = maDec.Lo32;
rAutomationDec.MiddleValue = maDec.Mid32;
rAutomationDec.HighValue = maDec.Hi32;
#else
(void)rAutomationDec;
#endif
}
SbxDecimal::~SbxDecimal()
{
}
void releaseDecimalPtr( SbxDecimal*& rpDecimal )
{
if( rpDecimal )
{
rpDecimal->mnRefCount--;
if( rpDecimal->mnRefCount == 0 )
{
delete rpDecimal;
rpDecimal = nullptr;
}
}
}
#ifdef _WIN32
bool SbxDecimal::operator -= ( const SbxDecimal &r )
{
HRESULT hResult = VarDecSub( &maDec, const_cast<LPDECIMAL>(&r.maDec), &maDec );
bool bRet = ( hResult == S_OK );
return bRet;
}
bool SbxDecimal::operator += ( const SbxDecimal &r )
{
HRESULT hResult = VarDecAdd( &maDec, const_cast<LPDECIMAL>(&r.maDec), &maDec );
bool bRet = ( hResult == S_OK );
return bRet;
}
bool SbxDecimal::operator /= ( const SbxDecimal &r )
{
HRESULT hResult = VarDecDiv( &maDec, const_cast<LPDECIMAL>(&r.maDec), &maDec );
bool bRet = ( hResult == S_OK );
return bRet;
}
bool SbxDecimal::operator *= ( const SbxDecimal &r )
{
HRESULT hResult = VarDecMul( &maDec, const_cast<LPDECIMAL>(&r.maDec), &maDec );
bool bRet = ( hResult == S_OK );
return bRet;
}
bool SbxDecimal::neg()
{
HRESULT hResult = VarDecNeg( &maDec, &maDec );
bool bRet = ( hResult == S_OK );
return bRet;
}
bool SbxDecimal::isZero()
{
SbxDecimal aZeroDec;
aZeroDec.setLong( 0 );
bool bZero = CmpResult::EQ == compare( *this, aZeroDec );
return bZero;
}
SbxDecimal::CmpResult compare( const SbxDecimal &rLeft, const SbxDecimal &rRight )
{
HRESULT hResult = VarDecCmp( const_cast<LPDECIMAL>(&rLeft.maDec), const_cast<LPDECIMAL>(&rRight.maDec) );
SbxDecimal::CmpResult eRes = (SbxDecimal::CmpResult)hResult;
return eRes;
}
void SbxDecimal::setChar( sal_Unicode val )
{
VarDecFromUI2( (sal_uInt16)val, &maDec );
}
void SbxDecimal::setByte( sal_uInt8 val )
{
VarDecFromUI1( val, &maDec );
}
void SbxDecimal::setShort( sal_Int16 val )
{
VarDecFromI2( (short)val, &maDec );
}
void SbxDecimal::setLong( sal_Int32 val )
{
VarDecFromI4( (long)val, &maDec );
}
void SbxDecimal::setUShort( sal_uInt16 val )
{
VarDecFromUI2( val, &maDec );
}
void SbxDecimal::setULong( sal_uInt32 val )
{
VarDecFromUI4( static_cast<ULONG>(val), &maDec );
}
bool SbxDecimal::setSingle( float val )
{
bool bRet = ( VarDecFromR4( val, &maDec ) == S_OK );
return bRet;
}
bool SbxDecimal::setDouble( double val )
{
bool bRet = ( VarDecFromR8( val, &maDec ) == S_OK );
return bRet;
}
void SbxDecimal::setInt( int val )
{
setLong( (sal_Int32)val );
}
void SbxDecimal::setUInt( unsigned int val )
{
setULong( (sal_uInt32)val );
}
bool SbxDecimal::setString( OUString* pOUString )
{
assert(pOUString);
static LCID nLANGID = MAKELANGID( LANG_ENGLISH, SUBLANG_ENGLISH_US );
// Convert delimiter
sal_Unicode cDecimalSep;
sal_Unicode cThousandSep;
ImpGetIntntlSep( cDecimalSep, cThousandSep );
bool bRet = false;
HRESULT hResult;
if( cDecimalSep != '.' || cThousandSep != ',' )
{
int nLen = pOUString->getLength();
std::unique_ptr<sal_Unicode[]> pBuffer(new sal_Unicode[nLen + 1]);
pBuffer[nLen] = 0;
const sal_Unicode* pSrc = pOUString->getStr();
int i;
for( i = 0 ; i < nLen ; ++i )
pBuffer[i] = pSrc[i];
sal_Unicode c;
i = 0;
while( (c = pBuffer[i]) != 0 )
{
if( c == cDecimalSep )
pBuffer[i] = '.';
else if( c == cThousandSep )
pBuffer[i] = ',';
i++;
}
hResult = VarDecFromStr(
reinterpret_cast<wchar_t const *>(pBuffer.get()), nLANGID, 0,
&maDec );
}
else
{
hResult = VarDecFromStr(
reinterpret_cast<wchar_t const *>(pOUString->getStr()), nLANGID, 0,
&maDec );
}
bRet = ( hResult == S_OK );
return bRet;
}
bool SbxDecimal::getChar( sal_Unicode& rVal )
{
USHORT n;
bool bRet = ( VarUI2FromDec( &maDec, &n ) == S_OK );
if (bRet) {
rVal = n;
}
return bRet;
}
bool SbxDecimal::getShort( sal_Int16& rVal )
{
bool bRet = ( VarI2FromDec( &maDec, &rVal ) == S_OK );
return bRet;
}
bool SbxDecimal::getLong( sal_Int32& rVal )
{
bool bRet = ( VarI4FromDec( &maDec, &rVal ) == S_OK );
return bRet;
}
bool SbxDecimal::getUShort( sal_uInt16& rVal )
{
bool bRet = ( VarUI2FromDec( &maDec, &rVal ) == S_OK );
return bRet;
}
bool SbxDecimal::getULong( sal_uInt32& rVal )
{
bool bRet = ( VarUI4FromDec( &maDec, &rVal ) == S_OK );
return bRet;
}
bool SbxDecimal::getSingle( float& rVal )
{
bool bRet = ( VarR4FromDec( &maDec, &rVal ) == S_OK );
return bRet;
}
bool SbxDecimal::getDouble( double& rVal )
{
bool bRet = ( VarR8FromDec( &maDec, &rVal ) == S_OK );
return bRet;
}
#else
// !WIN32
bool SbxDecimal::operator -= ( const SbxDecimal & )
{
return false;
}
bool SbxDecimal::operator += ( const SbxDecimal & )
{
return false;
}
bool SbxDecimal::operator /= ( const SbxDecimal & )
{
return false;
}
bool SbxDecimal::operator *= ( const SbxDecimal & )
{
return false;
}
bool SbxDecimal::neg()
{
return false;
}
bool SbxDecimal::isZero()
{
return false;
}
SbxDecimal::CmpResult compare( SAL_UNUSED_PARAMETER const SbxDecimal &, SAL_UNUSED_PARAMETER const SbxDecimal & )
{
return SbxDecimal::CmpResult::LT;
}
void SbxDecimal::setChar( SAL_UNUSED_PARAMETER sal_Unicode ) {}
void SbxDecimal::setByte( SAL_UNUSED_PARAMETER sal_uInt8 ) {}
void SbxDecimal::setShort( SAL_UNUSED_PARAMETER sal_Int16 ) {}
void SbxDecimal::setLong( SAL_UNUSED_PARAMETER sal_Int32 ) {}
void SbxDecimal::setUShort( SAL_UNUSED_PARAMETER sal_uInt16 ) {}
void SbxDecimal::setULong( SAL_UNUSED_PARAMETER sal_uInt32 ) {}
bool SbxDecimal::setSingle( SAL_UNUSED_PARAMETER float ) { return false; }
bool SbxDecimal::setDouble( SAL_UNUSED_PARAMETER double ) { return false; }
void SbxDecimal::setInt( SAL_UNUSED_PARAMETER int ) {}
void SbxDecimal::setUInt( SAL_UNUSED_PARAMETER unsigned int ) {}
bool SbxDecimal::setString( SAL_UNUSED_PARAMETER OUString* ) { return false; }
bool SbxDecimal::getChar( SAL_UNUSED_PARAMETER sal_Unicode& ) { return false; }
bool SbxDecimal::getShort( SAL_UNUSED_PARAMETER sal_Int16& ) { return false; }
bool SbxDecimal::getLong( SAL_UNUSED_PARAMETER sal_Int32& ) { return false; }
bool SbxDecimal::getUShort( SAL_UNUSED_PARAMETER sal_uInt16& ) { return false; }
bool SbxDecimal::getULong( SAL_UNUSED_PARAMETER sal_uInt32& ) { return false; }
bool SbxDecimal::getSingle( SAL_UNUSED_PARAMETER float& ) { return false; }
bool SbxDecimal::getDouble( SAL_UNUSED_PARAMETER double& ) { return false; }
#endif
void SbxDecimal::getString( OUString& rString )
{
#ifdef _WIN32
static LCID nLANGID = MAKELANGID( LANG_ENGLISH, SUBLANG_ENGLISH_US );
OLECHAR sz[100];
BSTR aBStr = SysAllocString( sz );
if( aBStr != nullptr )
{
HRESULT hResult = VarBstrFromDec( &maDec, nLANGID, 0, &aBStr );
if( hResult == S_OK )
{
// Convert delimiter
sal_Unicode cDecimalSep;
sal_Unicode cThousandSep;
ImpGetIntntlSep( cDecimalSep, cThousandSep );
if( cDecimalSep != '.' || cThousandSep != ',' )
{
sal_Unicode c;
int i = 0;
while( (c = aBStr[i]) != 0 )
{
if( c == '.' )
aBStr[i] = cDecimalSep;
else if( c == ',' )
aBStr[i] = cThousandSep;
i++;
}
}
rString = reinterpret_cast<const sal_Unicode*>(aBStr);
}
SysFreeString( aBStr );
}
#else
(void)rString;
#endif
}
SbxDecimal* ImpCreateDecimal( SbxValues* p )
{
if( !p )
return nullptr;
SbxDecimal*& rpDecimal = p->pDecimal;
if( rpDecimal == nullptr )
{
rpDecimal = new SbxDecimal();
rpDecimal->addRef();
}
return rpDecimal;
}
SbxDecimal* ImpGetDecimal( const SbxValues* p )
{
SbxValues aTmp;
SbxDecimal* pnDecRes;
SbxDataType eType = p->eType;
if( eType == SbxDECIMAL && p->pDecimal )
{
pnDecRes = new SbxDecimal( *p->pDecimal );
pnDecRes->addRef();
return pnDecRes;
}
pnDecRes = new SbxDecimal();
pnDecRes->addRef();
start:
switch( +eType )
{
case SbxNULL:
SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
SAL_FALLTHROUGH;
case SbxEMPTY:
pnDecRes->setShort( 0 ); break;
case SbxCHAR:
pnDecRes->setChar( p->nChar ); break;
case SbxBYTE:
pnDecRes->setByte( p->nByte ); break;
case SbxINTEGER:
case SbxBOOL:
pnDecRes->setInt( p->nInteger ); break;
case SbxERROR:
case SbxUSHORT:
pnDecRes->setUShort( p->nUShort ); break;
case SbxLONG:
pnDecRes->setLong( p->nLong ); break;
case SbxULONG:
pnDecRes->setULong( p->nULong ); break;
case SbxSINGLE:
if( !pnDecRes->setSingle( p->nSingle ) )
SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
break;
case SbxCURRENCY:
{
if( !pnDecRes->setDouble( ImpCurrencyToDouble( p->nInt64 ) ) )
SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
break;
}
case SbxSALINT64:
{
if( !pnDecRes->setDouble( (double)p->nInt64 ) )
SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
break;
}
case SbxSALUINT64:
{
if( !pnDecRes->setDouble( (double)p->uInt64 ) )
SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
break;
}
case SbxDATE:
case SbxDOUBLE:
{
double dVal = p->nDouble;
if( !pnDecRes->setDouble( dVal ) )
SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
break;
}
case SbxLPSTR:
case SbxSTRING:
case SbxBYREF | SbxSTRING:
if ( p->pOUString )
pnDecRes->setString( p->pOUString );
break;
case SbxOBJECT:
{
SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
if( pVal )
pnDecRes->setDecimal( pVal->GetDecimal() );
else
{
SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT );
pnDecRes->setShort( 0 );
}
break;
}
case SbxBYREF | SbxCHAR:
pnDecRes->setChar( *p->pChar ); break;
case SbxBYREF | SbxBYTE:
pnDecRes->setByte( *p->pByte ); break;
case SbxBYREF | SbxINTEGER:
case SbxBYREF | SbxBOOL:
pnDecRes->setInt( *p->pInteger ); break;
case SbxBYREF | SbxLONG:
pnDecRes->setLong( *p->pLong ); break;
case SbxBYREF | SbxULONG:
pnDecRes->setULong( *p->pULong ); break;
case SbxBYREF | SbxERROR:
case SbxBYREF | SbxUSHORT:
pnDecRes->setUShort( *p->pUShort ); break;
// from here on had to be tested
case SbxBYREF | SbxSINGLE:
aTmp.nSingle = *p->pSingle; goto ref;
case SbxBYREF | SbxDATE:
case SbxBYREF | SbxDOUBLE:
aTmp.nDouble = *p->pDouble; goto ref;
case SbxBYREF | SbxCURRENCY:
case SbxBYREF | SbxSALINT64:
aTmp.nInt64 = *p->pnInt64; goto ref;
case SbxBYREF | SbxSALUINT64:
aTmp.uInt64 = *p->puInt64; goto ref;
ref:
aTmp.eType = SbxDataType( p->eType & 0x0FFF );
p = &aTmp; goto start;
default:
SbxBase::SetError( ERRCODE_BASIC_CONVERSION ); pnDecRes->setShort( 0 );
}
return pnDecRes;
}
void ImpPutDecimal( SbxValues* p, SbxDecimal* pDec )
{
if( !pDec )
return;
SbxValues aTmp;
start:
switch( +p->eType )
{
// here had to be tested
case SbxCHAR:
aTmp.pChar = &p->nChar; goto direct;
case SbxBYTE:
aTmp.pByte = &p->nByte; goto direct;
case SbxULONG:
aTmp.pULong = &p->nULong; goto direct;
case SbxERROR:
case SbxUSHORT:
aTmp.pUShort = &p->nUShort; goto direct;
case SbxINTEGER:
case SbxBOOL:
aTmp.pInteger = &p->nInteger; goto direct;
case SbxLONG:
aTmp.pLong = &p->nLong; goto direct;
case SbxCURRENCY:
case SbxSALINT64:
aTmp.pnInt64 = &p->nInt64; goto direct;
case SbxSALUINT64:
aTmp.puInt64 = &p->uInt64; goto direct;
direct:
aTmp.eType = SbxDataType( p->eType | SbxBYREF );
p = &aTmp; goto start;
// from here on no longer
case SbxDECIMAL:
case SbxBYREF | SbxDECIMAL:
{
if( pDec != p->pDecimal )
{
releaseDecimalPtr( p->pDecimal );
p->pDecimal = pDec;
if( pDec )
pDec->addRef();
}
break;
}
case SbxSINGLE:
{
float f(0.0);
pDec->getSingle( f );
p->nSingle = f;
break;
}
case SbxDATE:
case SbxDOUBLE:
{
double d(0.0);
pDec->getDouble( d );
p->nDouble = d;
break;
}
case SbxLPSTR:
case SbxSTRING:
case SbxBYREF | SbxSTRING:
if( !p->pOUString )
p->pOUString = new OUString;
pDec->getString( *p->pOUString );
break;
case SbxOBJECT:
{
SbxValue* pVal = dynamic_cast<SbxValue*>( p->pObj );
if( pVal )
pVal->PutDecimal( pDec );
else
SbxBase::SetError( ERRCODE_BASIC_NO_OBJECT );
break;
}
case SbxBYREF | SbxCHAR:
if( !pDec->getChar( *p->pChar ) )
{
SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
*p->pChar = 0;
}
break;
case SbxBYREF | SbxBYTE:
if( !pDec->getChar( *p->pChar ) )
{
SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
*p->pByte = 0;
}
break;
case SbxBYREF | SbxINTEGER:
case SbxBYREF | SbxBOOL:
if( !pDec->getShort( *p->pInteger ) )
{
SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
*p->pInteger = 0;
}
break;
case SbxBYREF | SbxERROR:
case SbxBYREF | SbxUSHORT:
if( !pDec->getUShort( *p->pUShort ) )
{
SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
*p->pUShort = 0;
}
break;
case SbxBYREF | SbxLONG:
if( !pDec->getLong( *p->pLong ) )
{
SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
*p->pLong = 0;
}
break;
case SbxBYREF | SbxULONG:
if( !pDec->getULong( *p->pULong ) )
{
SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
*p->pULong = 0;
}
break;
case SbxBYREF | SbxCURRENCY:
{
double d(0.0);
if( !pDec->getDouble( d ) )
SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
*p->pnInt64 = ImpDoubleToCurrency( d );
}
break;
case SbxBYREF | SbxSALINT64:
{
double d(0.0);
if( !pDec->getDouble( d ) )
SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
else
*p->pnInt64 = ImpDoubleToSalInt64( d );
}
break;
case SbxBYREF | SbxSALUINT64:
{
double d(0.0);
if( !pDec->getDouble( d ) )
SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
else
*p->puInt64 = ImpDoubleToSalUInt64( d );
}
break;
case SbxBYREF | SbxSINGLE:
if( !pDec->getSingle( *p->pSingle ) )
{
SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
*p->pSingle = 0;
}
break;
case SbxBYREF | SbxDATE:
case SbxBYREF | SbxDOUBLE:
if( !pDec->getDouble( *p->pDouble ) )
{
SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW );
*p->pDouble = 0;
}
break;
default:
SbxBase::SetError( ERRCODE_BASIC_CONVERSION );
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|