1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: calc.hxx,v $
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 ************************************************************************/
33 #include <svtools/svarray.hxx>
35 #ifndef __SBX_SBXVALUE //autogen
36 #include <basic/sbxvar.hxx>
41 class LocaleDataWrapper
;
45 #define TBLSZ 47 // sollte Primzahl sein, wegen HashTable
47 const sal_Unicode cListDelim
= '|';
49 /******************************************************************************
50 * Calculate Operations
51 ******************************************************************************/
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
='%',
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,
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 ******************************************************************************/
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
124 //JP 03.02.99: immer auf eine Zahl defaulten, damit auch gerechnet wird.
125 // Ansonsten wird daraus ein SbxEMPTY und damit ist nichts
127 SwSbxValue( long n
= 0 ) : bVoid(false) { PutLong( n
); }
128 SwSbxValue( const double& rD
) : bVoid(false) { PutDouble( rD
); }
129 SwSbxValue( const SwSbxValue
& rVal
) :
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 ******************************************************************************/
152 SwHash( const String
& rStr
);
158 struct SwCalcExp
: public SwHash
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
174 _CalcOp
* FindOperator( const String
& rSearch
);
176 /******************************************************************************
178 ******************************************************************************/
181 SwHash
* VarTable
[ TBLSZ
];
182 String aVarName
, sCurrSym
;
185 SwSbxValue nLastLeft
;
186 SwSbxValue nNumberValue
;
188 xub_StrLen nCommandPos
;
191 LocaleDataWrapper
* pLclData
;
192 CharClass
* pCharClass
;
195 SwCalcOper eCurrOper
;
196 SwCalcOper eCurrListOper
;
200 SwCalcOper
GetToken();
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
& );
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
,
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 );