From 379ffeb21fd4f067ea542e6b45967bab1ca004d5 Mon Sep 17 00:00:00 2001 From: Renaud G Date: Mon, 6 Apr 2015 14:26:23 +0200 Subject: -Creation of dedicated class for alias management --- diceparser.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'diceparser.cpp') diff --git a/diceparser.cpp b/diceparser.cpp index 2c533dc..08decde 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -60,12 +60,11 @@ DiceParser::DiceParser() //m_OptionOp->insert(QObject::tr("@"),JumpBackward); - - m_aliasMap = new QMap; - m_aliasMap->insert("l5r","D10k"); - m_aliasMap->insert("l5R","D10K"); - m_aliasMap->insert("nwod","D10e10c[>7]"); - m_aliasMap->insert("nwod","D10e10c[>7]"); + m_aliasList = new QList(); + m_aliasList->append(new DiceAlias("l5r","D10k")); + m_aliasList->append(new DiceAlias("l5R","D10K")); + m_aliasList->append(new DiceAlias("nwod","D10e10c[>7]")); + m_aliasList->append(new DiceAlias("(.*)wod(.*)","\\1d10e[=10]c[>=\\2]-@c[=1]",false)); m_nodeActionMap = new QMap(); m_nodeActionMap->insert("@",JumpBackward); @@ -88,15 +87,16 @@ ExecutionNode* DiceParser::getLatestNode(ExecutionNode* node) } QString DiceParser::convertAlias(QString str) { - foreach(QString cmd, m_aliasMap->keys()) + foreach(DiceAlias* cmd, *m_aliasList) { - if(str.contains(cmd)) - { - str.replace(cmd,m_aliasMap->value(cmd)); - } + cmd->resolved(str); } return str; } +QList* DiceParser::getAliases() +{ + return m_aliasList; +} bool DiceParser::parseLine(QString str) { @@ -539,7 +539,7 @@ bool DiceParser::readCommand(QString& str,ExecutionNode* & node) } else if(str=="la") { - node = new ListAliasNode(m_aliasMap); + node = new ListAliasNode(m_aliasList); } return true; } -- cgit v1.2.3-70-g09d2 From 77ce7ecc41f1f5b6a4f74187451a948ad4ad7721 Mon Sep 17 00:00:00 2001 From: Renaud G Date: Tue, 7 Apr 2015 08:35:40 +0200 Subject: remove default diceAlias. It must be managed by the end-user application. --- diceparser.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'diceparser.cpp') diff --git a/diceparser.cpp b/diceparser.cpp index 08decde..09172a0 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -61,10 +61,10 @@ DiceParser::DiceParser() m_aliasList = new QList(); - m_aliasList->append(new DiceAlias("l5r","D10k")); + /*m_aliasList->append(new DiceAlias("l5r","D10k")); m_aliasList->append(new DiceAlias("l5R","D10K")); m_aliasList->append(new DiceAlias("nwod","D10e10c[>7]")); - m_aliasList->append(new DiceAlias("(.*)wod(.*)","\\1d10e[=10]c[>=\\2]-@c[=1]",false)); + m_aliasList->append(new DiceAlias("(.*)wod(.*)","\\1d10e[=10]c[>=\\2]-@c[=1]",false));*/ m_nodeActionMap = new QMap(); m_nodeActionMap->insert("@",JumpBackward); -- cgit v1.2.3-70-g09d2 From 5cfe48265ebad0ffa0a980dbc2d131fceeecdf3b Mon Sep 17 00:00:00 2001 From: Renaud G Date: Wed, 8 Apr 2015 21:34:02 +0200 Subject: -remove memory leaks -delete created objects --- diceParser.pro | 6 +++-- diceparser.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++--- diceparser.h | 4 ++++ main.cpp | 8 +++++-- node/countexecutenode.cpp | 9 +++++++- node/countexecutenode.h | 1 + node/dicerollernode.cpp | 8 +++---- node/dicerollernode.h | 3 +-- node/executionnode.cpp | 10 +++++++++ node/explosedicenode.cpp | 9 +++++++- node/explosedicenode.h | 1 + node/listsetrollnode.cpp | 8 +++++++ node/listsetrollnode.h | 1 + node/rerolldicenode.cpp | 10 ++++++++- node/rerolldicenode.h | 28 +++++++++++++++++++++++ node/scalaroperatornode.cpp | 18 ++++++++++++--- node/scalaroperatornode.h | 3 ++- parsingtoolbox.cpp | 9 ++++++++ parsingtoolbox.h | 7 ++++++ result/diceresult.cpp | 14 +++++------- result/diceresult.h | 4 ++++ result/stringresult.cpp | 4 ++++ result/stringresult.h | 4 ++++ 23 files changed, 195 insertions(+), 29 deletions(-) (limited to 'diceparser.cpp') diff --git a/diceParser.pro b/diceParser.pro index 2d72fcb..c339933 100644 --- a/diceParser.pro +++ b/diceParser.pro @@ -43,7 +43,8 @@ SOURCES += main.cpp \ result/result.cpp \ result/scalarresult.cpp \ parsingtoolbox.cpp \ - result/stringresult.cpp + result/stringresult.cpp \ + dicealias.cpp HEADERS += \ @@ -56,7 +57,8 @@ HEADERS += \ result/result.h \ result/scalarresult.h \ result/parsingtoolbox.h \ - result/stringresult.h + result/stringresult.h \ + dicealias.h OTHER_FILES += README.md \ HelpMe.md diff --git a/diceparser.cpp b/diceparser.cpp index 09172a0..5e6d06f 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -42,6 +42,7 @@ #define DEFAULT_FACES_NUMBER 10 DiceParser::DiceParser() + : m_start(NULL) { m_parsingToolbox = new ParsingToolBox(); @@ -75,6 +76,44 @@ DiceParser::DiceParser() m_commandList->append(QObject::tr("la")); } +DiceParser::~DiceParser() +{ + if(NULL!=m_commandList) + { + delete m_commandList; + m_commandList = NULL; + } + if(NULL!=m_nodeActionMap) + { + delete m_nodeActionMap; + m_nodeActionMap = NULL; + } + if(NULL!=m_OptionOp) + { + delete m_OptionOp; + m_OptionOp = NULL; + } + if(NULL!=m_mapDiceOp) + { + delete m_mapDiceOp; + m_mapDiceOp = NULL; + } + if(NULL!=m_parsingToolbox) + { + delete m_parsingToolbox; + m_parsingToolbox = NULL; + } + if(NULL!=m_aliasList) + { + delete m_aliasList; + m_aliasList = NULL; + } + if(NULL!=m_start) + { + delete m_start; + m_start = NULL; + } +} ExecutionNode* DiceParser::getLatestNode(ExecutionNode* node) { @@ -101,6 +140,11 @@ QList* DiceParser::getAliases() bool DiceParser::parseLine(QString str) { m_errorMap.clear(); + if(NULL!=m_start) + { + delete m_start; + m_start = NULL; + } m_command = str; m_start = new StartingNode(); ExecutionNode* newNode = NULL; @@ -599,6 +643,7 @@ bool DiceParser::readOperator(QString& str,ExecutionNode* previous) node->setInternalNode(nodeExec); if(NULL==nodeExec) { + delete node; return false; } if(node->getPriority()>=nodeExec->getPriority()) @@ -610,6 +655,10 @@ bool DiceParser::readOperator(QString& str,ExecutionNode* previous) return true; } + else + { + delete node; + } } else if(readInstructionOperator(str[0])) { @@ -851,10 +900,10 @@ bool DiceParser::readOperand(QString& str,ExecutionNode* & node) if(m_parsingToolbox->readNumber(str,myNumber)) { - NumberNode* myNumberNode = new NumberNode(); - myNumberNode->setNumber(myNumber); + NumberNode* numberNode = new NumberNode(); + numberNode->setNumber(myNumber); - node = myNumberNode; + node = numberNode; return true; } return false; diff --git a/diceparser.h b/diceparser.h index 7ec09ee..1636fc9 100644 --- a/diceparser.h +++ b/diceparser.h @@ -83,6 +83,10 @@ public: * @brief DiceParser default constructor */ DiceParser(); + /** + * @brief ~DiceParser + */ + virtual ~DiceParser(); /** * @brief parseLine, method to call for starting the dice roll. It will parse the command and run the execution tree. diff --git a/main.cpp b/main.cpp index 926e3d4..7b70e23 100644 --- a/main.cpp +++ b/main.cpp @@ -39,7 +39,8 @@ int main(int argc, char *argv[]) QStringList commands; - commands << "1L[cheminée,chocolat,épée,arc,chute de pierre]" + commands<< "10d10c[>6]+@c[=10]" + << "1L[cheminée,chocolat,épée,arc,chute de pierre]" << "10d10c[>=6]-@c[=1]" << "10d10c[>=6]-@c[=1]-@c[=1]" << "10d10c[>6]+@c[=10]" @@ -61,6 +62,7 @@ int main(int argc, char *argv[]) << "15D10e10c[8-10]" << "10d10e11" << "1D8+2D6+7" + << "100190D6666666s" << "D25" << "D25+D10" << "D25;D10" @@ -73,7 +75,7 @@ int main(int argc, char *argv[]) << "help" << "la" << "400000D20/400000" - << "100*3*8"; + << "100*3*8";// if(argc>1) { @@ -100,4 +102,6 @@ int main(int argc, char *argv[]) main.show(); return a.exec(); #endif + delete myParser; + return 0; } diff --git a/node/countexecutenode.cpp b/node/countexecutenode.cpp index 281fc80..1050efc 100644 --- a/node/countexecutenode.cpp +++ b/node/countexecutenode.cpp @@ -4,7 +4,7 @@ CountExecuteNode::CountExecuteNode() - : m_scalarResult(new ScalarResult()) + : m_scalarResult(new ScalarResult()),m_validator(NULL) { m_result = m_scalarResult; } @@ -12,6 +12,13 @@ void CountExecuteNode::setValidator(Validator* validator) { m_validator = validator; } +CountExecuteNode::~CountExecuteNode() +{ + if(NULL!=m_validator) + { + delete m_validator; + } +} void CountExecuteNode::run(ExecutionNode *previous) { diff --git a/node/countexecutenode.h b/node/countexecutenode.h index c7ecdfd..519403b 100644 --- a/node/countexecutenode.h +++ b/node/countexecutenode.h @@ -16,6 +16,7 @@ public: * @brief CountExecuteNode */ CountExecuteNode(); + virtual ~CountExecuteNode(); /** * @brief run * @param previous diff --git a/node/dicerollernode.cpp b/node/dicerollernode.cpp index 1fd1a2f..f32a033 100644 --- a/node/dicerollernode.cpp +++ b/node/dicerollernode.cpp @@ -33,11 +33,9 @@ /// \brief DiceRollerNode::DiceRollerNode ////////////////////////////////////////////////// DiceRollerNode::DiceRollerNode(quint64 faces) - : m_faces(faces),m_myDiceResult(new DiceResult()) + : m_faces(faces),m_diceResult(new DiceResult()) { - m_mutex=new QMutex(); - m_result=m_myDiceResult; - + m_result=m_diceResult; } void DiceRollerNode::run(ExecutionNode* previous) { @@ -55,7 +53,7 @@ void DiceRollerNode::run(ExecutionNode* previous) Die* die = new Die(); die->setFaces(m_faces); die->roll(); - m_myDiceResult->insertResult(die); + m_diceResult->insertResult(die); } if(NULL!=m_nextNode) { diff --git a/node/dicerollernode.h b/node/dicerollernode.h index d50fe95..744a4b5 100644 --- a/node/dicerollernode.h +++ b/node/dicerollernode.h @@ -36,8 +36,7 @@ public: private: quint64 m_diceCount; quint64 m_faces; /// faces - DiceResult* m_myDiceResult; - QMutex* m_mutex; + DiceResult* m_diceResult; }; #endif // DICEROLLERNODE_H diff --git a/node/executionnode.cpp b/node/executionnode.cpp index 8bcd6e4..343e8d9 100644 --- a/node/executionnode.cpp +++ b/node/executionnode.cpp @@ -8,6 +8,16 @@ ExecutionNode::ExecutionNode() ExecutionNode::~ExecutionNode() { + if(NULL!=m_result) + { + delete m_result; + m_result = NULL; + } + if(NULL!=m_nextNode) + { + delete m_nextNode; + m_nextNode = NULL; + } } Result* ExecutionNode::getResult() diff --git a/node/explosedicenode.cpp b/node/explosedicenode.cpp index 42b3c2d..4eae270 100644 --- a/node/explosedicenode.cpp +++ b/node/explosedicenode.cpp @@ -1,7 +1,7 @@ #include "explosedicenode.h" ExploseDiceNode::ExploseDiceNode() - : m_diceResult(new DiceResult()) + : m_diceResult(new DiceResult()),m_validator(NULL) { m_result = m_diceResult; } @@ -33,6 +33,13 @@ void ExploseDiceNode::run(ExecutionNode* previous) } } } +ExploseDiceNode::~ExploseDiceNode() +{ + if(NULL!=m_validator) + { + delete m_validator; + } +} void ExploseDiceNode::setValidator(Validator* val) { m_validator = val; diff --git a/node/explosedicenode.h b/node/explosedicenode.h index b00af1a..f5d0f6e 100644 --- a/node/explosedicenode.h +++ b/node/explosedicenode.h @@ -13,6 +13,7 @@ class ExploseDiceNode : public ExecutionNode { public: ExploseDiceNode(); + virtual ~ExploseDiceNode(); virtual void run(ExecutionNode* previous = NULL); virtual void setValidator(Validator* ); virtual QString toString()const; diff --git a/node/listsetrollnode.cpp b/node/listsetrollnode.cpp index 3ab9a69..777f7b0 100644 --- a/node/listsetrollnode.cpp +++ b/node/listsetrollnode.cpp @@ -6,6 +6,14 @@ ListSetRollNode::ListSetRollNode() { m_result = m_stringResult; } +ListSetRollNode::~ListSetRollNode() +{ + if(NULL!=m_diceResult) + { + delete m_diceResult; + m_diceResult =NULL; + } +} QStringList ListSetRollNode::getList() { diff --git a/node/listsetrollnode.h b/node/listsetrollnode.h index 26fb378..c7925b8 100644 --- a/node/listsetrollnode.h +++ b/node/listsetrollnode.h @@ -12,6 +12,7 @@ class ListSetRollNode : public ExecutionNode { public: ListSetRollNode(); + virtual ~ListSetRollNode(); virtual void run(ExecutionNode* previous = NULL); virtual QString toString()const; virtual qint64 getPriority() const; diff --git a/node/rerolldicenode.cpp b/node/rerolldicenode.cpp index 81b5e01..a7cc8c4 100644 --- a/node/rerolldicenode.cpp +++ b/node/rerolldicenode.cpp @@ -2,10 +2,18 @@ RerollDiceNode::RerollDiceNode() - : m_myDiceResult(new DiceResult()),m_adding(false) + : m_myDiceResult(new DiceResult()),m_adding(false),m_validator(NULL) { m_result=m_myDiceResult; } +RerollDiceNode::~RerollDiceNode() +{ + if(NULL!=m_validator) + { + delete m_validator; + m_validator = NULL; + } +} void RerollDiceNode::run(ExecutionNode* previous) { m_previousNode = previous; diff --git a/node/rerolldicenode.h b/node/rerolldicenode.h index 4674a3e..a97e448 100644 --- a/node/rerolldicenode.h +++ b/node/rerolldicenode.h @@ -12,16 +12,44 @@ class RerollDiceNode : public ExecutionNode { public: + /** + * @brief The ReRollMode enum + */ enum ReRollMode {EQUAL,LESSER,GREATER}; + /** + * @brief RerollDiceNode + */ RerollDiceNode(); + /** + * @brief ~RerollDiceNode + */ + virtual ~RerollDiceNode(); + /** + * @brief run + * @param previous + */ virtual void run(ExecutionNode* previous); + /** + * @brief setValidator + */ virtual void setValidator(Validator* ); + /** + * @brief toString + * @return + */ virtual QString toString()const; + /** + * @brief setAddingMode + */ virtual void setAddingMode(bool); + /** + * @brief getPriority + * @return + */ virtual qint64 getPriority() const; private: diff --git a/node/scalaroperatornode.cpp b/node/scalaroperatornode.cpp index 95fd077..34eb6b8 100644 --- a/node/scalaroperatornode.cpp +++ b/node/scalaroperatornode.cpp @@ -15,6 +15,14 @@ ScalarOperatorNode::ScalarOperatorNode() m_result = m_scalarResult; } +ScalarOperatorNode::~ScalarOperatorNode() +{ + if(NULL!=m_internalNode) + { + delete m_internalNode; + m_internalNode = NULL; + } +} void ScalarOperatorNode::run(ExecutionNode* previous) { @@ -43,7 +51,7 @@ void ScalarOperatorNode::run(ExecutionNode* previous) m_internalNode->getResult()->setPrevious(previousResult); } - switch(m_myOperator) + switch(m_operator) { case PLUS: m_scalarResult->setValue(add(previousResult->getResult(Result::SCALAR).toReal(),internalResult->getResult(Result::SCALAR).toReal())); @@ -74,7 +82,7 @@ bool ScalarOperatorNode::setOperatorChar(QChar c) { if(m_scalarOperationList.contains(c)) { - m_myOperator = m_scalarOperationList.value(c); + m_operator = m_scalarOperationList.value(c); return true; } return false; @@ -110,10 +118,14 @@ QString ScalarOperatorNode::toString() const } qint64 ScalarOperatorNode::getPriority() const { - if((m_myOperator==PLUS)||(m_myOperator==MINUS)) + if((m_operator==PLUS)||(m_operator==MINUS)) + { return 1; + } else + { return 2; + } } void ScalarOperatorNode::generateDotTree(QString& s) { diff --git a/node/scalaroperatornode.h b/node/scalaroperatornode.h index a67e296..7193118 100644 --- a/node/scalaroperatornode.h +++ b/node/scalaroperatornode.h @@ -12,6 +12,7 @@ class ScalarOperatorNode : public ExecutionNode public: enum ScalarOperator {PLUS,MINUS,DIVIDE,MULTIPLICATION}; ScalarOperatorNode(); + virtual ~ScalarOperatorNode(); virtual void run(ExecutionNode*); bool setOperatorChar(QChar c); void setInternalNode(ExecutionNode* node); @@ -28,7 +29,7 @@ private: qint64 multiple(qint64,qint64); private: - ScalarOperator m_myOperator; + ScalarOperator m_operator; ExecutionNode* m_internalNode; QMap m_scalarOperationList; ScalarResult* m_scalarResult; diff --git a/parsingtoolbox.cpp b/parsingtoolbox.cpp index e03690e..3e09bac 100644 --- a/parsingtoolbox.cpp +++ b/parsingtoolbox.cpp @@ -63,6 +63,14 @@ bool ParsingToolBox::readLogicOperator(QString& str,BooleanCondition::LogicOpera return false; } +ParsingToolBox::~ParsingToolBox() +{ + if(NULL!=m_logicOp) + { + delete m_logicOp; + m_logicOp = NULL; + } +} Validator* ParsingToolBox::readValidator(QString& str) { Validator* returnVal=NULL; @@ -181,6 +189,7 @@ bool ParsingToolBox::readList(QString& str,QStringList& list) { QString liststr = str.left(pos); list = liststr.split(","); + str=str.remove(0,pos+1); return true; } } diff --git a/parsingtoolbox.h b/parsingtoolbox.h index 43dca4e..d7fdc31 100644 --- a/parsingtoolbox.h +++ b/parsingtoolbox.h @@ -35,7 +35,14 @@ class ParsingToolBox { public: + /** + * @brief ParsingToolBox + */ ParsingToolBox(); + /** + * @brief ~ParsingToolBox + */ + virtual ~ParsingToolBox(); /** * @brief addSort * @param e diff --git a/result/diceresult.cpp b/result/diceresult.cpp index 0cabb11..f87079a 100644 --- a/result/diceresult.cpp +++ b/result/diceresult.cpp @@ -35,17 +35,15 @@ QList& DiceResult::getResultList() } void DiceResult::setResultList(QList list) { + qDeleteAll(m_diceValues.begin(), m_diceValues.end()); m_diceValues.clear(); m_diceValues << list; } -//bool DiceResult::isScalar() const -//{ -// if(m_diceValues.size()==1) -// { -// return true; -// } -// return false; -//} +DiceResult::~DiceResult() +{ + qDeleteAll(m_diceValues.begin(), m_diceValues.end()); + m_diceValues.clear(); +} QVariant DiceResult::getResult(RESULT_TYPE type) { diff --git a/result/diceresult.h b/result/diceresult.h index 838a83d..b805e73 100644 --- a/result/diceresult.h +++ b/result/diceresult.h @@ -35,6 +35,10 @@ public: * @brief DiceResult */ DiceResult(); + /** + * @brief ~DiceResult + */ + virtual ~DiceResult(); /** * @brief getResultList diff --git a/result/stringresult.cpp b/result/stringresult.cpp index 4831a76..9f7d2e1 100644 --- a/result/stringresult.cpp +++ b/result/stringresult.cpp @@ -8,6 +8,10 @@ void StringResult::setText(QString text) { m_value=text; } +StringResult::~StringResult() +{ + +} QString StringResult::getText() const { diff --git a/result/stringresult.h b/result/stringresult.h index caa7e06..17c43cd 100644 --- a/result/stringresult.h +++ b/result/stringresult.h @@ -13,6 +13,10 @@ public: * @brief StringResult */ StringResult(); + /** + * @brief StringResult + */ + virtual ~StringResult(); /** * @brief setText * @param text -- cgit v1.2.3-70-g09d2 From 64048b3fd2dba4220cb19946caf588f9a6f26093 Mon Sep 17 00:00:00 2001 From: Renaud G Date: Wed, 15 Apr 2015 01:07:36 +0200 Subject: add API to make easier the use of DiceParser. --- dicealias.cpp | 32 ++++++++++++++++++++++++++ dicealias.h | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- diceparser.cpp | 7 ++++++ diceparser.h | 4 ++++ 4 files changed, 110 insertions(+), 4 deletions(-) (limited to 'diceparser.cpp') diff --git a/dicealias.cpp b/dicealias.cpp index 2a09206..f9c366c 100644 --- a/dicealias.cpp +++ b/dicealias.cpp @@ -1,3 +1,24 @@ +/*************************************************************************** +* 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 "dicealias.h" #include @@ -65,3 +86,14 @@ bool DiceAlias::isReplace() } +void DiceAlias::setReplace(bool b) +{ + if(b) + { + m_type= REPLACE; + } + else + { + m_type = REGEXP; + } +} diff --git a/dicealias.h b/dicealias.h index 565b903..ebf7a02 100644 --- a/dicealias.h +++ b/dicealias.h @@ -1,24 +1,87 @@ +/*************************************************************************** +* 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. * +***************************************************************************/ #ifndef DICEALIAS_H #define DICEALIAS_H #include - +/** + * @brief The DiceAlias class + */ class DiceAlias { public: + enum RESOLUTION_TYPE { REPLACE,REGEXP}; + /** + * @brief DiceAlias + * @param cmd + * @param key + * @param isReplace + */ DiceAlias(QString cmd, QString key, bool isReplace = true); - ~DiceAlias(); - + /** + * @brief ~DiceAlias + */ + virtual ~DiceAlias(); + /** + * @brief resolved + * @param str + * @return + */ bool resolved(QString & str); - + /** + * @brief setCommand + * @param key + */ void setCommand(QString key); + /** + * @brief setValue + * @param value + */ void setValue(QString value); + /** + * @brief setType + */ void setType(RESOLUTION_TYPE ); + /** + * @brief getCommand + * @return + */ QString getCommand(); + /** + * @brief getValue + * @return + */ QString getValue(); + /** + * @brief isReplace + * @return + */ bool isReplace(); + /** + * @brief setReplace + */ + void setReplace(bool); private: QString m_command; QString m_value; diff --git a/diceparser.cpp b/diceparser.cpp index 5e6d06f..6933071 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -136,6 +136,13 @@ QList* DiceParser::getAliases() { return m_aliasList; } +void DiceParser::insertAlias(DiceAlias* dice, int i) +{ + if(i>m_aliasList->size()) + { + m_aliasList->insert(i, dice); + } +} bool DiceParser::parseLine(QString str) { diff --git a/diceparser.h b/diceparser.h index 1636fc9..b4fd429 100644 --- a/diceparser.h +++ b/diceparser.h @@ -167,6 +167,10 @@ public: * @return */ QList* getAliases(); + /** + * @brief insertAlias + */ + void insertAlias(DiceAlias*, int); /** * @brief DiceParser::convertAlias * @param str -- cgit v1.2.3-70-g09d2 From 186af57e85671f0c9c4b2b1faf0d2065f4b0f222 Mon Sep 17 00:00:00 2001 From: Renaud G Date: Tue, 21 Apr 2015 10:43:08 +0200 Subject: prepare much better way to give the result --- diceparser.cpp | 32 ++++++++++++++++++++++---------- die.cpp | 9 +++++++++ 2 files changed, 31 insertions(+), 10 deletions(-) (limited to 'diceparser.cpp') diff --git a/diceparser.cpp b/diceparser.cpp index 6933071..a2fa137 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -400,10 +400,12 @@ QString DiceParser::getStringResult() QString DiceParser::getLastDiceResult() { ExecutionNode* next = getLeafNode(); - QString str; - QTextStream stream(&str); + // QString str; + // QTextStream stream(&str); Result* result=next->getResult(); - QString dieValue("D%1 : {%2} "); + QMap diceValues; + //QString dieValue("%2 (D%1)"); + bool first=true; while(NULL!=result) { if(result->hasResultOfType(Result::DICE_LIST)) @@ -423,17 +425,16 @@ QString DiceParser::getLastDiceResult() die->displayed(); face = die->getFaces(); - if(die->hasChildrenValue()) { - resulStr+=" ["; + QString childDice(""); foreach(qint64 i, die->getListValue()) { - resulStr+=QString("%1,").arg(i); + childDice+=QString("%1,").arg(i); } - resulStr.remove(resulStr.size()-1,1); - resulStr+="]"; + childDice.remove(childDice.size()-1,1); + resulStr+=QString("[%1]").arg(childDice); } resulStr+=", "; } @@ -442,7 +443,17 @@ QString DiceParser::getLastDiceResult() if(!resulStr.isEmpty()) { - stream << dieValue.arg(face).arg(resulStr); + if(!diceValues.contains(face)) + { + diceValues.insert(face,resulStr); + } + else + { + QString tmp = diceValues.value(face); + tmp+=resulStr; + diceValues.insert(face,tmp); + } + //stream << dieValue.arg(face).arg(resulStr); } } } @@ -450,7 +461,8 @@ QString DiceParser::getLastDiceResult() result = result->getPrevious(); } - return str.simplified(); + //return str.simplified(); + return diceValues; } QString DiceParser::getDiceCommand() { diff --git a/die.cpp b/die.cpp index dc6576e..d8be064 100644 --- a/die.cpp +++ b/die.cpp @@ -132,3 +132,12 @@ void Die::displayed() { m_displayStatus = true; } +void Die::setHighlighted(bool a) +{ + m_highlighted = a; +} + +bool Die::isHighlighted() +{ + return m_highlighted; +} -- cgit v1.2.3-70-g09d2 From b36a3b448603514c392a8db8160af72788731c44 Mon Sep 17 00:00:00 2001 From: Renaud G Date: Wed, 22 Apr 2015 14:40:21 +0200 Subject: change the way die result are transmitted to client application --- diceparser.cpp | 48 ++++++++++++++++++------------------------------ diceparser.h | 7 ++++++- 2 files changed, 24 insertions(+), 31 deletions(-) (limited to 'diceparser.cpp') diff --git a/diceparser.cpp b/diceparser.cpp index a2fa137..f2a7ea5 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -392,77 +392,65 @@ QString DiceParser::getStringResult() str = result->getResult(Result::STRING).toString(); found = true; } - result = result->getPrevious(); } return str; } -QString DiceParser::getLastDiceResult() +void DiceParser::getLastDiceResult(ExportedDiceResult& diceValues) { ExecutionNode* next = getLeafNode(); - // QString str; - // QTextStream stream(&str); Result* result=next->getResult(); - QMap diceValues; - //QString dieValue("%2 (D%1)"); - bool first=true; + while(NULL!=result) { if(result->hasResultOfType(Result::DICE_LIST)) { - DiceResult* myDiceResult = dynamic_cast(result); - if(NULL!=myDiceResult) + DiceResult* diceResult = dynamic_cast(result); + if(NULL!=diceResult) { - - QString resulStr; + bool hasResult = false; quint64 face=0; - foreach(Die* die, myDiceResult->getResultList()) + ListDiceResult listpair; + foreach(Die* die, diceResult->getResultList()) { if(!die->hasBeenDisplayed()) { - resulStr+=QString("%1").arg(die->getValue()); + QList valuesResult; + hasResult=true; + valuesResult.append(die->getValue()); die->displayed(); face = die->getFaces(); - if(die->hasChildrenValue()) { - QString childDice(""); foreach(qint64 i, die->getListValue()) { - - childDice+=QString("%1,").arg(i); + valuesResult.append(i); } - childDice.remove(childDice.size()-1,1); - resulStr+=QString("[%1]").arg(childDice); } - resulStr+=", "; + QPair,bool> pair(valuesResult,die->isHighlighted()); + listpair.append(pair); } } - resulStr.remove(resulStr.size()-2,2); - - if(!resulStr.isEmpty()) + if(!listpair.isEmpty()) { if(!diceValues.contains(face)) { - diceValues.insert(face,resulStr); + diceValues.insert(face,listpair); } else { - QString tmp = diceValues.value(face); - tmp+=resulStr; + ListDiceResult tmp = diceValues.value(face); + tmp.append(listpair); diceValues.insert(face,tmp); } - //stream << dieValue.arg(face).arg(resulStr); + } } } result = result->getPrevious(); } - - //return str.simplified(); - return diceValues; } QString DiceParser::getDiceCommand() { diff --git a/diceparser.h b/diceparser.h index b4fd429..3f80acb 100644 --- a/diceparser.h +++ b/diceparser.h @@ -34,6 +34,11 @@ #include "parsingtoolbox.h" #include "dicealias.h" + +typedef QPair,bool> DiceAndHighlight; +typedef QList ListDiceResult; +typedef QMap ExportedDiceResult; + class ExploseDiceNode; /** * @mainpage DiceParser @@ -131,7 +136,7 @@ public: * @brief getLastDiceResult * @return */ - QString getLastDiceResult(); + void getLastDiceResult(ExportedDiceResult& diceValues); /** * @brief hasIntegerResultNotInFirst * @return -- cgit v1.2.3-70-g09d2 From f9d748791c1d7e3d2dc4f51bec4f86a6732083e4 Mon Sep 17 00:00:00 2001 From: Renaud G Date: Thu, 23 Apr 2015 10:52:16 +0200 Subject: improvement of the lib --- diceparser.cpp | 12 ++++++++++-- diceparser.h | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'diceparser.cpp') diff --git a/diceparser.cpp b/diceparser.cpp index f2a7ea5..a2015d8 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -915,12 +915,20 @@ bool DiceParser::readOperand(QString& str,ExecutionNode* & node) } return false; } -void DiceParser::displayDotTree() +void DiceParser::writeDownDotTree(QString filepath) { QString str("digraph ExecutionTree {\n"); m_start->generateDotTree(str); str.append("}"); - qDebug()<< str; + + /*QFile file(filepath); + if(file.open(QIODevice::WriteOnly)) + { + QTextStream in(&file); + in << str; + }*/ + + //qDebug()<< str; } diff --git a/diceparser.h b/diceparser.h index 3f80acb..249a630 100644 --- a/diceparser.h +++ b/diceparser.h @@ -121,7 +121,7 @@ public: /** * @brief displayDotTree */ - void displayDotTree(); + void writeDownDotTree(QString filepath); /** * @brief getLastIntegerResult * @return -- cgit v1.2.3-70-g09d2 From 7ec87c14e11b84dcf0af0b0176da15225362ada6 Mon Sep 17 00:00:00 2001 From: Renaud G Date: Fri, 24 Apr 2015 21:05:23 +0200 Subject: file output for dot --- diceparser.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'diceparser.cpp') diff --git a/diceparser.cpp b/diceparser.cpp index a2015d8..c710f55 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -23,7 +23,7 @@ #include #include #include - +#include #include "node/startingnode.h" #include "node/scalaroperatornode.h" @@ -922,12 +922,12 @@ void DiceParser::writeDownDotTree(QString filepath) str.append("}"); - /*QFile file(filepath); + QFile file(filepath); if(file.open(QIODevice::WriteOnly)) { QTextStream in(&file); in << str; - }*/ + } //qDebug()<< str; -- cgit v1.2.3-70-g09d2