Advertisement
milanmetal

C++ STL Command parser

Nov 8th, 2019
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <regex>
  4. #include <fstream>
  5.  
  6.  
  7. #define WIDTH 640
  8. #define HEIGHT 480
  9. #define MAX_PKT_SIZE (640*480*4)
  10.  
  11. // Image pixels storage
  12. unsigned int image[WIDTH * HEIGHT] = {};
  13.  
  14. // Exptected colors
  15. const std::string BLACK     = "BLACK";
  16. const std::string RED       = "RED";
  17. const std::string YELLOW    = "YELLOW";
  18. const std::string GREEN     = "GREEN";
  19. const std::string BLUE      = "BLUE";
  20.  
  21. // Expected commands
  22. const std::string BCKG_s    = "BCKG";
  23. const std::string LINE_H_s  = "LINE_H";
  24. const std::string LINE_V_s  = "LINE_V";
  25. const std::string RECT_s    = "RECT";
  26.  
  27. // Enumerate expected commands
  28. enum Command_enum {INVALID_CMD = 0, BCKG = 1, LINE_H = 2, LINE_V = 3, RECT = 4};
  29.  
  30. // Loads text lines into vector of strings
  31. void cmdvector_from_file (std::ifstream &ifs, std::vector<std::string> &cmd_lines)
  32. {
  33.   if (ifs) {
  34.     std::copy(std::istream_iterator<std::string>(ifs),
  35.               std::istream_iterator<std::string>(),
  36.               std::back_inserter(cmd_lines));
  37.   } else {
  38.     std::cerr << "Couldn't open file for reading\n";
  39.   }
  40. }
  41.  
  42. // Returns detected current command ID
  43. Command_enum get_cmd_id (std::string &cmd_str) {
  44.     Command_enum cmd_ret;
  45.    
  46.     if (!cmd_str.compare(BCKG_s)) {
  47.         cmd_ret = BCKG;
  48.     } else if (!cmd_str.compare(LINE_H_s)) {
  49.         cmd_ret = LINE_H;
  50.     } else if (!cmd_str.compare(LINE_V_s)) {
  51.         cmd_ret = LINE_V;
  52.     } else if (!cmd_str.compare(RECT_s)) {
  53.         cmd_ret = RECT;
  54.     } else {
  55.         cmd_ret = INVALID_CMD;
  56.         std::cout << "\n" << cmd_str << " is invalid command! Commands are: BCKG, LINE_H, LINE_V, RECT\n" << std::endl;
  57.     }
  58.     return cmd_ret;
  59. }
  60.  
  61. // Convert color to 16-bit hex values
  62. // Bitwise RGBA Values:
  63. // https://www.fayewilliams.com/2011/09/21/bitwise-rgba-values/
  64. unsigned int colorstr2hex (const std::string color_str)
  65. {
  66.     unsigned int black_c  = 0;
  67.     unsigned int red_c    = (1<<15) | (1<<14) | (1<<13) | (1<<12) | (1<<11);
  68.     unsigned int green_c  = (1<<10) | (1<<9)  | (1<<8)  | (1<<7)  | (1<<6)  | (1<<5);
  69.     unsigned int blue_c   = (1<<4)  | (1<3)   | (1<<2)  | (1<<1)  | (1<<0);
  70.     // Red and green form yellow in RGB
  71.     unsigned int yellow_c = red_c | green_c;
  72.    
  73.     if (!color_str.compare(BLACK)) {
  74.         return black_c;
  75.     } else if (!color_str.compare(RED)) {
  76.         return red_c;
  77.     } else if (!color_str.compare(GREEN)) {
  78.         return green_c;
  79.     } else if (!color_str.compare(BLUE)) {
  80.         return blue_c;
  81.     } else if (!color_str.compare(YELLOW)) {
  82.         return yellow_c;
  83.     } else {
  84.         std::cout << "Colors are: BLACK, RED, GREEN, BLUE, YELLOW" << std::endl;
  85.     }
  86. }
  87.  
  88. int main()
  89. {
  90.     // Vector to store command strings from command file
  91.     std::vector<std::string> file_vec;
  92.     file_vec.push_back("BCKG: BLACK");
  93.     file_vec.push_back("RE_CT: 120, 240, 100, 200; BLUE");
  94.     file_vec.push_back("LINE_H: 100, 200, 10; YELLOW");
  95.     file_vec.push_back("LINE_V: 150, 200, 10; GREEN");
  96.     file_vec.push_back("RECT: 120, 240, 100, 200; BLUE");
  97.  
  98.     // Filename to read the commands from
  99.     std::ifstream ifs("filename");
  100.     // Push all lines into the file_vec vector
  101.     cmdvector_from_file (ifs, file_vec);
  102.  
  103.     // Stores parsed commands
  104.     std::vector<std::vector<std::string>> parsed_cmds;
  105.  
  106.     // Matches any continuous character and digit string
  107.     // Skips interpunction --> string explode around , ;:
  108.     std::regex rgx("\\w+");
  109.        
  110.     std::cout << "Parsed tokens: " << std::endl;
  111.  
  112.     // For each line in a file...
  113.     for (int lid = 0; lid < file_vec.size(); lid++) {
  114.         // Create iterator...
  115.         std::regex_iterator<std::string::iterator> it(file_vec[lid].begin(), file_vec[lid].end(), rgx);
  116.         std::regex_iterator<std::string::iterator> end;
  117.  
  118.         // Storage for extracted segments of each command
  119.         std::vector<std::string> cmd_segs;
  120.  
  121.         // Iterate through all matched segments...
  122.         for (; it != end; ++it)
  123.         {
  124.             std::cout << it->str() << std::endl;
  125.             // And store them into cammand segments vector
  126.             cmd_segs.push_back(it->str());
  127.         }
  128.         // At this moment all segments are stored
  129.         // Push their vector to the parsed commands vector
  130.         parsed_cmds.push_back(cmd_segs);
  131.     }
  132.     std::cout << "\nParsed commands: " << parsed_cmds.size() << std::endl;
  133.    
  134.     // Printout parsed segments of all commands
  135.     /*for (int cmd = 0; cmd < parsed_cmds.size(); cmd++) {
  136.         std::cout << std::endl;
  137.    
  138.         //for (int seg = 0; seg < parsed_cmds[cmd].size(); seg++) {
  139.             std::cout << parsed_cmds[cmd][0] << "   ";
  140.         //}
  141.     }*/
  142.    
  143.     // Execute all parsed commands
  144.     for (int cmd = 0; cmd < parsed_cmds.size(); cmd++) {
  145.         // Current command ID
  146.         Command_enum cmd_id = get_cmd_id(parsed_cmds[cmd][0]);
  147.         int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
  148.         int color_val = 0;
  149.        
  150.         // std::cout << "Current cmd_id: " << cmd_id << std::endl;
  151.        
  152.         switch (cmd_id) {
  153.             case BCKG:
  154.                 color_val = colorstr2hex(parsed_cmds[cmd][1]);
  155.                 // Fill in image vector with color values
  156.                 for(int y = 0; y < HEIGHT; y++) {
  157.                     for(int x = 0; x < WIDTH; x++) {
  158.                        image[y * WIDTH + x] = color_val;
  159.                     }
  160.                 }
  161.             std::cout << "Setting background color..." << std::endl;
  162.             break;
  163.            
  164.             case LINE_H:
  165.                 color_val = colorstr2hex(parsed_cmds[cmd][4]);
  166.                 x1 = stoi(parsed_cmds[cmd][1]);
  167.                 x2 = stoi(parsed_cmds[cmd][2]);
  168.                 y1 = stoi(parsed_cmds[cmd][3]);
  169.                 std::cout   << "Drawing horizontal line "
  170.                             << "  x1 = " << x1
  171.                             << "  x2 = " << x2
  172.                             << "  y1 = " << y1 << std::endl;
  173.                 for (int x = x1; x < x2; ++x)
  174.                     image[y1 * WIDTH + x] = color_val;
  175.             break;
  176.            
  177.             case LINE_V:
  178.                 color_val = colorstr2hex(parsed_cmds[cmd][4]);
  179.                 x1 = stoi(parsed_cmds[cmd][1]);
  180.                 y1 = stoi(parsed_cmds[cmd][2]);
  181.                 y2 = stoi(parsed_cmds[cmd][3]);
  182.                 std::cout   << "Drawing vertical line "
  183.                             << "  x1 = " << x1
  184.                             << "  y1 = " << y1
  185.                             << "  y2 = " << y2 << std::endl;
  186.                 for (int y = y1; y < y2; ++y)
  187.                     image[y * WIDTH + x1] = color_val;
  188.             break;
  189.            
  190.             case RECT:
  191.                 color_val = colorstr2hex(parsed_cmds[cmd][5]);
  192.                 x1 = stoi(parsed_cmds[cmd][1]);
  193.                 x2 = stoi(parsed_cmds[cmd][2]);
  194.                 y1 = stoi(parsed_cmds[cmd][3]);
  195.                 y2 = stoi(parsed_cmds[cmd][4]);
  196.                 std::cout   << "Drawing rect "
  197.                             << "  x1 = " << x1
  198.                             << "  x2 = " << x2
  199.                             << "  y1 = " << y1
  200.                             << "  y2 = " << y2 << std::endl;
  201.                 // Fill in rectangle between given points  
  202.                 for (int y = y1; y < y2; ++y)
  203.                     for (int x = x1; x < x2; ++x)
  204.                        image[y * WIDTH + x] = color_val;
  205.             break;
  206.            
  207.             default:
  208.             // Chill out
  209.             break;
  210.         }
  211.     }
  212.  
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement