Bump version to 21.06.18.1
[LibreOffice.git] / sw / inc / calc.hxx
blobdee45a3cbc1da09fbea05dfa367e6cca9e72f044
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_SW_INC_CALC_HXX
21 #define INCLUDED_SW_INC_CALC_HXX
23 #include <memory>
24 #include <vector>
25 #include <basic/sbxvar.hxx>
26 #include <unotools/syslocale.hxx>
27 #include <rtl/ustrbuf.hxx>
28 #include <tools/solar.h>
29 #include <tools/long.hxx>
30 #include "swdllapi.h"
32 class CharClass;
33 class LocaleDataWrapper;
34 class SwFieldType;
35 class SwDoc;
36 class SwUserFieldType;
38 #define TBLSZ 47 // should be a prime, because of hash table
40 const sal_Unicode cListDelim = '|';
42 enum SwCalcOper
44 CALC_NAME, CALC_NUMBER, CALC_ENDCALC,
45 CALC_PLUS='+', CALC_MINUS='-', CALC_MUL='*',
46 CALC_DIV='/', CALC_PRINT=';', CALC_ASSIGN='=',
47 CALC_LP='(', CALC_RP=')', CALC_PHD='%',
48 CALC_POW='^',
49 CALC_NOT=256, CALC_AND=257, CALC_OR=258,
50 CALC_XOR=259, CALC_EQ=260, CALC_NEQ=261,
51 CALC_LEQ=262, CALC_GEQ=263, CALC_LES=264,
52 CALC_GRE=265, CALC_SUM=266, CALC_MEAN=267,
53 CALC_SQRT=268, CALC_MIN=269, CALC_MIN_IN=270,
54 CALC_MAX=271, CALC_MAX_IN=272, CALC_SIN=273,
55 CALC_COS=274, CALC_TAN=275, CALC_ASIN=276,
56 CALC_ACOS=278, CALC_ATAN=279, CALC_TDIF=280,
57 CALC_ROUND=281, CALC_DATE=282, CALC_MONTH=283,
58 CALC_DAY=284, CALC_PRODUCT=285, CALC_AVERAGE=286,
59 CALC_COUNT=287, CALC_SIGN=288, CALC_ABS=289
62 // Calculate Operations Strings
63 extern const char sCalc_Add[];
64 extern const char sCalc_Sub[];
65 extern const char sCalc_Mul[];
66 extern const char sCalc_Div[];
67 extern const char sCalc_Phd[];
68 extern const char sCalc_Sqrt[];
69 extern const char sCalc_Pow[];
70 extern const char sCalc_Or[];
71 extern const char sCalc_Xor[];
72 extern const char sCalc_And[];
73 extern const char sCalc_Not[];
74 extern const char sCalc_Eq[];
75 extern const char sCalc_Neq[];
76 extern const char sCalc_Leq[];
77 extern const char sCalc_Geq[];
78 extern const char sCalc_L[];
79 extern const char sCalc_G[];
80 extern const char sCalc_Sum[];
81 extern const char sCalc_Mean[];
82 extern const char sCalc_Average[];
83 extern const char sCalc_Product[];
84 extern const char sCalc_Count[];
85 extern const char sCalc_Min[];
86 extern const char sCalc_Max[];
87 extern const char sCalc_Sin[];
88 extern const char sCalc_Cos[];
89 extern const char sCalc_Tan[];
90 extern const char sCalc_Asin[];
91 extern const char sCalc_Acos[];
92 extern const char sCalc_Atan[];
93 extern const char sCalc_Round[];
94 extern const char sCalc_Date[];
95 extern const char sCalc_Sign[];
96 extern const char sCalc_Abs[];
98 // Calculate ErrorCodes
99 enum class SwCalcError
101 NONE=0,
102 NaN, // not a number (not an error, used for interoperability)
103 Syntax, // syntax error
104 DivByZero, // division by zero
105 FaultyBrackets, // faulty brackets
106 OverflowInPower, // overflow in power function
107 Overflow, // overflow
110 class SwSbxValue final : public SbxValue
112 bool m_bVoid;
113 bool m_bDBvalue;
114 public:
115 // always default to a number. otherwise it will become a SbxEMPTY
116 SwSbxValue( tools::Long n = 0 ) : m_bVoid(false), m_bDBvalue(false) { PutLong( n ); }
117 SwSbxValue( const double& rD ) : m_bVoid(false), m_bDBvalue(false) { PutDouble( rD ); }
119 bool GetBool() const;
120 double GetDouble() const;
121 SwSbxValue& MakeDouble();
123 bool IsVoidValue() const {return m_bVoid;}
124 void SetVoidValue(bool bSet) {m_bVoid = bSet;}
126 bool IsDBvalue() const {return m_bDBvalue;}
127 void SetDBvalue(bool bSet) {m_bDBvalue = bSet;}
130 // Calculate HashTables for VarTable and Operations
131 struct SwHash
133 SwHash( const OUString& rStr );
134 virtual ~SwHash();
135 OUString aStr;
136 std::unique_ptr<SwHash> pNext;
139 struct SwCalcExp final : public SwHash
141 SwSbxValue nValue;
142 const SwFieldType* pFieldType;
144 SwCalcExp( const OUString& rStr, const SwSbxValue& rVal,
145 const SwFieldType* pFieldType );
148 /// T should be a subclass of SwHash
149 template<class T>
150 class SwHashTable
152 std::vector<std::unique_ptr<T>> m_aData;
153 public:
154 SwHashTable(size_t nSize) : m_aData(nSize) {}
155 std::unique_ptr<T> & operator[](size_t idx) { return m_aData[idx]; }
156 std::unique_ptr<T> const & operator[](size_t idx) const { return m_aData[idx]; }
157 void resize(size_t nSize) { m_aData.resize(nSize); }
159 T* Find( const OUString& rStr, sal_uInt16* pPos = nullptr ) const
161 size_t nTableSize = m_aData.size();
162 sal_uLong ii = 0;
163 for( sal_Int32 n = 0; n < rStr.getLength(); ++n )
165 ii = ii << 1 ^ rStr[n];
167 ii %= nTableSize;
169 if( pPos )
170 *pPos = static_cast<sal_uInt16>(ii);
172 for( T* pEntry = m_aData[ii].get(); pEntry; pEntry = static_cast<T*>(pEntry->pNext.get()) )
174 if( rStr == pEntry->aStr )
176 return pEntry;
179 return nullptr;
185 // if CalcOp != 0, this is a valid operator
186 struct CalcOp;
187 CalcOp* FindOperator( const OUString& rSearch );
189 extern "C" typedef double (*pfCalc)(double);
191 class SwCalc
193 SwHashTable<SwCalcExp> m_aVarTable;
194 OUStringBuffer m_aVarName;
195 OUString m_sCurrSym;
196 OUString m_sCommand;
197 std::vector<const SwUserFieldType*> m_aRekurStack;
198 SwSbxValue m_nLastLeft;
199 SwSbxValue m_nNumberValue;
200 SwCalcExp m_aErrExpr;
201 sal_Int32 m_nCommandPos;
203 SwDoc& m_rDoc;
204 SvtSysLocale m_aSysLocale;
205 const LocaleDataWrapper* m_pLocaleDataWrapper;
206 CharClass* m_pCharClass;
208 sal_uInt16 m_nListPor;
209 bool m_bHasNumber; // fix COUNT() and AVERAGE(), if all cells are NaN
210 SwCalcOper m_eCurrOper;
211 SwCalcOper m_eCurrListOper;
212 SwCalcError m_eError;
214 SwCalcOper GetToken();
215 SwSbxValue Expr();
216 SwSbxValue Term();
217 SwSbxValue PrimFunc(bool &rChkPow);
218 SwSbxValue Prim();
219 SwSbxValue StdFunc(pfCalc pFnc, bool bChkTrig);
221 static OUString GetColumnName( const OUString& rName );
222 OUString GetDBName( const OUString& rName );
224 SwCalc( const SwCalc& ) = delete;
225 SwCalc& operator=( const SwCalc& ) = delete;
227 public:
228 SwCalc(SwDoc& rD);
229 ~SwCalc() COVERITY_NOEXCEPT_FALSE;
231 SwSbxValue Calculate( const OUString &rStr );
232 OUString GetStrResult( const SwSbxValue& rValue );
233 OUString GetStrResult( double );
235 SwCalcExp* VarInsert( const OUString& r );
236 SwCalcExp* VarLook( const OUString &rStr, bool bIns = false );
237 void VarChange( const OUString& rStr, const SwSbxValue& rValue );
238 void VarChange( const OUString& rStr, double );
239 SwHashTable<SwCalcExp> & GetVarTable() { return m_aVarTable; }
241 bool Push(const SwUserFieldType* pUserFieldType);
242 void Pop();
243 CharClass* GetCharClass();
245 void SetCalcError( SwCalcError eErr ) { m_eError = eErr; }
246 bool IsCalcError() const { return SwCalcError::NONE != m_eError && SwCalcError::NaN != m_eError; }
247 bool IsCalcNotANumber() const { return SwCalcError::NaN == m_eError; }
249 static bool Str2Double( const OUString& rStr, sal_Int32& rPos,
250 double& rVal );
251 static bool Str2Double( const OUString& rStr, sal_Int32& rPos,
252 double& rVal, SwDoc const *const pDoc );
254 SW_DLLPUBLIC static bool IsValidVarName( const OUString& rStr,
255 OUString* pValidName = nullptr );
258 #endif
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */