cc/paradigmas/t3/cliente/servidor.cpp

00001 #include "CGImap.h"
00002 #include <iostream>
00003 #include <string>
00004 #include <iostream>
00005 #include <fstream>
00006 #include <sstream>
00007 #include <stdio.h>
00008 #include <time.h>
00009 #include <ctype.h>
00010 
00011 using namespace std;
00012 
00017 string getUnicNameForFile() {
00018    stringstream s;
00019    time_t seconds;
00020    seconds = time (NULL);
00021    s << "questdata" << (long)seconds << ".csv";
00022    return s.str();
00023 }
00024 
00028 string intToStr(int valor) {
00029    stringstream s;
00030    s << valor;
00031    return s.str();
00032 }
00033 
00042 void saveFormToFile(CGImap& map) {
00043    fstream dataFile;
00044    dataFile.open(getUnicNameForFile().c_str(), fstream::out);
00045    cout << getUnicNameForFile() << endl;
00046    if (dataFile.fail()) {
00047       throw string("Arquivo não pode ser criado. Verifique as permissões do arquivo.");
00048    }
00049    if (map["quest.count"] == "") {
00050       throw string("Arquivo não pode ser salvo. Número de questoes não definido.");
00051    }
00052    int n = atoi(map["quest.count"].c_str());
00053    for (int i=1; i<=n; i++) {
00054        string tipo = "questao" + intToStr(i) + ".tipo";
00055        string resposta = "questao" + intToStr(i) + ".resposta";
00056        dataFile << map[tipo] << ",";
00057        dataFile << map[resposta] << endl; 
00058    }
00059    dataFile.close();
00060 }
00061 
00062 
00067 int main() {
00068   cout << "Content-type: text/plain\n" << endl;
00069   try {
00070       Post p; 
00071       CGImap query(p);
00072       try {
00073          saveFormToFile(query);
00074       } catch (string erro) {
00075          cout << "Erro: " << erro << endl;
00076          exit(1);
00077       }
00078   } catch (string erro) {
00079       cout << "Erro: " << erro << endl;
00080       exit(2);
00081   } catch (...) {
00082       cout << "Erro desconhecido";
00083       exit(3);
00084   }
00085   cout << "Formulário enviado com sucesso!" << endl;
00086   exit(0);
00087 } 

Gerado em Tue Aug 7 04:08:08 2007 para questgen por  doxygen 1.5.1