From 5d1efcc49ba47eaf5919923f6f6ba82566d9baaf Mon Sep 17 00:00:00 2001 From: deva Date: Thu, 29 May 2008 09:35:32 +0000 Subject: Rewrote the argument parser, for a more "The QT-Way"-like method. --- client/builder.cc | 4 ++- client/macro.cc | 4 ++- client/main.cc | 94 +++++++++++++++++++++++++++++++++++++++++++++++++-- client/sendrecieve.cc | 13 ++----- client/sendrecieve.h | 5 ++- 5 files changed, 105 insertions(+), 15 deletions(-) diff --git a/client/builder.cc b/client/builder.cc index 63cdcb9..f8addeb 100644 --- a/client/builder.cc +++ b/client/builder.cc @@ -23,6 +23,8 @@ extern QString cpr; extern QString user; +extern QString host; +extern quint16 port; Builder::Builder(QDomDocument *xml_doc) : QObject() @@ -166,7 +168,7 @@ bool Builder::doCommit() } // Commit the xml data to the server - SendRecieve macro_commit; + SendRecieve macro_commit(host, port); macro_commit.makeConnection(&xml_result); // Recieve answer from server whether successful or not //QByteArray ba = macro_commit.getResult(); diff --git a/client/macro.cc b/client/macro.cc index d06e4c0..7f8ea27 100644 --- a/client/macro.cc +++ b/client/macro.cc @@ -34,6 +34,8 @@ extern QString cpr; extern QString user; +extern QString host; +extern quint16 port; class MyEvent : public QEvent { public: @@ -52,7 +54,7 @@ void create_macro(QString name) QDomDocument xml_req = xml_request(name); // Fetch the XML document - SendRecieve xml_acquire; + SendRecieve xml_acquire(host, port); xml_acquire.makeConnection(&xml_req); QByteArray ba = xml_acquire.getResult(); diff --git a/client/main.cc b/client/main.cc index ca0d2c1..21e934f 100644 --- a/client/main.cc +++ b/client/main.cc @@ -28,10 +28,53 @@ #include #include #include +#include +#include #include "macro.h" -QString cpr; -QString user; +#define VERSION "1.0" + +#define CPR_DEFAULT "0000000000" +#define MACRO_DEFAULT "example2" +#define USER_DEFAULT "testuser" +#define CONFIG_DEFAULT "pracro.ini" + +QString macro = MACRO_DEFAULT; +QString cpr = CPR_DEFAULT; +QString user = USER_DEFAULT; +QString config = CONFIG_DEFAULT; +QString host; +quint16 port; + +static void print_usage() +{ + printf("Usage: pracro -m MACRO -c CPR -U USER\n"); + printf("Executes the requested Pracro MACRO using supplied CPR and USER.\n"); + printf("\n"); + printf(" -h, --help Displays this help text.\n"); + printf(" -m, --macro MACRO Requests macro MACRO from the Pracro \n" + " Server, defaults to \""MACRO_DEFAULT"\".\n"); + printf(" -c, --cpr CPR Defines the cpr for use with the macro,\n" + " defaults to \""CPR_DEFAULT"\".\n"); + printf(" -C, --config FILE The configfile to use. Default is \""CONFIG_DEFAULT"\"\n"); + printf(" -u, --user USER Defines the requesting user(not the patient),\n" + " defaults to \""USER_DEFAULT"\"\n"); +} + +static void print_version() +{ + printf("Pracro v"VERSION"\n"); +} + +static QString getParam(QStringList &args, QStringList::iterator &arg) +{ + arg++; + if(arg == args.end() || arg->at(0) == '-') { + print_usage(); + exit(1); + } + return *arg; +} int main(int argc, char *argv[]) { @@ -40,6 +83,45 @@ int main(int argc, char *argv[]) MyEventHandler *eventhandler = new MyEventHandler(); app.installEventFilter( eventhandler ); + QStringList args = app.arguments(); + QStringList::iterator arg = args.begin(); + arg++; // Skip program name... + while(arg != args.end()) { + if(*arg == "--help" || + *arg == "-h") { + print_usage(); + } + else if(*arg == "--version" || + *arg == "-v") { + print_version(); + } + else if(*arg == "--user" || + *arg == "-u") { + user = getParam(args,arg); + } + else if(*arg == "--macro" || + *arg == "-m") { + macro = getParam(args, arg); + } + else if(*arg == "--cpr" || + *arg == "-c") { + cpr = getParam(args, arg); + } + else if(*arg == "--config" || + *arg == "-C") { + config = getParam(args, arg); + } + else { + print_version(); + print_usage(); + return 1; + } + + arg++; + } + + +#if 0 char macro[100] = "start"; char _cpr[11] = ""; char _user[20] = ""; @@ -107,6 +189,14 @@ int main(int argc, char *argv[]) printf("cpr and user not set, exiting...\n"); return 1; } +#endif/*0*/ + + QSettings settings(config, QSettings::IniFormat); + + settings.beginGroup("server"); + host = settings.value("host").toString(); + port = settings.value("port").toInt(); + settings.endGroup(); new_macro(macro); diff --git a/client/sendrecieve.cc b/client/sendrecieve.cc index 0a664eb..95593c6 100644 --- a/client/sendrecieve.cc +++ b/client/sendrecieve.cc @@ -30,8 +30,6 @@ #include #include #include -#include -#include #include #include @@ -42,21 +40,16 @@ #include #endif -SendRecieve::SendRecieve() +SendRecieve::SendRecieve(QString host, quint16 port) : QObject() { + this->host = host; + this->port = port; has_result = false; } void SendRecieve::tcpConnect() { - QSettings settings("pracro.ini", QSettings::IniFormat); - - settings.beginGroup("server"); - QString host = settings.value("host").toString(); - int port = settings.value("port").toInt(); - settings.endGroup(); - printf("%s, %d\n", host.toStdString().c_str(), port); tcpsocket->connectToHost(host, port); tcpConnected = TCP_CONNECTING; diff --git a/client/sendrecieve.h b/client/sendrecieve.h index a884b81..9e6c171 100644 --- a/client/sendrecieve.h +++ b/client/sendrecieve.h @@ -42,7 +42,7 @@ class SendRecieve : public QObject { Q_OBJECT public: - SendRecieve(); + SendRecieve(QString host, quint16 port); void makeConnection(QDomDocument *xml_req); QByteArray getResult(); @@ -63,6 +63,9 @@ private: QByteArray ba_all; bool has_result; QDomDocument *xml_req; + + QString host; + quint16 port; }; #endif/*_SENDRECIEVE_H_*/ -- cgit v1.2.3