From 888c6cbf5a204712b591ce96148ea016eea0ba5d Mon Sep 17 00:00:00 2001 From: Renaud G Date: Thu, 23 Apr 2015 10:50:33 +0200 Subject: add file for cli main application --- cli/cli.pri | 2 ++ cli/main.cpp | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 cli/cli.pri create mode 100644 cli/main.cpp (limited to 'cli') diff --git a/cli/cli.pri b/cli/cli.pri new file mode 100644 index 0000000..69aa44f --- /dev/null +++ b/cli/cli.pri @@ -0,0 +1,2 @@ +SOURCES += \ + $$PWD/main.cpp diff --git a/cli/main.cpp b/cli/main.cpp new file mode 100644 index 0000000..7b70e23 --- /dev/null +++ b/cli/main.cpp @@ -0,0 +1,107 @@ +/*************************************************************************** +* 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. * +***************************************************************************/ +#ifdef HAVE_IRC +#include +#include "irc/mainwindow.h" +#endif + +#include +#include "diceparser.h" + +int main(int argc, char *argv[]) +{ + #ifdef HAVE_IRC + QApplication a(argc, argv); + + + MainWindow main; +#endif + DiceParser* myParser = new DiceParser(); + + QStringList commands; + + 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]" + << "1+1D10" + << "3d10c[>=5]" + << "3nwod" + << "1+(4*3)D10" + << "2+4/4" + << "2D10*2D20*8" + <<"1+(4*3)D10" + <<"(4D6)D10" + << "1D100a[>=95]a[>=96]a[>=97]a[>=98]a[>=99]e[>=100]" + << "3D100" + << "4k3" + << "10D10e[>=6]sc[>=6]" + << "100190D6666666s" + << "10D10e10s" + << "10D10s" + << "15D10e10c[8-10]" + << "10d10e11" + << "1D8+2D6+7" + << "100190D6666666s" + << "D25" + << "D25+D10" + << "D25;D10" + << "8+8+8" + << "1D20-88" + << "100*1D20*2D6" + << "100/28*3" + << "100/8" + << "100*3*8" + << "help" + << "la" + << "400000D20/400000" + << "100*3*8";// + + if(argc>1) + { + for(int i=1;iparseLine(cmd)) + { + myParser->Start(); + // myParser->displayDotTree(); + myParser->displayResult(); + } + else + { + qDebug() << "echec"; + } + } + #ifdef HAVE_IRC + main.show(); + return a.exec(); +#endif + delete myParser; + return 0; +} -- cgit v1.2.3-70-g09d2 From cd21a9b1252f4d000b18ccecacc9c4e92d59f67b Mon Sep 17 00:00:00 2001 From: Renaud G Date: Thu, 23 Apr 2015 10:51:01 +0200 Subject: amend files for cli main application --- cli/main.cpp | 122 ++++++++++++++++++++++++++++++++++++++++----------------- diceParser.pro | 7 +++- 2 files changed, 91 insertions(+), 38 deletions(-) (limited to 'cli') diff --git a/cli/main.cpp b/cli/main.cpp index 7b70e23..8f1ec67 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -19,27 +19,100 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ -#ifdef HAVE_IRC -#include -#include "irc/mainwindow.h" -#endif #include #include "diceparser.h" +#include +#include -int main(int argc, char *argv[]) +void startDiceParsing(QString& cmd,QString& treeFile,bool highlight) { - #ifdef HAVE_IRC - QApplication a(argc, argv); + DiceParser* parser = new DiceParser(); + if(parser->parseLine(cmd)) + { + parser->Start(); + // + if(treeFile.isEmpty()) + { + parser->displayResult(); + } + else + { + parser->writeDownDotTree(); + } + } +} + + - MainWindow main; -#endif - DiceParser* myParser = new DiceParser(); +int main(int argc, char *argv[]) +{ QStringList commands; + QString cmd; + QString dotFile; + + bool color=true; + + QCommandLineParser optionParser; + QCommandLineOption color(QStringList() << "co"<< "color-off", tr("Disable color to highlight result")); + QCommandLineOption version(QStringList() << "v"<< "version", tr("Show the version and quit.")); + QCommandLineOption reset(QStringList() << "reset-settings", tr("Erase the settings and use the default parameters")); + QCommandLineOption dotFile(QStringList() << "d"<<"dot-file", tr("Instead of rolling dice, generate the execution tree and write it in "),"dotfile"); + QCommandLineOption translation(QStringList() << "t"<<"translation", QObject::tr("path to the translation file: "),"translationfile"); + QCommandLineOption help(QStringList() << "h"<<"help", QObject::tr("Display this help")); + + optionParser.addOption(color); + optionParser.addOption(version); + optionParser.addOption(reset); + optionParser.addOption(dotFile); + optionParser.addOption(translation); + optionParser.addOption(help); + + for(int i=1;i6]+@c[=10]" + cmd = commands.first(); + + + startDiceParsing(cmd,dotFile,color); + + /*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]" @@ -76,32 +149,9 @@ int main(int argc, char *argv[]) << "la" << "400000D20/400000" << "100*3*8";// +*/ + - if(argc>1) - { - for(int i=1;iparseLine(cmd)) - { - myParser->Start(); - // myParser->displayDotTree(); - myParser->displayResult(); - } - else - { - qDebug() << "echec"; - } - } - #ifdef HAVE_IRC - main.show(); - return a.exec(); -#endif - delete myParser; return 0; } diff --git a/diceParser.pro b/diceParser.pro index c339933..8af57a4 100644 --- a/diceParser.pro +++ b/diceParser.pro @@ -18,8 +18,11 @@ TEMPLATE = app #CONFIG+= IRC #CONFIG+= GUI - - +CONFIG+= CLI +CLI { +DEFINES += CLI +include(cli/cli.pri)cd +} IRC { include(irc/irc.pri) QT += gui widgets -- cgit v1.2.3-70-g09d2 From c356c8bf53f46efb2ba0aaa6d91ef4c4ade320b6 Mon Sep 17 00:00:00 2001 From: Renaud G Date: Fri, 24 Apr 2015 11:08:32 +0200 Subject: add commandLine parser(not working yet) --- cli/main.cpp | 129 ++++++++++++++++++++++++++++++++++++++++++++++++--------- diceParser.pro | 3 +- 2 files changed, 110 insertions(+), 22 deletions(-) (limited to 'cli') diff --git a/cli/main.cpp b/cli/main.cpp index 8f1ec67..7476eb4 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -24,21 +24,101 @@ #include "diceparser.h" #include #include +#include + +QString diceToText(ExportedDiceResult& dice) +{ + QStringList resultGlobal; + foreach(int face, dice.keys()) + { + QStringList result; + ListDiceResult diceResult = dice.value(face); + bool previousHighlight=false; + QString patternColor("\e[0;31m"); + //patternColor = patternColorarg(); + foreach (DiceAndHighlight tmp, diceResult) + { + QStringList diceListStr; + QStringList diceListChildren; + if((previousHighlight)&&(!tmp.second)) + { + result << patternColor << result.join(',') <<"\e[0m"; + } + previousHighlight = tmp.second; + for(int i =0; i < tmp.first.size(); ++i) + { + quint64 dievalue = tmp.first[i]; + if(i==0) + { + diceListStr << QString::number(dievalue); + } + else + { + diceListChildren << QString::number(dievalue); + } + } + if(!diceListChildren.isEmpty()) + { + diceListStr << QString("[%1]").arg(diceListChildren.join(',')); + } + + result << diceListStr.join(' '); + } + if(previousHighlight) + { + QStringList list; + list << patternColor << result.join(',') << "\e[0m"; + result = list; + } + if(dice.keys().size()>1) + { + resultGlobal << QString(" d%2:(%1)").arg(result.join(' ')).arg(face); + } + else + { + resultGlobal << result; + } + } + return resultGlobal.join(' '); +} void startDiceParsing(QString& cmd,QString& treeFile,bool highlight) { DiceParser* parser = new DiceParser(); + if(parser->parseLine(cmd)) { parser->Start(); // if(treeFile.isEmpty()) { - parser->displayResult(); + ExportedDiceResult list; + parser->getLastDiceResult(list); + QString diceText = diceToText(list); + QString scalarText; + QString str; + + if(parser->hasIntegerResultNotInFirst()) + { + scalarText = QString("%1").arg(parser->getLastIntegerResult()); + } + else if(!list.isEmpty()) + { + scalarText = QString("%1").arg(parser->getSumOfDiceResult()); + } + + str = QString("Result: \e[0;31m%1\e[0m, details:[%3 (%2)]").arg(scalarText).arg(diceText).arg(parser->getDiceCommand()); + + if(parser->hasStringResult()) + { + str = parser->getStringResult().replace("\n","
"); + } + qDebug() << str; + } else { - parser->writeDownDotTree(); + parser->writeDownDotTree(treeFile); } } } @@ -51,19 +131,23 @@ int main(int argc, char *argv[]) QStringList commands; QString cmd; - QString dotFile; + QString dotFileStr; - bool color=true; + bool colorb=false; QCommandLineParser optionParser; - QCommandLineOption color(QStringList() << "co"<< "color-off", tr("Disable color to highlight result")); - QCommandLineOption version(QStringList() << "v"<< "version", tr("Show the version and quit.")); - QCommandLineOption reset(QStringList() << "reset-settings", tr("Erase the settings and use the default parameters")); - QCommandLineOption dotFile(QStringList() << "d"<<"dot-file", tr("Instead of rolling dice, generate the execution tree and write it in "),"dotfile"); - QCommandLineOption translation(QStringList() << "t"<<"translation", QObject::tr("path to the translation file: "),"translationfile"); - QCommandLineOption help(QStringList() << "h"<<"help", QObject::tr("Display this help")); - - optionParser.addOption(color); + QCommandLineOption color(QStringList() << "co"<< "color-off", "Disable color to highlight result"); + QCommandLineOption version(QStringList() << "v"<< "version", "Show the version and quit."); + QCommandLineOption reset(QStringList() << "reset-settings", "Erase the settings and use the default parameters"); + QCommandLineOption dotFile(QStringList() << "d"<<"dot-file", "Instead of rolling dice, generate the execution tree and write it in ","dotfile"); + QCommandLineOption translation(QStringList() << "t"<<"translation", "path to the translation file: ","translationfile"); + QCommandLineOption help(QStringList() << "h"<<"help", "Display this help"); + + if(!optionParser.addOption(color)) + { + qDebug()<< optionParser.errorText(); + } + optionParser.addOption(version); optionParser.addOption(reset); optionParser.addOption(dotFile); @@ -76,12 +160,20 @@ int main(int argc, char *argv[]) commands << QString::fromLatin1(argv[i]); } - optionParser.parse(commands); + if(!optionParser.parse(commands)) + { + qDebug()<< optionParser.errorText(); + } + else + { + qDebug() << "no error"; + } if(optionParser.isSet(color)) { - return 0; + qDebug() << "color"; + colorb = false; } else if(optionParser.isSet(version)) { @@ -93,7 +185,7 @@ int main(int argc, char *argv[]) } else if(optionParser.isSet(dotFile)) { - dotFile = optionParser.value(dotFile); + dotFileStr = optionParser.value(dotFile); } else if(optionParser.isSet(translation)) { @@ -101,16 +193,13 @@ int main(int argc, char *argv[]) } else if(optionParser.isSet(help)) { - cmd = "help"; } - - cmd = commands.first(); - - startDiceParsing(cmd,dotFile,color); + qDebug() << "super 5" << cmd << dotFileStr << colorb; + startDiceParsing(cmd,dotFileStr,colorb); /*commands<< "10d10c[>6]+@c[=10]" << "1L[cheminée,chocolat,épée,arc,chute de pierre]" diff --git a/diceParser.pro b/diceParser.pro index 8af57a4..3a95496 100644 --- a/diceParser.pro +++ b/diceParser.pro @@ -36,8 +36,7 @@ DEFINES+= HAVE_GUI } -SOURCES += main.cpp \ - diceparser.cpp \ +SOURCES += diceparser.cpp \ result/diceresult.cpp \ range.cpp \ booleancondition.cpp \ -- cgit v1.2.3-70-g09d2