1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
#include <iomanip>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
using namespace std;
struct FIELDS
{
string name;
string value;
};
const int cnt = 4;
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;
}
string get_qs ()
{
return qs;
}
void set_cnt (int f_how_many)
{
cnt = f_how_many;
} int get_cnt ()
{
return cnt;
}
//////////////////////////////////////////////////// // how_many() // This will count how many = signs in the QUERYSTRING ////////////////////////////////////////////////////
int how_many(string f_qs)
{
int start_pos = 0, pos, count = 0;
do
{
pos = f_qs.find ("=", start_pos);
count++;
start_pos = pos + 1;
}
while (pos != string::npos);
return count - 1;
}
////////////////////////////////////////////////////
//create_array
// Creates a dynamic array
////////////////////////////////////////////////////
FIELDS *create_array (int f_cnt)
{
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 [])
{
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);
start_pos = pos + 1;
pos = qs.find("&", start_pos);
if (pos == string::npos) {
pos = qs.length();
}
value = qs.substr(start_pos, pos - start_pos);
start_pos = pos + 1;
FIELDS field = {name, value};
f_name_value_pairs[counter] = field;
if (pos == qs.length()){
return;
}
}
}
/////////////////////////////////////////////
// param()
// Will receive the value for any given form field
/////////////////////////////////////////////
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 == lookUp){
return f_name_value_pairs[counter].value;
}
}
return "";
}
private:
string qs; // holds the QUERY_STRING
int cnt; // holds the number of fields found from the form
};
int main ()
{
WebApps wo;
return 0;
FIELDS name_value_pairs[cnt];
cout << "< br>debug in main with wo.get_cnt(): " << wo.
get_cnt () << "< br>" << endl;
FIELDS* name_value_pairs = wo.create_array (wo.get_cnt ());
testing array name_value_pairs[0].name = "worked< br>";
cout << "debug with name_value_pairs[0].name: " <<
name_value_pairs[0].name << "< br>" << endl;
wo.parse (wo.get_qs (), name_value_pairs);
cout << "debug print for name_value_pairs array: " << endl << "< br>";
for (int index = 0; index < wo.get_cnt (); index++)
{
cout << "name: " << name_value_pairs[index].name << " ";
cout << "value: " << name_value_pairs[index].
value << endl << "< br>";
}
return 0;
string first = wo.param("first", name_value_pairs, cnt);
string last = wo.param("last", name_value_pairs, cnt);
string color =wo.param("color", name_value_pairs, cnt);
string witch =wo.param("witch", name_value_pairs, cnt);
string about =wo.param("about", name_value_pairs, cnt);
cout<<"<html>";
cout<<"<style type='text/css'> .card { box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2); transition: 0.3s; border-radius: 8px; width: 40%; margin: auto; border: 3px solid "<<color<<";} .card:hover { box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2); } .container { padding: 2px 16px; } img { border-radius: 8px 8px 0 0; } </style>";
cout<<"<body><div><div class='card'>";
cout<<"<img src='"<<witch<<"' style='width:100%'>";
cout<<"<div class='container'><h4><b>FirstName: "<<first<<"</b></h4><h4><b>LastName :"<<last<<"</b></h4></div>"<<"About:"<<about<<"</b></h4><div>";
cout<<"</div></div></body></html>"<<endl;
return 0;
}
|