Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. class ParseURL{
  7. private:
  8. int qualify; // 0 means we qualify
  9. public:
  10. string node;
  11. string service;
  12. bool primary;
  13. bool health;
  14. string epoch;
  15. // methods
  16. ParseURL(string URL);
  17. ~ParseURL();
  18. };
  19.  
  20.  
  21. ParseURL::ParseURL(string URL){
  22. vector<string> g2 = {"node", "service", "primary", "health", "epoch"};
  23.  
  24. for (string tag : g2){
  25. auto found = URL.find(tag);
  26. if ( found != string::npos ){
  27. auto cut_from = found + 1 + tag.size() ;
  28. auto meh = URL.substr(cut_from, URL.substr(cut_from).find("&") );
  29. // is there a way we can avoid these lines below?
  30. if (tag.find("node",0) == 0){
  31. this->node = meh;
  32. } else if (tag.find("service",0) == 0 ){
  33. this->service = meh;
  34. } else if (tag.find("epoch",0) == 0) {
  35. this->epoch = meh;
  36. } else if (tag.find("health",0) == 0){
  37. this->health = (meh == "OK");
  38. } else if (tag.find("primary",0) == 0 ){
  39. this->primary == (this->node == meh);
  40. }
  41. }
  42. }
  43.  
  44. }
  45.  
  46. ParseURL::~ParseURL(){
  47. cout << "Tearing Down the classn";
  48. }
  49. int main(){
  50. char req[] = "GET /register?node=hostrpi3&service=potatoservice&primary=node1&health=OK&epoch=1559345106 HTTP";
  51. string Request = req;
  52. Request = Request.substr(Request.find_first_of(" ") );
  53. Request = Request.substr(0, Request.find(" HTTP"));
  54. ParseURL *a = new ParseURL(Request);
  55.  
  56. cout.width(12);
  57. cout << "a->node: " << a->node << endl;
  58. cout.width(12);
  59. cout << "a->service: " << a->service << endl;
  60. cout.width(12);
  61. cout << "a->epoch: " << a->epoch << endl;
  62. cout.width(12);
  63. cout << "a->health: " << a->health << endl;
  64. cout.width(12);
  65. cout << "a->primary: " << a->primary << endl;
  66.  
  67. delete(a);
  68.  
  69. return 0;
  70. }
  71.  
  72. a->node: hostrpi3
  73. a->service: potatoservice
  74. a->epoch: 1559345106
  75. a->health: 1
  76. a->primary: 0
  77. Tearing Down the class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement