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 : #ifndef INCLUDED_BASIC_SBXFORM_HXX
21 : #define INCLUDED_BASIC_SBXFORM_HXX
22 :
23 :
24 : // Implementation class for Basic command: Format$( d,formatStr )
25 :
26 : /*
27 : Grammar of format string (a try):
28 : -----------------------------------------------
29 :
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}
33 :
34 : percent_char := '%'
35 : special_char := \char | + | - | ( | ) | $ | space_char
36 : char := all_ascii_chars
37 : space_char := ' '
38 :
39 : {} repeated multiple times (incl. zero times)
40 : [] exactly one or zero times
41 : () parenthesis, e.g. (e | E) means e or E times
42 :
43 : Additional predefined formats for the format string:
44 : "General Number"
45 : "Currency"
46 : "Fixed"
47 : "Standard"
48 : "Percent"
49 : "Scientific"
50 : "Yes/No"
51 : "True/False"
52 : "On/Off"
53 :
54 : Note: invalid format string are ignored just as in VisualBasic, the output is
55 : probably 'undefined'. ASCII letters are outputted directly.
56 :
57 : Constraints in VisualBasic:
58 : - the exponent (scientific syntax) has a maximum of three digits!
59 :
60 : Constraints of new implementation:
61 : - the '+' sign is not allowed as wildcard in the mantissa
62 :
63 : TODO:
64 : - Date formatting
65 : Wildcards are: 'h', 'm', 's', 'y'
66 : predefined String-Constants/Commands:
67 : "AMPM", "Long Date", "Long Time"
68 : */
69 :
70 : /*
71 : There are two possibilities to get the number of digits of a number:
72 :
73 : a) use sprintf()
74 : b) use log10() and pow() digit
75 : */
76 : #define _with_sprintf // use a)
77 :
78 : #include <rtl/ustring.hxx>
79 : #include <rtl/ustrbuf.hxx>
80 : #include <basic/basicdllapi.h>
81 :
82 2 : class BASIC_DLLPUBLIC SbxBasicFormater {
83 : public:
84 : // Constructor takes signs for decimal point, thousand separation sign
85 : // and necessary resource strings.
86 : SbxBasicFormater( sal_Unicode _cDecPoint, sal_Unicode _cThousandSep,
87 : const OUString& _sOnStrg,
88 : const OUString& _sOffStrg,
89 : const OUString& _sYesStrg,
90 : const OUString& _sNoStrg,
91 : const OUString& _sTrueStrg,
92 : const OUString& _sFalseStrg,
93 : const OUString& _sCurrencyStrg,
94 : const OUString& _sCurrencyFormatStrg );
95 :
96 : /* Basic command: Format$( number,format-string )
97 :
98 : Parameter:
99 : dNumber : number to be formated
100 : sFormatStrg : the Format-String, e.g. ###0.0###
101 :
102 : Return value:
103 : String containing the formatted output
104 : */
105 : OUString BasicFormat( double dNumber, const OUString& sFormatStrg );
106 : OUString BasicFormatNull( const OUString& sFormatStrg );
107 :
108 : static bool isBasicFormat( const OUString& sFormatStrg );
109 :
110 : private:
111 : BASIC_DLLPRIVATE inline void ShiftString( OUStringBuffer& sStrg, sal_uInt16 nStartPos );
112 : BASIC_DLLPRIVATE void AppendDigit( OUStringBuffer& sStrg, short nDigit );
113 : BASIC_DLLPRIVATE void LeftShiftDecimalPoint( OUStringBuffer& sStrg );
114 : BASIC_DLLPRIVATE void StrRoundDigit( OUStringBuffer& sStrg, short nPos, bool& bOverflow );
115 : BASIC_DLLPRIVATE void StrRoundDigit( OUStringBuffer& sStrg, short nPos );
116 : BASIC_DLLPRIVATE void ParseBack( OUStringBuffer& sStrg, const OUString& sFormatStrg,
117 : short nFormatPos );
118 : #ifdef _with_sprintf
119 : // Methods for string conversion with sprintf():
120 : BASIC_DLLPRIVATE void InitScan( double _dNum );
121 : BASIC_DLLPRIVATE void InitExp( double _dNewExp );
122 : BASIC_DLLPRIVATE short GetDigitAtPosScan( short nPos, bool& bFoundFirstDigit );
123 : BASIC_DLLPRIVATE short GetDigitAtPosExpScan( double dNewExponent, short nPos,
124 : bool& bFoundFirstDigit );
125 : BASIC_DLLPRIVATE short GetDigitAtPosExpScan( short nPos, bool& bFoundFirstDigit );
126 : #else
127 : // Methods for direct 'calculation' with log10() and pow():
128 : BASIC_DLLPRIVATE short GetDigitAtPos( double dNumber, short nPos, double& dNextNumber,
129 : bool& bFoundFirstDigit );
130 : BASIC_DLLPRIVATE short RoundDigit( double dNumber );
131 : #endif
132 : BASIC_DLLPRIVATE OUString GetPosFormatString( const OUString& sFormatStrg, bool & bFound );
133 : BASIC_DLLPRIVATE OUString GetNegFormatString( const OUString& sFormatStrg, bool & bFound );
134 : BASIC_DLLPRIVATE OUString Get0FormatString( const OUString& sFormatStrg, bool & bFound );
135 : BASIC_DLLPRIVATE OUString GetNullFormatString( const OUString& sFormatStrg, bool & bFound );
136 : BASIC_DLLPRIVATE short AnalyseFormatString( const OUString& sFormatStrg,
137 : short& nNoOfDigitsLeft, short& nNoOfDigitsRight,
138 : short& nNoOfOptionalDigitsLeft,
139 : short& nNoOfExponentDigits,
140 : short& nNoOfOptionalExponentDigits,
141 : bool& bPercent, bool& bCurrency, bool& bScientific,
142 : bool& bGenerateThousandSeparator,
143 : short& nMultipleThousandSeparators );
144 : BASIC_DLLPRIVATE void ScanFormatString( double dNumber, const OUString& sFormatStrg,
145 : OUString& sReturnStrg, bool bCreateSign );
146 :
147 : //*** Data ***
148 : sal_Unicode cDecPoint; // sign for the decimal point
149 : sal_Unicode cThousandSep; // sign for thousand delimiter
150 : // Text for output:
151 : OUString sOnStrg;
152 : OUString sOffStrg;
153 : OUString sYesStrg;
154 : OUString sNoStrg;
155 : OUString sTrueStrg;
156 : OUString sFalseStrg;
157 : OUString sCurrencyStrg;
158 : OUString sCurrencyFormatStrg;
159 :
160 : //*** temporary data for scan loop ***
161 :
162 : // String containing the number in scientific format
163 : OUString sSciNumStrg;
164 : // String containing the exponent of the number
165 : OUString sNumExpStrg;
166 : double dNum; // the number that is scanned
167 : short nNumExp; // the exponent of the number
168 : short nExpExp; // the number of digits in the exponent
169 : };
170 :
171 : #endif
172 :
173 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|