1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
25 #include <unotools/syslocale.hxx>
27 #include <basic/sbxvar.hxx>
32 class LocaleDataWrapper
;
35 class SwUserFieldType
;
37 #define TBLSZ 47 // should be a prime, because of hash table
39 const sal_Unicode cListDelim
= '|';
41 /******************************************************************************
42 * Calculate Operations
43 ******************************************************************************/
46 CALC_NAME
, CALC_NUMBER
, CALC_ENDCALC
,
47 CALC_PLUS
='+', CALC_MINUS
='-', CALC_MUL
='*',
48 CALC_DIV
='/', CALC_PRINT
=';', CALC_ASSIGN
='=',
49 CALC_LP
='(', CALC_RP
=')', CALC_PHD
='%',
51 CALC_LISTOP
= cListDelim
,
52 CALC_NOT
=256, CALC_AND
=257, CALC_OR
=258,
53 CALC_XOR
=259, CALC_EQ
=260, CALC_NEQ
=261,
54 CALC_LEQ
=262, CALC_GEQ
=263, CALC_LES
=264,
55 CALC_GRE
=265, CALC_SUM
=266, CALC_MEAN
=267,
56 CALC_SQRT
=268, CALC_MIN
=269, CALC_MIN_IN
=270,
57 CALC_MAX
=271, CALC_MAX_IN
=272, CALC_SIN
=273,
58 CALC_COS
=274, CALC_TAN
=275, CALC_ASIN
=276,
59 CALC_ACOS
=278, CALC_ATAN
=279, CALC_TDIF
=280,
60 CALC_ROUND
=281, CALC_DATE
=282, CALC_MONTH
=283,
64 //-- Calculate Operations Strings -----------------------------------------
66 extern const sal_Char sCalc_Add
[];
67 extern const sal_Char sCalc_Sub
[];
68 extern const sal_Char sCalc_Mul
[];
69 extern const sal_Char sCalc_Div
[];
70 extern const sal_Char sCalc_Phd
[];
71 extern const sal_Char sCalc_Sqrt
[];
72 extern const sal_Char sCalc_Pow
[];
73 extern const sal_Char sCalc_Or
[];
74 extern const sal_Char sCalc_Xor
[];
75 extern const sal_Char sCalc_And
[];
76 extern const sal_Char sCalc_Not
[];
77 extern const sal_Char sCalc_Eq
[];
78 extern const sal_Char sCalc_Neq
[];
79 extern const sal_Char sCalc_Leq
[];
80 extern const sal_Char sCalc_Geq
[];
81 extern const sal_Char sCalc_L
[];
82 extern const sal_Char sCalc_G
[];
83 extern const sal_Char sCalc_Sum
[];
84 extern const sal_Char sCalc_Mean
[];
85 extern const sal_Char sCalc_Min
[];
86 extern const sal_Char sCalc_Max
[];
87 extern const sal_Char sCalc_Sin
[];
88 extern const sal_Char sCalc_Cos
[];
89 extern const sal_Char sCalc_Tan
[];
90 extern const sal_Char sCalc_Asin
[];
91 extern const sal_Char sCalc_Acos
[];
92 extern const sal_Char sCalc_Atan
[];
93 extern const sal_Char sCalc_Tdif
[];
94 extern const sal_Char sCalc_Round
[];
95 extern const sal_Char sCalc_Date
[];
97 /******************************************************************************
98 * Calculate ErrorCodes
99 ******************************************************************************/
103 CALC_SYNTAX
, // syntax error
104 CALC_ZERODIV
, // division by zero
105 CALC_BRACK
, // faulty brackets
106 CALC_POWERR
, // overflow in power function
107 CALC_VARNFND
, // variable was not found
108 CALC_OVERFLOW
, // overflow
109 CALC_WRONGTIME
// wrong time format
112 class SwSbxValue
: public SbxValue
116 // always default to a number. otherwise it will become a SbxEMPTY
117 SwSbxValue( long n
= 0 ) : bVoid(false) { PutLong( n
); }
118 SwSbxValue( const double& rD
) : bVoid(false) { PutDouble( rD
); }
119 SwSbxValue( const SwSbxValue
& rVal
) :
124 virtual ~SwSbxValue();
127 sal_Bool
GetBool() const;
128 double GetDouble() const;
129 SwSbxValue
& MakeDouble();
131 bool IsVoidValue() {return bVoid
;}
132 void SetVoidValue(bool bSet
) {bVoid
= bSet
;}
135 /******************************************************************************
136 * Calculate HashTables for VarTable und Operations
137 ******************************************************************************/
140 SwHash( const String
& rStr
);
146 struct SwCalcExp
: public SwHash
149 const SwFieldType
* pFldType
;
151 SwCalcExp( const String
& rStr
, const SwSbxValue
& rVal
,
152 const SwFieldType
* pFldType
= 0 );
155 SwHash
* Find( const String
& rSrch
, SwHash
** ppTable
,
156 sal_uInt16 nTblSize
, sal_uInt16
* pPos
= 0 );
158 void DeleteHashTable( SwHash
** ppTable
, sal_uInt16 nTblSize
);
160 // if _CalcOp != 0, this is a valid operator
162 _CalcOp
* FindOperator( const String
& rSearch
);
164 /******************************************************************************
166 ******************************************************************************/
169 SwHash
* VarTable
[ TBLSZ
];
170 String aVarName
, sCurrSym
;
172 std::vector
<const SwUserFieldType
*> aRekurStk
;
173 SwSbxValue nLastLeft
;
174 SwSbxValue nNumberValue
;
176 xub_StrLen nCommandPos
;
179 SvtSysLocale m_aSysLocale
;
180 const LocaleDataWrapper
* pLclData
;
181 CharClass
* pCharClass
;
184 SwCalcOper eCurrOper
;
185 SwCalcOper eCurrListOper
;
189 SwCalcOper
GetToken();
194 sal_Bool
ParseTime( sal_uInt16
*, sal_uInt16
*, sal_uInt16
* );
196 String
GetColumnName( const String
& rName
);
197 String
GetDBName( const String
& rName
);
199 // dont call this methods
200 SwCalc( const SwCalc
& );
201 SwCalc
& operator=( const SwCalc
& );
207 SwSbxValue
Calculate( const String
&rStr
);
208 String
GetStrResult( const SwSbxValue
& rValue
, sal_Bool bRound
= sal_True
);
209 String
GetStrResult( double, sal_Bool bRound
= sal_True
);
211 SwCalcExp
* VarInsert( const String
& r
);
212 SwCalcExp
* VarLook( const String
&rStr
, sal_uInt16 ins
= 0 );
213 void VarChange( const String
& rStr
, const SwSbxValue
& rValue
);
214 void VarChange( const String
& rStr
, double );
215 SwHash
** GetVarTable() { return VarTable
; }
217 bool Push(const SwUserFieldType
* pUserFieldType
);
220 void SetCalcError( SwCalcError eErr
) { eError
= eErr
; }
221 sal_Bool
IsCalcError() const { return 0 != eError
; }
223 static bool Str2Double( const String
& rStr
, xub_StrLen
& rPos
,
225 LocaleDataWrapper
const*const pData
= 0 );
226 static bool Str2Double( const String
& rStr
, xub_StrLen
& rPos
,
227 double& rVal
, SwDoc
*const pDoc
);
229 SW_DLLPUBLIC
static sal_Bool
IsValidVarName( const String
& rStr
,
230 String
* pValidName
= 0 );
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */