merged tag ooo/OOO330_m14
[LibreOffice.git] / sw / inc / calc.hxx
blobf5cbcc6c8c3857cd5040c229f24d0c89a155ccb6
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
27 #ifndef _CALC_HXX
28 #define _CALC_HXX
30 #include <svl/svarray.hxx>
32 #ifndef __SBX_SBXVALUE //autogen
33 #include <basic/sbxvar.hxx>
34 #endif
35 #include "swdllapi.h"
37 class CharClass;
38 class LocaleDataWrapper;
39 class SwFieldType;
40 class SwDoc;
42 #define TBLSZ 47 // sollte Primzahl sein, wegen HashTable
44 const sal_Unicode cListDelim = '|';
46 /******************************************************************************
47 * Calculate Operations
48 ******************************************************************************/
49 enum SwCalcOper
51 CALC_NAME, CALC_NUMBER, CALC_ENDCALC,
52 CALC_PLUS='+', CALC_MINUS='-', CALC_MUL='*',
53 CALC_DIV='/', CALC_PRINT=';', CALC_ASSIGN='=',
54 CALC_LP='(', CALC_RP=')', CALC_PHD='%',
55 CALC_POW='^',
56 CALC_LISTOP = cListDelim,
57 CALC_NOT=256, CALC_AND=257, CALC_OR=258,
58 CALC_XOR=259, CALC_EQ=260, CALC_NEQ=261,
59 CALC_LEQ=262, CALC_GEQ=263, CALC_LES=264,
60 CALC_GRE=265, CALC_SUM=266, CALC_MEAN=267,
61 CALC_SQRT=268, CALC_MIN=269, CALC_MIN_IN=270,
62 CALC_MAX=271, CALC_MAX_IN=272, CALC_SIN=273,
63 CALC_COS=274, CALC_TAN=275, CALC_ASIN=276,
64 CALC_ACOS=278, CALC_ATAN=279, CALC_TDIF=280,
65 CALC_ROUND=281, CALC_DATE=282, CALC_MONTH=283,
66 CALC_DAY=284
69 //-- Calculate Operations Strings -----------------------------------------
71 extern const sal_Char __FAR_DATA sCalc_Add[];
72 extern const sal_Char __FAR_DATA sCalc_Sub[];
73 extern const sal_Char __FAR_DATA sCalc_Mul[];
74 extern const sal_Char __FAR_DATA sCalc_Div[];
75 extern const sal_Char __FAR_DATA sCalc_Phd[];
76 extern const sal_Char __FAR_DATA sCalc_Sqrt[];
77 extern const sal_Char __FAR_DATA sCalc_Pow[];
78 extern const sal_Char __FAR_DATA sCalc_Or[];
79 extern const sal_Char __FAR_DATA sCalc_Xor[];
80 extern const sal_Char __FAR_DATA sCalc_And[];
81 extern const sal_Char __FAR_DATA sCalc_Not[];
82 extern const sal_Char __FAR_DATA sCalc_Eq[];
83 extern const sal_Char __FAR_DATA sCalc_Neq[];
84 extern const sal_Char __FAR_DATA sCalc_Leq[];
85 extern const sal_Char __FAR_DATA sCalc_Geq[];
86 extern const sal_Char __FAR_DATA sCalc_L[];
87 extern const sal_Char __FAR_DATA sCalc_G[];
88 extern const sal_Char __FAR_DATA sCalc_Sum[];
89 extern const sal_Char __FAR_DATA sCalc_Mean[];
90 extern const sal_Char __FAR_DATA sCalc_Min[];
91 extern const sal_Char __FAR_DATA sCalc_Max[];
92 extern const sal_Char __FAR_DATA sCalc_Sin[];
93 extern const sal_Char __FAR_DATA sCalc_Cos[];
94 extern const sal_Char __FAR_DATA sCalc_Tan[];
95 extern const sal_Char __FAR_DATA sCalc_Asin[];
96 extern const sal_Char __FAR_DATA sCalc_Acos[];
97 extern const sal_Char __FAR_DATA sCalc_Atan[];
98 extern const sal_Char __FAR_DATA sCalc_Tdif[];
99 extern const sal_Char __FAR_DATA sCalc_Round[];
100 extern const sal_Char __FAR_DATA sCalc_Date[];
102 /******************************************************************************
103 * Calculate ErrorCodes
104 ******************************************************************************/
105 enum SwCalcError
107 CALC_NOERR=0,
108 CALC_SYNTAX, // Syntax Fehler
109 CALC_ZERODIV, // Division durch Null
110 CALC_BRACK, // Fehlerhafte Klammerung
111 CALC_POWERR, // Ueberlauf in Quadratfunktion
112 CALC_VARNFND, // Variable wurde nicht gefunden
113 CALC_OVERFLOW, // Ueberlauf
114 CALC_WRONGTIME // falsches Zeitformat
117 class SwSbxValue : public SbxValue
119 bool bVoid;
120 public:
121 //JP 03.02.99: immer auf eine Zahl defaulten, damit auch gerechnet wird.
122 // Ansonsten wird daraus ein SbxEMPTY und damit ist nichts
123 // anzufangen.
124 SwSbxValue( long n = 0 ) : bVoid(false) { PutLong( n ); }
125 SwSbxValue( const double& rD ) : bVoid(false) { PutDouble( rD ); }
126 SwSbxValue( const SwSbxValue& rVal ) :
127 SvRefBase( rVal ),
128 SbxValue( rVal ),
129 bVoid(rVal.bVoid)
131 virtual ~SwSbxValue();
134 // Strings sonderbehandeln
135 BOOL GetBool() const;
136 // Strings sonderbehandeln BOOLs sonderbehandeln
137 double GetDouble() const;
138 SwSbxValue& MakeDouble();
140 bool IsVoidValue() {return bVoid;}
141 void SetVoidValue(bool bSet) {bVoid = bSet;}
144 /******************************************************************************
145 * Calculate HashTables fuer VarTable und Operations
146 ******************************************************************************/
147 struct SwHash
149 SwHash( const String& rStr );
150 virtual ~SwHash();
151 String aStr;
152 SwHash *pNext;
155 struct SwCalcExp : public SwHash
157 SwSbxValue nValue;
158 const SwFieldType* pFldType;
160 SwCalcExp( const String& rStr, const SwSbxValue& rVal,
161 const SwFieldType* pFldType = 0 );
164 SwHash* Find( const String& rSrch, SwHash** ppTable,
165 USHORT nTblSize, USHORT* pPos = 0 );
167 void DeleteHashTable( SwHash** ppTable, USHORT nTblSize );
169 // falls _CalcOp != 0, dann ist das ein gueltiger Operator
170 struct _CalcOp;
171 _CalcOp* FindOperator( const String& rSearch );
173 /******************************************************************************
174 * class SwCalc
175 ******************************************************************************/
176 class SwCalc
178 SwHash* VarTable[ TBLSZ ];
179 String aVarName, sCurrSym;
180 String sCommand;
181 SvPtrarr aRekurStk;
182 SwSbxValue nLastLeft;
183 SwSbxValue nNumberValue;
184 SwCalcExp aErrExpr;
185 xub_StrLen nCommandPos;
187 SwDoc& rDoc;
188 const LocaleDataWrapper* pLclData;
189 CharClass* pCharClass;
191 USHORT nListPor;
192 SwCalcOper eCurrOper;
193 SwCalcOper eCurrListOper;
194 SwCalcError eError;
197 SwCalcOper GetToken();
198 SwSbxValue Expr();
199 SwSbxValue Term();
200 SwSbxValue Prim();
202 BOOL ParseTime( USHORT*, USHORT*, USHORT* );
204 String GetColumnName( const String& rName );
205 String GetDBName( const String& rName );
207 // dont call this methods
208 SwCalc( const SwCalc& );
209 SwCalc& operator=( const SwCalc& );
211 public:
212 SwCalc( SwDoc& rD );
213 ~SwCalc();
215 SwSbxValue Calculate( const String &rStr );
216 String GetStrResult( const SwSbxValue& rValue, BOOL bRound = TRUE );
217 String GetStrResult( double, BOOL bRound = TRUE );
219 SwCalcExp* VarInsert( const String& r );
220 SwCalcExp* VarLook( const String &rStr, USHORT ins = 0 );
221 void VarChange( const String& rStr, const SwSbxValue& rValue );
222 void VarChange( const String& rStr, double );
223 SwHash** GetVarTable() { return VarTable; }
225 BOOL Push( const VoidPtr pPtr );
226 void Pop( const VoidPtr pPtr );
228 void SetCalcError( SwCalcError eErr ) { eError = eErr; }
229 BOOL IsCalcError() const { return 0 != eError; }
231 static BOOL Str2Double( const String& rStr, xub_StrLen& rPos,
232 double& rVal,
233 const LocaleDataWrapper* pData = 0 );
234 static BOOL Str2Double( const String& rStr, xub_StrLen& rPos,
235 double& rVal, SwDoc* pDoc );
237 SW_DLLPUBLIC static BOOL IsValidVarName( const String& rStr,
238 String* pValidName = 0 );
241 #endif