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