1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_basic.hxx"
24 #include <tools/errcode.hxx>
25 #include <basic/sbx.hxx>
26 #include "sbxconv.hxx"
27
28 #include "unotools/syslocale.hxx"
29
30 #if defined ( UNX )
31 #include <stdlib.h>
32 #endif
33
34 #ifndef _APP_HXX //autogen
35 #include <vcl/svapp.hxx>
36 #endif
37 #include <math.h>
38 #include <string.h>
39 #include <ctype.h>
40
41 #include "sbxres.hxx"
42 #include <basic/sbxbase.hxx>
43 #include <basic/sbxform.hxx>
44 #include <svtools/svtools.hrc>
45
46 #include "basrid.hxx"
47 #include "runtime.hxx"
48
49 #include <svl/zforlist.hxx>
50 #include <comphelper/processfactory.hxx>
51
52
ImpGetIntntlSep(sal_Unicode & rcDecimalSep,sal_Unicode & rcThousandSep)53 void ImpGetIntntlSep( sal_Unicode& rcDecimalSep, sal_Unicode& rcThousandSep )
54 {
55 SvtSysLocale aSysLocale;
56 const LocaleDataWrapper& rData = aSysLocale.GetLocaleData();
57 rcDecimalSep = rData.getNumDecimalSep().GetBuffer()[0];
58 rcThousandSep = rData.getNumThousandSep().GetBuffer()[0];
59 }
60
61 // Scannen eines Strings nach BASIC-Konventionen
62 // Dies entspricht den üblichen Konventionen, nur dass der Exponent
63 // auch ein D sein darf, was den Datentyp auf SbxDOUBLE festlegt.
64 // Die Routine versucht, den Datentyp so klein wie möglich zu gestalten.
65 // Das ganze gibt auch noch einen Konversionsfehler, wenn der Datentyp
66 // Fixed ist und das ganze nicht hineinpasst!
67
ImpScan(const::rtl::OUString & rWSrc,double & nVal,SbxDataType & rType,sal_uInt16 * pLen,sal_Bool bAllowIntntl,sal_Bool bOnlyIntntl)68 SbxError ImpScan( const ::rtl::OUString& rWSrc, double& nVal, SbxDataType& rType,
69 sal_uInt16* pLen, sal_Bool bAllowIntntl, sal_Bool bOnlyIntntl )
70 {
71 // Handle non-breaking space (U+00A0) as thousands separator.
72 // The ASCII US conversion below would replace NBSP with '?',
73 // causing locale-aware number parsing to fail (e.g. "1 000,5" in fr_FR).
74 sal_Unicode cDecimalSep, cThousandSep = 0;
75 if( bAllowIntntl || bOnlyIntntl )
76 {
77 ImpGetIntntlSep( cDecimalSep, cThousandSep );
78 if( cThousandSep == 0xA0 ) // NBSP
79 cThousandSep = (sal_Unicode)' ';
80 }
81
82 // Replace NBSP in source so the ASCII US conversion preserves it as ' '
83 rtl::OUString aUSrc = rWSrc;
84 if( cThousandSep == ' ' )
85 aUSrc = aUSrc.replace( sal_Unicode(0xA0), ' ' );
86 ::rtl::OString aBStr( ::rtl::OUStringToOString( aUSrc, RTL_TEXTENCODING_ASCII_US ) );
87
88 // Bei International Komma besorgen
89 char cIntntlComma, cIntntl1000;
90 char cNonIntntlComma = '.';
91
92 if( bAllowIntntl || bOnlyIntntl )
93 {
94 cIntntlComma = (char)cDecimalSep;
95 cIntntl1000 = (char)cThousandSep;
96 }
97 // Sonst einfach auch auf . setzen
98 else
99 {
100 cIntntlComma = cNonIntntlComma;
101 cIntntl1000 = cNonIntntlComma; // Unschädlich machen
102 }
103 // Nur International -> IntnlComma übernehmen
104 if( bOnlyIntntl )
105 {
106 cNonIntntlComma = cIntntlComma;
107 cIntntl1000 = (char)cThousandSep;
108 }
109
110 const char* pStart = aBStr.getStr();
111 const char* p = pStart;
112 char buf[ 80 ], *q = buf;
113 sal_Bool bRes = sal_True;
114 sal_Bool bMinus = sal_False;
115 nVal = 0;
116 SbxDataType eScanType = SbxSINGLE;
117 // Whitespace wech
118 while( *p &&( *p == ' ' || *p == '\t' ) ) p++;
119 // Zahl? Dann einlesen und konvertieren.
120 if( *p == '-' )
121 p++, bMinus = sal_True;
122 if( isdigit( *p ) ||( (*p == cNonIntntlComma || *p == cIntntlComma ||
123 *p == cIntntl1000) && isdigit( *(p+1 ) ) ) )
124 {
125 short exp = 0; // >0: Exponentteil
126 short comma = 0; // >0: Nachkomma
127 short ndig = 0; // Anzahl Ziffern
128 short ncdig = 0; // Anzahl Ziffern nach Komma
129 ByteString aSearchStr( "0123456789DEde" );
130 // Kommas ergaenzen
131 aSearchStr += cNonIntntlComma;
132 if( cIntntlComma != cNonIntntlComma )
133 aSearchStr += cIntntlComma;
134 if( bOnlyIntntl )
135 aSearchStr += cIntntl1000;
136 const char* pSearchStr = aSearchStr.GetBuffer();
137 while( strchr( pSearchStr, *p ) && *p )
138 {
139 // 1000er-Trenner überlesen
140 if( bOnlyIntntl && *p == cIntntl1000 )
141 {
142 p++;
143 continue;
144 }
145
146 // Komma oder Exponent?
147 if( *p == cNonIntntlComma || *p == cIntntlComma )
148 {
149 // Immer '.' einfügen, damit atof funktioniert
150 p++;
151 if( ++comma > 1 )
152 continue;
153 else
154 *q++ = '.';
155 }
156 else if( strchr( "DdEe", *p ) )
157 {
158 if( ++exp > 1 )
159 {
160 p++; continue;
161 }
162 if( toupper( *p ) == 'D' )
163 eScanType = SbxDOUBLE;
164 *q++ = 'E'; p++;
165 // Vorzeichen hinter Exponent?
166 if( *p == '+' )
167 p++;
168 else
169 if( *p == '-' )
170 *q++ = *p++;
171 }
172 else
173 {
174 *q++ = *p++;
175 if( comma && !exp ) ncdig++;
176 }
177 if( !exp ) ndig++;
178 }
179 *q = 0;
180 // Komma, Exponent mehrfach vorhanden?
181 if( comma > 1 || exp > 1 )
182 bRes = sal_False;
183 // Kann auf Integer gefaltet werden?
184 if( !comma && !exp )
185 {
186 if( nVal >= SbxMININT && nVal <= SbxMAXINT )
187 eScanType = SbxINTEGER;
188 else if( nVal >= SbxMINLNG && nVal <= SbxMAXLNG )
189 eScanType = SbxLONG;
190 }
191
192 nVal = atof( buf );
193 ndig = ndig - comma;
194 // zu viele Zahlen für SINGLE?
195 if( ndig > 15 || ncdig > 6 )
196 eScanType = SbxDOUBLE;
197
198 // Typkennung?
199 if( strchr( "%!&#", *p ) && *p ) p++;
200 }
201 // Hex/Oktalzahl? Einlesen und konvertieren:
202 else if( *p == '&' )
203 {
204 p++;
205 eScanType = SbxLONG;
206 const char *cmp = "0123456789ABCDEF";
207 char base = 16;
208 char ndig = 8;
209 char xch = *p++;
210 switch( toupper( xch ) )
211 {
212 case 'O': cmp = "01234567"; base = 8; ndig = 11; break;
213 case 'H': break;
214 default : bRes = sal_False;
215 }
216 sal_Int32 l = 0;
217 int i;
218 while( isalnum( *p ) )
219 {
220 char ch = sal::static_int_cast< char >( toupper( *p ) );
221 p++;
222 if( strchr( cmp, ch ) ) *q++ = ch;
223 else bRes = sal_False;
224 }
225 *q = 0;
226 for( q = buf; *q; q++ )
227 {
228 i =( *q & 0xFF ) - '0';
229 if( i > 9 ) i -= 7;
230 l =( l * base ) + i;
231 if( !ndig-- )
232 bRes = sal_False;
233 }
234 if( *p == '&' ) p++;
235 nVal = (double) l;
236 if( l >= SbxMININT && l <= SbxMAXINT )
237 eScanType = SbxINTEGER;
238 }
239 else if ( SbiRuntime::isVBAEnabled() )
240 {
241 OSL_TRACE("Reporting error converting");
242 return SbxERR_CONVERSION;
243 }
244 if( pLen )
245 *pLen = (sal_uInt16) ( p - pStart );
246 if( !bRes )
247 return SbxERR_CONVERSION;
248 if( bMinus )
249 nVal = -nVal;
250 rType = eScanType;
251 return SbxERR_OK;
252 }
253
254 // Schnittstelle für CDbl im Basic
ScanNumIntnl(const String & rSrc,double & nVal,sal_Bool bSingle)255 SbxError SbxValue::ScanNumIntnl( const String& rSrc, double& nVal, sal_Bool bSingle )
256 {
257 SbxDataType t;
258 sal_uInt16 nLen = 0;
259 SbxError nRetError = ImpScan( rSrc, nVal, t, &nLen,
260 /*bAllowIntntl*/sal_False, /*bOnlyIntntl*/sal_True );
261 // Komplett gelesen?
262 if( nRetError == SbxERR_OK && nLen != rSrc.Len() )
263 nRetError = SbxERR_CONVERSION;
264
265 if( bSingle )
266 {
267 SbxValues aValues( nVal );
268 nVal = (double)ImpGetSingle( &aValues ); // Hier Error bei Overflow
269 }
270 return nRetError;
271 }
272
273 ////////////////////////////////////////////////////////////////////////////
274
275 static double roundArray[] = {
276 5.0e+0, 0.5e+0, 0.5e-1, 0.5e-2, 0.5e-3, 0.5e-4, 0.5e-5, 0.5e-6, 0.5e-7,
277 0.5e-8, 0.5e-9, 0.5e-10,0.5e-11,0.5e-12,0.5e-13,0.5e-14,0.5e-15 };
278
279 /***************************************************************************
280 |*
281 |* void myftoa( double, char *, short, short, sal_Bool, sal_Bool )
282 |*
283 |* Beschreibung: Konversion double --> ASCII
284 |* Parameter: double die Zahl.
285 |* char * der Zielpuffer
286 |* short Anzahl Nachkommastellen
287 |* short Weite des Exponenten( 0=kein E )
288 |* sal_Bool sal_True: mit 1000er Punkten
289 |* sal_Bool sal_True: formatfreie Ausgabe
290 |*
291 ***************************************************************************/
292
myftoa(double nNum,char * pBuf,short nPrec,short nExpWidth,sal_Bool bPt,sal_Bool bFix,sal_Unicode cForceThousandSep=0)293 static void myftoa( double nNum, char * pBuf, short nPrec, short nExpWidth,
294 sal_Bool bPt, sal_Bool bFix, sal_Unicode cForceThousandSep = 0 )
295 {
296
297 short nExp = 0; // Exponent
298 short nDig = nPrec + 1; // Anzahl Digits in Zahl
299 short nDec; // Anzahl Vorkommastellen
300 int i, digit;
301
302 // Komma besorgen
303 sal_Unicode cDecimalSep, cThousandSep;
304 ImpGetIntntlSep( cDecimalSep, cThousandSep );
305 if( cForceThousandSep )
306 cThousandSep = cForceThousandSep;
307
308 // Exponentberechnung:
309 nExp = 0;
310 if( nNum > 0.0 )
311 {
312 while( nNum < 1.0 ) nNum *= 10.0, nExp--;
313 while( nNum >= 10.0 ) nNum /= 10.0, nExp++;
314 }
315 if( !bFix && !nExpWidth )
316 nDig = nDig + nExp;
317 else if( bFix && !nPrec )
318 nDig = nExp + 1;
319
320 // Zahl runden:
321 if( (nNum += roundArray [( nDig > 16 ) ? 16 : nDig] ) >= 10.0 )
322 {
323 nNum = 1.0;
324 ++nExp;
325 if( !nExpWidth ) ++nDig;
326 }
327
328 // Bestimmung der Vorkommastellen:
329 if( !nExpWidth )
330 {
331 if( nExp < 0 )
332 {
333 // #41691: Auch bei bFix eine 0 spendieren
334 *pBuf++ = '0';
335 if( nPrec ) *pBuf++ = (char)cDecimalSep;
336 i = -nExp - 1;
337 if( nDig <= 0 ) i = nPrec;
338 while( i-- ) *pBuf++ = '0';
339 nDec = 0;
340 }
341 else
342 nDec = nExp+1;
343 }
344 else
345 nDec = 1;
346
347 // Zahl ausgeben:
348 if( nDig > 0 )
349 {
350 for( i = 0 ; ; ++i )
351 {
352 if( i < 16 )
353 {
354 digit = (int) nNum;
355 *pBuf++ = sal::static_int_cast< char >(digit + '0');
356 nNum =( nNum - digit ) * 10.0;
357 } else
358 *pBuf++ = '0';
359 if( --nDig == 0 ) break;
360 if( nDec )
361 {
362 nDec--;
363 if( !nDec )
364 *pBuf++ = (char)cDecimalSep;
365 else if( !(nDec % 3 ) && bPt )
366 *pBuf++ = (char)cThousandSep;
367 }
368 }
369 }
370
371 // Exponent ausgeben:
372 if( nExpWidth )
373 {
374 if( nExpWidth < 3 ) nExpWidth = 3;
375 nExpWidth -= 2;
376 *pBuf++ = 'E';
377 *pBuf++ =( nExp < 0 ) ?( (nExp = -nExp ), '-' ) : '+';
378 while( nExpWidth > 3 ) *pBuf++ = '0', nExpWidth--;
379 if( nExp >= 100 || nExpWidth == 3 )
380 {
381 *pBuf++ = sal::static_int_cast< char >(nExp/100 + '0');
382 nExp %= 100;
383 }
384 if( nExp/10 || nExpWidth >= 2 )
385 *pBuf++ = sal::static_int_cast< char >(nExp/10 + '0');
386 *pBuf++ = sal::static_int_cast< char >(nExp%10 + '0');
387 }
388 *pBuf = 0;
389 }
390
391 // Die Zahl wird unformatiert mit der angegebenen Anzahl NK-Stellen
392 // aufbereitet. Evtl. wird ein Minus vorangestellt.
393 // Diese Routine ist public, weil sie auch von den Put-Funktionen
394 // der Klasse SbxImpSTRING verwendet wird.
395
396 #ifdef _MSC_VER
397 #pragma optimize( "", off )
398 #pragma warning(disable: 4748) // "... because optimizations are disabled ..."
399 #endif
400
ImpCvtNum(double nNum,short nPrec,::rtl::OUString & rRes,sal_Bool bCoreString)401 void ImpCvtNum( double nNum, short nPrec, ::rtl::OUString& rRes, sal_Bool bCoreString )
402 {
403 char *q;
404 char cBuf[ 40 ], *p = cBuf;
405
406 sal_Unicode cDecimalSep, cThousandSep;
407 ImpGetIntntlSep( cDecimalSep, cThousandSep );
408 if( bCoreString )
409 cDecimalSep = '.';
410
411 if( nNum < 0.0 ) {
412 nNum = -nNum;
413 *p++ = '-';
414 }
415 double dMaxNumWithoutExp = (nPrec == 6) ? 1E6 : 1E14;
416 myftoa( nNum, p, nPrec,( nNum &&( nNum < 1E-1 || nNum >= dMaxNumWithoutExp ) ) ? 4:0,
417 sal_False, sal_True, cDecimalSep );
418 // Trailing Zeros weg:
419 for( p = cBuf; *p &&( *p != 'E' ); p++ ) {}
420 q = p; p--;
421 while( nPrec && *p == '0' ) nPrec--, p--;
422 if( *p == cDecimalSep ) p--;
423 while( *q ) *++p = *q++;
424 *++p = 0;
425 rRes = ::rtl::OUString::createFromAscii( cBuf );
426 }
427
428 #ifdef _MSC_VER
429 #pragma optimize( "", on )
430 #endif
431
ImpConvStringExt(::rtl::OUString & rSrc,SbxDataType eTargetType)432 sal_Bool ImpConvStringExt( ::rtl::OUString& rSrc, SbxDataType eTargetType )
433 {
434 // Merken, ob überhaupt was geändert wurde
435 sal_Bool bChanged = sal_False;
436 ::rtl::OUString aNewString;
437
438 // Nur Spezial-Fälle behandeln, als Default tun wir nichts
439 switch( eTargetType )
440 {
441 // Bei Fliesskomma International berücksichtigen
442 case SbxSINGLE:
443 case SbxDOUBLE:
444 case SbxCURRENCY:
445 {
446 ::rtl::OString aBStr( ::rtl::OUStringToOString( rSrc, RTL_TEXTENCODING_ASCII_US ) );
447
448 // Komma besorgen
449 sal_Unicode cDecimalSep, cThousandSep;
450 ImpGetIntntlSep( cDecimalSep, cThousandSep );
451 aNewString = rSrc;
452
453 // Ersetzen, wenn DecimalSep kein '.' (nur den ersten)
454 if( cDecimalSep != (sal_Unicode)'.' )
455 {
456 sal_Int32 nPos = aNewString.indexOf( cDecimalSep );
457 if( nPos != -1 )
458 {
459 sal_Unicode* pStr = (sal_Unicode*)aNewString.getStr();
460 pStr[nPos] = (sal_Unicode)'.';
461 bChanged = sal_True;
462 }
463 }
464 break;
465 }
466
467 // Bei sal_Bool sal_True und sal_False als String prüfen
468 case SbxBOOL:
469 {
470 if( rSrc.equalsIgnoreAsciiCaseAscii( "true" ) )
471 {
472 aNewString = ::rtl::OUString::valueOf( (sal_Int32)SbxTRUE );
473 bChanged = sal_True;
474 }
475 else
476 if( rSrc.equalsIgnoreAsciiCaseAscii( "false" ) )
477 {
478 aNewString = ::rtl::OUString::valueOf( (sal_Int32)SbxFALSE );
479 bChanged = sal_True;
480 }
481 break;
482 }
483 default: break;
484 }
485 // String bei Änderung übernehmen
486 if( bChanged )
487 rSrc = aNewString;
488 return bChanged;
489 }
490
491
492 // Formatierte Zahlenausgabe
493 // Der Returnwert ist die Anzahl Zeichen, die aus dem
494 // Format verwendet wurden.
495
496 #ifdef _old_format_code_
497 // lasse diesen Code vorl"aufig drin, zum 'abgucken'
498 // der bisherigen Implementation
499
printfmtnum(double nNum,XubString & rRes,const XubString & rWFmt)500 static sal_uInt16 printfmtnum( double nNum, XubString& rRes, const XubString& rWFmt )
501 {
502 const String& rFmt = rWFmt;
503 char cFill = ' '; // Fuellzeichen
504 char cPre = 0; // Startzeichen( evtl. "$" )
505 short nExpDig= 0; // Anzahl Exponentstellen
506 short nPrec = 0; // Anzahl Nachkommastellen
507 short nWidth = 0; // Zahlenweite gesamnt
508 short nLen; // Laenge konvertierte Zahl
509 sal_Bool bPoint = sal_False; // sal_True: mit 1000er Kommas
510 sal_Bool bTrail = sal_False; // sal_True, wenn folgendes Minus
511 sal_Bool bSign = sal_False; // sal_True: immer mit Vorzeichen
512 sal_Bool bNeg = sal_False; // sal_True: Zahl ist negativ
513 char cBuf [1024]; // Zahlenpuffer
514 char * p;
515 const char* pFmt = rFmt;
516 rRes.Erase();
517 // $$ und ** abfangen. Einfach wird als Zeichen ausgegeben.
518 if( *pFmt == '$' )
519 if( *++pFmt != '$' ) rRes += '$';
520 if( *pFmt == '*' )
521 if( *++pFmt != '*' ) rRes += '*';
522
523 switch( *pFmt++ )
524 {
525 case 0:
526 break;
527 case '+':
528 bSign = sal_True; nWidth++; break;
529 case '*':
530 nWidth++; cFill = '*';
531 if( *pFmt == '$' ) nWidth++, pFmt++, cPre = '$';
532 break;
533 case '$':
534 nWidth++; cPre = '$'; break;
535 case '#':
536 case '.':
537 case ',':
538 pFmt--; break;
539 }
540 // Vorkomma:
541 for( ;; )
542 {
543 while( *pFmt == '#' ) pFmt++, nWidth++;
544 // 1000er Kommas?
545 if( *pFmt == ',' )
546 {
547 nWidth++; pFmt++; bPoint = sal_True;
548 } else break;
549 }
550 // Nachkomma:
551 if( *pFmt == '.' )
552 {
553 while( *++pFmt == '#' ) nPrec++;
554 nWidth += nPrec + 1;
555 }
556 // Exponent:
557 while( *pFmt == '^' )
558 pFmt++, nExpDig++, nWidth++;
559 // Folgendes Minus:
560 if( !bSign && *pFmt == '-' )
561 pFmt++, bTrail = sal_True;
562
563 // Zahl konvertieren:
564 if( nPrec > 15 ) nPrec = 15;
565 if( nNum < 0.0 ) nNum = -nNum, bNeg = sal_True;
566 p = cBuf;
567 if( bSign ) *p++ = bNeg ? '-' : '+';
568 myftoa( nNum, p, nPrec, nExpDig, bPoint, sal_False );
569 nLen = strlen( cBuf );
570
571 // Überlauf?
572 if( cPre ) nLen++;
573 if( nLen > nWidth ) rRes += '%';
574 else {
575 nWidth -= nLen;
576 while( nWidth-- ) rRes += (xub_Unicode)cFill;
577 if( cPre ) rRes += (xub_Unicode)cPre;
578 }
579 rRes += (xub_Unicode*)&(cBuf[0]);
580 if( bTrail )
581 rRes += bNeg ? '-' : ' ';
582
583 return (sal_uInt16) ( pFmt - (const char*) rFmt );
584 }
585
586 #endif //_old_format_code_
587
printfmtstr(const XubString & rStr,XubString & rRes,const XubString & rFmt)588 static sal_uInt16 printfmtstr( const XubString& rStr, XubString& rRes, const XubString& rFmt )
589 {
590 const xub_Unicode* pStr = rStr.GetBuffer();
591 const xub_Unicode* pFmtStart = rFmt.GetBuffer();
592 const xub_Unicode* pFmt = pFmtStart;
593 rRes.Erase();
594 switch( *pFmt )
595 {
596 case '!':
597 rRes += *pStr++; pFmt++; break;
598 case '\\':
599 do
600 {
601 rRes += *pStr ? *pStr++ : static_cast< xub_Unicode >(' ');
602 pFmt++;
603 } while( *pFmt != '\\' );
604 rRes += *pStr ? *pStr++ : static_cast< xub_Unicode >(' ');
605 pFmt++; break;
606 case '&':
607 rRes = rStr;
608 pFmt++; break;
609 default:
610 rRes = rStr;
611 break;
612 }
613 return (sal_uInt16) ( pFmt - pFmtStart );
614 }
615
616 /////////////////////////////////////////////////////////////////////////
617
Scan(const XubString & rSrc,sal_uInt16 * pLen)618 sal_Bool SbxValue::Scan( const XubString& rSrc, sal_uInt16* pLen )
619 {
620 SbxError eRes = SbxERR_OK;
621 if( !CanWrite() )
622 eRes = SbxERR_PROP_READONLY;
623 else
624 {
625 double n;
626 SbxDataType t;
627 eRes = ImpScan( rSrc, n, t, pLen );
628 if( eRes == SbxERR_OK )
629 {
630 if( !IsFixed() )
631 SetType( t );
632 PutDouble( n );
633 }
634 }
635 if( eRes )
636 {
637 SetError( eRes ); return sal_False;
638 }
639 else
640 return sal_True;
641 }
642
643
implGetResMgr(void)644 ResMgr* implGetResMgr( void )
645 {
646 static ResMgr* pResMgr = NULL;
647 if( !pResMgr )
648 {
649 ::com::sun::star::lang::Locale aLocale = Application::GetSettings().GetUILocale();
650 pResMgr = ResMgr::CreateResMgr(CREATEVERSIONRESMGR_NAME(sb), aLocale );
651 }
652 return pResMgr;
653 }
654
655 class SbxValueFormatResId : public ResId
656 {
657 public:
SbxValueFormatResId(sal_uInt16 nId)658 SbxValueFormatResId( sal_uInt16 nId )
659 : ResId( nId, *implGetResMgr() )
660 {}
661 };
662
663
664 enum VbaFormatType
665 {
666 VBA_FORMAT_TYPE_OFFSET, // standard number format
667 VBA_FORMAT_TYPE_USERDEFINED, // user defined number format
668 VBA_FORMAT_TYPE_NULL
669 };
670
671 struct VbaFormatInfo
672 {
673 VbaFormatType meType;
674 const char* mpVbaFormat; // Format string in vba
675 NfIndexTableOffset meOffset; // SvNumberFormatter format index, if meType = VBA_FORMAT_TYPE_OFFSET
676 const char* mpOOoFormat; // if meType = VBA_FORMAT_TYPE_USERDEFINED
677 };
678
679 #define VBA_FORMAT_OFFSET( pcUtf8, eOffset ) \
680 { VBA_FORMAT_TYPE_OFFSET, pcUtf8, eOffset, 0 }
681
682 #define VBA_FORMAT_USERDEFINED( pcUtf8, pcDefinedUtf8 ) \
683 { VBA_FORMAT_TYPE_USERDEFINED, pcUtf8, NF_NUMBER_STANDARD, pcDefinedUtf8 }
684
685 static VbaFormatInfo pFormatInfoTable[] =
686 {
687 VBA_FORMAT_OFFSET( "Long Date", NF_DATE_SYSTEM_LONG ),
688 VBA_FORMAT_USERDEFINED( "Medium Date", "DD-MMM-YY" ),
689 VBA_FORMAT_OFFSET( "Short Date", NF_DATE_SYSTEM_SHORT ),
690 VBA_FORMAT_USERDEFINED( "Long Time", "H:MM:SS AM/PM" ),
691 VBA_FORMAT_OFFSET( "Medium Time", NF_TIME_HHMMAMPM ),
692 VBA_FORMAT_OFFSET( "Short Time", NF_TIME_HHMM ),
693 VBA_FORMAT_OFFSET( "ddddd", NF_DATE_SYSTEM_SHORT ),
694 VBA_FORMAT_OFFSET( "dddddd", NF_DATE_SYSTEM_LONG ),
695 VBA_FORMAT_USERDEFINED( "ttttt", "H:MM:SS AM/PM" ),
696 VBA_FORMAT_OFFSET( "ww", NF_DATE_WW ),
697 { VBA_FORMAT_TYPE_NULL, 0, NF_INDEX_TABLE_ENTRIES, 0 }
698 };
699
getFormatInfo(const String & rFmt)700 VbaFormatInfo* getFormatInfo( const String& rFmt )
701 {
702 VbaFormatInfo* pInfo = NULL;
703 sal_Int16 i = 0;
704 while( (pInfo = pFormatInfoTable + i )->mpVbaFormat != NULL )
705 {
706 if( rFmt.EqualsIgnoreCaseAscii( pInfo->mpVbaFormat ) )
707 break;
708 i++;
709 }
710 return pInfo;
711 }
712
713 #define VBAFORMAT_GENERALDATE "General Date"
714 #define VBAFORMAT_C "c"
715 #define VBAFORMAT_N "n"
716 #define VBAFORMAT_NN "nn"
717 #define VBAFORMAT_W "w"
718 #define VBAFORMAT_Y "y"
719 #define VBAFORMAT_LOWERCASE "<"
720 #define VBAFORMAT_UPPERCASE ">"
721
722 // From methods1.cxx
723 sal_Int16 implGetWeekDay( double aDate, bool bFirstDayParam = false, sal_Int16 nFirstDay = 0 );
724 // from methods.cxx
725 sal_Int16 implGetMinute( double dDate );
726 sal_Int16 implGetDateYear( double aDate );
727 sal_Bool implDateSerial( sal_Int16 nYear, sal_Int16 nMonth, sal_Int16 nDay, double& rdRet );
728
Format(XubString & rRes,const XubString * pFmt) const729 void SbxValue::Format( XubString& rRes, const XubString* pFmt ) const
730 {
731 short nComma = 0;
732 double d = 0;
733
734 // pflin, It is better to use SvNumberFormatter to handle the date/time/number format.
735 // the SvNumberFormatter output is mostly compatible with
736 // VBA output besides the AOO-BASIC output
737 if( pFmt && !SbxBasicFormater::isBasicFormat( *pFmt ) )
738 {
739 String aStr = GetString();
740
741 SvtSysLocale aSysLocale;
742 const CharClass& rCharClass = aSysLocale.GetCharClass();
743
744 if( pFmt->EqualsIgnoreCaseAscii( VBAFORMAT_LOWERCASE ) )
745 {
746 rCharClass.toLower( aStr );
747 rRes = aStr;
748 return;
749 }
750 if( pFmt->EqualsIgnoreCaseAscii( VBAFORMAT_UPPERCASE ) )
751 {
752 rCharClass.toUpper( aStr );
753 rRes = aStr;
754 return;
755 }
756
757 LanguageType eLangType = GetpApp()->GetSettings().GetLanguage();
758 com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory >
759 xFactory = comphelper::getProcessServiceFactory();
760 SvNumberFormatter aFormatter( xFactory, eLangType );
761
762 sal_uInt32 nIndex;
763 xub_StrLen nCheckPos = 0;
764 short nType;
765 double nNumber;
766 Color* pCol;
767
768 sal_Bool bSuccess = aFormatter.IsNumberFormat( aStr, nIndex, nNumber );
769
770 // number format, use SvNumberFormatter to handle it.
771 if( bSuccess )
772 {
773 String aFmtStr = *pFmt;
774 VbaFormatInfo* pInfo = getFormatInfo( aFmtStr );
775 if( pInfo && pInfo->meType != VBA_FORMAT_TYPE_NULL )
776 {
777 if( pInfo->meType == VBA_FORMAT_TYPE_OFFSET )
778 {
779 nIndex = aFormatter.GetFormatIndex( pInfo->meOffset, eLangType );
780 }
781 else
782 {
783 aFmtStr.AssignAscii( pInfo->mpOOoFormat );
784 aFormatter.PutandConvertEntry( aFmtStr, nCheckPos, nType, nIndex, LANGUAGE_ENGLISH, eLangType );
785 }
786 aFormatter.GetOutputString( nNumber, nIndex, rRes, &pCol );
787 }
788 else if( aFmtStr.EqualsIgnoreCaseAscii( VBAFORMAT_GENERALDATE )
789 || aFmtStr.EqualsIgnoreCaseAscii( VBAFORMAT_C ))
790 {
791 if( nNumber <=-1.0 || nNumber >= 1.0 )
792 {
793 // short date
794 nIndex = aFormatter.GetFormatIndex( NF_DATE_SYSTEM_SHORT, eLangType );
795 aFormatter.GetOutputString( nNumber, nIndex, rRes, &pCol );
796
797 // long time
798 if( floor( nNumber ) != nNumber )
799 {
800 aFmtStr.AssignAscii( "H:MM:SS AM/PM" );
801 aFormatter.PutandConvertEntry( aFmtStr, nCheckPos, nType, nIndex, LANGUAGE_ENGLISH, eLangType );
802 String aTime;
803 aFormatter.GetOutputString( nNumber, nIndex, aTime, &pCol );
804 rRes.AppendAscii(" ");
805 rRes += aTime;
806 }
807 }
808 else
809 {
810 // long time only
811 aFmtStr.AssignAscii( "H:MM:SS AM/PM" );
812 aFormatter.PutandConvertEntry( aFmtStr, nCheckPos, nType, nIndex, LANGUAGE_ENGLISH, eLangType );
813 aFormatter.GetOutputString( nNumber, nIndex, rRes, &pCol );
814 }
815 }
816 else if( aFmtStr.EqualsIgnoreCaseAscii( VBAFORMAT_N )
817 || aFmtStr.EqualsIgnoreCaseAscii( VBAFORMAT_NN ))
818 {
819 sal_Int32 nMin = implGetMinute( nNumber );
820 if( nMin < 10 && aFmtStr.EqualsIgnoreCaseAscii( VBAFORMAT_NN ) )
821 {
822 // Minute in two digits
823 sal_Unicode* p = rRes.AllocBuffer( 2 );
824 *p++ = '0';
825 *p = sal_Unicode( '0' + nMin );
826 }
827 else
828 {
829 rRes = String::CreateFromInt32( nMin );
830 }
831 }
832 else if( aFmtStr.EqualsIgnoreCaseAscii( VBAFORMAT_W ))
833 {
834 sal_Int32 nWeekDay = implGetWeekDay( nNumber );
835 rRes = String::CreateFromInt32( nWeekDay );
836 }
837 else if( aFmtStr.EqualsIgnoreCaseAscii( VBAFORMAT_Y ))
838 {
839 sal_Int16 nYear = implGetDateYear( nNumber );
840 double dBaseDate;
841 implDateSerial( nYear, 1, 1, dBaseDate );
842 sal_Int32 nYear32 = 1 + sal_Int32( nNumber - dBaseDate );
843 rRes = String::CreateFromInt32( nYear32 );
844 }
845 else
846 {
847 aFormatter.PutandConvertEntry( aFmtStr, nCheckPos, nType, nIndex, LANGUAGE_ENGLISH, eLangType );
848 aFormatter.GetOutputString( nNumber, nIndex, rRes, &pCol );
849 }
850
851 return;
852 }
853 }
854
855 SbxDataType eType = GetType();
856 switch( eType )
857 {
858 case SbxCHAR:
859 case SbxBYTE:
860 case SbxINTEGER:
861 case SbxUSHORT:
862 case SbxLONG:
863 case SbxULONG:
864 case SbxINT:
865 case SbxUINT:
866 case SbxNULL: // #45929 NULL mit durchschummeln
867 nComma = 0; goto cvt;
868 case SbxSINGLE:
869 nComma = 6; goto cvt;
870 case SbxDOUBLE:
871 nComma = 14;
872
873 cvt:
874 if( eType != SbxNULL )
875 d = GetDouble();
876
877 // #45355 weiterer Einsprungpunkt für isnumeric-String
878 cvt2:
879 if( pFmt )
880 {
881 // hole die 'statischen' Daten f"ur Sbx
882 SbxAppData* pData = GetSbxData_Impl();
883
884 LanguageType eLangType = GetpApp()->GetSettings().GetLanguage();
885 if( pData->pBasicFormater )
886 {
887 if( pData->eBasicFormaterLangType != eLangType )
888 {
889 delete pData->pBasicFormater;
890 pData->pBasicFormater = NULL;
891 }
892 }
893 pData->eBasicFormaterLangType = eLangType;
894
895 // falls bisher noch kein BasicFormater-Objekt
896 // existiert, so erzeuge dieses
897 if( !pData->pBasicFormater )
898 {
899 SvtSysLocale aSysLocale;
900 const LocaleDataWrapper& rData = aSysLocale.GetLocaleData();
901 sal_Unicode cComma = rData.getNumDecimalSep().GetBuffer()[0];
902 sal_Unicode c1000 = rData.getNumThousandSep().GetBuffer()[0];
903 String aCurrencyStrg = rData.getCurrSymbol();
904
905 // Initialisierung des Basic-Formater-Hilfsobjekts:
906 // hole die Ressourcen für die vordefinierten Ausgaben
907 // des Format()-Befehls, z.B. für "On/Off".
908 String aOnStrg = String( SbxValueFormatResId(
909 STR_BASICKEY_FORMAT_ON ) );
910 String aOffStrg = String( SbxValueFormatResId(
911 STR_BASICKEY_FORMAT_OFF) );
912 String aYesStrg = String( SbxValueFormatResId(
913 STR_BASICKEY_FORMAT_YES) );
914 String aNoStrg = String( SbxValueFormatResId(
915 STR_BASICKEY_FORMAT_NO) );
916 String aTrueStrg = String( SbxValueFormatResId(
917 STR_BASICKEY_FORMAT_TRUE) );
918 String aFalseStrg = String( SbxValueFormatResId(
919 STR_BASICKEY_FORMAT_FALSE) );
920 String aCurrencyFormatStrg = String( SbxValueFormatResId(
921 STR_BASICKEY_FORMAT_CURRENCY) );
922 // erzeuge das Basic-Formater-Objekt
923 pData->pBasicFormater
924 = new SbxBasicFormater( cComma,c1000,aOnStrg,aOffStrg,
925 aYesStrg,aNoStrg,aTrueStrg,aFalseStrg,
926 aCurrencyStrg,aCurrencyFormatStrg );
927 }
928 // Bem.: Aus Performance-Gründen wird nur EIN BasicFormater-
929 // Objekt erzeugt und 'gespeichert', dadurch erspart man
930 // sich das teure Ressourcen-Laden (für landesspezifische
931 // vordefinierte Ausgaben, z.B. "On/Off") und die ständige
932 // String-Erzeugungs Operationen.
933 // ABER: dadurch ist dieser Code NICHT multithreading f"ahig !
934
935 // hier gibt es Probleme mit ;;;Null, da diese Methode nur aufgerufen
936 // wird, wenn der SbxValue eine Zahl ist !!!
937 // dazu koennte: pData->pBasicFormater->BasicFormatNull( *pFmt ); aufgerufen werden !
938 if( eType != SbxNULL )
939 {
940 rRes = pData->pBasicFormater->BasicFormat( d ,*pFmt );
941 }
942 else
943 {
944 rRes = pData->pBasicFormater->BasicFormatNull( *pFmt );
945 }
946
947 // Die alte Implementierung:
948 //old: printfmtnum( GetDouble(), rRes, *pFmt );
949 }
950 else
951 {
952 ::rtl::OUString aTmpString( rRes );
953 ImpCvtNum( GetDouble(), nComma, aTmpString );
954 rRes = aTmpString;
955 }
956 break;
957 case SbxSTRING:
958 if( pFmt )
959 {
960 // #45355 wenn es numerisch ist, muss gewandelt werden
961 if( IsNumericRTL() )
962 {
963 ScanNumIntnl( GetString(), d, /*bSingle*/sal_False );
964 goto cvt2;
965 }
966 else
967 {
968 // Sonst String-Formatierung
969 printfmtstr( GetString(), rRes, *pFmt );
970 }
971 }
972 else
973 rRes = GetString();
974 break;
975 default:
976 rRes = GetString();
977 }
978 }
979
980 /* vim: set noet sw=4 ts=4: */
981