Advertisement
Guest User

working_webapps.h

a guest
May 23rd, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. using namespace std;
  2.  
  3.  
  4.  
  5. struct FIELDS
  6. {
  7. string name;
  8. string value;
  9. };
  10.  
  11.  
  12. //cnt should be set to the number of fields the html form contains
  13.  
  14. class WebApps {
  15. public: //public access specifier.
  16. WebApps (){ //constructor
  17.  
  18. cout << "Content-type:text/html\n\n"; //get ready to print on browser
  19. set_qs(getenv("QUERY_STRING")); //save string to private qs
  20. cout << "debug with get_qs: " << get_qs(); //testing functions
  21. set_cnt(how_many(get_qs()));
  22. cout << "<br>debug with get_cnt: " << get_cnt();//testing functions
  23. }
  24.  
  25. void set_qs(string f_getenv)
  26. {
  27. qs = f_getenv;
  28. }
  29.  
  30. void set_cnt(int f_how_many)
  31. {
  32. cnt = f_how_many;
  33. }
  34.  
  35. string get_qs()
  36. {
  37. return qs;
  38. }
  39.  
  40. int get_cnt()
  41. {
  42. return cnt;
  43. }
  44.  
  45. /////////////////////////////////////////////////////
  46. // how_many()
  47. // This will count and return how many = signs in the QUERYSTRING
  48. ////////////////////////////////////////////////////
  49. int how_many (string f_qs)
  50. {
  51. //initialize variables
  52. int startPos = 0, pos, count = 0;
  53.  
  54. do {
  55. pos = f_qs.find ("=", startPos);
  56. count++;
  57. startPos = pos + 1;
  58. }
  59. while (pos != string::npos);
  60. return count - 1;
  61. //returns the count or 0 if f_qs is empty
  62. }
  63.  
  64.  
  65. ////////////////////////////////////////////////////
  66. //create_array
  67. // Creates a dynamic array
  68. ////////////////////////////////////////////////////
  69.  
  70. FIELDS * create_array (int f_cnt)
  71. {
  72. //modify function from last assignment
  73. FIELDS *array= new FIELDS [f_cnt]; //creating the dynamic array
  74. return array;
  75. }
  76.  
  77. /////////////////////////////////////////////
  78. // parse()
  79. // This will separate the name/value pairs found after the ? in the URL
  80. /////////////////////////////////////////////
  81.  
  82. void parse (string f_qs, FIELDS f_name_value_pairs [])
  83. {
  84. // cout << "debug in parse<br>\n" << endl;
  85. string name, value;
  86. int start_pos = 0, pos;
  87. for (int counter=0; counter < cnt; counter++) {
  88. pos = qs.find("=", start_pos);
  89. name = qs.substr(start_pos, pos - start_pos);
  90. // cout << "name: " << name << "<br>" << endl;
  91. start_pos = pos + 1;
  92. pos = qs.find("&", start_pos);
  93. if (pos == string::npos) {
  94. pos = qs.length();
  95. }
  96. value = qs.substr(start_pos, pos - start_pos);
  97. f_name_value_pairs[counter].name=name;
  98. f_name_value_pairs[counter].value=value;
  99. // cout << "value: " << value << "<br>" << endl;
  100. start_pos = pos + 1;
  101. }
  102.  
  103. }
  104.  
  105. //*******************************************
  106. // param()
  107. // This will find the field value based on the
  108. // field name
  109. //*******************************************
  110. string param(string lookUp, FIELDS f_name_value_pairs[], int f_cnt)
  111. {
  112. for(int counter = 0; counter < f_cnt; counter++)
  113. {
  114. if(f_name_value_pairs[counter].name.compare(lookUp)==0)
  115. return f_name_value_pairs[counter].value;
  116. }
  117. return "";
  118. }
  119. private: // private access specifier
  120. string qs; // holds the QUERY_STRING
  121. int cnt; // holds the number of fields found from the form
  122.  
  123. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement