use QtxEvalExpr.* instead QtxParser.*, QtxOperations.*, QtxListOfOperations.* and QtxStdOperations.*.
+++ /dev/null
-// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
-//
-// This library is free software; you can redistribute it and/or
-// modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either
-// version 2.1 of the License.
-//
-// This library is distributed in the hope that it will be useful
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public
-// License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-//
-// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
-//
-// File: QtxDblValidator.cxx
-// Author: Alexandre SOLOVYOV
-
-#include "QtxDblValidator.h"
-
-/*!
- Constructor
- \param bot - minimal possible value
- \param top - maximal possible value
- \param dec - number of digits
- \param o - parent object
- \param name - name of validator
-*/
-QtxDblValidator::QtxDblValidator( const double bot, const double top, const int dec,
- QObject* o, const char* name )
-: QDoubleValidator( bot, top, dec, o, name )
-{
-}
-
-/*!
- Destructor
-*/
-QtxDblValidator::~QtxDblValidator()
-{
-}
-
-/*!
- Corrects string: if it represent double value less then bottom, it becomes equal to bottom,
- if it is more then top, it becomes equal to top, if it isn't number is becomes '0'
-*/
-void QtxDblValidator::fixup( QString& str ) const
-{
- bool ok = false;
- double d = str.toDouble( &ok );
- if( ok )
- {
- if( d<bottom() )
- str = QString::number( bottom() );
- else if( d>top() )
- str = QString::number( top() );
- }
- else
- str = "0";
-}
+++ /dev/null
-// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
-//
-// This library is free software; you can redistribute it and/or
-// modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either
-// version 2.1 of the License.
-//
-// This library is distributed in the hope that it will be useful
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public
-// License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-//
-// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
-//
-// File: QtxDblValidator.h
-// Author: Alexandre SOLOVYOV
-
-#ifndef QTX_DOUBLE_VALIDATOR
-#define QTX_DOUBLE_VALIDATOR
-
-#include "Qtx.h"
-
-#include <qvalidator.h>
-
-class QTX_EXPORT QtxDblValidator : public QDoubleValidator
-{
- Q_OBJECT
-
-public:
- QtxDblValidator( const double, const double, const int,
- QObject*, const char* = 0 );
- ~QtxDblValidator();
-
- virtual void fixup( QString& ) const;
-};
-
-#endif
+++ /dev/null
-// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
-//
-// This library is free software; you can redistribute it and/or
-// modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either
-// version 2.1 of the License.
-//
-// This library is distributed in the hope that it will be useful
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public
-// License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-//
-// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
-//
-
-#include "QtxListOfOperations.h"
-#include <stdarg.h>
-
-/*!
- Constructor
-*/
-QtxListOfOperations::QtxListOfOperations()
-{
-}
-
-/*!
- Destructor
-*/
-QtxListOfOperations::~QtxListOfOperations()
-{
-}
-
-/*!
- \return fills list with brackets symbols
- \param list - list to be filled
- \param open - if it is true, it is necessary to fill with open brackets otherwise with close
-*/
-void QtxListOfOperations::bracketsList( QStringList& list, bool open ) const
-{
- OperationSetsIterator anIt = mySets.begin(),
- aLast = mySets.end();
- QStringList custom;
- for( ; anIt!=aLast; anIt++ )
- {
- custom.clear();
- (*anIt).myOperations->bracketsList( custom, open );
- QStringList::const_iterator aSIt = custom.begin(),
- aSLast = custom.end();
- for( ; aSIt!=aSLast; aSIt++ )
- if( list.contains( *aSIt )==0 )
- list.append( *aSIt );
- }
-}
-
-/*!
- Fills list with operation names
-*/
-void QtxListOfOperations::opersList( QStringList& list ) const
-{
- OperationSetsIterator anIt = mySets.begin(),
- aLast = mySets.end();
- QStringList custom;
- for( ; anIt!=aLast; anIt++ )
- {
- custom.clear();
- (*anIt).myOperations->opersList( custom );
- QStringList::const_iterator aSIt = custom.begin(),
- aSLast = custom.end();
- for( ; aSIt!=aSLast; aSIt++ )
- if( list.contains( *aSIt )==0 )
- list.append( *aSIt );
- }
-}
-
-/*!
- Create some value by it's string representation
-*/
-bool QtxListOfOperations::createValue( const QString& str, QtxValue& val ) const
-{
- bool ok;
- OperationSetsIterator anIt = mySets.begin(),
- aLast = mySets.end();
- for( ; anIt!=aLast; anIt++ )
- {
- ok = (*anIt).myOperations->createValue( str, val );
- if( ok )
- break;
- }
- return ok;
-}
-
-/*!
- \return priority of operation
-*/
-int QtxListOfOperations::prior( const QString& op, bool isBin ) const
-{
- OperationSetsIterator anIt = mySets.begin(),
- aLast = mySets.end();
- int prior = 0;
- for( ; anIt!=aLast; anIt++ )
- {
- prior = (*anIt).myOperations->prior( op, isBin );
- if( prior>0 )
- {
- prior+=(*anIt).myAddPrior;
- break;
- }
- }
- return prior>0 ? prior : 0;
-}
-
-/*!
- \return whether values with passed types are valid for arguments of operation
- \param op - name of operation
- \param t1 - type of first argument
- \param t2 - type of second argument
-*/
-QtxParser::Error QtxListOfOperations::isValid( const QString& op,
- const QVariant::Type t1,
- const QVariant::Type t2 ) const
-{
- OperationSetsIterator anIt = mySets.begin(),
- aLast = mySets.end();
- QtxParser::Error err = QtxParser::OK;
- for( ; anIt!=aLast; anIt++ )
- {
- err = (*anIt).myOperations->isValid( op, t1, t2 );
- if( err==QtxParser::OK )
- break;
- }
- return err;
-}
-
-/*!
- Calculates result of operation
- \return one of error states
- \param op - name of operation
- \param v1 - first operation argument (must be used also to store result)
- \param v2 - second operation argument
-*/
-QtxParser::Error QtxListOfOperations::calculate( const QString& op,
- QtxValue& v1,
- QtxValue& v2 ) const
-{
- const char* deb = op.latin1();
-
- OperationSetsIterator anIt = mySets.begin(),
- aLast = mySets.end();
- QtxValue nv1, nv2;
- for( ; anIt!=aLast; anIt++ )
- {
- nv1 = v1;
- nv2 = v2;
- if( (*anIt).myOperations->isValid( op, v1.type(), v2.type() ) == QtxParser::OK )
- {
- QtxParser::Error err = (*anIt).myOperations->calculate( op, nv1, nv2 );
- if( err==QtxParser::OK || err==QtxParser::InvalidResult )
- {
- QString oop = (*anIt).myName;
- const char* ooo = oop.latin1();
- v1 = nv1; v2 = nv2;
- return err;
- }
- }
- }
- return QtxParser::InvalidOperation;
-}
-
-/*!
- Clears list of operations
-*/
-void QtxListOfOperations::clear()
-{
- mySets.clear();
-}
-
-/*!
- \return true if list contains operations
- \param name - name of operation
-*/
-bool QtxListOfOperations::has( const QString& name ) const
-{
- OperationSetsIterator anIt = mySets.begin(),
- aLast = mySets.end();
- for( ; anIt!=aLast; anIt++ )
- if( (*anIt).myName == name )
- return true;
- return false;
-}
-
-/*!
- Appends operations to list
- \param name - name of operations
- \param oper - operations
- \param prior - additional prior
-*/
-void QtxListOfOperations::append( const QString& name, QtxOperations* oper,
- int prior )
-{
- insert( name, oper, prior );
-}
-
-/*!
- Prepends operations to list
- \param name - name of operations
- \param oper - operations
- \param prior - additional prior
-*/
-void QtxListOfOperations::prepend( const QString& name, QtxOperations* oper,
- int prior )
-{
- insert( name, oper, prior, 0 );
-}
-
-/*!
- Inserts operations to list
- \param name - name of operations
- \param oper - operations
- \param prior - additional prior
- \param pos - insert position
-*/
-void QtxListOfOperations::insert( const QString& name, QtxOperations* oper,
- int prior, int pos )
-{
- if( has( name ) || oper==NULL || prior<0 )
- return;
-
- OperationSet op;
- op.myName = name;
- op.myOperations = oper;
- op.myAddPrior = prior;
- if( pos<0 )
- mySets.append( op );
- else
- mySets.insert( mySets.at( pos ), op );
-}
-
-/*!
- Removes operations from list
- \param name - name of operations
-*/
-void QtxListOfOperations::remove( const QString& name )
-{
- OperationSets::iterator anIt = mySets.begin(),
- aLast = mySets.end();
- for( ; anIt!=aLast; anIt++ )
- if( (*anIt).myName == name )
- {
- mySets.erase( anIt );
- break;
- }
-}
-
-/*!
- \return count of operations in list
-*/
-int QtxListOfOperations::count() const
-{
- return mySets.count();
-}
-
-/*!
- \return operations by name
- \name - name of operations
-*/
-QtxOperations* QtxListOfOperations::operations( const QString& name ) const
-{
- OperationSetsIterator anIt = mySets.begin(),
- aLast = mySets.end();
- for( ; anIt!=aLast; anIt++ )
- if( (*anIt).myName == name )
- return (*anIt).myOperations;
- return NULL;
-}
+++ /dev/null
-// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
-//
-// This library is free software; you can redistribute it and/or
-// modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either
-// version 2.1 of the License.
-//
-// This library is distributed in the hope that it will be useful
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public
-// License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-//
-// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
-//
-// File: QtxListOfOperations.h
-// Author: Alexander SOLOVYOV
-
-#ifndef __QTX_LIST_OF_OPERATIONS_HEADER__
-#define __QTX_LIST_OF_OPERATIONS_HEADER__
-
-#include "Qtx.h"
-#include "QtxOperations.h"
-
-#ifdef WIN32
-#pragma warning( disable:4251 )
-#endif
-
-
-/*!
- Class: QtxListOfOperations
-
- This class provides special variant of QtxOperations. It contains list of QtxOperations* with
- fixed order. When it is necessary to calculate operation or get it's priority, this operation is
- being found starting from first item. It makes possible to create unions of operations. Every QtxOperations*
- has priority, which is added to priority of operation, so that it makes possible to increase priority of
- whole group of operations
-*/
-class QTX_EXPORT QtxListOfOperations : public QtxOperations
-{
-public:
- QtxListOfOperations();
- virtual ~QtxListOfOperations();
-
- virtual void opersList( QStringList& ) const;
- virtual void bracketsList( QStringList&, bool open ) const;
-
- virtual bool createValue( const QString&, QtxValue& ) const;
- virtual int prior ( const QString&, bool isBin ) const;
- virtual QtxParser::Error isValid( const QString&,
- const QVariant::Type,
- const QVariant::Type ) const;
- virtual QtxParser::Error calculate( const QString&, QtxValue&, QtxValue& ) const;
-
- void clear ();
- bool has ( const QString& ) const;
- void append ( const QString&, QtxOperations*, int prior );
- void prepend( const QString&, QtxOperations*, int prior );
- void insert ( const QString&, QtxOperations*, int prior, int pos = -1 );
- void remove ( const QString& );
- int count () const;
- QtxOperations* operations( const QString& ) const;
-
-private:
- typedef struct
- {
- QString myName;
- QtxOperations* myOperations;
- int myAddPrior;
-
- } OperationSet;
-
- typedef QValueList< OperationSet > OperationSets;
- typedef OperationSets::const_iterator OperationSetsIterator;
-
- OperationSets mySets;
-};
-
-#endif
+++ /dev/null
-// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
-//
-// This library is free software; you can redistribute it and/or
-// modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either
-// version 2.1 of the License.
-//
-// This library is distributed in the hope that it will be useful
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public
-// License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-//
-// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
-//
-
-#include "QtxOperations.h"
-
-/*!
- Default constructor
-*/
-QtxOperations::QtxOperations()
-{
-}
-
-/*!
- Destructor
-*/
-QtxOperations::~QtxOperations()
-{
-}
-
-/*!
- Creates QtxValue by it's string representation
-*/
-bool QtxOperations::createValue( const QString& str, QtxValue& v ) const
-{
- v = str;
- return false;
-}
+++ /dev/null
-// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
-//
-// This library is free software; you can redistribute it and/or
-// modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either
-// version 2.1 of the License.
-//
-// This library is distributed in the hope that it will be useful
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public
-// License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-//
-// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
-//
-// File: QtxOperations.h
-// Author: Alexander SOLOVYOV
-
-#ifndef __QTX_OPERATIONS_HEADER__
-#define __QTX_OPERATIONS_HEADER__
-
-#include "Qtx.h"
-#include "QtxParser.h"
-
-class QTX_EXPORT QtxOperations
-{
-public:
- QtxOperations();
- virtual ~QtxOperations();
-
- virtual void opersList( QStringList& ) const = 0;
- //list of possible operations
-
- virtual void bracketsList( QStringList&, bool open ) const = 0;
- //list of open/close brackets
-
- virtual bool createValue( const QString&, QtxValue& ) const;
- //by default, the String value will be set, it corresponds to parameter
- //base method returns false (always parameter)
- //successor's method returns true if it has created custom value
- //or call base if it hasn't
-
- virtual int prior( const QString&, bool isBin ) const = 0;
- //returns prioritet of operation;
- //if operation is impossible, it must return 0 or less
-
- virtual QtxParser::Error isValid( const QString&,
- const QVariant::Type,
- const QVariant::Type ) const = 0;
- //return OK if this parameter types is valid for operation
- //return OperandsNotMatch or InvalidOperation otherwise
-
- virtual QtxParser::Error calculate( const QString&, QtxValue&, QtxValue& ) const = 0;
- //process binary operation with values
- //for unary operation the second QtxValue will be passed as invalid
-};
-
-#endif
+++ /dev/null
-// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
-//
-// This library is free software; you can redistribute it and/or
-// modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either
-// version 2.1 of the License.
-//
-// This library is distributed in the hope that it will be useful
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public
-// License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-//
-// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
-//
-
-#include "QtxParser.h"
-#include "QtxOperations.h"
-
-/*!
- Constructor
-*/
-QtxParser::QtxParser( QtxOperations* operations, const QString& expr )
-: myOperations( operations )
-{
- if( myOperations )
- {
- setLastError( OK );
- setExpr( expr );
- }
- else
- setLastError( OperationsNull );
-}
-
-/*!
- Destructor
-*/
-QtxParser::~QtxParser()
-{
-}
-
-/*!
- Search elements of list as substrings starting on 'offset'
- \returns the least position of substrings inside string
- \param list - list of substrings
- \param str - string where search
- \param offset - starting index for search
- \param matchLen - the length of appropriate substring
- \param listind - list index of appropriate substring
-*/
-int QtxParser::search( const QStringList& list, const QString& str, int offset,
- int& matchLen, int& listind )
-{
- QStringList::const_iterator anIt = list.begin(), aLast = list.end();
- int min = -1;
- for( int ind = 0; anIt!=aLast; anIt++, ind++ )
- {
- //const char* where = str.latin1(), *what = (*anIt).latin1();
- int pos = str.find( *anIt, offset );
- if( pos>=0 && ( min < 0 || min > pos ||
- ( min==pos && matchLen< (int)(*anIt).length() ) ) )
- {
- min = pos;
- listind = ind;
- matchLen = (*anIt).length();
- }
- }
- if( min<0 )
- matchLen = 0;
- return min;
-}
-
-/*!
- \return substring
- \param str - string
- \param pos - start position of substring
- \param len - length of substring
-*/
-QString QtxParser::note( const QString& str, int pos, int len )
-{
- return str.mid( pos, len ).stripWhiteSpace();
-}
-
-/*!
- First step of parsing: finding tokens, determining its types and creating of unsorted pseudo-postfix (with brackets)
- \param expr - string expression
- \param post - postfix to be created
-*/
-bool QtxParser::prepare( const QString& expr, Postfix& post )
-{
- int pos = 0, len = expr.length();
- QValueStack< int > aBracketStack;
- QStringList anOpers, anOpenBr, aCloseBr;
- if( myOperations )
- {
- myOperations->bracketsList( anOpenBr, true );
- myOperations->bracketsList( aCloseBr, false );
- myOperations->opersList( anOpers );
- }
- else
- {
- setLastError( OperationsNull );
- return false;
- }
-
- while( pos < len && lastError()==OK )
- {
- PostfixItem item;
- while( expr[ pos ].isSpace() && pos<len ) pos++;
- if( pos>=len )
- break;
-
- int mBrLen = 0, mLen = 0, br_ind = -1, op_ind = -1;
- int oPos = search( anOpenBr, expr, pos, mBrLen, br_ind ),
- cPos = oPos==pos ? -1 : search( aCloseBr, expr, pos, mBrLen, br_ind ),
- opPos = search( anOpers, expr, pos, mLen, op_ind );
-
- if( expr[ pos ]=="'" )
- {
- int vpos = pos+1;
- while ( vpos< (int)expr.length() && expr[ vpos ]!="'" )
- vpos++;
-
- mLen = vpos-pos+1;
-
- int res = myOperations->createValue( note( expr, pos, mLen ), item.myValue );
- item.myType = res ? Value : Param;
- post.append( item );
- pos = vpos+1;
- continue;
- }
-
- if( oPos==pos )
- {
- aBracketStack.push( br_ind );
- item.myValue = note( expr, pos, mBrLen );
- item.myType = Open;
- post.append( item );
- }
-
- else if( cPos==pos )
- {
- if( aBracketStack.count()==0 )
- {
- setLastError( ExcessClose );
- break;
- }
- if( br_ind!=aBracketStack.top() )
- {
- setLastError( BracketsNotMatch );
- break;
- }
- else
- {
- aBracketStack.pop();
- item.myValue = note( expr, pos, mBrLen );
- item.myType = Close;
- post.append( item );
- }
- }
- else
- mBrLen = 0;
-
- if( opPos==pos )
- {
- mBrLen = 0;
- item.myValue = note( expr, pos, mLen );
- item.myType = Binary;
- //the type is set by default;
- //the method setOperationTypes will set correct types
-
- if( oPos==pos )
- post.insert( post.at( post.count()-1 ), item );
- else
- post.append( item );
- }
- else
- {
- mLen = 0;
- if( oPos!=pos && cPos!=pos )
- {
- int i;
- for( i=pos+1; i<(int)expr.length(); i++ )
- if( expr[ i ].isSpace() )
- break;
-
- int vpos = i;
- if( oPos>=0 && oPos<vpos )
- vpos = oPos;
- if( cPos>=0 && cPos<vpos )
- vpos = cPos;
- if( opPos>=0 && opPos<vpos )
- vpos = opPos;
-
- while( vpos<(int)expr.length() &&
- ( expr[vpos].isLetter() || expr[vpos].isDigit() || expr[vpos]=='_' ) )
- vpos++;
-
- mLen = vpos-pos;
- bool res = myOperations->createValue( note( expr, pos, mLen ), item.myValue );
- item.myType = res ? Value : Param;
- post.append( item );
- }
- }
-
- pos+=mBrLen+mLen;
- }
-
- //Bracket checking
- PostfixIterator anIt = post.begin(),
- aLast = post.end();
- int brValue = 0;
- for( ; anIt!=aLast; anIt++ )
- if( (*anIt).myType==Open )
- brValue++;
- else if( (*anIt).myType==Close )
- if( brValue>0 )
- brValue--;
- else
- {
- setLastError( ExcessClose );
- break;
- }
- if( brValue>0 )
- setLastError( CloseExpected );
-
- return lastError()==OK;
-}
-
-/*!
- Second step of parsing: determining types of operations
- \param post - unsorted postfix
-*/
-bool QtxParser::setOperationTypes( Postfix& post )
-{
- Postfix::iterator aStart = post.begin(),
- aLast = post.end(),
- anIt = aStart, aPrev, aNext;
- QStringList anOpen, aClose;
- if( myOperations )
- {
- myOperations->bracketsList( anOpen, true );
- myOperations->bracketsList( aClose, false );
- }
- else
- return false;
-
- for( ; anIt!=aLast; anIt++ )
- {
- aPrev = anIt; aPrev--;
- aNext = anIt; aNext++;
- if( (*anIt).myType != Binary )
- continue;
-
- if( ( anIt==aStart || (*aPrev).myType == Open ||
- (*aPrev).myType == Pre ||
- (*aPrev).myType == Binary )
- &&
- aNext!=aLast && ( (*aNext).myType == Value ||
- (*aNext).myType == Param ||
- (*aNext).myType == Open ||
- (*aNext).myType == Binary ) )
- (*anIt).myType = Pre;
-
- else if( anIt!=aStart && ( (*aPrev).myType == Close ||
- (*aPrev).myType == Param ||
- (*aPrev).myType == Value ||
- (*aPrev).myType == Pre ||
- (*aPrev).myType == Post ||
- (*aPrev).myType == Binary )
- &&
- ( aNext==aLast || (*aNext).myType == Close ) )
- (*anIt).myType = Post;
-
- if( anOpen.contains( ( *anIt ).myValue.toString() )>0 )
- (*anIt).myType = Pre;
- else if( aClose.contains( ( *anIt ).myValue.toString() )>0 )
- (*anIt).myType = Post;
- }
-
- return lastError()==OK;
-}
-
-/*!
- \return how many global brackets there is (for example '((2+3))' has 2 global brackets)
- \param post - postfix to be checked
- \param f - start index to search
- \param l - last index to search
-*/
-int QtxParser::globalBrackets( const QtxParser::Postfix& post, int f, int l )
-{
- int i,
- start_br = 0,
- fin_br = 0,
- br = 0,
- br_num = 0,
- min_br_num = (l-f+1)*5;
-
- for( i=f; i<=l; i++ )
- if( post[ i ].myType==QtxParser::Open )
- start_br++;
- else
- break;
- for( i=l; i>=f; i-- )
- if( post[ i ].myType==QtxParser::Close )
- fin_br++;
- else
- break;
-
- br = start_br<fin_br ? start_br : fin_br;
- for( i=f+br; i<=l-br; i++ )
- {
- if( post[i].myType==QtxParser::Open )
- br_num++;
- else if( post[i].myType==QtxParser::Close )
- br_num--;
- if( br_num<min_br_num )
- min_br_num = br_num;
- }
-
- return br+min_br_num;
-}
-
-/*!
- Third step of parsing: sorting of postfix in order to convert it to real postfix
- \param post - source postfix
- \param res - destination postfix
- \param anOpen - list of open brackets
- \param aClose - list of close brackets
- \param f - start index of postfix to sorting
- \param l - last index of postfix to sorting
-*/
-bool QtxParser::sort( const Postfix& post, Postfix& res,
- const QStringList& anOpen,
- const QStringList& aClose,
- int f, int l )
-{
- if( l<f )
- return true;
-
- if( f<0 )
- f = 0;
- if( l<0 )
- l = post.count()-1;
-
- int br = globalBrackets( post, f, l );
- f+=br; l-=br;
-
- if( f==l && f>=0 )
- res.append( post[ f ] );
- if( l<=f )
- return true;
-
- if( myOperations )
- {
- int min = -1;
- QIntList argmin;
- QValueList< PostfixItemType > min_types;
-
- //Find operation with minimal priority
- //PostfixIterator anIt = post.at( f ),
- // aLast = post.at( l+1 );
- for( int i=0, j=f; j<=l; i++, j++ )
- {
- const PostfixItem& item = post[ j ];
- PostfixItemType tt = item.myType;
- if( tt==Binary || tt==Pre || tt==Post )
- {
- int cur_pr = myOperations->prior( item.myValue.toString(), tt==Binary );
- if( cur_pr>0 )
- {
- if( min<0 || min>=cur_pr )
- {
- if( min==cur_pr )
- {
- argmin.append( f+i );
- min_types.append( tt );
- }
- else
- {
- min = cur_pr;
- argmin.clear(); argmin.append( f+i );
- min_types.clear(); min_types.append( tt );
- }
- }
- }
- else
- {
- setLastError( InvalidOperation );
- break;
- }
- }
- else if( tt==Open )
- {
- QString opBr = item.myValue.toString();
- int ind = anOpen.findIndex( opBr ), brValue = 0;
- while( j<=l )
- {
- const PostfixItem& anItem = post[ j ];
- if( anItem.myType==Open )
- brValue++;
-
- if( anItem.myType==Close )
- {
- brValue--;
- QString clBr = anItem.myValue.toString();
- if( aClose.findIndex( clBr )==ind && brValue==0 )
- break;
- }
- i++; j++;
- }
- if( brValue>0 )
- {
- setLastError( CloseExpected );
- break;
- }
- }
- }
-
- if( lastError()==OK )
- if( min>=0 )
- {
- QValueList< Postfix > parts;
- QIntList::const_iterator anIt = argmin.begin(),
- aLast = argmin.end();
- Postfix one;
- bool ok = sort( post, one, anOpen, aClose, f, *anIt - 1 );
- parts.append( one );
- one.clear();
- for( ; anIt!=aLast && ok; anIt++ )
- {
- QIntList::const_iterator aNext = anIt; aNext++;
- ok = sort( post, one, anOpen, aClose, *anIt + 1, aNext==aLast ? l : *aNext - 1 );
- parts.append( one );
- one.clear();
- }
- if( !ok )
- return false;
-
- QValueList< Postfix >::const_iterator aPIt = parts.begin();
- QValueList< PostfixItemType >::const_iterator aTIt = min_types.begin();
- QValueStack< PostfixItem > aStack;
- res += (*aPIt); aPIt++;
- anIt = argmin.begin();
- for( ; anIt!=aLast; anIt++, aPIt++, aTIt++ )
- {
- if( *aTIt==Pre )
- if( anOpen.contains( post[ *anIt ].myValue.toString() )==0 )
- {
- res+=(*aPIt);
- aStack.push( post[ *anIt ] );
- }
- else
- {
- res.append( post[ *anIt ] );
- res+=(*aPIt);
- }
- else
- {
- res+=(*aPIt);
- while( !aStack.isEmpty() )
- {
- res.append( aStack.top() );
- aStack.pop();
- }
- res.append( post[ *anIt ] );
- }
- }
- while( !aStack.isEmpty() )
- {
- res.append( aStack.top() );
- aStack.pop();
- }
- }
- else
- { //there are no operations
- PostfixIterator anIt = post.at( f ),
- aLast = post.at( l+1 );
- for( ; anIt!=aLast; anIt++ )
- if( (*anIt).myType==Value || (*anIt).myType==Param )
- res.append( *anIt );
- }
- }
- else
- setLastError( OperationsNull );
-
- return lastError()==OK;
-}
-
-/*!
- Build posfix by expression
- \param expr - string expression
-*/
-bool QtxParser::parse( const QString& expr )
-{
- myPost.clear();
-
- Postfix p;
- QStringList opens, closes;
-
- if( myOperations )
- {
- setLastError( OK );
- myOperations->bracketsList( opens, true );
- myOperations->bracketsList( closes, false );
- }
- else
- {
- setLastError( OperationsNull );
- return false;
- }
-
- //return prepare( expr, myPost ) && setOperationTypes( myPost );
- return prepare( expr, p ) && setOperationTypes( p ) &&
- sort( p, myPost, opens, closes );
-}
-
-/*!
- Calculate operation
- \param op - operation name
- \param v1 - first argument (it is not valid for unary prefix operations and it is used to store result)
- \param v2 - second argument (it is not valid for unary postfix operations)
-*/
-bool QtxParser::calculate( const QString& op, QtxValue& v1, QtxValue& v2 )
-{
- Error err = myOperations->isValid( op, v1.type(), v2.type() );
- if( err==OK )
- setLastError( myOperations->calculate( op, v1, v2 ) );
- else
- setLastError( err );
-
- return lastError()==OK;
-}
-
-/*!
- Calculates expression without postfix rebuilding
- \return QtxValue as result (it is invalid if there were errors during calculation)
-*/
-QtxValue QtxParser::calculate()
-{
- setLastError( OK );
-
- QStringList anOpen, aClose;
- if( myOperations )
- {
- myOperations->bracketsList( anOpen, true );
- myOperations->bracketsList( aClose, false );
- }
- else
- {
- setLastError( OperationsNull );
- return QtxValue();
- }
-
- QtxValueStack aStack;
- PostfixIterator anIt = myPost.begin(),
- aLast = myPost.end();
- for( ; anIt!=aLast && lastError()==OK; anIt++ )
- {
- QString nn = (*anIt).myValue.toString();
- if( (*anIt).myType==Param )
- {
- if( has( nn ) )
- {
- QVariant& v = myParameters[ nn ];
- if( v.isValid() )
- aStack.push( v );
- else
- setLastError( InvalidToken );
- }
- else
- setLastError( InvalidToken );
- }
-
- else if( (*anIt).myType==Value )
- aStack.push( (*anIt).myValue );
-
- else if( (*anIt).myType==Pre || (*anIt).myType==Post )
- {
- if( anOpen.contains( nn )>0 )
- {
- QtxValue inv;
- if( calculate( nn, inv, inv ) )
- aStack.push( QtxValue() );
- }
- else if( aClose.contains( nn )>0 )
- {
- QValueList< QtxValue > set;
- while( true )
- {
- if( aStack.count()==0 )
- {
- setLastError( StackUnderflow );
- break;
- }
- if( aStack.top().isValid() )
- {
- set.append( aStack.top() );
- aStack.pop();
- }
- else
- {
- aStack.pop();
- break;
- }
- }
-
- QtxValue qSet = set, inv;
- if( calculate( nn, qSet, inv ) )
- aStack.push( set );
- }
- else if( aStack.count()>=1 )
- {
- QtxValue inv;
- QtxValue* v1 = &aStack.top(), *v2 = &inv; //"post-" case
- if( (*anIt).myType==Pre )
- {
- v2 = &aStack.top(); v1 = &inv;
- }
-
- calculate( nn, *v1, *v2 );
- }
- else
- setLastError( StackUnderflow );
- }
-
- else if( (*anIt).myType==Binary )
- {
- if( aStack.count()>=2 )
- {
- QVariant v2 = aStack.top(); aStack.pop();
- calculate( nn, aStack.top(), v2 );
- }
- else
- setLastError( StackUnderflow );
- }
- }
-
- QtxValue res;
- if( lastError()==OK )
- {
- int count = aStack.count();
- if( count==0 )
- setLastError( StackUnderflow );
- else if( count==1 )
- res = aStack.top();
- else
- setLastError( ExcessData );
- }
- return res;
-}
-
-/*!
- Change expression, rebuild postfix and calculate it
- \return QtxValue as result (it is invalid if there were errors during calculation)
-*/
-QtxValue QtxParser::calculate( const QString& expr )
-{
- setExpr( expr );
- return calculate();
-}
-
-/*!
- Change expression and rebuild postfix
-*/
-bool QtxParser::setExpr( const QString& expr )
-{
- return parse( expr );
-}
-
-/*!
- \return true, if parser contain parameter
- \param name - name of parameter
-*/
-bool QtxParser::has( const QString& name ) const
-{
- return myParameters.contains( name.stripWhiteSpace() );
-}
-
-/*!
- Sets parameter value
- \param name - name of parameter
- \param value - value of parameter
-*/
-void QtxParser::set( const QString& name, const QtxValue& value )
-{
- myParameters[ name.stripWhiteSpace() ] = value;
-}
-
-/*!
- Removes parameter
- \param name - name of parameter
-*/
-bool QtxParser::remove( const QString& name )
-{
- QString sname = name.stripWhiteSpace();
- bool res = has( sname );
- if( res )
- myParameters.remove( sname );
- return res;
-}
-
-/*!
- \return value of parameter (result is invalid if there is no such parameter)
- \param name - name of parameter
-*/
-QtxValue QtxParser::value( const QString& name ) const
-{
- QString sname = name.stripWhiteSpace();
- if( has( sname ) )
- return myParameters[ sname ].toString();
- else
- return QtxValue();
-}
-
-/*!
- Searches first parameter with assigned invalid QtxValue
- \return true if it is found
- \param name - variable to return name of parameter
-*/
-bool QtxParser::firstInvalid( QString& name ) const
-{
- QMap< QString, QtxValue >::const_iterator anIt = myParameters.begin(),
- aLast = myParameters.end();
- for( ; anIt!=aLast; anIt++ )
- if( !anIt.data().isValid() )
- {
- name = anIt.key();
- return true;
- }
- return false;
-}
-
-/*!
- Removes all parameters with assigned invalid QtxValues
-*/
-void QtxParser::removeInvalids()
-{
- QStringList toDelete;
- QMap< QString, QtxValue >::const_iterator anIt = myParameters.begin(),
- aLast = myParameters.end();
- for( ; anIt!=aLast; anIt++ )
- if( !anIt.data().isValid() )
- toDelete.append( anIt.key() );
-
- QStringList::const_iterator aLIt = toDelete.begin(),
- aLLast = toDelete.end();
- for( ; aLIt!=aLLast; aLIt++ )
- myParameters.remove( *aLIt );
-}
-
-/*!
- \return last error occured during parsing
-*/
-QtxParser::Error QtxParser::lastError() const
-{
- return myLastError;
-}
-
-/*!
- Sets last error occured during parsing (for internal using only)
-*/
-void QtxParser::setLastError( QtxParser::Error err )
-{
- myLastError = err;
-}
-
-/*!
- \return string dump of internal parser postfix
-*/
-QString QtxParser::dump() const
-{
- return dump( myPost );
-}
-
-/*!
- \return string dump of postfix
- \param post - postfix to be dumped
-*/
-QString QtxParser::dump( const Postfix& post ) const
-{
- QString res;
-
- if( myOperations )
- {
- PostfixIterator anIt = post.begin(),
- aLast = post.end();
- for( ; anIt!=aLast; anIt++ )
- {
- if( (*anIt).myType == Value &&
- ( ( *anIt ).myValue.type()==QVariant::String ||
- ( *anIt ).myValue.type()==QVariant::CString ) )
- res += "'" + ( *anIt ).myValue.toString() + "'";
- else
- res += ( *anIt ).myValue.toString();
- if( (*anIt).myType == Pre )
- res += "(pre)";
- else if( (*anIt).myType == Post )
- res += "(post)";
- else if( (*anIt).myType == Binary )
- res += "(bin)";
-
- res += "_";
- }
- }
- return res;
-}
-
-/*!
- Fills list with names of parameters
- \param list - list to be filled
-*/
-void QtxParser::paramsList( QStringList& list )
-{
- PostfixIterator anIt = myPost.begin(),
- aLast = myPost.end();
- for( ; anIt!=aLast; anIt++ )
- if( (*anIt).myType==Param )
- {
- QString name = (*anIt).myValue.toString();
- if( list.contains( name )==0 )
- list.append( name );
- }
-}
-
-/*!
- Removes all parameters
-*/
-void QtxParser::clear()
-{
- myParameters.clear();
-}
-
-/*!
- \return string representation for list of QtxValues
- \param list - list to be converted
-*/
-QString QtxParser::toString( const QValueList< QtxValue >& list )
-{
- QValueList< QtxValue >::const_iterator anIt = list.begin(),
- aLast = list.end();
- QString res = "set : [ ";
- for( ; anIt!=aLast; anIt++ )
- res+=(*anIt).toString()+" ";
- res+="]";
- return res;
-}
+++ /dev/null
-// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
-//
-// This library is free software; you can redistribute it and/or
-// modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either
-// version 2.1 of the License.
-//
-// This library is distributed in the hope that it will be useful
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public
-// License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-//
-// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
-//
-// File: QtxParser.h
-// Author: Alexander SOLOVYOV
-
-#ifndef __QTX_PARSER_HEADER__
-#define __QTX_PARSER_HEADER__
-
-#include "Qtx.h"
-#include <qvaluestack.h>
-#include <qvariant.h>
-
-#ifdef WIN32
-#pragma warning( disable:4251 )
-#endif
-
-
-class QtxOperations;
-
-/*! \var QtxValue
- \brief Alias for QVariant
-*/
-typedef QVariant QtxValue;
-
-
-/*!
- \class QtxParser
-
- This class allows to calculate values of expressions using different set of operations.
- It is provided some of standard set of operations (arithmetics, logic, strings, etc - in QtxStdOperations.h).
- This parser allows to use parameters with help of methods has(), set(), remove(), value(). It uses
- postfix representation of expressions and uses class QtxOperations in order to make certain operation
- Every instance of parser contains only one postfix, so that if expression has been changed, then postfix
- must be rebuilt. In order to increase performance of frequent calculation for many of expressions it is
- recommended to use different instances of parser for expressions
-
-*/
-class QTX_EXPORT QtxParser
-{
-public:
- /*!
- \enum Error
- \brief Errors during parsing
- */
- typedef enum
- {
- OK, /*! \var All right */
- OperandsNotMatch, /*! \var Types of arguments are invalid for this operation */
- InvalidResult, /*! \var Operation cannot find result (for example, division by zero) */
- InvalidOperation, /*! \var Name of operation is unknown */
- OperationsNull, /*! \var Internal operations pointer of parser is null */
- InvalidToken, /*! \var It isn't operation, parameter of value */
- CloseExpected, /*! \var Close bracket is expected */
- ExcessClose, /*! \var The one of close bracket is excess */
- BracketsNotMatch, /*! \var Last open and this close bracket are different, for example [) */
- StackUnderflow, /*! \var There is no arguments in stack for operation */
- ExcessData /*! \var The parsing is finished, but there is more then one value in stack */
-
- } Error;
-
-public:
- QtxParser( QtxOperations*, const QString& = QString::null );
- virtual ~QtxParser();
-
- QtxValue calculate();
- QtxValue calculate( const QString& );
- bool setExpr( const QString& );
-
- virtual void clear();
- virtual bool has ( const QString& name ) const;
- virtual void set ( const QString& name, const QtxValue& value );
- virtual bool remove( const QString& name );
- virtual QtxValue value ( const QString& name ) const;
-
- bool firstInvalid( QString& ) const;
- void removeInvalids();
- QString dump() const;
- Error lastError() const;
- void paramsList( QStringList& );
-
- static QString toString( const QValueList< QtxValue >& );
-
-protected:
- /*!
- \enum PostfixItemType
- \brief Types of postfix representation elements
- */
- typedef enum
- {
- Value, /*! \var Value (number, string, etc.)*/
- Param, /*! \var Parameter */
- Open, /*! \var Open bracket */
- Close, /*! \var Close bracket */
- Pre, /*! \var Unary prefix operation */
- Post, /*! \var Unary postfix operation */
- Binary /*! \var Binary operation */
-
- } PostfixItemType;
-
- /*! \var postfix representation element */
- typedef struct
- {
- QtxValue myValue;
- PostfixItemType myType;
-
- } PostfixItem;
-
- /*! \var postfix representation */
- typedef QValueList< PostfixItem > Postfix;
-
- /*! \var postfix representation iterator */
- typedef Postfix::const_iterator PostfixIterator;
-
-protected:
- QString dump( const Postfix& ) const;
- virtual bool prepare( const QString&, Postfix& );
- virtual bool setOperationTypes( Postfix& );
- virtual bool sort( const Postfix&, Postfix&,
- const QStringList&, const QStringList&,
- int f=-1, int l=-1 );
-
- virtual bool parse( const QString& );
- virtual void setLastError( const Error );
-
- bool calculate( const QString&, QtxValue&, QtxValue& );
-
- static int search ( const QStringList&, const QString&, int offset,
- int& matchLen, int& listind );
- static QString note ( const QString& str, int pos, int len );
- static int globalBrackets( const Postfix&, int, int );
-
-private:
- /*! \var stack of QtxValues */
- typedef QValueStack < QtxValue > QtxValueStack;
-
-private:
- QtxOperations* myOperations;
- QMap< QString, QtxValue > myParameters;
- Error myLastError;
- Postfix myPost;
-};
-
-#endif
+++ /dev/null
-// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
-//
-// This library is free software; you can redistribute it and/or
-// modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either
-// version 2.1 of the License.
-//
-// This library is distributed in the hope that it will be useful
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public
-// License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-//
-// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
-//
-#include "QtxStdOperations.h"
-
-#include <math.h>
-#include <stdlib.h>
-
-/*!
- Default constructor
-*/
-QtxStdOperations::QtxStdOperations()
-{
-}
-
-/*!
- Destructor
-*/
-QtxStdOperations::~QtxStdOperations()
-{
-}
-
-/*!
- Fills list of brackets treated as open (when 'open' is 'true') or close ('open' is 'false')
-*/
-void QtxStdOperations::bracketsList( QStringList& list, bool open ) const
-{
- if( open )
- list.append( "(" );
- else
- list.append( ")" );
-}
-
-/*!
- Fills list with operation names by copying of internal list of operations
-*/
-void QtxStdOperations::opersList( QStringList& list ) const
-{
- list += myOpers;
-}
-
-/*!
- Add operation names from list to internal list of operations
-*/
-void QtxStdOperations::addOperations( const QStringList& list )
-{
- QStringList::const_iterator anIt = list.begin(),
- aLast = list.end();
- for( ; anIt!=aLast; anIt++ )
- if( myOpers.contains( *anIt )==0 )
- myOpers.append( *anIt );
-}
-
-/*!
- Append operation names from 'list' to internal list of operations
-*/
-void QtxStdOperations::addTypes( const ListOfTypes& list )
-{
- ListOfTypes::const_iterator anIt = list.begin(),
- aLast = list.end();
- for( ; anIt!=aLast; anIt++ )
- if( myTypes.contains( *anIt )==0 )
- myTypes.append( *anIt );
-}
-
-/*!
- \return whether values with passed types are valid for arguments of operation
- \param op - name of operation
- \param t1 - type of first argument
- \param t2 - type of second argument
-*/
-QtxParser::Error QtxStdOperations::isValid( const QString& op,
- const QVariant::Type t1,
- const QVariant::Type t2 ) const
-{
- if( ( t1==QVariant::Invalid || myTypes.contains( t1 ) ) &&
- ( t2==QVariant::Invalid || myTypes.contains( t2 ) ) &&
- ( t1!=QVariant::Invalid || t2!=QVariant::Invalid ) )
- if( prior( op, t1!=QVariant::Invalid && t2!=QVariant::Invalid ) > 0 )
- return QtxParser::OK;
- else
- return QtxParser::InvalidOperation;
- else
- return QtxParser::OperandsNotMatch;
-}
-
-
-
-
-
-
-
-
-/*!
- Default constructor
-*/
-QtxArithmetics::QtxArithmetics()
-: QtxStdOperations()
-{
- QStringList aList;
- aList.append( "+" );
- aList.append( "-" );
- aList.append( "*" );
- aList.append( "/" );
- aList.append( "=" );
- aList.append( "<" );
- aList.append( ">" );
- aList.append( "<=" );
- aList.append( ">=" );
- aList.append( "<>" );
- aList.append( "!=" ); // same as "<>" - for C++ addicts
- addOperations( aList );
-
- ListOfTypes aTypes;
- aTypes.append( QVariant::Int );
- aTypes.append( QVariant::UInt );
- aTypes.append( QVariant::Double );
- addTypes( aTypes );
-}
-
-/*!
- Destructor
-*/
-QtxArithmetics::~QtxArithmetics()
-{
-}
-
-/*!
- Creates numbers by it's string representation [redefined virtual]
-*/
-bool QtxArithmetics::createValue( const QString& str, QtxValue& v ) const
-{
- bool ok = false;
- v = str.toInt( &ok );
-
- if( !ok )
- {
- v = str.toDouble( &ok );
- if( !ok )
- ok = QtxStdOperations::createValue( str, v );
- }
- return ok;
-}
-
-/*!
- \return priority of arithmetic operation 'op'.
- \param isBin indicate whether the operation is binary
-*/
-int QtxArithmetics::prior( const QString& op, bool isBin ) const
-{
- if( isBin )
- if( op=="<" || op==">" || op=="=" ||
- op=="<=" || op==">=" || op=="<>" || op=="!=" )
- return 1;
- else if( op=="+" || op=="-" )
- return 2;
- else if( op=="*" || op=="/" )
- return 3;
- else
- return 0;
- else if( op=="+" || op=="-" )
- return 5;
- else
- return 0;
-}
-
-void set( QVariant& v1, bool v2 )
-{
- v1 = QVariant( v2, 0 );
-}
-
-/*!
- Calculates result of operation
- \return one of error states
- \param op - name of operation
- \param v1 - first operation argument (must be used also to store result)
- \param v2 - second operation argument
-*/
-QtxParser::Error QtxArithmetics::calculate( const QString& op,
- QtxValue& v1, QtxValue& v2 ) const
-{
- QtxParser::Error err = QtxParser::OK;
-
- if( v1.isValid() && v2.isValid() )
- // binary operations
- if( ( v1.type()==QVariant::Int || v1.type()==QVariant::UInt ) &&
- ( v2.type()==QVariant::Int || v2.type()==QVariant::UInt ) )
- {
- int _v1 = v1.toInt(),
- _v2 = v2.toInt();
-
- if( op=="+" )
- v1 = _v1 + _v2;
- else if( op=="-" )
- v1 = _v1 - _v2;
- else if( op=="*" )
- v1 = _v1 * _v2;
- else if( op=="/" )
- if( _v2!=0 )
- if( _v1%_v2==0 )
- v1 = _v1 / _v2;
- else
- v1 = double( _v1 ) / double( _v2 );
- else
- err = QtxParser::InvalidResult;
- else if( op=="<" )
- set( v1, _v1<_v2 );
- else if( op==">" )
- set( v1, _v1>_v2 );
- else if( op=="=" )
- set( v1, _v1==_v2 );
- else if( op=="<=" )
- set( v1, _v1<=_v2 );
- else if( op==">=" )
- set( v1, _v1>=_v2 );
- else if( op=="<>" || op=="!=" )
- set( v1, _v1!=_v2 );
- }
- else if( ( v1.type()==QVariant::Int || v1.type()==QVariant::Double ) &&
- ( v2.type()==QVariant::Int || v2.type()==QVariant::Double ) )
- {
- double _v1 = v1.toDouble(),
- _v2 = v2.toDouble();
-
- if( op=="+" )
- v1 = _v1 + _v2;
- else if( op=="-" )
- v1 = _v1 - _v2;
- else if( op=="*" )
- v1 = _v1 * _v2;
- else if( op=="/" )
- if( _v2!=0 )
- v1 = _v1 / _v2;
- else
- err = QtxParser::InvalidResult;
- else if( op=="<" )
- set( v1, _v1<_v2 );
- else if( op==">" )
- set( v1, _v1>_v2 );
- else if( op=="=" )
- set( v1, _v1==_v2 );
- else if( op=="<=" )
- set( v1, _v1<=_v2 );
- else if( op==">=" )
- set( v1, _v1>=_v2 );
- else if( op=="<>" || op=="!=" )
- set( v1, _v1!=_v2 );
- }
- else
- // prefix operations
- if( op=="-" )
- if( v2.type()==QVariant::Int )
- v2 = -v2.toInt();
- else if( v2.type()==QVariant::Double )
- v2 = -v2.toDouble();
-
- return err;
-}
-
-
-
-
-
-
-
-
-
-
-
-/*!
- Default constructor
-*/
-QtxLogic::QtxLogic()
-: QtxStdOperations()
-{
- QStringList aList;
- aList.append( "and" );
- aList.append( "&&" );
- aList.append( "or" );
- aList.append( "||" );
- aList.append( "xor" );
- aList.append( "not" );
- aList.append( "!" );
- aList.append( "imp" );
- aList.append( "=" );
- addOperations( aList );
-
- ListOfTypes aTypes;
- aTypes.append( QVariant::Bool );
- aTypes.append( QVariant::Int );
- aTypes.append( QVariant::UInt );
- addTypes( aTypes );
-}
-
-/*!
- Destructor
-*/
-QtxLogic::~QtxLogic()
-{
-}
-
-/*!
- Creates value 'true' or 'false' it's string representation [redefined virtual]
-*/
-bool QtxLogic::createValue( const QString& str, QtxValue& v ) const
-{
- bool ok = true;
- if( str.lower()=="true" )
- v = QtxValue( true, 0 );
- else if( str.lower()=="false" )
- v = QtxValue( false, 0 );
- else
- ok = QtxStdOperations::createValue( str, v );
-
- return ok;
-}
-
-/*!
- \return priority of arithmetic operation 'op'.
- \param isBin indicate whether the operation is binary
-*/
-int QtxLogic::prior( const QString& op, bool isBin ) const
-{
- if( isBin )
- if( op=="and" || op=="or" || op=="xor" ||
- op=="&&" || op=="||" ||
- op=="imp" )
- return 1;
- else if( op=="=" )
- return 2;
- else
- return 0;
- else if( op=="not" || op=="!" )
- return 5;
- else
- return 0;
-}
-
-bool boolean_value( const QtxValue& v )
-{
- if( v.type()==QVariant::Bool )
- return v.toBool();
- else if( v.type()==QVariant::Int )
- return v.toInt()!=0;
- else if( v.type()==QVariant::UInt )
- return v.toUInt()!=0;
- else
- return false;
-}
-
-/*!
- Calculates result of operation
- \return one of error states
- \param op - name of operation
- \param v1 - first operation argument (must be used also to store result)
- \param v2 - second operation argument
-*/
-QtxParser::Error QtxLogic::calculate( const QString& op,
- QtxValue& v1, QtxValue& v2 ) const
-{
- QtxParser::Error err = QtxParser::OK;
- bool val1 = boolean_value( v1 ),
- val2 = boolean_value( v2 );
- if( v1.isValid() && v2.isValid() )
- {
- if( op=="and" || op=="&&" )
- set( v1, val1 && val2 );
- else if( op=="or" || op=="||" )
- set( v1, val1 || val2 );
- else if( op=="xor" )
- set( v1, ( !val1 && val2 ) || ( val1 && !val2 ) );
- else if( op=="imp" )
- set( v1, !val1 || val2 );
- else if( op=="=" )
- set( v1, val1==val2 );
- }
- else
- if( op=="not" || op=="!" )
- set( v2, !val2 );
-
- return err;
-}
-
-
-
-
-
-
-/*!
- Default constructor
-*/
-QtxFunctions::QtxFunctions()
-: QtxStdOperations()
-{
- QStringList aList;
- aList.append( "sqrt" );
- aList.append( "abs" );
- aList.append( "sin" );
- aList.append( "cos" );
- aList.append( "rad2grad" );
- aList.append( "grad2rad" );
- addOperations( aList );
-
- ListOfTypes aTypes;
- aTypes.append( QVariant::Int );
- aTypes.append( QVariant::Double );
- addTypes( aTypes );
-}
-
-/*!
- Destructor
-*/
-QtxFunctions::~QtxFunctions()
-{
-}
-
-/*!
- Creates numbers by it's string representation [redefined virtual]
-*/
-bool QtxFunctions::createValue( const QString& str, QtxValue& v ) const
-{
- bool ok = false;
- v = str.toInt( &ok );
-
- if( !ok )
- {
- v = str.toDouble( &ok );
- if( !ok )
- ok = QtxStdOperations::createValue( str, v );
- }
- return ok;
-}
-
-/*!
- \return priority of arithmetic operation 'op'.
- \param isBin indicate whether the operation is binary
-*/
-int QtxFunctions::prior( const QString& op, bool isBin ) const
-{
- if( isBin )
- return 0;
- else if( op=="sqrt" || op=="abs" || op=="sin" || op=="cos" ||
- op=="rad2grad" || op=="grad2rad" )
- return 1;
- else
- return 0;
-}
-
-/*!
- Calculates result of operation
- \return one of error states
- \param op - name of operation
- \param v1 - first operation argument (must be used also to store result)
- \param v2 - second operation argument
-*/
-QtxParser::Error QtxFunctions::calculate( const QString& op,
- QtxValue& v1, QtxValue& v2 ) const
-{
- QtxParser::Error err = QtxParser::OK;
- double val = v2.toDouble();
- if( op=="sqrt" )
- if( val>=0 )
- v2 = sqrt( val );
- else
- err = QtxParser::InvalidResult;
- else if( op=="abs" )
- if( v2.type()==QVariant::Int )
- v2 = abs( v2.toInt() );
- else
- v2 = fabs( v2.toDouble() );
- else if( op=="sin" )
- v2 = sin( val );
- else if( op=="cos" )
- v2 = cos( val );
- else if( op=="grad2rad" )
- v2 = val * 3.14159256 / 180.0;
- else if( op=="rad2grad" )
- v2 = val * 180.0 / 3.14159256;
-
- return err;
-}
-
-
-
-
-
-
-
-
-/*!
- Default constructor
-*/
-QtxStrings::QtxStrings()
-: QtxStdOperations()
-{
- QStringList aList;
- aList.append( "+" );
- aList.append( "=" );
- aList.append( "<" );
- aList.append( ">" );
- aList.append( "<=" );
- aList.append( ">=" );
- aList.append( "<>" );
- aList.append( "!=" ); // same as "<>" - for C++ addicts
- aList.append( "length" );
- aList.append( "lower" );
- aList.append( "upper" );
- addOperations( aList );
-
- ListOfTypes aTypes;
- aTypes.append( QVariant::Int );
- aTypes.append( QVariant::Double );
- aTypes.append( QVariant::String );
- aTypes.append( QVariant::CString );
- addTypes( aTypes );
-}
-
-
-/*!
- Destructor
-*/
-QtxStrings::~QtxStrings()
-{
-}
-
-/*!
- Creates string QtxValue by it's Qt string representation [redefined virtual]
-*/
-bool QtxStrings::createValue( const QString& str, QtxValue& v ) const
-{
- QChar st = str[0],
- fin = str[ ( int )( str.length()-1 ) ];
- if( st=="'" && fin=="'" )
- {
- v = str.mid( 1, str.length()-2 );
- return true;
- }
- else
- return QtxStdOperations::createValue( str, v );
-}
-
-/*!
- \return priority of arithmetic operation 'op'.
- \param isBin indicate whether the operation is binary
-*/
-int QtxStrings::prior( const QString& op, bool isBin ) const
-{
- if( isBin )
- if( op=="+" )
- return 2;
- else if( op=="=" || op=="<" || op==">" ||
- op=="<=" || op==">=" || op=="<>" || op=="!=" )
- return 1;
- else
- return 0;
- else
- if( op=="length" || op=="lower" || op=="upper" )
- return 5;
- else
- return 0;
-}
-
-/*!
- Calculates result of operation
- \return one of error states
- \param op - name of operation
- \param v1 - first operation argument (must be used also to store result)
- \param v2 - second operation argument
-*/
-QtxParser::Error QtxStrings::calculate( const QString& op,
- QtxValue& v1, QtxValue& v2 ) const
-{
- QtxParser::Error err = QtxParser::OK;
- if( v1.isValid() && v2.isValid() )
- {
- QString _v1 = v1.toString(),
- _v2 = v2.toString();
- if( op=="+" )
- v1 = _v1 + _v2;
- else if( op=="=" )
- set( v1, _v1==_v2 );
- else if( op=="<" )
- set( v1, _v1<_v2 );
- else if( op==">" )
- set( v1, _v1>_v2 );
- else if( op=="<>" || op=="!=" )
- set( v1, _v1!=_v2 );
- else if( op=="<=" )
- set( v1, _v1<_v2 || _v1==_v2 );
- else if( op==">=" )
- set( v1, _v1>_v2 || _v1==_v2 );
- }
- else if( !v1.isValid() && v2.isValid() )
- {
- QString val = v2.toString();
- if( op=="length" )
- v2 = ( int )val.length();
- else if( op=="lower" )
- v2 = val.lower();
- else if( op=="upper" )
- v2 = val.upper();
- }
-
- return err;
-}
-
-
-
-
-
-
-/*!
- Default constructor
-*/
-QtxSets::QtxSets()
-{
- QStringList aList;
- aList.append( "{" );
- aList.append( "}" );
- aList.append( "=" );
- aList.append( "<>" );
- aList.append( "!=" ); // same as "<>" - for C++ addicts
- aList.append( "+" );
- aList.append( "-" );
- aList.append( "*" );
- aList.append( "in" );
- aList.append( "count" );
- addOperations( aList );
-
- ListOfTypes aTypes;
- aTypes.append( QVariant::List );
- addTypes( aTypes );
-}
-
-/*!
- Destructor
-*/
-QtxSets::~QtxSets()
-{
-}
-
-/*!
- Fills list of brackets treated as open (when 'open' is 'true') or close ('open' is 'false')
-*/
-void QtxSets::bracketsList( QStringList& list, bool open ) const
-{
- if( open )
- list.append( "{" );
- else
- list.append( "}" );
- QtxStdOperations::bracketsList( list, open );
-}
-
-/*!
- Creates set of QtxValues by their string representation [redefined virtual]
-*/
-bool QtxSets::createValue( const QString& str, QtxValue& val ) const
-{
- return QtxStdOperations::createValue( str, val );
-}
-
-/*!
- \return priority of arithmetic operation 'op'.
- \param isBin indicate whether the operation is binary
-*/
-int QtxSets::prior( const QString& op, bool isBin ) const
-{
- if( isBin )
- if( op=="=" || op=="<>" || op=="!=" )
- return 1;
- else if( op=="+" || op=="-" || op=="*" )
- return 2;
- else if( op=="in" )
- return 3;
- else
- return 0;
-
- else
- if( op=="{" || op=="}" )
- return 5;
- else if( op=="count" )
- return 4;
- else
- return 0;
-}
-
-/*!
- \return whether values with passed types are valid for arguments of operation
- \param op - name of operation
- \param t1 - type of first argument
- \param t2 - type of second argument
-*/
-QtxParser::Error QtxSets::isValid( const QString& op,
- const QVariant::Type t1,
- const QVariant::Type t2 ) const
-{
- if( op=="{" )
- return QtxParser::OK;
- else if( op=="in" )
- if( t1!=QVariant::Invalid && t2==QVariant::List )
- return QtxParser::OK;
- else
- return QtxParser::OperandsNotMatch;
- else
- return QtxStdOperations::isValid( op, t1, t2 );
-}
-
-/*!
- Adds new value 'v' to set 'set' [static]
-*/
-void QtxSets::add( ValueSet& set, const QtxValue& v )
-{
- if( v.isValid() && set.contains( v )==0 )
- set.append( v );
-}
-
-/*!
- Adds values from set 's2' to set 's1'
-*/
-void QtxSets::add( ValueSet& s1, const ValueSet& s2 )
-{
- ValueSet::const_iterator anIt = s2.begin(),
- aLast = s2.end();
- for( ; anIt!=aLast; anIt++ )
- add( s1, *anIt );
-}
-
-/*!
- Removes value 'v' from set 'set'
-*/
-void QtxSets::remove( ValueSet& set, const QtxValue& v )
-{
- set.remove( v );
-}
-
-/*!
- Removes values of set 's2' from set 's1'
-*/
-void QtxSets::remove( ValueSet& s1, const ValueSet& s2 )
-{
- ValueSet::const_iterator anIt = s2.begin(),
- aLast = s2.end();
- for( ; anIt!=aLast; anIt++ )
- s1.remove( *anIt );
-}
-
-/*!
- Calculates result of operation
- \return one of error states
- \param op - name of operation
- \param v1 - first operation argument (must be used also to store result)
- \param v2 - second operation argument
-*/
-QtxParser::Error QtxSets::calculate( const QString& op, QtxValue& v1, QtxValue& v2 ) const
-{
- QtxParser::Error err = QtxParser::OK;
-
- if( op!="{" )
- if( op=="}" )
- {
- ValueSet aNewList;
- add( aNewList, v1.toList() );
- v1 = aNewList;
- }
-
- else if( op=="=" || op=="<>" || op=="!=" || op=="+" || op=="-" || op=="*" )
- {
- ValueSet aNewList;
- add( aNewList, v1.toList() );
-
- if( op=="=" || op=="<>" || op=="!=" || op=="-" )
- {
- remove( aNewList, v2.toList() );
-
- if( op=="=" )
- set( v1, aNewList.isEmpty() && v1.toList().count()==v2.toList().count() );
- else if( op=="<>" || op=="!=" )
- set( v1, !aNewList.isEmpty() || v1.toList().count()!=v2.toList().count() );
- else
- v1 = aNewList;
- }
- else if( op=="+" )
- {
- add( aNewList, v2.toList() );
- v1 = aNewList;
- }
- else if( op=="*" )
- {
- ValueSet toDelete;
- add( toDelete, aNewList );
- remove( toDelete, v2.toList() );
- remove( aNewList, toDelete );
- v1 = aNewList;
- }
- }
- else if( op=="count" )
- v2 = ( int )v2.toList().count();
- else if( op=="in" )
- {
- if( v1.type()==QVariant::List )
- {
- bool res = true;
- ValueSet::const_iterator anIt = v1.toList().begin(),
- aLast = v1.toList().end();
- for( ; anIt!=aLast && res; anIt++ )
- res = v2.toList().contains( *anIt )>0;
-
- set( v1, res );
- }
- else
- {
- const QValueList< QVariant >& list = v2.toList();
- v1 = ( bool )( list.find( v1 )!=list.end() );
- }
- }
-
- return err;
-}
+++ /dev/null
-// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
-//
-// This library is free software; you can redistribute it and/or
-// modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either
-// version 2.1 of the License.
-//
-// This library is distributed in the hope that it will be useful
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public
-// License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-//
-// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
-//
-// File: QtxStdOperations.h
-// Author: Alexander SOLOVYOV
-
-#ifndef __QTX_STD_OPERATIONS_HEADER__
-#define __QTX_STD_OPERATIONS_HEADER__
-
-#include "Qtx.h"
-#include "QtxOperations.h"
-
-/*!
- \class QtxStdOperations
- Provides simplified interface for standard operations.
- Conatins list of operation names, priority and possible types
-*/
-class QTX_EXPORT QtxStdOperations : public QtxOperations
-{
-public:
- QtxStdOperations();
- virtual ~QtxStdOperations();
-
- virtual void opersList( QStringList& ) const;
- virtual void bracketsList( QStringList&, bool open ) const;
-
- virtual QtxParser::Error isValid( const QString&,
- const QVariant::Type,
- const QVariant::Type ) const;
-protected:
- typedef QValueList< QVariant::Type > ListOfTypes;
-
- void addOperations( const QStringList& );
- void addTypes( const ListOfTypes& );
-
-private:
- QStringList myOpers;
- ListOfTypes myTypes;
-};
-
-/*!
- \class QtxArithmetics
- Provides set of arithmetic operations for parser
-*/
-class QTX_EXPORT QtxArithmetics : public QtxStdOperations
-{
-public:
- QtxArithmetics();
- virtual ~QtxArithmetics();
-
- virtual bool createValue( const QString&, QtxValue& ) const;
- virtual int prior( const QString&, bool isBin ) const;
- virtual QtxParser::Error calculate( const QString&, QtxValue&, QtxValue& ) const;
-};
-
-/*!
- \class QtxLogic
- Provides set of logic operations for parser
-*/
-class QTX_EXPORT QtxLogic : public QtxStdOperations
-{
-public:
- QtxLogic();
- virtual ~QtxLogic();
-
- virtual bool createValue( const QString&, QtxValue& ) const;
- virtual int prior( const QString&, bool isBin ) const;
- virtual QtxParser::Error calculate( const QString&, QtxValue&, QtxValue& ) const;
-};
-
-/*!
- \class QtxFunctions
- Provides set of more complex operations for parser (sqrt, sin, cos, etc)
-*/
-class QTX_EXPORT QtxFunctions : public QtxStdOperations
-{
-public:
- QtxFunctions();
- virtual ~QtxFunctions();
-
- virtual bool createValue( const QString&, QtxValue& ) const;
- virtual int prior( const QString&, bool isBin ) const;
- virtual QtxParser::Error calculate( const QString&, QtxValue&, QtxValue& ) const;
-};
-
-/*!
- \class QtxStrings
- Provides set of string operations for parser
-*/
-class QTX_EXPORT QtxStrings : public QtxStdOperations
-{
-public:
- QtxStrings();
- virtual ~QtxStrings();
-
- virtual bool createValue( const QString&, QtxValue& ) const;
- virtual int prior( const QString&, bool isBin ) const;
- virtual QtxParser::Error calculate( const QString&, QtxValue&, QtxValue& ) const;
-};
-
-/*!
- \class QtxSets
- Provides set of operations with sets for parser
-*/
-class QTX_EXPORT QtxSets : public QtxStdOperations
-{
-public:
- typedef QValueList< QtxValue > ValueSet;
-
-public:
- QtxSets();
- virtual ~QtxSets();
-
- virtual void bracketsList( QStringList&, bool open ) const;
- virtual bool createValue( const QString&, QtxValue& ) const;
- virtual int prior( const QString&, bool isBin ) const;
- virtual QtxParser::Error isValid( const QString&,
- const QVariant::Type,
- const QVariant::Type ) const;
- virtual QtxParser::Error calculate( const QString&, QtxValue&, QtxValue& ) const;
-
- static void add( ValueSet&, const QtxValue& );
- static void add( ValueSet&, const ValueSet& );
- static void remove( ValueSet&, const QtxValue& );
- static void remove( ValueSet&, const ValueSet& );
-};
-
-
-#endif