2010-10-14 08:30:41 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2011-03-31 10:05:04 +02:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2012-07-05 10:42:47 +02:00
|
|
|
|
|
|
|
#ifndef SW_CALC_HXX
|
|
|
|
#define SW_CALC_HXX
|
|
|
|
|
|
|
|
#include <vector>
|
2000-09-18 16:15:01 +00:00
|
|
|
|
2013-09-02 09:03:31 +01:00
|
|
|
#include <tools/string.hxx>
|
2010-10-07 11:27:28 +01:00
|
|
|
#include <unotools/syslocale.hxx>
|
2000-09-18 16:15:01 +00:00
|
|
|
|
2005-04-13 08:41:52 +00:00
|
|
|
#include <basic/sbxvar.hxx>
|
2012-07-05 10:42:47 +02:00
|
|
|
|
2004-08-23 07:28:29 +00:00
|
|
|
#include "swdllapi.h"
|
|
|
|
|
2000-09-18 16:15:01 +00:00
|
|
|
class CharClass;
|
2000-11-20 08:16:31 +00:00
|
|
|
class LocaleDataWrapper;
|
2000-09-18 16:15:01 +00:00
|
|
|
class SwFieldType;
|
|
|
|
class SwDoc;
|
2012-06-25 16:24:29 +02:00
|
|
|
class SwUserFieldType;
|
2000-09-18 16:15:01 +00:00
|
|
|
|
2010-11-15 23:37:49 +01:00
|
|
|
#define TBLSZ 47 // should be a prime, because of hash table
|
2000-09-18 16:15:01 +00:00
|
|
|
|
|
|
|
const sal_Unicode cListDelim = '|';
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Calculate Operations
|
|
|
|
******************************************************************************/
|
|
|
|
enum SwCalcOper
|
|
|
|
{
|
|
|
|
CALC_NAME, CALC_NUMBER, CALC_ENDCALC,
|
|
|
|
CALC_PLUS='+', CALC_MINUS='-', CALC_MUL='*',
|
|
|
|
CALC_DIV='/', CALC_PRINT=';', CALC_ASSIGN='=',
|
|
|
|
CALC_LP='(', CALC_RP=')', CALC_PHD='%',
|
|
|
|
CALC_POW='^',
|
|
|
|
CALC_LISTOP = cListDelim,
|
|
|
|
CALC_NOT=256, CALC_AND=257, CALC_OR=258,
|
|
|
|
CALC_XOR=259, CALC_EQ=260, CALC_NEQ=261,
|
|
|
|
CALC_LEQ=262, CALC_GEQ=263, CALC_LES=264,
|
|
|
|
CALC_GRE=265, CALC_SUM=266, CALC_MEAN=267,
|
|
|
|
CALC_SQRT=268, CALC_MIN=269, CALC_MIN_IN=270,
|
|
|
|
CALC_MAX=271, CALC_MAX_IN=272, CALC_SIN=273,
|
|
|
|
CALC_COS=274, CALC_TAN=275, CALC_ASIN=276,
|
|
|
|
CALC_ACOS=278, CALC_ATAN=279, CALC_TDIF=280,
|
2008-06-16 11:18:16 +00:00
|
|
|
CALC_ROUND=281, CALC_DATE=282, CALC_MONTH=283,
|
|
|
|
CALC_DAY=284
|
2000-09-18 16:15:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//-- Calculate Operations Strings -----------------------------------------
|
|
|
|
|
2010-12-06 10:36:22 +00:00
|
|
|
extern const sal_Char sCalc_Add[];
|
|
|
|
extern const sal_Char sCalc_Sub[];
|
|
|
|
extern const sal_Char sCalc_Mul[];
|
|
|
|
extern const sal_Char sCalc_Div[];
|
|
|
|
extern const sal_Char sCalc_Phd[];
|
|
|
|
extern const sal_Char sCalc_Sqrt[];
|
|
|
|
extern const sal_Char sCalc_Pow[];
|
|
|
|
extern const sal_Char sCalc_Or[];
|
|
|
|
extern const sal_Char sCalc_Xor[];
|
|
|
|
extern const sal_Char sCalc_And[];
|
|
|
|
extern const sal_Char sCalc_Not[];
|
|
|
|
extern const sal_Char sCalc_Eq[];
|
|
|
|
extern const sal_Char sCalc_Neq[];
|
|
|
|
extern const sal_Char sCalc_Leq[];
|
|
|
|
extern const sal_Char sCalc_Geq[];
|
|
|
|
extern const sal_Char sCalc_L[];
|
|
|
|
extern const sal_Char sCalc_G[];
|
|
|
|
extern const sal_Char sCalc_Sum[];
|
|
|
|
extern const sal_Char sCalc_Mean[];
|
|
|
|
extern const sal_Char sCalc_Min[];
|
|
|
|
extern const sal_Char sCalc_Max[];
|
|
|
|
extern const sal_Char sCalc_Sin[];
|
|
|
|
extern const sal_Char sCalc_Cos[];
|
|
|
|
extern const sal_Char sCalc_Tan[];
|
|
|
|
extern const sal_Char sCalc_Asin[];
|
|
|
|
extern const sal_Char sCalc_Acos[];
|
|
|
|
extern const sal_Char sCalc_Atan[];
|
|
|
|
extern const sal_Char sCalc_Tdif[];
|
|
|
|
extern const sal_Char sCalc_Round[];
|
|
|
|
extern const sal_Char sCalc_Date[];
|
2000-09-18 16:15:01 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Calculate ErrorCodes
|
|
|
|
******************************************************************************/
|
|
|
|
enum SwCalcError
|
|
|
|
{
|
|
|
|
CALC_NOERR=0,
|
2010-11-15 23:37:49 +01:00
|
|
|
CALC_SYNTAX, // syntax error
|
|
|
|
CALC_ZERODIV, // division by zero
|
|
|
|
CALC_BRACK, // faulty brackets
|
|
|
|
CALC_POWERR, // overflow in power function
|
|
|
|
CALC_VARNFND, // variable was not found
|
|
|
|
CALC_OVERFLOW, // overflow
|
|
|
|
CALC_WRONGTIME // wrong time format
|
2000-09-18 16:15:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class SwSbxValue : public SbxValue
|
|
|
|
{
|
2005-10-19 07:21:02 +00:00
|
|
|
bool bVoid;
|
2000-09-18 16:15:01 +00:00
|
|
|
public:
|
2010-11-15 23:37:49 +01:00
|
|
|
// always default to a number. otherwise it will become a SbxEMPTY
|
2005-10-19 07:21:02 +00:00
|
|
|
SwSbxValue( long n = 0 ) : bVoid(false) { PutLong( n ); }
|
|
|
|
SwSbxValue( const double& rD ) : bVoid(false) { PutDouble( rD ); }
|
|
|
|
SwSbxValue( const SwSbxValue& rVal ) :
|
2007-09-27 06:56:41 +00:00
|
|
|
SvRefBase( rVal ),
|
2005-10-19 07:21:02 +00:00
|
|
|
SbxValue( rVal ),
|
|
|
|
bVoid(rVal.bVoid)
|
|
|
|
{}
|
2000-09-18 16:15:01 +00:00
|
|
|
virtual ~SwSbxValue();
|
|
|
|
|
2003-03-27 14:45:43 +00:00
|
|
|
|
2011-01-17 15:06:54 +01:00
|
|
|
sal_Bool GetBool() const;
|
2000-09-18 16:15:01 +00:00
|
|
|
double GetDouble() const;
|
|
|
|
SwSbxValue& MakeDouble();
|
2005-10-19 07:21:02 +00:00
|
|
|
|
|
|
|
bool IsVoidValue() {return bVoid;}
|
2005-11-11 17:47:46 +00:00
|
|
|
void SetVoidValue(bool bSet) {bVoid = bSet;}
|
2000-09-18 16:15:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************
|
2010-11-15 23:37:49 +01:00
|
|
|
* Calculate HashTables for VarTable und Operations
|
2000-09-18 16:15:01 +00:00
|
|
|
******************************************************************************/
|
|
|
|
struct SwHash
|
|
|
|
{
|
2013-10-01 13:39:46 +01:00
|
|
|
SwHash( const OUString& rStr );
|
2000-09-18 16:15:01 +00:00
|
|
|
virtual ~SwHash();
|
2013-10-01 13:39:46 +01:00
|
|
|
OUString aStr;
|
2000-09-18 16:15:01 +00:00
|
|
|
SwHash *pNext;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SwCalcExp : public SwHash
|
|
|
|
{
|
|
|
|
SwSbxValue nValue;
|
|
|
|
const SwFieldType* pFldType;
|
|
|
|
|
2013-10-01 13:39:46 +01:00
|
|
|
SwCalcExp( const OUString& rStr, const SwSbxValue& rVal,
|
2000-09-18 16:15:01 +00:00
|
|
|
const SwFieldType* pFldType = 0 );
|
|
|
|
};
|
|
|
|
|
2013-10-01 13:39:46 +01:00
|
|
|
SwHash* Find( const OUString& rSrch, SwHash** ppTable,
|
2011-01-17 15:06:54 +01:00
|
|
|
sal_uInt16 nTblSize, sal_uInt16* pPos = 0 );
|
2000-09-18 16:15:01 +00:00
|
|
|
|
2011-01-17 15:06:54 +01:00
|
|
|
void DeleteHashTable( SwHash** ppTable, sal_uInt16 nTblSize );
|
2000-09-18 16:15:01 +00:00
|
|
|
|
2010-11-15 23:37:49 +01:00
|
|
|
// if _CalcOp != 0, this is a valid operator
|
2000-09-18 16:15:01 +00:00
|
|
|
struct _CalcOp;
|
2013-10-01 13:39:46 +01:00
|
|
|
_CalcOp* FindOperator( const OUString& rSearch );
|
2000-09-18 16:15:01 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* class SwCalc
|
|
|
|
******************************************************************************/
|
|
|
|
class SwCalc
|
|
|
|
{
|
|
|
|
SwHash* VarTable[ TBLSZ ];
|
|
|
|
String aVarName, sCurrSym;
|
2013-10-05 10:14:54 +01:00
|
|
|
OUString sCommand;
|
2012-06-25 16:24:29 +02:00
|
|
|
std::vector<const SwUserFieldType*> aRekurStk;
|
2000-09-18 16:15:01 +00:00
|
|
|
SwSbxValue nLastLeft;
|
|
|
|
SwSbxValue nNumberValue;
|
|
|
|
SwCalcExp aErrExpr;
|
2013-10-05 16:38:37 +01:00
|
|
|
sal_Int32 nCommandPos;
|
2000-09-18 16:15:01 +00:00
|
|
|
|
|
|
|
SwDoc& rDoc;
|
2010-10-07 11:27:28 +01:00
|
|
|
SvtSysLocale m_aSysLocale;
|
2009-09-08 04:57:32 +00:00
|
|
|
const LocaleDataWrapper* pLclData;
|
2000-09-18 16:15:01 +00:00
|
|
|
CharClass* pCharClass;
|
|
|
|
|
2011-01-17 15:06:54 +01:00
|
|
|
sal_uInt16 nListPor;
|
2000-09-18 16:15:01 +00:00
|
|
|
SwCalcOper eCurrOper;
|
|
|
|
SwCalcOper eCurrListOper;
|
|
|
|
SwCalcError eError;
|
|
|
|
|
|
|
|
|
|
|
|
SwCalcOper GetToken();
|
|
|
|
SwSbxValue Expr();
|
|
|
|
SwSbxValue Term();
|
|
|
|
SwSbxValue Prim();
|
|
|
|
|
2011-01-17 15:06:54 +01:00
|
|
|
sal_Bool ParseTime( sal_uInt16*, sal_uInt16*, sal_uInt16* );
|
2000-09-18 16:15:01 +00:00
|
|
|
|
|
|
|
String GetColumnName( const String& rName );
|
|
|
|
String GetDBName( const String& rName );
|
|
|
|
|
|
|
|
// dont call this methods
|
|
|
|
SwCalc( const SwCalc& );
|
|
|
|
SwCalc& operator=( const SwCalc& );
|
|
|
|
|
|
|
|
public:
|
|
|
|
SwCalc( SwDoc& rD );
|
|
|
|
~SwCalc();
|
|
|
|
|
|
|
|
SwSbxValue Calculate( const String &rStr );
|
2011-01-17 15:06:54 +01:00
|
|
|
String GetStrResult( const SwSbxValue& rValue, sal_Bool bRound = sal_True );
|
|
|
|
String GetStrResult( double, sal_Bool bRound = sal_True );
|
2000-09-18 16:15:01 +00:00
|
|
|
|
|
|
|
SwCalcExp* VarInsert( const String& r );
|
2011-01-17 15:06:54 +01:00
|
|
|
SwCalcExp* VarLook( const String &rStr, sal_uInt16 ins = 0 );
|
2000-09-18 16:15:01 +00:00
|
|
|
void VarChange( const String& rStr, const SwSbxValue& rValue );
|
|
|
|
void VarChange( const String& rStr, double );
|
|
|
|
SwHash** GetVarTable() { return VarTable; }
|
|
|
|
|
2012-06-25 16:24:29 +02:00
|
|
|
bool Push(const SwUserFieldType* pUserFieldType);
|
|
|
|
void Pop();
|
2000-09-18 16:15:01 +00:00
|
|
|
|
|
|
|
void SetCalcError( SwCalcError eErr ) { eError = eErr; }
|
2011-01-17 15:06:54 +01:00
|
|
|
sal_Bool IsCalcError() const { return 0 != eError; }
|
2000-09-18 16:15:01 +00:00
|
|
|
|
2013-10-02 13:34:02 +01:00
|
|
|
static bool Str2Double( const OUString& rStr, sal_Int32& rPos,
|
2001-02-13 19:32:47 +00:00
|
|
|
double& rVal,
|
2010-10-07 11:37:07 +01:00
|
|
|
LocaleDataWrapper const*const pData = 0 );
|
2013-10-02 13:34:02 +01:00
|
|
|
static bool Str2Double( const OUString& rStr, sal_Int32& rPos,
|
2010-10-07 11:37:07 +01:00
|
|
|
double& rVal, SwDoc *const pDoc );
|
2001-02-13 19:32:47 +00:00
|
|
|
|
2013-08-22 20:51:25 +02:00
|
|
|
SW_DLLPUBLIC static sal_Bool IsValidVarName( const OUString& rStr,
|
|
|
|
OUString* pValidName = 0 );
|
2000-09-18 16:15:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
2010-10-14 08:30:41 +02:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|