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