[cig-commits] r8928 - cs/benchmark/cigma/trunk/src

luis at geodynamics.org luis at geodynamics.org
Wed Dec 19 12:04:21 PST 2007


Author: luis
Date: 2007-12-19 12:04:21 -0800 (Wed, 19 Dec 2007)
New Revision: 8928

Added:
   cs/benchmark/cigma/trunk/src/CommandSet.cpp
   cs/benchmark/cigma/trunk/src/CommandSet.h
Log:
Container for a set of commands

Added: cs/benchmark/cigma/trunk/src/CommandSet.cpp
===================================================================
--- cs/benchmark/cigma/trunk/src/CommandSet.cpp	2007-12-19 20:04:08 UTC (rev 8927)
+++ cs/benchmark/cigma/trunk/src/CommandSet.cpp	2007-12-19 20:04:21 UTC (rev 8928)
@@ -0,0 +1,117 @@
+#include <iostream>
+#include <cstdlib>
+#include <cassert>
+#include "CommandSet.h"
+#include "HelpCmd.h"
+#include "ExtractCmd.h"
+#include "EvalCmd.h"
+#include "CompareCmd.h"
+
+cigma::CommandSet::CommandSet()
+{
+}
+
+cigma::CommandSet::~CommandSet()
+{
+    for(CmdMap::iterator i = commands.begin(); i != commands.end(); ++i)
+    {
+        Command *cmd = i->second;
+        assert(cmd != 0);
+        delete cmd;
+    }
+}
+
+void cigma::CommandSet::initialize()
+{
+    /* create help command separately */
+    HelpCmd *help = new HelpCmd();
+
+    /* assemble set of commands */
+    addCommand(help);
+    addCommand(new ExtractCmd());
+    addCommand(new EvalCmd());
+    addCommand(new CompareCmd());
+
+    /* once assembled, pass set of commands to help command */
+    help->setCmdMap(&commands);
+}
+
+void cigma::CommandSet::addCommand(Command *cmd)
+{
+    assert(cmd != 0);
+    commands[cmd->name] = cmd;
+}
+
+cigma::Command *
+cigma::CommandSet::getCommand(std::string name)
+{
+    Command *cmd = 0;
+    CmdMap::iterator it = commands.find(name);
+    if (it != commands.end())
+    {
+        cmd = it->second;
+    }
+    return cmd;
+}
+
+int cigma::CommandSet::main(int argc, char *argv[])
+{
+    AnyOption *opt = new AnyOption();
+    Command *cmd = 0;
+    int status = 0;
+    bool helpFlag = false;
+
+    /* Check argv[1] for a command, but intercept help requests */
+    if (argc > 1)
+    {
+        if ((strcmp(argv[1], "--help") == 0) ||
+            (strcmp(argv[1], "-h") == 0))
+        {
+            cmd = getCommand("help");
+            helpFlag = true;
+        }
+        else
+        {
+            cmd = getCommand(argv[1]);
+        }
+    }
+    else
+    {
+        cmd = getCommand("help");
+        helpFlag = true;
+    }
+
+    if (cmd != 0)
+    {
+        cmd->setupOptions(opt);
+        opt->processCommandArgs(argc-1, argv+1);
+
+        if (helpFlag || cmd->helpFlag(opt))
+        {
+            opt->printUsage();
+            status = 1;
+        }
+        else
+        {
+            cmd->configure(opt);
+            status = cmd->run();
+        }
+    }
+    else
+    {
+        /* argv[1] was not a valid command */
+
+        assert(argc > 1);
+
+        std::cerr << "Unknown "
+                  << ((argv[1][0] == '-') ? "option" : "command")
+                  << ": '" << argv[1] << "'"
+                  << std::endl;
+
+        status = 1;
+    }
+
+    delete opt;
+
+    return status;
+}

Added: cs/benchmark/cigma/trunk/src/CommandSet.h
===================================================================
--- cs/benchmark/cigma/trunk/src/CommandSet.h	2007-12-19 20:04:08 UTC (rev 8927)
+++ cs/benchmark/cigma/trunk/src/CommandSet.h	2007-12-19 20:04:21 UTC (rev 8928)
@@ -0,0 +1,39 @@
+#ifndef __COMMAND_SET_H__
+#define __COMMAND_SET_H__
+
+#include <map>
+#include <string>
+
+#include "Command.h"
+
+namespace cigma
+{
+    class CommandSet;
+}
+
+/**
+ * @brief Container for a set of commands
+ *
+ */
+class cigma::CommandSet
+{
+public:
+    typedef std::map<std::string,Command*> CmdMap;
+
+public:
+    CommandSet();
+    ~CommandSet();
+
+public:
+    void initialize();
+    int main(int argc, char *argv[]);
+
+public:
+    void addCommand(Command *cmd);
+    Command *getCommand(std::string name);
+
+public:
+    CmdMap commands;
+};
+
+#endif



More information about the cig-commits mailing list