Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef SW_CALC_HXX
30 : : #define SW_CALC_HXX
31 : :
32 : : #include <vector>
33 : :
34 : : #include <unotools/syslocale.hxx>
35 : :
36 : : #include <basic/sbxvar.hxx>
37 : :
38 : : #include "swdllapi.h"
39 : :
40 : : class CharClass;
41 : : class LocaleDataWrapper;
42 : : class SwFieldType;
43 : : class SwDoc;
44 : : class SwUserFieldType;
45 : :
46 : : #define TBLSZ 47 // should be a prime, because of hash table
47 : :
48 : : const sal_Unicode cListDelim = '|';
49 : :
50 : : /******************************************************************************
51 : : * Calculate Operations
52 : : ******************************************************************************/
53 : : enum SwCalcOper
54 : : {
55 : : CALC_NAME, CALC_NUMBER, CALC_ENDCALC,
56 : : CALC_PLUS='+', CALC_MINUS='-', CALC_MUL='*',
57 : : CALC_DIV='/', CALC_PRINT=';', CALC_ASSIGN='=',
58 : : CALC_LP='(', CALC_RP=')', CALC_PHD='%',
59 : : CALC_POW='^',
60 : : CALC_LISTOP = cListDelim,
61 : : CALC_NOT=256, CALC_AND=257, CALC_OR=258,
62 : : CALC_XOR=259, CALC_EQ=260, CALC_NEQ=261,
63 : : CALC_LEQ=262, CALC_GEQ=263, CALC_LES=264,
64 : : CALC_GRE=265, CALC_SUM=266, CALC_MEAN=267,
65 : : CALC_SQRT=268, CALC_MIN=269, CALC_MIN_IN=270,
66 : : CALC_MAX=271, CALC_MAX_IN=272, CALC_SIN=273,
67 : : CALC_COS=274, CALC_TAN=275, CALC_ASIN=276,
68 : : CALC_ACOS=278, CALC_ATAN=279, CALC_TDIF=280,
69 : : CALC_ROUND=281, CALC_DATE=282, CALC_MONTH=283,
70 : : CALC_DAY=284
71 : : };
72 : :
73 : : //-- Calculate Operations Strings -----------------------------------------
74 : :
75 : : extern const sal_Char sCalc_Add[];
76 : : extern const sal_Char sCalc_Sub[];
77 : : extern const sal_Char sCalc_Mul[];
78 : : extern const sal_Char sCalc_Div[];
79 : : extern const sal_Char sCalc_Phd[];
80 : : extern const sal_Char sCalc_Sqrt[];
81 : : extern const sal_Char sCalc_Pow[];
82 : : extern const sal_Char sCalc_Or[];
83 : : extern const sal_Char sCalc_Xor[];
84 : : extern const sal_Char sCalc_And[];
85 : : extern const sal_Char sCalc_Not[];
86 : : extern const sal_Char sCalc_Eq[];
87 : : extern const sal_Char sCalc_Neq[];
88 : : extern const sal_Char sCalc_Leq[];
89 : : extern const sal_Char sCalc_Geq[];
90 : : extern const sal_Char sCalc_L[];
91 : : extern const sal_Char sCalc_G[];
92 : : extern const sal_Char sCalc_Sum[];
93 : : extern const sal_Char sCalc_Mean[];
94 : : extern const sal_Char sCalc_Min[];
95 : : extern const sal_Char sCalc_Max[];
96 : : extern const sal_Char sCalc_Sin[];
97 : : extern const sal_Char sCalc_Cos[];
98 : : extern const sal_Char sCalc_Tan[];
99 : : extern const sal_Char sCalc_Asin[];
100 : : extern const sal_Char sCalc_Acos[];
101 : : extern const sal_Char sCalc_Atan[];
102 : : extern const sal_Char sCalc_Tdif[];
103 : : extern const sal_Char sCalc_Round[];
104 : : extern const sal_Char sCalc_Date[];
105 : :
106 : : /******************************************************************************
107 : : * Calculate ErrorCodes
108 : : ******************************************************************************/
109 : : enum SwCalcError
110 : : {
111 : : CALC_NOERR=0,
112 : : CALC_SYNTAX, // syntax error
113 : : CALC_ZERODIV, // division by zero
114 : : CALC_BRACK, // faulty brackets
115 : : CALC_POWERR, // overflow in power function
116 : : CALC_VARNFND, // variable was not found
117 : : CALC_OVERFLOW, // overflow
118 : : CALC_WRONGTIME // wrong time format
119 : : };
120 : :
121 : 102 : class SwSbxValue : public SbxValue
122 : : {
123 : : bool bVoid;
124 : : public:
125 : : // always default to a number. otherwise it will become a SbxEMPTY
126 [ + - ][ + - ]: 180 : SwSbxValue( long n = 0 ) : bVoid(false) { PutLong( n ); }
127 [ # # ][ # # ]: 0 : SwSbxValue( const double& rD ) : bVoid(false) { PutDouble( rD ); }
128 : 654 : SwSbxValue( const SwSbxValue& rVal ) :
129 : : SvRefBase( rVal ),
130 : : SbxValue( rVal ),
131 [ + - ]: 654 : bVoid(rVal.bVoid)
132 : 654 : {}
133 : : virtual ~SwSbxValue();
134 : :
135 : :
136 : : sal_Bool GetBool() const;
137 : : double GetDouble() const;
138 : : SwSbxValue& MakeDouble();
139 : :
140 : 9 : bool IsVoidValue() {return bVoid;}
141 : 42 : void SetVoidValue(bool bSet) {bVoid = bSet;}
142 : : };
143 : :
144 : : /******************************************************************************
145 : : * Calculate HashTables for VarTable und Operations
146 : : ******************************************************************************/
147 : : struct SwHash
148 : : {
149 : : SwHash( const String& rStr );
150 : : virtual ~SwHash();
151 : : String aStr;
152 : : SwHash *pNext;
153 : : };
154 : :
155 [ + - ][ - + ]: 1284 : struct SwCalcExp : public SwHash
156 : : {
157 : : SwSbxValue nValue;
158 : : const SwFieldType* pFldType;
159 : :
160 : : SwCalcExp( const String& rStr, const SwSbxValue& rVal,
161 : : const SwFieldType* pFldType = 0 );
162 : : };
163 : :
164 : : SwHash* Find( const String& rSrch, SwHash** ppTable,
165 : : sal_uInt16 nTblSize, sal_uInt16* pPos = 0 );
166 : :
167 : : void DeleteHashTable( SwHash** ppTable, sal_uInt16 nTblSize );
168 : :
169 : : // if _CalcOp != 0, this is a valid operator
170 : : struct _CalcOp;
171 : : _CalcOp* FindOperator( const String& rSearch );
172 : :
173 : : /******************************************************************************
174 : : * class SwCalc
175 : : ******************************************************************************/
176 : : class SwCalc
177 : : {
178 : : SwHash* VarTable[ TBLSZ ];
179 : : String aVarName, sCurrSym;
180 : : String sCommand;
181 : : std::vector<const SwUserFieldType*> aRekurStk;
182 : : SwSbxValue nLastLeft;
183 : : SwSbxValue nNumberValue;
184 : : SwCalcExp aErrExpr;
185 : : xub_StrLen nCommandPos;
186 : :
187 : : SwDoc& rDoc;
188 : : SvtSysLocale m_aSysLocale;
189 : : const LocaleDataWrapper* pLclData;
190 : : CharClass* pCharClass;
191 : :
192 : : sal_uInt16 nListPor;
193 : : SwCalcOper eCurrOper;
194 : : SwCalcOper eCurrListOper;
195 : : SwCalcError eError;
196 : :
197 : :
198 : : SwCalcOper GetToken();
199 : : SwSbxValue Expr();
200 : : SwSbxValue Term();
201 : : SwSbxValue Prim();
202 : :
203 : : sal_Bool ParseTime( sal_uInt16*, sal_uInt16*, sal_uInt16* );
204 : :
205 : : String GetColumnName( const String& rName );
206 : : String GetDBName( const String& rName );
207 : :
208 : : // dont call this methods
209 : : SwCalc( const SwCalc& );
210 : : SwCalc& operator=( const SwCalc& );
211 : :
212 : : public:
213 : : SwCalc( SwDoc& rD );
214 : : ~SwCalc();
215 : :
216 : : SwSbxValue Calculate( const String &rStr );
217 : : String GetStrResult( const SwSbxValue& rValue, sal_Bool bRound = sal_True );
218 : : String GetStrResult( double, sal_Bool bRound = sal_True );
219 : :
220 : : SwCalcExp* VarInsert( const String& r );
221 : : SwCalcExp* VarLook( const String &rStr, sal_uInt16 ins = 0 );
222 : : void VarChange( const String& rStr, const SwSbxValue& rValue );
223 : : void VarChange( const String& rStr, double );
224 : 0 : SwHash** GetVarTable() { return VarTable; }
225 : :
226 : : bool Push(const SwUserFieldType* pUserFieldType);
227 : : void Pop();
228 : :
229 : 0 : void SetCalcError( SwCalcError eErr ) { eError = eErr; }
230 : 0 : sal_Bool IsCalcError() const { return 0 != eError; }
231 : :
232 : : static bool Str2Double( const String& rStr, xub_StrLen& rPos,
233 : : double& rVal,
234 : : LocaleDataWrapper const*const pData = 0 );
235 : : static bool Str2Double( const String& rStr, xub_StrLen& rPos,
236 : : double& rVal, SwDoc *const pDoc );
237 : :
238 : : SW_DLLPUBLIC static sal_Bool IsValidVarName( const String& rStr,
239 : : String* pValidName = 0 );
240 : : };
241 : :
242 : : #endif
243 : :
244 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|