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