12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- // SNF4CGP/InputProcessor.cpp
- // Copyright (C) 2009 ARM Research Labs, LLC.
- // See www.armresearch.com for more information.
-
- #include "InputProcessor.hpp"
- #include "Command.hpp"
-
- #include <iostream>
- #include <sstream>
- #include <string>
-
- using namespace std;
-
- //// InputProcessor ////////////////////////////////////////////////////////////
-
- Command& parseCommandNumber(istringstream& S, Command& C) {
- S >> C.Number;
- S.ignore();
- return C;
- }
-
- const string INTFCommand = "INTF";
- const string QUITCommand = "QUIT";
- const string FILECommand = "FILE";
-
- Command& parseCommandType(istringstream& S, Command& C) {
- string TypeString;
- S >> TypeString;
- S.ignore();
-
- switch(TypeString[0]) {
- case 'I' : if(INTFCommand == TypeString) C.Type = Command::INTF; break;
- case 'F' : if(FILECommand == TypeString) C.Type = Command::FILE; break;
- case 'Q' : if(QUITCommand == TypeString) C.Type = Command::QUIT; break;
- default : break;
- }
- return C;
- }
-
- Command& parseCommandData(istringstream& S, Command& C) {
- getline(S,C.Data);
- return C;
- }
-
- Command InputProcessor::getCommand() { // How to get/parse a command
- string InputLine;
- getline(cin, InputLine);
- istringstream InputLineStream(InputLine);
-
- Command NewCommand;
- parseCommandNumber(InputLineStream, NewCommand); // First thing is always a number.
- parseCommandType(InputLineStream, NewCommand); // Next is always a command name.
- parseCommandData(InputLineStream, NewCommand); // The rest is data for the command.
-
- if(Command::UNKNOWN == NewCommand.Type) NewCommand.Data = InputLine; // Capture unknown inputs for later.
-
- return NewCommand;
- }
|