From ef6fb1dd0cc7908a98f7ff37e4e6a66903b1d9b9 Mon Sep 17 00:00:00 2001 From: obiwankennedy Date: Mon, 20 Jan 2014 10:21:21 +0100 Subject: Create parsingtoolbox.cpp library of parsing methods --- parsingtoolbox.cpp | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 parsingtoolbox.cpp diff --git a/parsingtoolbox.cpp b/parsingtoolbox.cpp new file mode 100644 index 0000000..46f159d --- /dev/null +++ b/parsingtoolbox.cpp @@ -0,0 +1,132 @@ +#include + +#include "parsingtoolbox.h" +#include "node/sortresult.h" + + +ParsingToolBox::ParsingToolBox() +{ + m_logicOp = new QMap(); + m_logicOp->insert(">=",BooleanCondition::GreaterOrEqual); + m_logicOp->insert("<=",BooleanCondition::LesserOrEqual); + m_logicOp->insert("<",BooleanCondition::LesserThan); + m_logicOp->insert("=",BooleanCondition::Equal); + m_logicOp->insert(">",BooleanCondition::GreaterThan); +} +ExecutionNode* ParsingToolBox::addSort(ExecutionNode* e,bool b) +{ + SortResultNode* nodeSort = new SortResultNode(); + nodeSort->setSortAscending(b); + e->setNextNode(nodeSort); + return nodeSort; +} +bool ParsingToolBox::readLogicOperator(QString& str,BooleanCondition::LogicOperator& op) +{ + QString longKey; + foreach(QString tmp, m_logicOp->keys()) + { + if(str.startsWith(tmp)) + { + if(longKey.size()0) + { + str=str.remove(0,longKey.size()); + op = m_logicOp->value(longKey); + return true; + } + + return false; +} +Validator* ParsingToolBox::readValidator(QString& str) +{ + Validator* returnVal=NULL; + bool expectSquareBrasket=false; + bool isOk = true; + if((str.startsWith("["))) + { + str=str.remove(0,1); + expectSquareBrasket = true; + } + + BooleanCondition::LogicOperator myLogicOp = BooleanCondition::Equal; + bool hasReadLogicOperator = readLogicOperator(str,myLogicOp); + int value=0; + + if(readNumber(str,value)) + { + if(str.startsWith("-")) + { + str=str.remove(0,1); + int end=0; + if(readNumber(str,end)) + { + if(expectSquareBrasket) + { + if(str.startsWith("]")) + { + str=str.remove(0,1); + isOk=true; + } + else + { + isOk=false; + } + } + if(isOk) + { + str=str.remove(0,1); + Range* range = new Range(); + range->setValue(value,end); + returnVal = range; + } + } + } + else + { + if((expectSquareBrasket)&&(str.startsWith("]"))) + { + str=str.remove(0,1); + isOk=true; + } + if(isOk) + { + BooleanCondition* condition = new BooleanCondition(); + condition->setValue(value); + condition->setOperator(myLogicOp); + returnVal = condition; + } + } + } + return returnVal; +} +bool ParsingToolBox::readNumber(QString& str, int& myNumber) +{ + if(str.isEmpty()) + return false; + + QString number; + int i=0; + + while(i