1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_BASIC_SBXFORM_HXX
21 #define INCLUDED_BASIC_SBXFORM_HXX
24 // Implementation class for Basic command: Format$( d,formatStr )
27 Grammar of format string (a try):
28 -----------------------------------------------
30 format_string := {\special_char} general_format | scientific_format {\special_char} {;format_string}
31 general_format := {#[,]}{0[,]}[.{0}{#}]
32 scientific_format := {0}[.{0}{#}](e | E)(+ | -){#}{0}
35 special_char := \char | + | - | ( | ) | $ | space_char
36 char := all_ascii_chars
39 {} repeated multiple times (incl. zero times)
40 [] exactly one or zero times
41 () parenthesis, e.g. (e | E) means e or E times
43 Additional predefined formats for the format string:
54 Note: invalid format string are ignored just as in VisualBasic, the output is
55 probably 'undefined'. ASCII letters are outputted directly.
57 Constraints in VisualBasic:
58 - the exponent (scientific syntax) has a maximum of three digits!
60 Constraints of new implementation:
61 - the '+' sign is not allowed as wildcard in the mantissa
65 Wildcards are: 'h', 'm', 's', 'y'
66 predefined String-Constants/Commands:
67 "AMPM", "Long Date", "Long Time"
70 #include <rtl/ustring.hxx>
71 #include <rtl/ustrbuf.hxx>
72 #include <basic/basicdllapi.h>
74 class SbxBasicFormater
{
76 // Constructor takes signs for decimal point, thousand separation sign
77 // and necessary resource strings.
78 SbxBasicFormater( sal_Unicode _cDecPoint
, sal_Unicode _cThousandSep
,
79 const OUString
& _sOnStrg
,
80 const OUString
& _sOffStrg
,
81 const OUString
& _sYesStrg
,
82 const OUString
& _sNoStrg
,
83 const OUString
& _sTrueStrg
,
84 const OUString
& _sFalseStrg
,
85 const OUString
& _sCurrencyStrg
,
86 const OUString
& _sCurrencyFormatStrg
);
88 /* Basic command: Format$( number,format-string )
91 dNumber : number to be formatted
92 sFormatStrg : the Format-String, e.g. ###0.0###
95 String containing the formatted output
97 OUString
BasicFormat( double dNumber
, const OUString
& sFormatStrg
);
98 static OUString
BasicFormatNull( const OUString
& sFormatStrg
);
100 static bool isBasicFormat( const OUString
& sFormatStrg
);
103 static inline void ShiftString( OUStringBuffer
& sStrg
, sal_uInt16 nStartPos
);
104 static void AppendDigit( OUStringBuffer
& sStrg
, short nDigit
);
105 void LeftShiftDecimalPoint( OUStringBuffer
& sStrg
);
106 void StrRoundDigit( OUStringBuffer
& sStrg
, short nPos
, bool& bOverflow
);
107 void StrRoundDigit( OUStringBuffer
& sStrg
, short nPos
);
108 static void ParseBack( OUStringBuffer
& sStrg
, const OUString
& sFormatStrg
,
110 // Methods for string conversion with sprintf():
111 void InitScan( double _dNum
);
112 void InitExp( double _dNewExp
);
113 short GetDigitAtPosScan( short nPos
, bool& bFoundFirstDigit
);
114 short GetDigitAtPosExpScan( double dNewExponent
, short nPos
,
115 bool& bFoundFirstDigit
);
116 short GetDigitAtPosExpScan( short nPos
, bool& bFoundFirstDigit
);
117 static OUString
GetPosFormatString( const OUString
& sFormatStrg
, bool & bFound
);
118 static OUString
GetNegFormatString( const OUString
& sFormatStrg
, bool & bFound
);
119 static OUString
Get0FormatString( const OUString
& sFormatStrg
, bool & bFound
);
120 static OUString
GetNullFormatString( const OUString
& sFormatStrg
, bool & bFound
);
121 static void AnalyseFormatString( const OUString
& sFormatStrg
,
122 short& nNoOfDigitsLeft
, short& nNoOfDigitsRight
,
123 short& nNoOfOptionalDigitsLeft
,
124 short& nNoOfExponentDigits
,
125 short& nNoOfOptionalExponentDigits
,
126 bool& bPercent
, bool& bCurrency
, bool& bScientific
,
127 bool& bGenerateThousandSeparator
,
128 short& nMultipleThousandSeparators
);
129 void ScanFormatString( double dNumber
, const OUString
& sFormatStrg
,
130 OUString
& sReturnStrg
, bool bCreateSign
);
133 sal_Unicode cDecPoint
; // sign for the decimal point
134 sal_Unicode cThousandSep
; // sign for thousand delimiter
142 OUString sCurrencyStrg
;
143 OUString sCurrencyFormatStrg
;
145 //*** temporary data for scan loop ***
147 // String containing the number in scientific format
148 OUString sSciNumStrg
;
149 // String containing the exponent of the number
150 OUString sNumExpStrg
;
151 double dNum
; // the number that is scanned
152 short nNumExp
; // the exponent of the number
153 short nExpExp
; // the number of digits in the exponent
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */