Advertisement
Guest User

ConsoleC++CommandExample

a guest
Jul 29th, 2015
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <cmath>
  5. #include <cassert>
  6. #include <algorithm>
  7. #include <fstream>
  8.  
  9. class CommandBase{
  10. protected:
  11. std::string m_name;//The internal name
  12. std::string m_help;//The internal help line
  13. public:
  14. //The public interface for name.
  15. const std::string &name = m_name;
  16. //The public interface for the help line.
  17. const std::string &help = m_help;
  18. virtual void execute(const std::string &line){}
  19.  
  20. CommandBase(){
  21. m_name = "BASE_COMMAND";
  22. m_help = "BASE_HELP_MESSAGE";
  23. }
  24.  
  25. };
  26.  
  27. class CommandExit : public CommandBase{
  28.  
  29. public:
  30. CommandExit(){
  31. m_name = "exit";
  32. m_help = "exit ~ Will cause the program to terminate. ";
  33. }
  34.  
  35. virtual void execute(const std::string &line){
  36. std::cout<<"Exiting"<<std::endl;
  37. exit(0);
  38. }
  39. }commandExit;
  40.  
  41. class CommandDouble : public CommandBase{
  42. public:
  43. CommandDouble(){
  44. m_name = "double";
  45. m_help = "double <number1> ~ Will output number1 times two. ";
  46. }
  47.  
  48. virtual void execute(const std::string &line){
  49. int i = std::atoi(line.c_str());
  50. std::cout<<"The double of: "<<i<<" is "<<i*2<<std::endl;
  51. }
  52. }commandDouble;
  53.  
  54. int split(const std::string& line, const std::string& seperator, std::vector<std::string> * values){
  55. std::string tString = "";
  56. unsigned counter = 0;
  57. for(unsigned l = 0; l < line.size(); ++l){
  58. for(unsigned i = 0; i < seperator.size(); ++i){
  59. if(line[l+i]==seperator[i]){
  60. if(i==seperator.size()-1){
  61. values->push_back(tString);
  62. tString = "";
  63. ++counter;
  64. }else continue;
  65. }else{
  66. tString.push_back(line[l]);
  67. break;
  68. }
  69. }
  70. }
  71. if(tString!="")values->push_back(tString);
  72. return counter;
  73. }
  74.  
  75. class CommandPower : public CommandBase{
  76. public:
  77. CommandPower(){
  78. m_name = "pow";
  79. m_help = "pow <number1> <number2> ~ Will raise number 1 to the power of number 2. ";
  80. }
  81.  
  82. virtual void execute(const std::string &line){
  83. double d, exp;
  84. std::cout<<"Param:"<<line<<std::endl;
  85. std::vector<std::string> vals;
  86. split(line," ",&vals);
  87. assert(vals.size()>=2);//We don't want a nasty Out Of Bounds.
  88. for(unsigned i = 0; i < vals.size(); ++i){
  89. std::cout<<"Vals["<<i<<"] = "<<vals[i]<<std::endl;
  90. }
  91. d = std::atof(vals[0].c_str());
  92. exp = std::atof(vals[1].c_str());
  93. std::cout<<d<<" raised to the power of: "<<exp<<" is "<<pow(d,exp)<<std::endl;
  94. }
  95. }commandPower;
  96.  
  97. class CommandFileWrite : public CommandBase{
  98. public:
  99. CommandFileWrite(){
  100. m_name = "write";
  101. m_help = "write <filename> [message] ~ Will append the message to the specified file. ";
  102. }
  103.  
  104. virtual void execute(const std::string &line){
  105. std::cout<<"Param:"<<line<<std::endl;
  106. std::vector<std::string> vals;
  107. split(line," ",&vals);
  108. assert(vals.size()>=1);//We don't want a nasty Out Of Bounds.
  109. for(unsigned i = 0; i < vals.size(); ++i){
  110. std::cout<<"Vals["<<i<<"] = "<<vals[i]<<std::endl;
  111. }
  112.  
  113. //The stream for the file we will save.
  114. std::ofstream fts;
  115. fts.open(vals[0].c_str(), std::ios::out|std::ios::app);
  116. if(fts.is_open()){
  117. for(unsigned i = 1; i < vals.size(); ++i){
  118. if(i+1<vals.size()) fts<<vals[i]<<" ";
  119. else fts<<vals[i]<<std::endl;
  120. }
  121. }else{
  122. std::cerr<<"Cannot open the file: "<<vals[0]<<std::endl;
  123. return;
  124. }
  125. }
  126. }commandFileWrite;
  127.  
  128. void help(const std::vector<CommandBase*> &commandList){
  129. std::cout<<"---------------Operating Instructions---------------"<<std::endl;
  130. for(unsigned i = 0; i < commandList.size(); ++i){
  131. std::cout<<commandList[i]->help<<std::endl;
  132. }
  133. std::cout<<"---------------------------------------------------"<<std::endl<<std::endl;
  134. }
  135.  
  136. void foo(){
  137. std::vector<CommandBase*> commandList;
  138. commandList.push_back(&commandExit);
  139. commandList.push_back(&commandDouble);
  140. commandList.push_back(&commandPower);
  141. commandList.push_back(&commandFileWrite);
  142.  
  143.  
  144. std::string ourCommand;
  145. std::string ourParameters;
  146.  
  147. help(commandList);
  148.  
  149. while(true){
  150. std::cin>>ourCommand;
  151. std::getline(std::cin,ourParameters);
  152.  
  153. //Remove any preceeding whitespace.
  154. ourParameters.erase(ourParameters.begin(), std::find_if(ourParameters.begin(), ourParameters.end(), std::bind1st(std::not_equal_to<char>(), ' ')));
  155.  
  156. std::cout<<"We will execute the command: "<<ourCommand<<std::endl;
  157. std::cout<<"We will use the parameters: "<<ourParameters<<std::endl;
  158.  
  159.  
  160. bool foundCommand = false;
  161. for(unsigned i = 0; i < commandList.size(); ++i){
  162. if(commandList[i]->name == ourCommand){
  163. foundCommand = true;
  164. commandList[i]->execute(ourParameters);
  165. break;
  166. }else continue;
  167. }
  168. if(!foundCommand){
  169. std::cout<<"The command: "<<ourCommand<<" was not reconized. Please try again."<<std::endl;
  170. help(commandList);
  171. }
  172. }
  173. }
  174.  
  175.  
  176. int main(){
  177. foo();
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement