Advertisement
Guest User

cltools prototyp

a guest
Sep 16th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <iterator>
  3. #include <vector>
  4. #include <sstream>
  5. #include <map>
  6.  
  7. namespace cltools {
  8.  
  9.     typedef std::map< std::string, int(*)( std::vector< std::string > ) > cm_map;
  10.     typedef std::pair<  std::string, int(*)( std::vector< std::string > ) > cm_action;
  11.     typedef cm_map::iterator cm_map_itr;
  12.     typedef std::string clarg;
  13.     typedef std::vector<clarg> clargs;
  14.  
  15.     class Commandline {
  16.    
  17.     private:
  18.  
  19.         typedef std::istream_iterator<std::string> iit;
  20.  
  21.         cm_map commands;
  22.         bool stop = false;
  23.  
  24.     public:
  25.  
  26.         Commandline (cm_map commands) : commands ( commands ) {};
  27.  
  28.         int start_cl ( int(*init_func)() ) {
  29.            
  30.             using namespace std;
  31.  
  32.             int return_val = 0;
  33.        
  34.             int init_return_val;
  35.             if ((init_return_val =init_func()) != 0) {
  36.                 return init_return_val;
  37.             }
  38.  
  39.             while (!this->stop) {
  40.                 string inp;
  41.                 getline(cin, inp);
  42.  
  43.                 if (inp[0] == '/') {
  44.                     inp = inp.substr(1,inp.size()-1);
  45.                     istringstream iss (inp);
  46.                     clargs cmd_args;
  47.        
  48.                     copy (iit(iss), iit(), back_inserter(cmd_args));
  49.                     string cmd = cmd_args[0];
  50.                     cmd_args.erase(cmd_args.begin());
  51.                    
  52.                     if (commands.count( cmd ) > 0) {
  53.                         return_val = commands[cmd] ( cmd_args );   
  54.                     } else {
  55.                         cout << "Unknown command '" << cmd << "'" << endl;
  56.                     }
  57.  
  58.                 }
  59.             }
  60.  
  61.             return return_val;
  62.         }
  63.  
  64.         void stop_cl () {
  65.             this->stop = true;
  66.         }
  67.  
  68.     };
  69. }
  70.  
  71. int action_quit (cltools::clargs);
  72. int action_tell (cltools::clargs);
  73. int action_mockerr (cltools::clargs);
  74. int init_cmd ();
  75.  
  76. cltools::Commandline * cl;
  77.  
  78. int main () {
  79.    
  80.     using namespace cltools;
  81.  
  82.     cm_map commands {
  83.         cm_action("quit", &action_quit),
  84.         cm_action("tell", &action_tell),
  85.         cm_action("merr", &action_mockerr)
  86.     };
  87.  
  88.     cl = new Commandline (commands);
  89.  
  90.     int return_val =  cl->start_cl( &init_cmd );
  91.  
  92.     delete cl;
  93.     return return_val;
  94. }
  95.  
  96. int init_cmd () {
  97.  
  98.     using namespace std;
  99.  
  100.     cout << "Init it" << endl;
  101.  
  102.     return 0;
  103. }
  104.  
  105. int action_quit (cltools::clargs args) {
  106.     cl->stop_cl();
  107.     return 0;
  108. }
  109.  
  110. int action_tell (cltools::clargs args) {
  111.  
  112.     for (cltools::clarg arg : args) {
  113.         std::cout << arg << std::endl;
  114.     }
  115.  
  116.     return 0;
  117. }
  118.  
  119. int action_mockerr (cltools::clargs) {
  120.  
  121.     using namespace std;
  122.  
  123.     cout << "Mocked some error" << endl;
  124.    
  125.     return 1;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement