It looks like depending on what the string contains you want to do certain action. You should look into the command pattern. Anyways here is one way.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Command{
virtual void execute(const std::string& commandString) = 0;
~Command() {}
};
struct StoreCommand : Command{
string& registerA;
string& registerB;
StoreCommand(string& regA, string& regB): registerA(regA), registerB(regB){}
void execute(const std::string& commandString){
//get string to store
size_t startPos = commandString.find_first_not_of("store");
size_t endPos = commandString.find("in");
string data = commandString.substr(startPos, endPos - startPos - 1);
//get which register to store in
bool storeInRegA = commandString.find("registerA") != string::npos;
if(storeInRegA) registerA = data;
else registerB = data;
}
};
//other commands...
//the dispatcher
void dispatch(string& regA, string& regB, const std::string& cmdStr)
{
if(cmdStr.find("store") != string::npos){
StoreCommand cmd(regA,regB);
cmd.execute(cmdStr);
}else{
//command not supported
}
}
int main (int argc, const char * argv[])
{
using namespace std;
string regA;
string regB;
string cmdStr = "store Hello World in registerA";
dispatch(regA, regB, cmdStr);
cout << "regA = " << regA << endl;
cout << "regB = " << regB << endl;
return 0;
}
Of course there are different ways of doing it. The basic logic is to find the keywords and from those keywords take the appropriate course of actions.