Command Parsing Class

Advertisemen


#include <iostream>
#include <vector>
#include <string>

using namespace std;

class commandParser
{
       bool DebugMode; //prints errors if true
       string InvalidCommandMessage;
       vector<string> Commands;
public:
       commandParser(bool debugMode=false, string invalidCommandMessage="You entered an invalid command") { DebugMode=debugMode; InvalidCommandMessage=invalidCommandMessage; }
       ~commandParser() {}

       void AddCommand( const string& cmd ) { Commands.push_back(cmd); }
      
       void RemoveCommand( const string& );
       void RemoveCommand( int );

       void PrintAllCommands() { for (int i=0;i<Commands.size();i++) cout << Commands[i] << endl; }

       bool IsACommand( const string& );

       string FormatInputCommand( const string& );
};

void commandParser::RemoveCommand( int index )
{
       //error protection - don't trust the user
       if (index > Commands.size()-1)
       {
              if (DebugMode) { cout << "Attempt to remove a command with an index greater than all added commands" << endl; }
              return;
       }

       Commands.erase(Commands.begin() + index);
}

void commandParser::RemoveCommand ( const string& cmd )
{
       for (int i=0;i<Commands.size();i++)
              if (Commands[i].compare(cmd) == 0) //same strings
              {
                     Commands.erase(Commands.begin() + i);
                     return; //removed so return
              }
      
       if (DebugMode) cout << "Command '" << cmd << "' deletion request failed - non-existant" << endl;
}

bool commandParser::IsACommand ( const string& cmd )
{
       for (int i=0;i<Commands.size();i++)
              if (Commands[i].compare(cmd) == 0)
                     return true;
       return false;
}

string commandParser::FormatInputCommand( const string& cmd )
{
       string retval;
       retval.assign(cmd);

       //set lowercase
       for (int i=0;i<retval.length();i++)
              tolower(retval[i]);

       return retval;
}

int main()
{
       //set up some commands
       commandParser *CommandParser = new commandParser(true);
      
       CommandParser->AddCommand("hi");
       CommandParser->AddCommand("goto");
       CommandParser->AddCommand("kill");
       CommandParser->AddCommand("march");
       CommandParser->AddCommand("seventy");
       CommandParser->AddCommand("remove this");
       CommandParser->AddCommand("pyramid");

       cout << "First Commands:" << endl;
       CommandParser->PrintAllCommands();

       CommandParser->RemoveCommand("remove this");
       CommandParser->RemoveCommand(3); //march

       cout << "\nNon-removed Commands:" << endl;
       CommandParser->PrintAllCommands();

       cout << "\nUser Input (enter \"exit\" to close)" << endl;
       string userInput = "";

       while (true)
       {
              cin >> userInput;

              if (userInput.compare("exit")==0) return 0;

              if (CommandParser->IsACommand((CommandParser->FormatInputCommand(userInput))))
                     cout << "Correct command" << endl;
              else cout << "Incorrect command" << endl;
       }

       return 0;
}

Advertisemen

Disclaimer: Gambar, artikel ataupun video yang ada di web ini terkadang berasal dari berbagai sumber media lain. Hak Cipta sepenuhnya dipegang oleh sumber tersebut. Jika ada masalah terkait hal ini, Anda dapat menghubungi kami disini.

Tidak ada komentar:

Posting Komentar

© Copyright 2017 Game Engine Tutorial