aboutsummaryrefslogtreecommitdiff
path: root/src/Command.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-04-28 11:52:04 +0200
committerdec05eba <dec05eba@protonmail.com>2018-04-28 11:53:18 +0200
commit3b03f87070d91f63f0dc3c7152723727781dcccf (patch)
tree7b06163f34248d1e632c032cfaf306787675e585 /src/Command.cpp
parentb0bfb8b8d1479502bd5adf17e6a1b94ec00c63ca (diff)
Add commands, users side panel, improve image download
start using odhtdb
Diffstat (limited to 'src/Command.cpp')
-rw-r--r--src/Command.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/Command.cpp b/src/Command.cpp
new file mode 100644
index 0000000..a074930
--- /dev/null
+++ b/src/Command.cpp
@@ -0,0 +1,37 @@
+#include "../include/Command.hpp"
+#include <unordered_map>
+
+using namespace std;
+
+namespace dchat
+{
+ unordered_map<string, CommandHandlerFunc> commandHandlerFuncs;
+
+ bool Command::add(const string &cmd, CommandHandlerFunc handlerFunc)
+ {
+ auto it = commandHandlerFuncs.find(cmd);
+ if(it != commandHandlerFuncs.end())
+ return false;
+
+ commandHandlerFuncs[cmd] = handlerFunc;
+ return true;
+ }
+
+ bool Command::call(const string &cmd, const vector<string> &args)
+ {
+ auto it = commandHandlerFuncs.find(cmd);
+ if(it != commandHandlerFuncs.end())
+ {
+ try
+ {
+ it->second(args);
+ }
+ catch(exception &e)
+ {
+ fprintf(stderr, "Failed while executing command %s, reason: %s\n", cmd.c_str(), e.what());
+ }
+ return true;
+ }
+ return false;
+ }
+}