/*************************************************************************** * Copyright (C) 2014 by Renaud Guezennec * * http://renaudguezennec.homelinux.org/accueil,3.html * * * * This file is part of DiceParser * * * * DiceParser is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program 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 General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #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; } ParsingToolBox::~ParsingToolBox() { if(NULL!=m_logicOp) { delete m_logicOp; m_logicOp = NULL; } } 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& ranges) { if(str.startsWith("[")) { str=str.remove(0,1); int pos = str.lastIndexOf("]"); if(-1!=pos) { QString liststr = str.left(pos); list = liststr.split(","); str=str.remove(0,pos+1); readProbability(list,ranges); return true; } } return false; } bool ParsingToolBox::readAscending(QString& str) { if(str.isEmpty()) { return false; } else if(str.at(0)=='l') { str=str.remove(0,1); return true; } return false; } bool ParsingToolBox::isValidValidator(ExecutionNode* previous, Validator* val) { DiceRollerNode* node = getDiceRollerNode(previous); if(NULL!=node) { return (val->getValidRangeSize(node->getFaces())getFaces()); } else { return true; } } DiceRollerNode* ParsingToolBox::getDiceRollerNode(ExecutionNode* previous) { while(NULL!=previous) { DiceRollerNode* node = dynamic_cast(previous); if(NULL!=node) { return node; } previous = previous->getPreviousNode(); } } bool ParsingToolBox::readDiceRange(QString& str,int& start, int& end) { bool expectSquareBrasket=false; if((str.startsWith("["))) { str=str.remove(0,1); expectSquareBrasket = true; } if(readNumber(str,start)) { if(str.startsWith("-")) { str=str.remove(0,1); if(readNumber(str,end)) { if(expectSquareBrasket) { if(str.startsWith("]")) { str=str.remove(0,1); return true; } else { return false; } } } else { return false; } } } } ParsingToolBox::LIST_OPERATOR ParsingToolBox::readListOperator(QString& str) { if(str.startsWith('u')) { return UNIQUE; } return NONE; } void ParsingToolBox::readProbability(QStringList& str,QList& ranges) { quint64 totalDistance=0; quint64 undefDistance = 0; int undefCount=0; int maxValue = 0; int i=0; int j=0; //range foreach(QString line,str) { int pos = line.indexOf('['); if(-1!=pos) { QString range = line.right(line.length()-pos); line = line.left(pos); str[j]=line; int start; int end; if(readDiceRange(range,start,end)) { Range range; range.setValue(start,end); ranges.append(range); totalDistance += end-start+1; ++i; } else { Range range; range.setStart(start); ranges.append(range); ++undefCount; undefDistance +=start; } if((end>maxValue)||(i==1)) { maxValue = end; } } ++j; } qint64 totalDistPourcent = totalDistance * undefDistance / (100-undefDistance); if(totalDistPourcent