You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InputProcessor.cpp 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // SNF4CGP/InputProcessor.cpp
  2. // Copyright (C) 2009 ARM Research Labs, LLC.
  3. // See www.armresearch.com for more information.
  4. #include "InputProcessor.hpp"
  5. #include "Command.hpp"
  6. #include <iostream>
  7. #include <sstream>
  8. #include <string>
  9. using namespace std;
  10. //// InputProcessor ////////////////////////////////////////////////////////////
  11. Command& parseCommandNumber(istringstream& S, Command& C) {
  12. S >> C.Number;
  13. S.ignore();
  14. return C;
  15. }
  16. const string INTFCommand = "INTF";
  17. const string QUITCommand = "QUIT";
  18. const string FILECommand = "FILE";
  19. Command& parseCommandType(istringstream& S, Command& C) {
  20. string TypeString;
  21. S >> TypeString;
  22. S.ignore();
  23. switch(TypeString[0]) {
  24. case 'I' : if(INTFCommand == TypeString) C.Type = Command::INTF; break;
  25. case 'F' : if(FILECommand == TypeString) C.Type = Command::FILE; break;
  26. case 'Q' : if(QUITCommand == TypeString) C.Type = Command::QUIT; break;
  27. default : break;
  28. }
  29. return C;
  30. }
  31. Command& parseCommandData(istringstream& S, Command& C) {
  32. getline(S,C.Data);
  33. return C;
  34. }
  35. Command InputProcessor::getCommand() { // How to get/parse a command
  36. string InputLine;
  37. getline(cin, InputLine);
  38. istringstream InputLineStream(InputLine);
  39. Command NewCommand;
  40. parseCommandNumber(InputLineStream, NewCommand); // First thing is always a number.
  41. parseCommandType(InputLineStream, NewCommand); // Next is always a command name.
  42. parseCommandData(InputLineStream, NewCommand); // The rest is data for the command.
  43. if(Command::UNKNOWN == NewCommand.Type) NewCommand.Data = InputLine; // Capture unknown inputs for later.
  44. return NewCommand;
  45. }