| 1 | /**
|
|---|
| 2 | * @file command_list.cpp
|
|---|
| 3 | * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
|
|---|
| 4 | * @date Tue Apr 5 20:15:06 2011
|
|---|
| 5 | *
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | #ifdef HAVE_CONFIG_H
|
|---|
| 9 | #include <config.h>
|
|---|
| 10 | #endif
|
|---|
| 11 |
|
|---|
| 12 | #include <cstdio>
|
|---|
| 13 |
|
|---|
| 14 | #include "base/command.hpp"
|
|---|
| 15 | #include "base/command_list.hpp"
|
|---|
| 16 | #include "base/defs.hpp"
|
|---|
| 17 | #include "mg.hpp"
|
|---|
| 18 |
|
|---|
| 19 | using namespace VMG;
|
|---|
| 20 |
|
|---|
| 21 | Request CommandList::ExecuteList()
|
|---|
| 22 | {
|
|---|
| 23 | Request request;
|
|---|
| 24 | Request final_request = (commands.size() == 0 ? StopCycleNow : Continue);
|
|---|
| 25 |
|
|---|
| 26 | for (CommandList::iterator iter=commands.begin(); iter!=commands.end(); ++iter) {
|
|---|
| 27 |
|
|---|
| 28 | #ifdef DEBUG
|
|---|
| 29 | const int num_args = (iter->second.size() > 1 ? iter->second.size() : (iter->second[0] == "" ? 0 : 1));
|
|---|
| 30 | MG::GetFactory().CheckNumberOfArguments(iter->first, num_args);
|
|---|
| 31 | #endif
|
|---|
| 32 |
|
|---|
| 33 | request = MG::GetFactory().GetCommand((*iter).first)->Run((*iter).second);
|
|---|
| 34 |
|
|---|
| 35 | if (request == StopCycleLater)
|
|---|
| 36 | final_request = StopCycleNow;
|
|---|
| 37 | else if (request == StopCycleNow) {
|
|---|
| 38 | final_request = StopCycleNow;
|
|---|
| 39 | break;
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | return final_request;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | void CommandList::AddCommand(std::string command, std::string arguments)
|
|---|
| 47 | {
|
|---|
| 48 | std::vector<std::string> argument_list;
|
|---|
| 49 | size_t pos;
|
|---|
| 50 |
|
|---|
| 51 | do {
|
|---|
| 52 | pos = arguments.find(':');
|
|---|
| 53 | argument_list.push_back(arguments.substr(0, pos));
|
|---|
| 54 | arguments.erase(0, pos+1);
|
|---|
| 55 | }while (pos != std::string::npos);
|
|---|
| 56 |
|
|---|
| 57 | commands.push_back(std::pair<std::string, std::vector<std::string> >(command, argument_list));
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | void CommandList::DeleteCommand(const CommandList::iterator& iter)
|
|---|
| 61 | {
|
|---|
| 62 | commands.erase(iter);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | void CommandList::Print()
|
|---|
| 66 | {
|
|---|
| 67 | for (CommandList::iterator iter=commands.begin(); iter!=commands.end(); ++iter)
|
|---|
| 68 | printf("%s\n", (*iter).first.c_str());
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | void CommandList::Clear()
|
|---|
| 72 | {
|
|---|
| 73 | commands.clear();
|
|---|
| 74 | }
|
|---|