Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <iostream>
#include <string>
#include <map>
void something(){
std::cout<<"something called \n";
}
void somethingElse(){
std::cout<<"somethingElse called \n";
}
class Console {
public:
typedef void strfunc_t();
typedef std::map<std::string, strfunc_t*> CommandStringMap;
static CommandStringMap getCommandStringMap() {
CommandStringMap commandStringMap;
commandStringMap["something"] = something;
commandStringMap["somethingElse"] = somethingElse;
return commandStringMap;
}
static void executeConsoleCommands(std::string str) {
std::string delimiter = " ";
size_t position;
CommandStringMap commandStringMap = getCommandStringMap();
if((position = str.find(delimiter)) != std::string::npos) {
std::string token = str.substr(0, position); // token is "scott"
str = str.erase(0, position + delimiter.length());
commandStringMap.find(token)->second();
executeConsoleCommands(str);
} else {
commandStringMap.find(str)->second();
}
}
};
int main(int argc, char *argv[]) {
Console::executeConsoleCommands("something somethingElse something");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment