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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#include <iomanip>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <fstream> //for isalnum()
using namespace std;
struct FIELDS
{
string name;
string value;
};
void parse(string, FIELDS []); //will go through qs and find the "=" to seperate fields
string param(string, FIELDS []); //will check to make sure that the feilds match
int how_many(string); //counts how many feilds there are
FIELDS* create_FIELDS_array(int); //creates a dynamic array
void create_form(); //creates html form
void save_data(ofstream &, const char *,string, string); //will save data to file named survey.txt
void read_data(ifstream &, const char *);
void display_result();
int count_records(ifstream &); //will tally the votes and display the result in a format like this
void populate_survey_data_array(ifstream &, FIELDS [],int); //Y|70.36.224.178 - with a \n at the end to create a new line for the next record
bool IP_not_duplicated(string); //will make sure that there is no matching REMOTE_ADDR on the file
string qs(getenv("QUERY_STRING"));
const int cnt = how_many(qs);
int main(
{
cout << "Content-type:text/html\n\n";
create_form();
ofstream surveyfile;
ifstream input_file;
string ra(getenv("REMOTE_ADDR"));
FIELDS* name_value_pairs = create_FIELDS_array(cnt);
cout << "Stage 3" << "<p>" << endl;
parse (qs, name_value_pairs);
string vote = param("vote", name_value_pairs);
if (qs.length() != 0) {
cout << "debug with qs: " << qs << "<p>" << endl;
cout << "<p>" << endl;
cout << "debug for .name after parse:" << name_value_pairs[0].name << "<p>" << endl;
cout << "debug for .value after parse:" << name_value_pairs[0].value << "<p>" << endl;
cout << "debug for vote after param:" << vote << "<p>" << endl;
cout << "debug for IP:" << ra << "<p>" << endl;
save_data(surveyfile,"survey.txt", ra, vote);
read_data(input_file, "survey.txt");
}
else {
create_form();
}
return 0;
}
void parse (string 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);
f_name_value_pairs[counter].name = name;
cout << "name: " << f_name_value_pairs[counter].name << "," << 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].value = value;
cout << "value: " << value << "" << endl;
start_pos = pos + 1;
}
}
string param(string lookUp, FIELDS f_name_value_pairs[])
{
string f_value;
bool found = false;
if(f_name_value_pairs[0].name != lookUp);
else {
found = true;
f_value = f_name_value_pairs[0].value;
}
if (found)
return f_value;
else
return "";
}
int how_many (string qs)
{
int pos=0;
int count=0;
int start_pos=0;
do {
pos = qs.find("=", start_pos);
if (pos != string::npos){
count++;
start_pos= pos+1;
}
} while (pos != string::npos);
return count;
}
FIELDS * create_FIELDS_array (int f_cnt)
{
FIELDS * address;
address = new FIELDS[f_cnt];
return (address);
}
void create_form() {
cout << "<html><head><title>Exercise Survey</title></head>" <<
"<body>\n" <<
"<form action=\"survey.cgi\" method=\"GET\">\n" <<
" Do You Prefer Running Over Biking?<br>\n" <<
" <label for=\"vote\">Yes</label>\n" <<
" <input name=\"vote\" value=\"Y\" type=\"radio\"></td><br>\n" <<
" <label for=\"vote\">No</label>\n" <<
" <input name=\"vote\" value=\"N\" type=\"radio\"></td><p>\n" <<
" <p><input type=\"submit\" value=\"Submit\">\n" <<
"</form>\n" <<
"</body>\n" <<
"</html>\n";
}
void save_data(ofstream &surveyfile, char const *file_name, string f_ra, string f_vote)
{
surveyfile.open (file_name, ios::app);
string new_line;
if (surveyfile.fail());
else{
new_line= f_vote + "|" + f_ra + "\n";
surveyfile << new_line;
}
surveyfile.close();
}
void read_data(ifstream &input_file, char const *file_name)
{
input_file.open (file_name);
if (input_file.fail());
else{
int vote_count=count_records(input_file);
FIELDS* data_array = create_FIELDS_array(cnt);
populate_survey_data_array(input_file, data_array, vote_count);
}
}
int count_records(ifstream &surveyfile) //responsible for counting the votes
{
cout << "Counting how many records in input_file_open ... " << endl;
int survey_text_count = 0; //Extra record gets counted at the end of the file
while (!surveyfile.fail())
{
string data;
getline(surveyfile, data);
survey_text_count++;
cout << "data is :" << data << endl;
}
return survey_text_count -1;
}
void populate_survey_data_array (ifstream &f_input_file, FIELDS f_data_pairs, int vote_count)
{
f_input_file.clear();
f_input_file.seekg(0L, ios::beg);
string data;
for (int i=0; i<vote_count;i++)
{
getline(f_input_file, data);
f_data_pairs[i]=data;
}
}
|