From ea8e56dd688c0bdc4dd63533b106fcfa4f88255e Mon Sep 17 00:00:00 2001 From: Thunder422 Date: Mon, 26 Nov 2012 20:28:03 -0500 Subject: [PATCH] added new command line class for handling command arguments the new CommandLine class now handles the Tester instance the main function contains the instance of the CommandLine class the CommandLine class handles the version option and usage message the CommandLine class contains the GPL statement lines the GPL statement lines are passed to Tester for output --- CMakeLists.txt | 2 + commandline.cpp | 122 ++++++++++++++++++++++++++++++++++++++++++++++++ commandline.h | 54 +++++++++++++++++++++ main.cpp | 64 ++++--------------------- test_ibcp.cpp | 17 ++++--- test_ibcp.h | 4 +- 6 files changed, 199 insertions(+), 64 deletions(-) create mode 100644 commandline.cpp create mode 100644 commandline.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e5787e7..7288fe1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -132,6 +132,7 @@ add_custom_command(OUTPUT test_names.h # list the include files set(ibcp_HEADERS + commandline.h ibcp.h parser.h table.h @@ -143,6 +144,7 @@ set(ibcp_HEADERS # list the main program sources set(ibcp_SOURCES commandhandlers.cpp + commandline.cpp main.cpp parser.cpp table.cpp diff --git a/commandline.cpp b/commandline.cpp new file mode 100644 index 0000000..d5314ba --- /dev/null +++ b/commandline.cpp @@ -0,0 +1,122 @@ +// vim:ts=4:sw=4: +// +// Interactive BASIC Compiler Project +// File: commandline.n - command line functions source file +// Copyright (C) 2012 Thunder422 +// +// This program 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 3 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. +// +// For a copy of the GNU General Public License, +// see . +// +// +// Change History: +// +// 2012-11-25 initial version + +#include + +#include "ibcp_config.h" // for cmake +#include "commandline.h" +#include "test_ibcp.h" + + +CommandLine::CommandLine(const QStringList &args) +{ + // get base file name of program from first argument + m_programName = QFileInfo(args.at(0)).baseName(); + m_processed = false; + + // parse command line arguments + if (version(args)) + { + m_processed = true; + return; + } + + // create GPL statement string list + m_gplStatement + .append(QString(QT_TR_NOOP("%1 Copyright (C) 2010-%2 Thunder422")) + .arg(m_programName).arg(ibcp_COPYRIGHT_YEAR)); + m_gplStatement + .append(QT_TR_NOOP("This program comes with ABSOLUTELY NO WARRANTY.")); + m_gplStatement + .append(QT_TR_NOOP("This is free software, and you are welcome to")); + m_gplStatement + .append(QT_TR_NOOP("redistribute it under certain conditions.")); + + Tester tester(args); + if (tester.hasError()) + { + qWarning("%s", qPrintable(tester.errorMessage())); + m_processed = true; + return; + } + else if (tester.hasOption()) + { + if (!tester.run(cout(), m_gplStatement)) + { + qWarning("%s", qPrintable(tester.errorMessage())); + } + m_processed = true; + return; + } + // NOTE: leave m_processed set to false to start GUI, + // for now there is no GUI so always processed + + // unsupported option (NOTE: other options get checked before this) + QStringList options = tester.options(); + options.prepend("-v"); + // append any other options here + qWarning("%s: %s %s", qPrintable(tr("usage")), qPrintable(m_programName), + qPrintable(options.join("|"))); + m_processed = true; +} + + +CommandLine::~CommandLine() +{ + if (m_cout.device()) + { + QIODevice *output = m_cout.device(); + m_cout.setDevice(0); + delete output; + } +} + + +QTextStream &CommandLine::cout(void) +{ + if (!m_cout.device()) + { + // setup standard output stream first time + QFile *output = new QFile; + output->open(stdout, QIODevice::WriteOnly | QIODevice::Unbuffered); + m_cout.setDevice(output); + } + return m_cout; +} + + +// function to print version number +bool CommandLine::version(const QStringList &args) +{ + if (args.count() != 2 || args.at(1) != "-v") + { + return false; // not our option or extra/invalid options + } + cout() << tr("%1 version %2").arg(m_programName) + .arg(ibcp_RELEASE_STRING + 7) << endl; + return true; +} + + +// end: commandline.cpp diff --git a/commandline.h b/commandline.h new file mode 100644 index 0000000..e43a3f7 --- /dev/null +++ b/commandline.h @@ -0,0 +1,54 @@ +// vim:ts=4:sw=4: +// +// Interactive BASIC Compiler Project +// File: commandline.h - command line header file +// Copyright (C) 2012 Thunder422 +// +// This program 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 3 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. +// +// For a copy of the GNU General Public License, +// see . +// +// +// Change History: +// +// 2012-11-25 initial version + +#ifndef COMMANDLINE_H +#define COMMANDLINE_H + +#include +#include +#include + +class CommandLine +{ + Q_DECLARE_TR_FUNCTIONS(CommandLine) + + bool version(const QStringList &args); + QTextStream &cout(void); + + QString m_programName; + QStringList m_gplStatement; + bool m_processed; + QTextStream m_cout; +public: + CommandLine(const QStringList &args); + ~CommandLine(); + + bool processed(void) const // if processed then exit program + { + return m_processed; + } +}; + + +#endif // COMMANDLINE_H diff --git a/main.cpp b/main.cpp index 9ed3014..2f93c61 100644 --- a/main.cpp +++ b/main.cpp @@ -23,73 +23,27 @@ // 2010-03-13 initial version #include -#include #include #include #include "ibcp_config.h" // for cmake -#include "test_ibcp.h" - - -void printGplHeader(QTextStream &cout, const QString &name) -{ - cout << endl << name << " Copyright (C) 2010-" << ibcp_COPYRIGHT_YEAR - << " Thunder422" << endl - << "This program comes with ABSOLUTELY NO WARRANTY." << endl - << "This is free software, and you are welcome to" << endl - << "redistribute it under certain conditions." << endl << endl; -} - - -// function to print version number -bool ibcpVersion(QTextStream &cout, const QString &name, QStringList &args) -{ - if (args.count() != 2 || args.at(1) != "-v") - { - return false; // not our option or extra/invalid options - } - cout << name << QObject::tr(" version %1").arg(ibcp_RELEASE_STRING + 7) - << endl; - return true; -} +#include "commandline.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); - QStringList args = app.arguments(); - - // get base file name of program from first argument - QString programName = QFileInfo(args.at(0)).baseName(); - - // setup standard output stream - QFile output; - output.open(stdout, QIODevice::WriteOnly | QIODevice::Unbuffered); - QTextStream cout(&output); - - if (!ibcpVersion(cout, programName, args)) + CommandLine commandLine(app.arguments()); + if (commandLine.processed()) { - Tester tester(args); - if (tester.hasError()) - { - qWarning("%s", qPrintable(tester.errorMessage())); - } - else if (!tester.hasOption()) - { - QStringList options = tester.options(); - options.prepend("-v"); - qWarning("%s: %s %s", qPrintable(QObject::tr("usage")), - qPrintable(programName), qPrintable(options.join("|"))); - } - else if (!tester.run(cout)) - { - qWarning("%s", qPrintable(tester.errorMessage())); - } + // force quit once event processing loop is started + QTimer::singleShot(0, &app, SLOT(quit())); + } + else + { + // start GUI here } - - // force quit once event processing loop is started - QTimer::singleShot(0, &app, SLOT(quit())); return app.exec(); } diff --git a/test_ibcp.cpp b/test_ibcp.cpp index 3b6aaed..e82e545 100644 --- a/test_ibcp.cpp +++ b/test_ibcp.cpp @@ -34,12 +34,9 @@ #include "translator.h" -extern void printGplHeader(QTextStream &cout, const QString &name); - - // function to process a test input file specified on the command line // or accept input lines from the user -Tester::Tester(QStringList &args) +Tester::Tester(const QStringList &args) { QString name[OptSizeOf]; name[OptParser] = "parser"; @@ -123,7 +120,7 @@ QStringList Tester::options(void) const } -bool Tester::run(QTextStream &cout) +bool Tester::run(QTextStream &cout, const QStringList &gplStatement) { QFile file; QTextStream input(&file); @@ -158,8 +155,14 @@ bool Tester::run(QTextStream &cout) } qFatal("%s", qPrintable(tr("Program aborting!"))); } - printGplHeader(cout, m_programName); - cout << "Table initialization successful." << endl; + + cout << endl; + foreach (QString line, gplStatement) + { + cout << line << endl; + } + + cout << endl << "Table initialization successful." << endl; Translator translator(Table::instance()); diff --git a/test_ibcp.h b/test_ibcp.h index fa8ef4c..6a246bf 100644 --- a/test_ibcp.h +++ b/test_ibcp.h @@ -63,11 +63,11 @@ class Tester QString m_testFileName; // name of test file (OptFile only) QString m_errorMessage; // message if error occurred public: - Tester(QStringList &args); + Tester(const QStringList &args); ~Tester(void) {} QStringList options(void) const; - bool run(QTextStream &cout); + bool run(QTextStream &cout, const QStringList &gplStatement); bool hasOption(void) const // has a test option been specified? { return m_option != OptNone;