#include <iostream>
// Stuff for AJAX
#include "/home/class/csc3004/cgicc/Cgicc.h"
#include "/home/class/csc3004/cgicc/HTTPHTMLHeader.h"
#include "/home/class/csc3004/cgicc/HTMLClasses.h"

//Stuff for pipes
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <iostream>
#include <fstream>
#include "fifo.h"

using namespace std;
using namespace cgicc; // Needed for AJAX functions.

// Possible values returned by results_select
#define ONE   "One"
#define LIST    "List"
#define LAST  "Last"
#define MALE "Male"
#define FEMALE "Female"

//Indeed, myString needs to be a copy of the original string
std::string StringToUpper(std::string myString)
{
  const int length = myString.length();
  for(int i=0; i!=length ; ++i)
    {
      myString[i] = std::toupper(myString[i]);
    }
  return myString;
}

// Take a string of words and make a HTML table line.
void makeTableLine(string& line) {

  cout << "<tr><td> ";
  for (int i = 0; i <  line.length() ; i++) {

    if (line[i] == ' ') {
      cout << " <td> ";
    } else {
      cout << line[i];
    }
  }
  cout << " </tr> ";
}

// Pipes for communication
char *receive_pipe = "namereply";
char *send_pipe = "namerequest";

int main() {
  Cgicc cgi;    // Ajax object
  char *cstr;
  // Create AJAX objects to recieve information from web page.
  form_iterator rs = cgi.getElement("results_select");
  form_iterator ts = cgi.getElement("type_select");
  form_iterator name = cgi.getElement("name");

  string type = "$LAST*";
  if (**ts == LAST) {
 type = "$LAST*";
  } else if (**ts == MALE) {
 type = "$MALE*";
  } else if (**ts == FEMALE) {
 type = "$FEMALE*";
  }

  // Call server to get results
  string stname = **name;
  stname = StringToUpper(stname);
  string message =  type+stname;
  send(message,send_pipe);
  
  string results = recv(receive_pipe);

  cout << "Content-Type: text/plain\n\n";
  
  if (**rs == ONE) {
 cout << "<H3>" << results<< "</H3>";
  } else {
 string prev = "$PREV*";
 string next = "$NEXT*";

 // Go back ten
 for (int i=1 ; i<= 10 ; i++) {
   results="";
   send(prev,send_pipe);
   results = recv(receive_pipe);
 }

 //Print out twenty
 cout << "<table border=""2"">";
 cout << "<tr><th>Name</th><th>Percent*</th><th>Rank</th></tr>" << endl;
 for (int i=1 ; i<= 20 ; i++) {
   send(next,send_pipe);
   results="";
   results = recv(receive_pipe);
   makeTableLine(results);
   //      cout  << results << "<p>";
 }
 cout << "</table>";
 cout << "<p>*Percent of 0.000 means less then 0.000%";
  }

return 0;
}

-- JimSkon - 2011-03-02

Topic revision: r2 - 2011-03-08 - JimSkon
 
This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback