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_SW_INC_CALC_HXX
21 : #define INCLUDED_SW_INC_CALC_HXX
22 :
23 : #include <vector>
24 : #include <basic/sbxvar.hxx>
25 : #include <unotools/syslocale.hxx>
26 : #include "swdllapi.h"
27 :
28 : class CharClass;
29 : class LocaleDataWrapper;
30 : class SwFieldType;
31 : class SwDoc;
32 : class SwUserFieldType;
33 :
34 : #define TBLSZ 47 // should be a prime, because of hash table
35 :
36 : const sal_Unicode cListDelim = '|';
37 :
38 : /******************************************************************************
39 : * Calculate Operations
40 : ******************************************************************************/
41 : enum SwCalcOper
42 : {
43 : CALC_NAME, CALC_NUMBER, CALC_ENDCALC,
44 : CALC_PLUS='+', CALC_MINUS='-', CALC_MUL='*',
45 : CALC_DIV='/', CALC_PRINT=';', CALC_ASSIGN='=',
46 : CALC_LP='(', CALC_RP=')', CALC_PHD='%',
47 : CALC_POW='^',
48 : CALC_LISTOP = cListDelim,
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
59 : };
60 :
61 : //-- Calculate Operations Strings -----------------------------------------
62 :
63 : extern const sal_Char sCalc_Add[];
64 : extern const sal_Char sCalc_Sub[];
65 : extern const sal_Char sCalc_Mul[];
66 : extern const sal_Char sCalc_Div[];
67 : extern const sal_Char sCalc_Phd[];
68 : extern const sal_Char sCalc_Sqrt[];
69 : extern const sal_Char sCalc_Pow[];
70 : extern const sal_Char sCalc_Or[];
71 : extern const sal_Char sCalc_Xor[];
72 : extern const sal_Char sCalc_And[];
73 : extern const sal_Char sCalc_Not[];
74 : extern const sal_Char sCalc_Eq[];
75 : extern const sal_Char sCalc_Neq[];
76 : extern const sal_Char sCalc_Leq[];
77 : extern const sal_Char sCalc_Geq[];
78 : extern const sal_Char sCalc_L[];
79 : extern const sal_Char sCalc_G[];
80 : extern const sal_Char sCalc_Sum[];
81 : extern const sal_Char sCalc_Mean[];
82 : extern const sal_Char sCalc_Min[];
83 : extern const sal_Char sCalc_Max[];
84 : extern const sal_Char sCalc_Sin[];
85 : extern const sal_Char sCalc_Cos[];
86 : extern const sal_Char sCalc_Tan[];
87 : extern const sal_Char sCalc_Asin[];
88 : extern const sal_Char sCalc_Acos[];
89 : extern const sal_Char sCalc_Atan[];
90 : extern const sal_Char sCalc_Tdif[];
91 : extern const sal_Char sCalc_Round[];
92 : extern const sal_Char sCalc_Date[];
93 :
94 : /******************************************************************************
95 : * Calculate ErrorCodes
96 : ******************************************************************************/
97 : enum SwCalcError
98 : {
99 : CALC_NOERR=0,
100 : CALC_SYNTAX, // syntax error
101 : CALC_ZERODIV, // division by zero
102 : CALC_BRACK, // faulty brackets
103 : CALC_POWERR, // overflow in power function
104 : CALC_VARNFND, // variable was not found
105 : CALC_OVERFLOW, // overflow
106 : CALC_WRONGTIME // wrong time format
107 : };
108 :
109 0 : class SwSbxValue : public SbxValue
110 : {
111 : bool bVoid;
112 : public:
113 : // always default to a number. otherwise it will become a SbxEMPTY
114 0 : SwSbxValue( long n = 0 ) : bVoid(false) { PutLong( n ); }
115 0 : SwSbxValue( const double& rD ) : bVoid(false) { PutDouble( rD ); }
116 0 : SwSbxValue( const SwSbxValue& rVal ) :
117 : SvRefBase( rVal ),
118 : SbxValue( rVal ),
119 0 : bVoid(rVal.bVoid)
120 0 : {}
121 : virtual ~SwSbxValue();
122 :
123 : sal_Bool GetBool() const;
124 : double GetDouble() const;
125 : SwSbxValue& MakeDouble();
126 :
127 0 : bool IsVoidValue() {return bVoid;}
128 0 : void SetVoidValue(bool bSet) {bVoid = bSet;}
129 : };
130 :
131 : /******************************************************************************
132 : * Calculate HashTables for VarTable und Operations
133 : ******************************************************************************/
134 : struct SwHash
135 : {
136 : SwHash( const OUString& rStr );
137 : virtual ~SwHash();
138 : OUString aStr;
139 : SwHash *pNext;
140 : };
141 :
142 0 : struct SwCalcExp : public SwHash
143 : {
144 : SwSbxValue nValue;
145 : const SwFieldType* pFldType;
146 :
147 : SwCalcExp( const OUString& rStr, const SwSbxValue& rVal,
148 : const SwFieldType* pFldType = 0 );
149 : };
150 :
151 : SwHash* Find( const OUString& rSrch, SwHash** ppTable,
152 : sal_uInt16 nTblSize, sal_uInt16* pPos = 0 );
153 :
154 : void DeleteHashTable( SwHash** ppTable, sal_uInt16 nTblSize );
155 :
156 : // if _CalcOp != 0, this is a valid operator
157 : struct _CalcOp;
158 : _CalcOp* FindOperator( const OUString& rSearch );
159 :
160 : /******************************************************************************
161 : * class SwCalc
162 : ******************************************************************************/
163 : class SwCalc
164 : {
165 : SwHash* VarTable[ TBLSZ ];
166 : OUString aVarName, sCurrSym;
167 : OUString sCommand;
168 : std::vector<const SwUserFieldType*> aRekurStk;
169 : SwSbxValue nLastLeft;
170 : SwSbxValue nNumberValue;
171 : SwCalcExp aErrExpr;
172 : sal_Int32 nCommandPos;
173 :
174 : SwDoc& rDoc;
175 : SvtSysLocale m_aSysLocale;
176 : const LocaleDataWrapper* pLclData;
177 : CharClass* pCharClass;
178 :
179 : sal_uInt16 nListPor;
180 : SwCalcOper eCurrOper;
181 : SwCalcOper eCurrListOper;
182 : SwCalcError eError;
183 :
184 : SwCalcOper GetToken();
185 : SwSbxValue Expr();
186 : SwSbxValue Term();
187 : SwSbxValue Prim();
188 :
189 : sal_Bool ParseTime( sal_uInt16*, sal_uInt16*, sal_uInt16* );
190 :
191 : OUString GetColumnName( const OUString& rName );
192 : OUString GetDBName( const OUString& rName );
193 :
194 : // dont call this methods
195 : SwCalc( const SwCalc& );
196 : SwCalc& operator=( const SwCalc& );
197 :
198 : public:
199 : SwCalc( SwDoc& rD );
200 : ~SwCalc();
201 :
202 : SwSbxValue Calculate( const OUString &rStr );
203 : OUString GetStrResult( const SwSbxValue& rValue, sal_Bool bRound = sal_True );
204 : OUString GetStrResult( double, sal_Bool bRound = sal_True );
205 :
206 : SwCalcExp* VarInsert( const OUString& r );
207 : SwCalcExp* VarLook( const OUString &rStr, sal_uInt16 ins = 0 );
208 : void VarChange( const OUString& rStr, const SwSbxValue& rValue );
209 : void VarChange( const OUString& rStr, double );
210 0 : SwHash** GetVarTable() { return VarTable; }
211 :
212 : bool Push(const SwUserFieldType* pUserFieldType);
213 : void Pop();
214 :
215 0 : void SetCalcError( SwCalcError eErr ) { eError = eErr; }
216 0 : sal_Bool IsCalcError() const { return 0 != eError; }
217 :
218 : static bool Str2Double( const OUString& rStr, sal_Int32& rPos,
219 : double& rVal,
220 : LocaleDataWrapper const*const pData = 0 );
221 : static bool Str2Double( const OUString& rStr, sal_Int32& rPos,
222 : double& rVal, SwDoc *const pDoc );
223 :
224 : SW_DLLPUBLIC static sal_Bool IsValidVarName( const OUString& rStr,
225 : OUString* pValidName = 0 );
226 : };
227 :
228 : #endif
229 :
230 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|