Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace std;
- struct FIELDS
- {
- string name;
- string value;
- };
- //cnt should be set to the number of fields the html form contains
- class WebApps {
- public: //public access specifier.
- WebApps (){ //constructor
- cout << "Content-type:text/html\n\n"; //get ready to print on browser
- set_qs(getenv("QUERY_STRING")); //save string to private qs
- cout << "debug with get_qs: " << get_qs(); //testing functions
- set_cnt(how_many(get_qs()));
- cout << "<br>debug with get_cnt: " << get_cnt();//testing functions
- }
- void set_qs(string f_getenv)
- {
- qs = f_getenv;
- }
- void set_cnt(int f_how_many)
- {
- cnt = f_how_many;
- }
- string get_qs()
- {
- return qs;
- }
- int get_cnt()
- {
- return cnt;
- }
- /////////////////////////////////////////////////////
- // how_many()
- // This will count and return how many = signs in the QUERYSTRING
- ////////////////////////////////////////////////////
- int how_many (string f_qs)
- {
- //initialize variables
- int startPos = 0, pos, count = 0;
- do {
- pos = f_qs.find ("=", startPos);
- count++;
- startPos = pos + 1;
- }
- while (pos != string::npos);
- return count - 1;
- //returns the count or 0 if f_qs is empty
- }
- ////////////////////////////////////////////////////
- //create_array
- // Creates a dynamic array
- ////////////////////////////////////////////////////
- FIELDS * create_array (int f_cnt)
- {
- //modify function from last assignment
- FIELDS *array= new FIELDS [f_cnt]; //creating the dynamic array
- return array;
- }
- /////////////////////////////////////////////
- // parse()
- // This will separate the name/value pairs found after the ? in the URL
- /////////////////////////////////////////////
- void parse (string f_qs, FIELDS f_name_value_pairs [])
- {
- // cout << "debug in parse<br>\n" << endl;
- string name, value;
- int start_pos = 0, pos;
- for (int counter=0; counter < cnt; counter++) {
- pos = qs.find("=", start_pos);
- name = qs.substr(start_pos, pos - start_pos);
- // cout << "name: " << name << "<br>" << endl;
- start_pos = pos + 1;
- pos = qs.find("&", start_pos);
- if (pos == string::npos) {
- pos = qs.length();
- }
- value = qs.substr(start_pos, pos - start_pos);
- f_name_value_pairs[counter].name=name;
- f_name_value_pairs[counter].value=value;
- // cout << "value: " << value << "<br>" << endl;
- start_pos = pos + 1;
- }
- }
- //*******************************************
- // param()
- // This will find the field value based on the
- // field name
- //*******************************************
- string param(string lookUp, FIELDS f_name_value_pairs[], int f_cnt)
- {
- for(int counter = 0; counter < f_cnt; counter++)
- {
- if(f_name_value_pairs[counter].name.compare(lookUp)==0)
- return f_name_value_pairs[counter].value;
- }
- return "";
- }
- private: // private access specifier
- string qs; // holds the QUERY_STRING
- int cnt; // holds the number of fields found from the form
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement