Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <stdlib.h>
22 :
23 : #include <basic/sbxform.hxx>
24 : #include <rtl/ustrbuf.hxx>
25 :
26 : /*
27 : TODO: are there any Star-Basic characteristics unconsidered?
28 :
29 : what means: * as placeholder
30 :
31 : COMMENT: Visual-Basic treats the following (invalid) format-strings
32 : as shown:
33 :
34 : ##0##.##0## --> ##000.000##
35 :
36 : (this class behaves the same way)
37 : */
38 :
39 : #include <stdio.h> // for: sprintf()
40 : #include <float.h> // for: DBL_DIG, DBL_EPSILON
41 : #include <math.h> // for: floor(), fabs(), log10(), pow()
42 :
43 : //=================================================================
44 : //=========================== DEFINES =============================
45 : //=================================================================
46 :
47 : #define _NO_DIGIT -1
48 :
49 : #define MAX_NO_OF_DIGITS DBL_DIG
50 : #define MAX_DOUBLE_BUFFER_LENGTH MAX_NO_OF_DIGITS + 9
51 : // +1 for leading sign
52 : // +1 for digit before the decimal point
53 : // +1 for decimal point
54 : // +2 for exponent E and exp. leading sign
55 : // +3 for the exponent's value
56 : // +1 for closing 0
57 :
58 : // Defines for the digits:
59 : #define ASCII_0 '0' // 48
60 : #define ASCII_9 '9' // 57
61 :
62 : #define CREATE_1000SEP_CHAR '@'
63 :
64 : #define FORMAT_SEPARATOR ';'
65 :
66 : // predefined formats for the Format$()-command:
67 : #define BASICFORMAT_GENERALNUMBER "General Number"
68 : #define BASICFORMAT_CURRENCY "Currency"
69 : #define BASICFORMAT_FIXED "Fixed"
70 : #define BASICFORMAT_STANDARD "Standard"
71 : #define BASICFORMAT_PERCENT "Percent"
72 : #define BASICFORMAT_SCIENTIFIC "Scientific"
73 : #define BASICFORMAT_YESNO "Yes/No"
74 : #define BASICFORMAT_TRUEFALSE "True/False"
75 : #define BASICFORMAT_ONOFF "On/Off"
76 :
77 : #define EMPTYFORMATSTRING ""
78 :
79 : // Comment: Visual-Basic has a maximum of 12 positions after the
80 : // decimal point for floating-point-numbers.
81 : // all format-strings are compatible to Visual-Basic:
82 : #define GENERALNUMBER_FORMAT "0.############"
83 : #define FIXED_FORMAT "0.00"
84 : #define STANDARD_FORMAT "@0.00"
85 : #define PERCENT_FORMAT "0.00%"
86 : #define SCIENTIFIC_FORMAT "#.00E+00"
87 : // Comment: the character @ means that thousand-separators shall
88 : // be generated. That's a StarBasic 'extension'.
89 :
90 : //=================================================================
91 :
92 :
93 22 : double get_number_of_digits( double dNumber )
94 : //double floor_log10_fabs( double dNumber )
95 : {
96 22 : if( dNumber==0.0 )
97 0 : return 0.0; // used to be 1.0, now 0.0 because of #40025;
98 : else
99 22 : return floor( log10( fabs( dNumber ) ) );
100 : }
101 :
102 : //=================================================================
103 : //======================= IMPLEMENTATION ==========================
104 : //=================================================================
105 :
106 1 : SbxBasicFormater::SbxBasicFormater( sal_Unicode _cDecPoint, sal_Unicode _cThousandSep,
107 : OUString _sOnStrg,
108 : OUString _sOffStrg,
109 : OUString _sYesStrg,
110 : OUString _sNoStrg,
111 : OUString _sTrueStrg,
112 : OUString _sFalseStrg,
113 : OUString _sCurrencyStrg,
114 1 : OUString _sCurrencyFormatStrg )
115 : {
116 1 : cDecPoint = _cDecPoint;
117 1 : cThousandSep = _cThousandSep;
118 1 : sOnStrg = _sOnStrg;
119 1 : sOffStrg = _sOffStrg;
120 1 : sYesStrg = _sYesStrg;
121 1 : sNoStrg = _sNoStrg;
122 1 : sTrueStrg = _sTrueStrg;
123 1 : sFalseStrg = _sFalseStrg;
124 1 : sCurrencyStrg = _sCurrencyStrg;
125 1 : sCurrencyFormatStrg = _sCurrencyFormatStrg;
126 1 : }
127 :
128 : // function for ouput of a error-text (for debugging)
129 : // displaces all characters of the string, starting from nStartPos
130 : // for one position to larger indexes, i. e. place for a new
131 : // character (which is to be inserted) is created.
132 : // ATTENTION: the string MUST be long enough!
133 0 : inline void SbxBasicFormater::ShiftString( OUStringBuffer& sStrg, sal_uInt16 nStartPos )
134 : {
135 0 : sStrg.remove(nStartPos,1);
136 0 : }
137 :
138 43 : void SbxBasicFormater::AppendDigit( OUStringBuffer& sStrg, short nDigit )
139 : {
140 43 : if( nDigit>=0 && nDigit<=9 )
141 : {
142 41 : sStrg.append((sal_Unicode)(nDigit+ASCII_0));
143 : }
144 43 : }
145 :
146 0 : void SbxBasicFormater::LeftShiftDecimalPoint( OUStringBuffer& sStrg )
147 : {
148 0 : sal_Int32 nPos = -1;
149 :
150 0 : for(sal_Int32 i = 0; i < sStrg.getLength(); i++)
151 : {
152 0 : if(sStrg[i] == cDecPoint)
153 : {
154 0 : nPos = i;
155 0 : break;
156 : }
157 : }
158 0 : if( nPos >= 0 )
159 : {
160 0 : sStrg[nPos] = sStrg[nPos - 1];
161 0 : sStrg[nPos - 1] = cDecPoint;
162 : }
163 0 : }
164 :
165 : // returns a flag if rounding a 9
166 3 : void SbxBasicFormater::StrRoundDigit( OUStringBuffer& sStrg, short nPos, sal_Bool& bOverflow )
167 : {
168 3 : if( nPos<0 )
169 : {
170 0 : return;
171 : }
172 3 : bOverflow = sal_False;
173 3 : sal_Unicode c = sStrg[nPos];
174 3 : if( nPos > 0 && (c == cDecPoint || c == cThousandSep) )
175 : {
176 0 : StrRoundDigit( sStrg, nPos - 1, bOverflow );
177 : // CHANGE from 9.3.1997: end the method immediately after recursive call!
178 0 : return;
179 : }
180 : // skip non-digits:
181 : // COMMENT:
182 : // in a valid format-string the number's output should be done
183 : // in one piece, i. e. special characters should ONLY be in
184 : // front OR behind the number and not right in the middle of
185 : // the format information for the number
186 7 : while( nPos >= 0 && ( sStrg[nPos] < ASCII_0 || sStrg[nPos] > ASCII_9 ))
187 : {
188 1 : nPos--;
189 : }
190 3 : if( nPos==-1 )
191 : {
192 0 : ShiftString( sStrg, 0 );
193 0 : sStrg[0] = (sal_Unicode)'1';
194 0 : bOverflow = sal_True;
195 : }
196 : else
197 : {
198 3 : sal_Unicode c2 = sStrg[nPos];
199 3 : if( c2 >= ASCII_0 && c2 <= ASCII_9 )
200 : {
201 6 : if( c2 == ASCII_9 )
202 : {
203 0 : sStrg[nPos] = (sal_Unicode)'0';
204 0 : StrRoundDigit( sStrg, nPos - 1, bOverflow );
205 : }
206 : else
207 : {
208 3 : sStrg[nPos] = c2 + 1;
209 : }
210 : }
211 : else
212 : {
213 0 : ShiftString( sStrg,nPos+1 );
214 0 : sStrg[nPos + 1] = (sal_Unicode)'1';
215 0 : bOverflow = sal_True;
216 : }
217 : }
218 : }
219 :
220 2 : void SbxBasicFormater::StrRoundDigit( OUStringBuffer& sStrg, short nPos )
221 : {
222 : sal_Bool bOverflow;
223 :
224 2 : StrRoundDigit( sStrg, nPos, bOverflow );
225 2 : }
226 :
227 9 : void SbxBasicFormater::ParseBack( OUStringBuffer& sStrg, const OUString& sFormatStrg,
228 : short nFormatPos )
229 : {
230 30 : for( sal_Int32 i = nFormatPos;
231 15 : i>0 && sFormatStrg[ i ] == (sal_Unicode)'#' && sStrg[sStrg.getLength() - 1] == (sal_Unicode)'0';
232 : i-- )
233 : {
234 6 : sStrg.setLength(sStrg.getLength() - 1 );
235 : }
236 9 : }
237 :
238 : #ifdef _with_sprintf
239 :
240 :
241 6 : void SbxBasicFormater::InitScan( double _dNum )
242 : {
243 : char sBuffer[ MAX_DOUBLE_BUFFER_LENGTH ];
244 :
245 6 : dNum = _dNum;
246 6 : InitExp( get_number_of_digits( dNum ) );
247 : // maximum of 15 positions behind the decimal point, example: -1.234000000000000E-001
248 6 : /*int nCount =*/ sprintf( sBuffer,"%+22.15lE",dNum );
249 6 : sSciNumStrg = OUString::createFromAscii( sBuffer );
250 6 : }
251 :
252 :
253 8 : void SbxBasicFormater::InitExp( double _dNewExp )
254 : {
255 : char sBuffer[ MAX_DOUBLE_BUFFER_LENGTH ];
256 8 : nNumExp = (short)_dNewExp;
257 8 : /*int nCount =*/ sprintf( sBuffer,"%+i",nNumExp );
258 8 : sNumExpStrg = OUString::createFromAscii( sBuffer );
259 8 : nExpExp = (short)get_number_of_digits( (double)nNumExp );
260 8 : }
261 :
262 :
263 44 : short SbxBasicFormater::GetDigitAtPosScan( short nPos, sal_Bool& bFoundFirstDigit )
264 : {
265 : // trying to read a higher digit,
266 : // e. g. position 4 in 1.234,
267 : // or to read a digit outside of the
268 : // number's dissolution (double)
269 44 : if( nPos>nNumExp || abs(nNumExp-nPos)>MAX_NO_OF_DIGITS )
270 : {
271 3 : return _NO_DIGIT;
272 : }
273 : // determine the index of the position in the number-string:
274 : // skip the leading sign
275 41 : sal_uInt16 no = 1;
276 : // skip the decimal point if necessary
277 41 : if( nPos<nNumExp )
278 35 : no++;
279 41 : no += nNumExp-nPos;
280 : // query of the number's first valid digit --> set flag
281 41 : if( nPos==nNumExp )
282 6 : bFoundFirstDigit = sal_True;
283 41 : return (short)(sSciNumStrg[ no ] - ASCII_0);
284 : }
285 :
286 2 : short SbxBasicFormater::GetDigitAtPosExpScan( short nPos, sal_Bool& bFoundFirstDigit )
287 : {
288 2 : if( nPos>nExpExp )
289 0 : return -1;
290 :
291 2 : sal_uInt16 no = 1;
292 2 : no += nExpExp-nPos;
293 :
294 2 : if( nPos==nExpExp )
295 2 : bFoundFirstDigit = sal_True;
296 2 : return (short)(sNumExpStrg[ no ] - ASCII_0);
297 : }
298 :
299 : // a value for the exponent can be given because the number maybe shall
300 : // not be displayed in a normed way (e. g. 1.2345e-03) but maybe 123.345e-3 !
301 2 : short SbxBasicFormater::GetDigitAtPosExpScan( double dNewExponent, short nPos,
302 : sal_Bool& bFoundFirstDigit )
303 : {
304 2 : InitExp( dNewExponent );
305 :
306 2 : return GetDigitAtPosExpScan( nPos,bFoundFirstDigit );
307 : }
308 :
309 : #else
310 :
311 : /* Problems with the following method:
312 :
313 : TODO: an 'intelligent' peek-parser might be needed to detect rounding
314 : mistakes at double-numbers - e. g. for 0.00115 #.#e-000
315 :
316 : problem with: format( 0.3345 , "0.000" )
317 : problem with: format( 0.00115 , "0.0000" )
318 :
319 : */
320 : // returns the digit at the given '10 system'-position,
321 : // i. e. positive nPos for positions before the decimal
322 : // point and negative for positions after.
323 : // nPos==0 means first position after the decimalpoint, so 10^0.
324 : // returns 0..9 for valid digits and -1 for not existing,
325 : // i. e. if the passed number is too small
326 : // (e. g. position 5 of dNumber=123).
327 : // Furthermore in dNextNumber the number shorted by leading
328 : // positions (till nPos) is returned, e. g.
329 : // GetDigitAtPos( 3434.565 , 2 , dNewNumber ) --> dNewNumber = 434.565
330 : // In bFoundFirstDigit a flag is set if a digit has been found,
331 : // this is used to prevent 'errors' on parsing 202
332 : // ATTENTION: apparently there are sometimes still problems with rounding mistakes!
333 : short SbxBasicFormater::GetDigitAtPos( double dNumber, short nPos,
334 : double& dNextNumber, sal_Bool& bFoundFirstDigit )
335 : {
336 : double dDigit;
337 : short nMaxDigit;
338 :
339 : dNumber = fabs( dNumber );
340 :
341 : nMaxDigit = (short)get_number_of_digits( dNumber );
342 : // error only at numbers > 0, i. e. for digits before
343 : // the decimal point
344 : if( nMaxDigit<nPos && !bFoundFirstDigit && nPos>=0 )
345 : return _NO_DIGIT;
346 :
347 : bFoundFirstDigit = sal_True;
348 : for( short i=nMaxDigit; i>=nPos; i-- )
349 : {
350 : double dI = (double)i;
351 : double dTemp1 = pow( 10.0,dI );
352 :
353 : dDigit = floor( pow( 10.0,log10( fabs( dNumber ) )-dI ) );
354 : dNumber -= dTemp1 * dDigit;
355 : }
356 : // for optimized loop run
357 : dNextNumber = dNumber;
358 :
359 : return RoundDigit( dDigit );
360 : }
361 :
362 :
363 : short SbxBasicFormater::RoundDigit( double dNumber )
364 : {
365 : if( dNumber<0.0 || dNumber>10.0 )
366 : return -1;
367 : short nTempHigh = (short)(dNumber+0.5); // maybe floor( )
368 : return nTempHigh;
369 : }
370 :
371 : #endif
372 :
373 : // Copies the respective part of the format-string, if existing, and returns it.
374 : // So a new string is created, which has to be freed by the caller later.
375 6 : OUString SbxBasicFormater::GetPosFormatString( const OUString& sFormatStrg, sal_Bool & bFound )
376 : {
377 6 : bFound = sal_False; // default...
378 6 : sal_Int32 nPos = sFormatStrg.indexOf( FORMAT_SEPARATOR );
379 :
380 6 : if( nPos >= 0 )
381 : {
382 0 : bFound = sal_True;
383 : // the format-string for positive numbers is
384 : // everything before the first ';'
385 0 : return sFormatStrg.copy( 0,nPos );
386 : }
387 :
388 6 : OUString aRetStr;
389 6 : aRetStr = OUString::createFromAscii( EMPTYFORMATSTRING );
390 6 : return aRetStr;
391 : }
392 :
393 : // see also GetPosFormatString()
394 6 : OUString SbxBasicFormater::GetNegFormatString( const OUString& sFormatStrg, sal_Bool & bFound )
395 : {
396 6 : bFound = sal_False; // default...
397 6 : sal_Int32 nPos = sFormatStrg.indexOf( FORMAT_SEPARATOR );
398 :
399 6 : if( nPos >= 0)
400 : {
401 : // the format-string for negative numbers is
402 : // everything between the first and the second ';'
403 0 : OUString sTempStrg = sFormatStrg.copy( nPos+1 );
404 0 : nPos = sTempStrg.indexOf( FORMAT_SEPARATOR );
405 0 : bFound = sal_True;
406 0 : if( nPos < 0 )
407 : {
408 0 : return sTempStrg;
409 : }
410 : else
411 : {
412 0 : return sTempStrg.copy( 0,nPos );
413 0 : }
414 : }
415 6 : OUString aRetStr;
416 6 : aRetStr = OUString::createFromAscii( EMPTYFORMATSTRING );
417 6 : return aRetStr;
418 : }
419 :
420 : // see also GetPosFormatString()
421 6 : OUString SbxBasicFormater::Get0FormatString( const OUString& sFormatStrg, sal_Bool & bFound )
422 : {
423 6 : bFound = sal_False; // default...
424 6 : sal_Int32 nPos = sFormatStrg.indexOf( FORMAT_SEPARATOR );
425 :
426 6 : if( nPos >= 0 )
427 : {
428 : // the format string for the zero is
429 : // everything after the second ';'
430 0 : OUString sTempStrg = sFormatStrg.copy( nPos+1 );
431 0 : nPos = sTempStrg.indexOf( FORMAT_SEPARATOR );
432 0 : if( nPos >= 0 )
433 : {
434 0 : bFound = sal_True;
435 0 : sTempStrg = sTempStrg.copy( nPos+1 );
436 0 : nPos = sTempStrg.indexOf( FORMAT_SEPARATOR );
437 0 : if( nPos < 0 )
438 : {
439 0 : return sTempStrg;
440 : }
441 : else
442 : {
443 0 : return sTempStrg.copy( 0,nPos );
444 : }
445 0 : }
446 : }
447 :
448 6 : OUString aRetStr;
449 6 : aRetStr = OUString::createFromAscii( EMPTYFORMATSTRING );
450 6 : return aRetStr;
451 : }
452 :
453 : // see also GetPosFormatString()
454 0 : OUString SbxBasicFormater::GetNullFormatString( const OUString& sFormatStrg, sal_Bool & bFound )
455 : {
456 0 : bFound = sal_False; // default...
457 0 : sal_Int32 nPos = sFormatStrg.indexOf( FORMAT_SEPARATOR );
458 :
459 0 : if( nPos >= 0 )
460 : {
461 : // the format-string for the Null is
462 : // everything after the third ';'
463 0 : OUString sTempStrg = sFormatStrg.copy( nPos+1 );
464 0 : nPos = sTempStrg.indexOf( FORMAT_SEPARATOR );
465 0 : if( nPos >= 0 )
466 : {
467 0 : sTempStrg = sTempStrg.copy( nPos+1 );
468 0 : nPos = sTempStrg.indexOf( FORMAT_SEPARATOR );
469 0 : if( nPos >= 0 )
470 : {
471 0 : bFound = sal_True;
472 0 : return sTempStrg.copy( nPos+1 );
473 : }
474 0 : }
475 : }
476 :
477 0 : OUString aRetStr;
478 0 : aRetStr = OUString::createFromAscii( EMPTYFORMATSTRING );
479 0 : return aRetStr;
480 : }
481 :
482 : // returns value <> 0 in case of an error
483 6 : short SbxBasicFormater::AnalyseFormatString( const OUString& sFormatStrg,
484 : short& nNoOfDigitsLeft, short& nNoOfDigitsRight,
485 : short& nNoOfOptionalDigitsLeft,
486 : short& nNoOfExponentDigits, short& nNoOfOptionalExponentDigits,
487 : sal_Bool& bPercent, sal_Bool& bCurrency, sal_Bool& bScientific,
488 : sal_Bool& bGenerateThousandSeparator,
489 : short& nMultipleThousandSeparators )
490 : {
491 : sal_Int32 nLen;
492 6 : short nState = 0;
493 :
494 6 : nLen = sFormatStrg.getLength();
495 6 : nNoOfDigitsLeft = 0;
496 6 : nNoOfDigitsRight = 0;
497 6 : nNoOfOptionalDigitsLeft = 0;
498 6 : nNoOfExponentDigits = 0;
499 6 : nNoOfOptionalExponentDigits = 0;
500 6 : bPercent = sal_False;
501 6 : bCurrency = sal_False;
502 6 : bScientific = sal_False;
503 : // from 11.7.97: as soon as a comma (point?) is found in the format string,
504 : // all three decimal powers are marked (i. e. thousand, million, ...)
505 6 : bGenerateThousandSeparator = sFormatStrg.indexOf( ',' ) >= 0;
506 6 : nMultipleThousandSeparators = 0;
507 :
508 50 : for( sal_Int32 i = 0; i < nLen; i++ )
509 : {
510 44 : sal_Unicode c = sFormatStrg[ i ];
511 44 : switch( c )
512 : {
513 : case '#':
514 : case '0':
515 32 : if( nState==0 )
516 : {
517 6 : nNoOfDigitsLeft++;
518 : // TODO here maybe better error inspection of the mantissa for valid syntax (see grammar)h
519 : // ATTENTION: 'undefined' behaviour if # and 0 are combined!
520 : // REMARK: #-placeholders are actually useless for
521 : // scientific display before the decimal point!
522 6 : if( c=='#' )
523 : {
524 2 : nNoOfOptionalDigitsLeft++;
525 : }
526 : }
527 26 : else if( nState==1 )
528 : {
529 22 : nNoOfDigitsRight++;
530 : }
531 4 : else if( nState==-1 ) // search 0 in the exponent
532 : {
533 4 : if( c=='#' ) // # switches on the condition
534 : {
535 0 : nNoOfOptionalExponentDigits++;
536 0 : nState = -2;
537 : }
538 4 : nNoOfExponentDigits++;
539 : }
540 0 : else if( nState==-2 ) // search # in the exponent
541 : {
542 0 : if( c=='0' )
543 : {
544 : // ERROR: 0 after # in the exponent is NOT allowed!!
545 0 : return -4;
546 : }
547 0 : nNoOfOptionalExponentDigits++;
548 0 : nNoOfExponentDigits++;
549 : }
550 32 : break;
551 : case '.':
552 6 : nState++;
553 6 : if( nState>1 )
554 : {
555 0 : return -1; // ERROR: too many decimal points
556 : }
557 6 : break;
558 : case '%':
559 1 : bPercent = sal_True;
560 1 : break;
561 : case '(':
562 0 : bCurrency = sal_True;
563 0 : break;
564 : case ',':
565 : {
566 0 : sal_Unicode ch = sFormatStrg[ i+1 ];
567 :
568 0 : if( ch!=0 && (ch==',' || ch=='.') )
569 : {
570 0 : nMultipleThousandSeparators++;
571 : }
572 : }
573 0 : break;
574 : case 'e':
575 : case 'E':
576 : // #i13821 not when no digits before
577 2 : if( nNoOfDigitsLeft > 0 || nNoOfDigitsRight > 0 )
578 : {
579 2 : nState = -1; // abort counting digits
580 2 : bScientific = sal_True;
581 : }
582 2 : break;
583 : // OWN command-character which turns on
584 : // the creation of thousand-separators
585 : case '\\':
586 : // Ignore next char
587 0 : i++;
588 0 : break;
589 : case CREATE_1000SEP_CHAR:
590 1 : bGenerateThousandSeparator = sal_True;
591 1 : break;
592 : }
593 : }
594 6 : return 0;
595 : }
596 :
597 : // the flag bCreateSign says that at the mantissa a leading sign
598 : // shall be created
599 6 : void SbxBasicFormater::ScanFormatString( double dNumber,
600 : const OUString& sFormatStrg, OUString& sReturnStrgFinal,
601 : sal_Bool bCreateSign )
602 : {
603 : short /*nErr,*/nNoOfDigitsLeft,nNoOfDigitsRight,nNoOfOptionalDigitsLeft,
604 : nNoOfExponentDigits,nNoOfOptionalExponentDigits,
605 : nMultipleThousandSeparators;
606 : sal_Bool bPercent,bCurrency,bScientific,bGenerateThousandSeparator;
607 :
608 6 : OUStringBuffer sReturnStrg = OUStringBuffer();
609 :
610 : // analyse the format-string, i. e. determine the following values:
611 : /*
612 : - number of digits before decimal point
613 : - number of digits after decimal point
614 : - optional digits before decimal point
615 : - number of digits in the exponent
616 : - optional digits in the exponent
617 : - percent-character found?
618 : - () for negative leading sign?
619 : - exponetial-notation?
620 : - shall thousand-separators be generated?
621 : - is a percent-character being found? --> dNumber *= 100.0;
622 : - are there thousand-separators in a row?
623 : ,, or ,. --> dNumber /= 1000.0;
624 : - other errors? multiple decimal points, E's, etc.
625 : --> errors are simply ignored at the moment
626 : */
627 : AnalyseFormatString( sFormatStrg, nNoOfDigitsLeft, nNoOfDigitsRight,
628 : nNoOfOptionalDigitsLeft, nNoOfExponentDigits,
629 : nNoOfOptionalExponentDigits,
630 : bPercent, bCurrency, bScientific,
631 6 : bGenerateThousandSeparator, nMultipleThousandSeparators );
632 : // special handling for special characters
633 6 : if( bPercent )
634 : {
635 1 : dNumber *= 100.0;
636 : }
637 : // TODO: this condition (,, or ,.) is NOT Visual-Basic compatible!
638 : // Question: shall this stay here (requirements)?
639 6 : if( nMultipleThousandSeparators )
640 : {
641 0 : dNumber /= 1000.0;
642 : }
643 : double dExponent;
644 : short i,nLen;
645 : short nState,nDigitPos,nExponentPos,nMaxDigit,nMaxExponentDigit;
646 : sal_Bool bFirstDigit,bFirstExponentDigit,bFoundFirstDigit,
647 : bIsNegative,bZeroSpaceOn, bSignHappend,bDigitPosNegative;
648 :
649 6 : bSignHappend = sal_False;
650 6 : bFoundFirstDigit = sal_False;
651 6 : bIsNegative = dNumber < 0.0;
652 6 : nLen = sFormatStrg.getLength();
653 6 : dExponent = get_number_of_digits( dNumber );
654 6 : nExponentPos = 0;
655 6 : nMaxExponentDigit = 0;
656 6 : nMaxDigit = (short)dExponent;
657 6 : bDigitPosNegative = false;
658 6 : if( bScientific )
659 : {
660 2 : dExponent = dExponent - (double)(nNoOfDigitsLeft-1);
661 2 : nDigitPos = nMaxDigit;
662 2 : nMaxExponentDigit = (short)get_number_of_digits( dExponent );
663 2 : nExponentPos = nNoOfExponentDigits - 1 - nNoOfOptionalExponentDigits;
664 : }
665 : else
666 : {
667 4 : nDigitPos = nNoOfDigitsLeft - 1; // counting starts at 0, 10^0
668 : // no exponent-data is needed here!
669 4 : bDigitPosNegative = (nDigitPos < 0);
670 : }
671 6 : bFirstDigit = sal_True;
672 6 : bFirstExponentDigit = sal_True;
673 6 : nState = 0; // 0 --> mantissa; 1 --> exponent
674 6 : bZeroSpaceOn = 0;
675 :
676 :
677 : #ifdef _with_sprintf
678 6 : InitScan( dNumber );
679 : #endif
680 : // scanning the format-string:
681 6 : sal_Unicode cForce = 0;
682 48 : for( i = 0; i < nLen; i++ )
683 : {
684 : sal_Unicode c;
685 42 : if( cForce )
686 : {
687 0 : c = cForce;
688 0 : cForce = 0;
689 : }
690 : else
691 : {
692 42 : c = sFormatStrg[ i ];
693 : }
694 42 : switch( c )
695 : {
696 : case '0':
697 : case '#':
698 32 : if( nState==0 )
699 : {
700 : // handling of the mantissa
701 28 : if( bFirstDigit )
702 : {
703 : // remark: at bCurrency the negative
704 : // leading sign shall be shown with ()
705 6 : if( bIsNegative && !bCreateSign && !bSignHappend )
706 : {
707 1 : bSignHappend = sal_True;
708 1 : sReturnStrg.append('-');
709 : }
710 : // output redundant positions, i. e. those which
711 : // are undocumented by the format-string
712 6 : if( nMaxDigit > nDigitPos )
713 : {
714 14 : for( short j = nMaxDigit; j > nDigitPos; j-- )
715 : {
716 : short nTempDigit;
717 : #ifdef _with_sprintf
718 11 : AppendDigit( sReturnStrg, nTempDigit = GetDigitAtPosScan( j, bFoundFirstDigit ) );
719 : #else
720 : AppendDigit( sReturnStrg, nTempDigit = GetDigitAtPos( dNumber, j, dNumber, bFoundFirstDigit ) );
721 : #endif
722 11 : if( nTempDigit!=_NO_DIGIT )
723 : {
724 11 : bFirstDigit = sal_False;
725 : }
726 11 : if( bGenerateThousandSeparator && ( c=='0' || nMaxDigit >= nDigitPos ) && j > 0 && (j % 3 == 0) )
727 : {
728 1 : sReturnStrg.append(cThousandSep );
729 : }
730 : }
731 : }
732 : }
733 :
734 28 : if( nMaxDigit<nDigitPos && ( c=='0' || bZeroSpaceOn ) )
735 : {
736 1 : AppendDigit( sReturnStrg, 0 );
737 1 : bFirstDigit = sal_False;
738 1 : bZeroSpaceOn = 1;
739 : // Remark: in Visual-Basic the first 0 turns on the 0 for
740 : // all the following # (up to the decimal point),
741 : // this behaviour is simulated here with the flag.
742 2 : if( bGenerateThousandSeparator && ( c=='0' || nMaxDigit >= nDigitPos ) && nDigitPos > 0 && (nDigitPos % 3 == 0) )
743 : {
744 0 : sReturnStrg.append(cThousandSep);
745 : }
746 : }
747 : else
748 : {
749 : short nTempDigit;
750 : #ifdef _with_sprintf
751 27 : AppendDigit( sReturnStrg, nTempDigit = GetDigitAtPosScan( nDigitPos, bFoundFirstDigit ) );
752 : #else
753 : AppendDigit( sReturnStrg, nTempDigit = GetDigitAtPos( dNumber, nDigitPos, dNumber, bFoundFirstDigit ) );
754 : #endif
755 :
756 27 : if( nTempDigit != _NO_DIGIT )
757 : {
758 25 : bFirstDigit = sal_False;
759 : }
760 27 : if( bGenerateThousandSeparator && ( c=='0' || nMaxDigit>=nDigitPos ) && nDigitPos>0 && (nDigitPos % 3 == 0) )
761 : {
762 0 : sReturnStrg.append(cThousandSep);
763 : }
764 : }
765 28 : nDigitPos--;
766 : }
767 : else
768 : {
769 : // handling the exponent
770 4 : if( bFirstExponentDigit )
771 : {
772 : // leading sign has been given out at e/E already
773 2 : bFirstExponentDigit = sal_False;
774 2 : if( nMaxExponentDigit > nExponentPos )
775 : // output redundant positions, i. e. those which
776 : // are undocumented by the format-string
777 : {
778 0 : for( short j = nMaxExponentDigit; j > nExponentPos; j-- )
779 : {
780 : #ifdef _with_sprintf
781 0 : AppendDigit( sReturnStrg, GetDigitAtPosExpScan( dExponent, j, bFoundFirstDigit ) );
782 : #else
783 : AppendDigit( sReturnStrg,GetDigitAtPos( dExponent, j, dExponent, bFoundFirstDigit ) );
784 : #endif
785 : }
786 : }
787 : }
788 :
789 4 : if( nMaxExponentDigit < nExponentPos && c=='0' )
790 : {
791 2 : AppendDigit( sReturnStrg, 0 );
792 : }
793 : else
794 : {
795 : #ifdef _with_sprintf
796 2 : AppendDigit( sReturnStrg, GetDigitAtPosExpScan( dExponent, nExponentPos, bFoundFirstDigit ) );
797 : #else
798 : AppendDigit( sReturnStrg, GetDigitAtPos( dExponent, nExponentPos, dExponent, bFoundFirstDigit ) );
799 : #endif
800 : }
801 4 : nExponentPos--;
802 : }
803 32 : break;
804 : case '.':
805 6 : if( bDigitPosNegative ) // #i13821: If no digits before .
806 : {
807 0 : bDigitPosNegative = false;
808 0 : nDigitPos = 0;
809 0 : cForce = '#';
810 0 : i-=2;
811 0 : break;
812 : }
813 6 : sReturnStrg.append(cDecPoint);
814 6 : break;
815 : case '%':
816 : // maybe remove redundant 0s, e. g. 4.500e4 in 0.0##e-00
817 1 : ParseBack( sReturnStrg, sFormatStrg, i-1 );
818 1 : sReturnStrg.append('%');
819 1 : break;
820 : case 'e':
821 : case 'E':
822 : // does mantissa have to be rounded, before the exponent is displayed?
823 : {
824 : // is there a mantissa at all?
825 2 : if( bFirstDigit )
826 : {
827 : // apparently not, i. e. invalid format string, e. g. E000.00
828 : // so ignore these e and E characters
829 : // maybe output an error (like in Visual Basic)?
830 :
831 : // #i13821: VB 6 behaviour
832 0 : sReturnStrg.append(c);
833 0 : break;
834 : }
835 :
836 2 : sal_Bool bOverflow = sal_False;
837 : #ifdef _with_sprintf
838 2 : short nNextDigit = GetDigitAtPosScan( nDigitPos, bFoundFirstDigit );
839 : #else
840 : short nNextDigit = GetDigitAtPos( dNumber, nDigitPos, dNumber, bFoundFirstDigit );
841 : #endif
842 2 : if( nNextDigit>=5 )
843 : {
844 1 : StrRoundDigit( sReturnStrg, sReturnStrg.getLength() - 1, bOverflow );
845 : }
846 2 : if( bOverflow )
847 : {
848 : // a leading 9 has been rounded
849 0 : LeftShiftDecimalPoint( sReturnStrg );
850 0 : sReturnStrg[sReturnStrg.getLength() - 1] = 0;
851 0 : dExponent += 1.0;
852 : }
853 : // maybe remove redundant 0s, e. g. 4.500e4 in 0.0##e-00
854 2 : ParseBack( sReturnStrg, sFormatStrg, i-1 );
855 : }
856 : // change the scanner's condition
857 2 : nState++;
858 : // output exponent character
859 2 : sReturnStrg.append(c);
860 : // i++; // MANIPULATION of the loop-variable!
861 2 : c = sFormatStrg[ ++i ];
862 : // output leading sign / exponent
863 2 : if( c != 0 )
864 : {
865 2 : if( c == '-' )
866 : {
867 0 : if( dExponent < 0.0 )
868 : {
869 0 : sReturnStrg.append('-');
870 : }
871 : }
872 2 : else if( c == '+' )
873 : {
874 2 : if( dExponent < 0.0 )
875 : {
876 0 : sReturnStrg.append('-');
877 : }
878 : else
879 : {
880 2 : sReturnStrg.append('+');
881 : }
882 : }
883 : }
884 2 : break;
885 : case ',':
886 0 : break;
887 : case ';':
888 0 : break;
889 : case '(':
890 : case ')':
891 : // maybe remove redundant 0s, e. g. 4.500e4 in 0.0##e-00
892 0 : ParseBack( sReturnStrg, sFormatStrg, i-1 );
893 0 : if( bIsNegative )
894 : {
895 0 : sReturnStrg.append(c);
896 : }
897 0 : break;
898 : case '$':
899 : // append the string for the currency:
900 0 : sReturnStrg.append(sCurrencyStrg);
901 0 : break;
902 : case ' ':
903 : case '-':
904 : case '+':
905 0 : ParseBack( sReturnStrg, sFormatStrg, i-1 );
906 0 : sReturnStrg.append(c);
907 0 : break;
908 : case '\\':
909 0 : ParseBack( sReturnStrg, sFormatStrg, i-1 );
910 : // special character found, output next
911 : // character directly (if existing)
912 0 : c = sFormatStrg[ ++i ];
913 0 : if( c!=0 )
914 : {
915 0 : sReturnStrg.append(c);
916 : }
917 0 : break;
918 : case CREATE_1000SEP_CHAR:
919 : // ignore here, action has already been
920 : // executed in AnalyseFormatString
921 1 : break;
922 : default:
923 : // output characters and digits, too (like in Visual-Basic)
924 0 : if( ( c>='a' && c<='z' ) ||
925 0 : ( c>='A' && c<='Z' ) ||
926 0 : ( c>='1' && c<='9' ) )
927 : {
928 0 : sReturnStrg.append(c);
929 : }
930 : }
931 : }
932 :
933 : // scan completed - rounding necessary?
934 6 : if( !bScientific )
935 : {
936 : #ifdef _with_sprintf
937 4 : short nNextDigit = GetDigitAtPosScan( nDigitPos, bFoundFirstDigit );
938 : #else
939 : short nNextDigit = GetDigitAtPos( dNumber, nDigitPos, dNumber, bFoundFirstDigit );
940 : #endif
941 4 : if( nNextDigit>=5 )
942 : {
943 2 : StrRoundDigit( sReturnStrg, sReturnStrg.getLength() - 1 );
944 : }
945 : }
946 :
947 6 : if( nNoOfDigitsRight>0 )
948 : {
949 6 : ParseBack( sReturnStrg, sFormatStrg, sFormatStrg.getLength()-1 );
950 : }
951 6 : sReturnStrgFinal = sReturnStrg.makeStringAndClear();
952 6 : }
953 :
954 0 : OUString SbxBasicFormater::BasicFormatNull( OUString sFormatStrg )
955 : {
956 : sal_Bool bNullFormatFound;
957 0 : OUString sNullFormatStrg = GetNullFormatString( sFormatStrg, bNullFormatFound );
958 :
959 0 : if( bNullFormatFound )
960 : {
961 0 : return sNullFormatStrg;
962 : }
963 0 : return OUString("null");
964 : }
965 :
966 12 : OUString SbxBasicFormater::BasicFormat( double dNumber, OUString sFormatStrg )
967 : {
968 : sal_Bool bPosFormatFound,bNegFormatFound,b0FormatFound;
969 :
970 : // analyse format-string concerning predefined formats:
971 12 : if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_GENERALNUMBER ) )
972 : {
973 1 : sFormatStrg = OUString::createFromAscii( GENERALNUMBER_FORMAT );
974 : }
975 12 : if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_CURRENCY ) )
976 : {
977 0 : sFormatStrg = sCurrencyFormatStrg;
978 : }
979 12 : if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_FIXED ) )
980 : {
981 1 : sFormatStrg = OUString::createFromAscii( FIXED_FORMAT );
982 : }
983 12 : if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_STANDARD ) )
984 : {
985 1 : sFormatStrg = OUString::createFromAscii( STANDARD_FORMAT );
986 : }
987 12 : if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_PERCENT ) )
988 : {
989 1 : sFormatStrg = OUString::createFromAscii( PERCENT_FORMAT );
990 : }
991 12 : if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_SCIENTIFIC ) )
992 : {
993 2 : sFormatStrg = OUString::createFromAscii( SCIENTIFIC_FORMAT );
994 : }
995 12 : if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_YESNO ) )
996 : {
997 2 : return ( dNumber==0.0 ) ? sNoStrg : sYesStrg ;
998 : }
999 10 : if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_TRUEFALSE ) )
1000 : {
1001 2 : return ( dNumber==0.0 ) ? sFalseStrg : sTrueStrg ;
1002 : }
1003 8 : if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_ONOFF ) )
1004 : {
1005 2 : return ( dNumber==0.0 ) ? sOffStrg : sOnStrg ;
1006 : }
1007 :
1008 : // analyse format-string concerning ';', i. e. format-strings for
1009 : // positive-, negative- and 0-values
1010 6 : OUString sPosFormatStrg = GetPosFormatString( sFormatStrg, bPosFormatFound );
1011 12 : OUString sNegFormatStrg = GetNegFormatString( sFormatStrg, bNegFormatFound );
1012 12 : OUString s0FormatStrg = Get0FormatString( sFormatStrg, b0FormatFound );
1013 :
1014 12 : OUString sReturnStrg;
1015 12 : OUString sTempStrg;
1016 :
1017 6 : if( dNumber==0.0 )
1018 : {
1019 0 : sTempStrg = sFormatStrg;
1020 0 : if( b0FormatFound )
1021 : {
1022 0 : if( s0FormatStrg.isEmpty() && bPosFormatFound )
1023 : {
1024 0 : sTempStrg = sPosFormatStrg;
1025 : }
1026 : else
1027 : {
1028 0 : sTempStrg = s0FormatStrg;
1029 : }
1030 : }
1031 0 : else if( bPosFormatFound )
1032 : {
1033 0 : sTempStrg = sPosFormatStrg;
1034 : }
1035 0 : ScanFormatString( dNumber, sTempStrg, sReturnStrg,/*bCreateSign=*/sal_False );
1036 : }
1037 : else
1038 : {
1039 6 : if( dNumber<0.0 )
1040 : {
1041 1 : if( bNegFormatFound )
1042 : {
1043 0 : if( sNegFormatStrg.isEmpty() && bPosFormatFound )
1044 : {
1045 0 : sTempStrg = "-";
1046 0 : sTempStrg += sPosFormatStrg;
1047 : }
1048 : else
1049 : {
1050 0 : sTempStrg = sNegFormatStrg;
1051 : }
1052 : }
1053 : else
1054 : {
1055 1 : sTempStrg = sFormatStrg;
1056 : }
1057 : // if NO format-string especially for negative
1058 : // values is given, output the leading sign
1059 1 : ScanFormatString( dNumber, sTempStrg, sReturnStrg,/*bCreateSign=*/bNegFormatFound/*sNegFormatStrg!=EMPTYFORMATSTRING*/ );
1060 : }
1061 : else // if( dNumber>0.0 )
1062 : {
1063 : ScanFormatString( dNumber,
1064 : (/*sPosFormatStrg!=EMPTYFORMATSTRING*/bPosFormatFound ? sPosFormatStrg : sFormatStrg),
1065 5 : sReturnStrg,/*bCreateSign=*/sal_False );
1066 : }
1067 : }
1068 12 : return sReturnStrg;
1069 : }
1070 :
1071 26 : sal_Bool SbxBasicFormater::isBasicFormat( OUString sFormatStrg )
1072 : {
1073 26 : if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_GENERALNUMBER ) )
1074 : {
1075 1 : return sal_True;
1076 : }
1077 25 : if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_CURRENCY ) )
1078 : {
1079 0 : return sal_True;
1080 : }
1081 25 : if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_FIXED ) )
1082 : {
1083 1 : return sal_True;
1084 : }
1085 24 : if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_STANDARD ) )
1086 : {
1087 1 : return sal_True;
1088 : }
1089 23 : if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_PERCENT ) )
1090 : {
1091 1 : return sal_True;
1092 : }
1093 22 : if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_SCIENTIFIC ) )
1094 : {
1095 2 : return sal_True;
1096 : }
1097 20 : if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_YESNO ) )
1098 : {
1099 2 : return sal_True;
1100 : }
1101 18 : if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_TRUEFALSE ) )
1102 : {
1103 2 : return sal_True;
1104 : }
1105 16 : if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_ONOFF ) )
1106 : {
1107 2 : return sal_True;
1108 : }
1109 14 : return sal_False;
1110 : }
1111 :
1112 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|