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