Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F132479
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
6 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/include/r-tech1/argument.h b/include/r-tech1/argument.h
index 981b0d1d..ced80cea 100644
--- a/include/r-tech1/argument.h
+++ b/include/r-tech1/argument.h
@@ -1,34 +1,60 @@
#ifndef _rtech1_util_argument_h
#define _rtech1_util_argument_h
#include <vector>
#include <string>
#include "pointer.h"
-class ArgumentAction{
+namespace Argument {
+
+class Action{
public:
virtual void act() = 0;
- virtual ~ArgumentAction();
+ virtual ~Action();
};
-typedef std::vector<Util::ReferenceCount<ArgumentAction> > ActionRefs;
+typedef std::vector<Util::ReferenceCount<Action> > ActionRefs;
-class Argument{
+class Parameter{
public:
/* Keywords on the command line that should invoke this argument */
virtual std::vector<std::string> keywords() const = 0;
/* Parse more strings from the command line. Any actions that should take place
* after the command line has been parsed should be put into 'actions'
*/
virtual std::vector<std::string>::iterator parse(std::vector<std::string>::iterator current, std::vector<std::string>::iterator end, ActionRefs & actions) = 0;
/* Description of what this argument does */
virtual std::string description() const = 0;
bool isArg(const std::string & what) const;
- virtual ~Argument();
+ virtual ~Parameter();
};
+typedef std::vector<Util::ReferenceCount<Parameter> > ParameterRefs;
+
+class Handler{
+public:
+
+ Handler();
+
+ ~Handler();
+
+ void add(Util::ReferenceCount<Parameter>);
+
+ void add(const ParameterRefs &);
+
+ void setDefaultAction(Util::ReferenceCount<Action>);
+
+ void runActions(int argc, char ** argv);
+
+private:
+ ParameterRefs parameters;
+ Util::ReferenceCount<Action> defaultAction;
+};
+
+}
+
#endif
diff --git a/src/argument.cpp b/src/argument.cpp
index 0385df00..7d6a4548 100644
--- a/src/argument.cpp
+++ b/src/argument.cpp
@@ -1,21 +1,125 @@
#include "r-tech1/argument.h"
+
+#include "r-tech1/exceptions/shutdown_exception.h"
+#include "r-tech1/exceptions/exception.h"
+#include "r-tech1/exceptions/load_exception.h"
+#include "r-tech1/ftalleg.h"
+#include "r-tech1/menu/menu-exception.h"
+#include "r-tech1/token_exception.h"
+#include "r-tech1/system.h"
+
+#include "r-tech1/file-system.h"
+
#include <string.h>
using std::vector;
using std::string;
-bool Argument::isArg(const string & what) const {
+Argument::Action::~Action(){
+}
+
+bool Argument::Parameter::isArg(const string & what) const {
std::vector<std::string> match = keywords();
for (std::vector<std::string>::iterator it = match.begin(); it != match.end(); it++){
if (strcasecmp(it->c_str(), what.c_str()) == 0){
return true;
}
}
return false;
}
-Argument::~Argument(){
+Argument::Parameter::~Parameter(){
+}
+
+Argument::Handler::Handler(){
+}
+
+Argument::Handler::~Handler(){
+}
+
+void Argument::Handler::add(Util::ReferenceCount<Parameter> parameter){
+ parameters.push_back(parameter);
+}
+
+void Argument::Handler::add(const std::vector<Util::ReferenceCount<Parameter> > & parameters){
+ for (ParameterRefs::const_iterator i = parameters.begin(); i != parameters.end(); ++i){
+ add(*i);
+ }
+}
+
+void Argument::Handler::setDefaultAction(Util::ReferenceCount<Action> action){
+ defaultAction = action;
}
+
+void Argument::Handler::runActions(int argc, char ** argv){
+ vector<string> stringArgs;
+ for (int q = 1; q < argc; q++){
+ stringArgs.push_back(argv[q]);
+ }
+
+ std::vector<Util::ReferenceCount<Action> > actions;
+
+ /* Sort of a hack but if we are already at the end of the argument list (because some
+ * argument already reached the end) then we don't increase the argument iterator
+ */
+ for (vector<string>::iterator it = stringArgs.begin(); it != stringArgs.end(); (it != stringArgs.end()) ? it++ : it){
+ for (ParameterRefs::iterator arg = parameters.begin(); arg != parameters.end(); arg++){
+ Util::ReferenceCount<Parameter> parameter = *arg;
+ if (parameter->isArg(*it)){
+ it = parameter->parse(it, stringArgs.end(), actions);
+
+ /* Only parse one argument */
+ break;
+ }
+ }
+ }
+
+ /* If there are no actions then add default */
+ if (actions.size() == 0){
+ actions.push_back(defaultAction);
+ }
-ArgumentAction::~ArgumentAction(){
+ bool allow_quit = true;
+ /* Run em */
+ while (true){
+ bool normal_quit = false;
+ try{
+ for (ActionRefs::const_iterator it = actions.begin(); it != actions.end(); it++){
+ Util::ReferenceCount<Action> action = *it;
+ action->act();
+ }
+ normal_quit = true;
+ } catch (const Filesystem::Exception & ex){
+ Global::debug(0) << "There was a problem loading an argument. Error was:\n " << ex.getTrace() << std::endl;
+ } catch (const TokenException & ex){
+ Global::debug(0) << "There was a problem with the token. Error was:\n " << ex.getTrace() << std::endl;
+ return;
+ } catch (const LoadException & ex){
+ Global::debug(0) << "There was a problem loading an argument. Error was:\n " << ex.getTrace() << std::endl;
+ return;
+ } catch (const Exception::Return & ex){
+ } catch (const ShutdownException & shutdown){
+ Global::debug(1) << "Forced a shutdown. Cya!" << std::endl;
+ } catch (const ReloadMenuException & ex){
+ Global::debug(1) << "Menu Reload Requested. Restarting...." << std::endl;
+ continue;
+ } catch (const ftalleg::Exception & ex){
+ Global::debug(0) << "Freetype exception caught. Error was:\n" << ex.getReason() << std::endl;
+ } catch (const Exception::Base & base){
+ Global::debug(0) << "Base exception: " << base.getTrace() << std::endl;
+ } catch (const std::bad_alloc & fail){
+ Global::debug(0) << "Failed to allocate memory. Usage is " << System::memoryUsage() << std::endl;
+ }
+ // Do not catch all, end user should
+ /*catch (...){
+ Global::debug(0) << "Uncaught exception!" << std::endl;
+ }*/
+
+ if (allow_quit && normal_quit){
+ break;
+ } else if (normal_quit && !allow_quit){
+ } else if (!normal_quit){
+ break;
+ }
+ }
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Tue, Jun 16, 12:48 AM (2 w, 1 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
69753
Default Alt Text
(6 KB)
Attached To
Mode
R75 R-Tech1
Attached
Detach File
Event Timeline