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_SW_INC_CALC_HXX
21 #define INCLUDED_SW_INC_CALC_HXX
25 #include <basic/sbxvar.hxx>
26 #include <unotools/syslocale.hxx>
30 class LocaleDataWrapper
;
33 class SwUserFieldType
;
35 #define TBLSZ 47 // should be a prime, because of hash table
37 const sal_Unicode cListDelim
= '|';
41 CALC_NAME
, CALC_NUMBER
, CALC_ENDCALC
,
42 CALC_PLUS
='+', CALC_MINUS
='-', CALC_MUL
='*',
43 CALC_DIV
='/', CALC_PRINT
=';', CALC_ASSIGN
='=',
44 CALC_LP
='(', CALC_RP
=')', CALC_PHD
='%',
46 CALC_NOT
=256, CALC_AND
=257, CALC_OR
=258,
47 CALC_XOR
=259, CALC_EQ
=260, CALC_NEQ
=261,
48 CALC_LEQ
=262, CALC_GEQ
=263, CALC_LES
=264,
49 CALC_GRE
=265, CALC_SUM
=266, CALC_MEAN
=267,
50 CALC_SQRT
=268, CALC_MIN
=269, CALC_MIN_IN
=270,
51 CALC_MAX
=271, CALC_MAX_IN
=272, CALC_SIN
=273,
52 CALC_COS
=274, CALC_TAN
=275, CALC_ASIN
=276,
53 CALC_ACOS
=278, CALC_ATAN
=279, CALC_TDIF
=280,
54 CALC_ROUND
=281, CALC_DATE
=282, CALC_MONTH
=283,
58 // Calculate Operations Strings
59 extern const sal_Char sCalc_Add
[];
60 extern const sal_Char sCalc_Sub
[];
61 extern const sal_Char sCalc_Mul
[];
62 extern const sal_Char sCalc_Div
[];
63 extern const sal_Char sCalc_Phd
[];
64 extern const sal_Char sCalc_Sqrt
[];
65 extern const sal_Char sCalc_Pow
[];
66 extern const sal_Char sCalc_Or
[];
67 extern const sal_Char sCalc_Xor
[];
68 extern const sal_Char sCalc_And
[];
69 extern const sal_Char sCalc_Not
[];
70 extern const sal_Char sCalc_Eq
[];
71 extern const sal_Char sCalc_Neq
[];
72 extern const sal_Char sCalc_Leq
[];
73 extern const sal_Char sCalc_Geq
[];
74 extern const sal_Char sCalc_L
[];
75 extern const sal_Char sCalc_G
[];
76 extern const sal_Char sCalc_Sum
[];
77 extern const sal_Char sCalc_Mean
[];
78 extern const sal_Char sCalc_Min
[];
79 extern const sal_Char sCalc_Max
[];
80 extern const sal_Char sCalc_Sin
[];
81 extern const sal_Char sCalc_Cos
[];
82 extern const sal_Char sCalc_Tan
[];
83 extern const sal_Char sCalc_Asin
[];
84 extern const sal_Char sCalc_Acos
[];
85 extern const sal_Char sCalc_Atan
[];
86 extern const sal_Char sCalc_Tdif
[];
87 extern const sal_Char sCalc_Round
[];
88 extern const sal_Char sCalc_Date
[];
90 // Calculate ErrorCodes
91 enum class SwCalcError
94 Syntax
, // syntax error
95 DivByZero
, // division by zero
96 FaultyBrackets
, // faulty brackets
97 OverflowInPower
, // overflow in power function
101 class SwSbxValue
: public SbxValue
106 // always default to a number. otherwise it will become a SbxEMPTY
107 SwSbxValue( long n
= 0 ) : bVoid(false), bDBvalue(false) { PutLong( n
); }
108 SwSbxValue( const double& rD
) : bVoid(false), bDBvalue(false) { PutDouble( rD
); }
110 bool GetBool() const;
111 double GetDouble() const;
112 SwSbxValue
& MakeDouble();
114 bool IsVoidValue() {return bVoid
;}
115 void SetVoidValue(bool bSet
) {bVoid
= bSet
;}
117 bool IsDBvalue() {return bDBvalue
;}
118 void SetDBvalue(bool bSet
) {bDBvalue
= bSet
;}
121 // Calculate HashTables for VarTable and Operations
124 SwHash( const OUString
& rStr
);
127 std::unique_ptr
<SwHash
> pNext
;
130 struct SwCalcExp
: public SwHash
133 const SwFieldType
* pFieldType
;
135 SwCalcExp( const OUString
& rStr
, const SwSbxValue
& rVal
,
136 const SwFieldType
* pFieldType
);
139 SwHash
* Find( const OUString
& rSrch
, SwHash
* const * ppTable
,
140 sal_uInt16 nTableSize
, sal_uInt16
* pPos
= nullptr );
142 void DeleteHashTable( SwHash
** ppTable
, sal_uInt16 nTableSize
);
144 // if CalcOp != 0, this is a valid operator
146 CalcOp
* FindOperator( const OUString
& rSearch
);
148 extern "C" typedef double (*pfCalc
)(double);
152 SwHash
* m_aVarTable
[ TBLSZ
];
153 OUString m_aVarName
, m_sCurrSym
;
155 std::vector
<const SwUserFieldType
*> m_aRekurStack
;
156 SwSbxValue m_nLastLeft
;
157 SwSbxValue m_nNumberValue
;
158 SwCalcExp m_aErrExpr
;
159 sal_Int32 m_nCommandPos
;
162 SvtSysLocale m_aSysLocale
;
163 const LocaleDataWrapper
* m_pLocaleDataWrapper
;
164 CharClass
* m_pCharClass
;
166 sal_uInt16 m_nListPor
;
167 SwCalcOper m_eCurrOper
;
168 SwCalcOper m_eCurrListOper
;
169 SwCalcError m_eError
;
171 SwCalcOper
GetToken();
174 SwSbxValue
PrimFunc(bool &rChkPow
);
176 SwSbxValue
StdFunc(pfCalc pFnc
, bool bChkTrig
);
178 static OUString
GetColumnName( const OUString
& rName
);
179 OUString
GetDBName( const OUString
& rName
);
181 SwCalc( const SwCalc
& ) = delete;
182 SwCalc
& operator=( const SwCalc
& ) = delete;
188 SwSbxValue
Calculate( const OUString
&rStr
);
189 OUString
GetStrResult( const SwSbxValue
& rValue
);
190 OUString
GetStrResult( double );
192 SwCalcExp
* VarInsert( const OUString
& r
);
193 SwCalcExp
* VarLook( const OUString
&rStr
, bool bIns
= false );
194 void VarChange( const OUString
& rStr
, const SwSbxValue
& rValue
);
195 void VarChange( const OUString
& rStr
, double );
196 SwHash
** GetVarTable() { return m_aVarTable
; }
198 bool Push(const SwUserFieldType
* pUserFieldType
);
201 void SetCalcError( SwCalcError eErr
) { m_eError
= eErr
; }
202 bool IsCalcError() const { return SwCalcError::NONE
!= m_eError
; }
204 static bool Str2Double( const OUString
& rStr
, sal_Int32
& rPos
,
206 static bool Str2Double( const OUString
& rStr
, sal_Int32
& rPos
,
207 double& rVal
, SwDoc
*const pDoc
);
209 SW_DLLPUBLIC
static bool IsValidVarName( const OUString
& rStr
,
210 OUString
* pValidName
= nullptr );
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */