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 --- dicealias.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ dicealias.h | 29 ++++++++++++++++++++++ diceparser.cpp | 24 +++++++++--------- diceparser.h | 4 ++- diceparser.pri | 6 +++-- node/listaliasnode.cpp | 11 ++++----- node/listaliasnode.h | 6 ++--- 7 files changed, 123 insertions(+), 24 deletions(-) create mode 100644 dicealias.cpp create mode 100644 dicealias.h diff --git a/dicealias.cpp b/dicealias.cpp new file mode 100644 index 0000000..2a09206 --- /dev/null +++ b/dicealias.cpp @@ -0,0 +1,67 @@ +#include "dicealias.h" +#include + +DiceAlias::DiceAlias(QString cmd, QString key, bool isReplace) + : m_command(cmd),m_value(key) +{ + if(isReplace) + { + m_type = REPLACE; + } + else + { + m_type = REGEXP; + } +} + +DiceAlias::~DiceAlias() +{ + +} + +bool DiceAlias::resolved(QString & str) +{ + if((m_type == REPLACE)&&(str.contains(m_command))) + { + str.replace(m_command,m_value); + return true; + } + else if(m_type == REGEXP) + { + QRegularExpression exp(m_command); + str.replace(exp,m_value); + return true; + } + return false; +} + +void DiceAlias::setCommand(QString key) +{ + m_command = key; +} + +void DiceAlias::setValue(QString value) +{ + m_value = value; +} + +void DiceAlias::setType(RESOLUTION_TYPE type) +{ + m_type = type; +} +QString DiceAlias::getCommand() +{ + return m_command; +} + +QString DiceAlias::getValue() +{ + return m_value; +} + +bool DiceAlias::isReplace() +{ + return (m_type == REPLACE) ? true : false; +} + + diff --git a/dicealias.h b/dicealias.h new file mode 100644 index 0000000..565b903 --- /dev/null +++ b/dicealias.h @@ -0,0 +1,29 @@ +#ifndef DICEALIAS_H +#define DICEALIAS_H + +#include + +class DiceAlias +{ +public: + enum RESOLUTION_TYPE { REPLACE,REGEXP}; + DiceAlias(QString cmd, QString key, bool isReplace = true); + ~DiceAlias(); + + bool resolved(QString & str); + + void setCommand(QString key); + void setValue(QString value); + void setType(RESOLUTION_TYPE ); + + QString getCommand(); + QString getValue(); + bool isReplace(); +private: + QString m_command; + QString m_value; + RESOLUTION_TYPE m_type; + +}; + +#endif // DICEALIAS_H 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; } diff --git a/diceparser.h b/diceparser.h index 683be88..f9db840 100644 --- a/diceparser.h +++ b/diceparser.h @@ -32,6 +32,7 @@ #include "range.h" #include "booleancondition.h" #include "parsingtoolbox.h" +#include "dicealias.h" class ExploseDiceNode; /** @@ -157,6 +158,7 @@ public: * @return */ QString humanReadableError(); + QList* getAliases(); private: /** @@ -253,7 +255,7 @@ private: QMap* m_mapDiceOp; QMap* m_OptionOp; QMap* m_nodeActionMap; - QMap* m_aliasMap; + QList* m_aliasList; QStringList* m_commandList; QMap m_errorMap; diff --git a/diceparser.pri b/diceparser.pri index b9af1d7..cacca31 100644 --- a/diceparser.pri +++ b/diceparser.pri @@ -12,7 +12,8 @@ SOURCES += $$PWD/diceparser.cpp \ $$PWD/result/result.cpp \ $$PWD/result/scalarresult.cpp \ $$PWD/parsingtoolbox.cpp \ - $$PWD/result/stringresult.cpp + $$PWD/result/stringresult.cpp \ + $$PWD/dicealias.cpp HEADERS += \ @@ -25,7 +26,8 @@ HEADERS += \ $$PWD/result/result.h \ $$PWD/result/scalarresult.h \ $$PWD/result/parsingtoolbox.h \ - $$PWD/result/stringresult.h + $$PWD/result/stringresult.h \ + $$PWD/dicealias.h HEADERS += \ diff --git a/node/listaliasnode.cpp b/node/listaliasnode.cpp index 1d50c80..b85d4c9 100644 --- a/node/listaliasnode.cpp +++ b/node/listaliasnode.cpp @@ -1,7 +1,7 @@ #include "listaliasnode.h" -ListAliasNode::ListAliasNode(QMap* apAlias) - : m_mapAlias(apAlias) +ListAliasNode::ListAliasNode(QList* apAlias) + : m_aliasList(apAlias) { m_result = new StringResult(); } @@ -29,14 +29,13 @@ void ListAliasNode::run(ExecutionNode* previous ) m_nextNode->run(this); } } -QString ListAliasNode::toString()const +QString ListAliasNode::toString() const { QString result(QObject::tr("List of Alias:\n")); - foreach(QString key, m_mapAlias->keys()) + foreach(DiceAlias* key, *m_aliasList) { - result+=QString("%1 : %2\n").arg(key).arg(m_mapAlias->value(key)); + result+=QString("%1 : %2\n").arg(key->getCommand()).arg(key->getValue()); } - return result; } diff --git a/node/listaliasnode.h b/node/listaliasnode.h index d01d17a..ea70fe7 100644 --- a/node/listaliasnode.h +++ b/node/listaliasnode.h @@ -3,12 +3,12 @@ #include "executionnode.h" #include "result/stringresult.h" - +#include "dicealias.h" class ListAliasNode : public ExecutionNode { public: - ListAliasNode(QMap* mapAlias); + ListAliasNode(QList* mapAlias); /** * @brief run * @param previous @@ -27,7 +27,7 @@ public: virtual qint64 getPriority() const; private: - QMap* m_mapAlias; + QList* m_aliasList; }; #endif // LISTALIASNODE_H -- cgit v1.2.3-70-g09d2