Bump version to 6.4-15
[LibreOffice.git] / include / basic / sbxform.hxx
blobb07a1945c7c7804ba053fc688dbb83ca54beb479
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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}
34 percent_char := '%'
35 special_char := \char | + | - | ( | ) | $ | space_char
36 char := all_ascii_chars
37 space_char := ' '
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:
44 "General Number"
45 "Currency"
46 "Fixed"
47 "Standard"
48 "Percent"
49 "Scientific"
50 "Yes/No"
51 "True/False"
52 "On/Off"
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
63 TODO:
64 - Date formatting
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 {
75 public:
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 )
90 Parameter:
91 dNumber : number to be formatted
92 sFormatStrg : the Format-String, e.g. ###0.0###
94 Return value:
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 );
102 private:
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,
109 short nFormatPos );
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 );
132 //*** Data ***
133 sal_Unicode cDecPoint; // sign for the decimal point
134 sal_Unicode cThousandSep; // sign for thousand delimiter
135 // Text for output:
136 OUString sOnStrg;
137 OUString sOffStrg;
138 OUString sYesStrg;
139 OUString sNoStrg;
140 OUString sTrueStrg;
141 OUString sFalseStrg;
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
156 #endif
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */