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