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