/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/*

CgiCppHelper
Janitha Karunaratne (janitha at janitha.com)
July 21 2005

This is a simple class to assist when developing
c++ applications specifically for the use through a cgi
enviornment.

Current features:
	* POST request interpretation
	* Setting content-type header before outputting anything

Usage
	* include this file in your applications
	* declare a Cgicpphelper object in your applications
	* to read a POST variable's value sent to the applications
		Cgicpphelper::getvalue("variablename");
		and this would return the value

Example interpretation

	//the POST data set var2=value1&var2=value2&vvv3=value3 is sent

	#include "Cgicpphelper.h"
	int main() {
		Cgicpphelper o;
		o.getvalue("vvv3");
		//would equal to value3
	}
*/

#include <iostream>
#include <vector>
#include <string>
using namespace std;
/*
	Keeps each variable - value set
*/
class Varset {
public:
	std::string var, value;
	Varset(std::string var, std::string value)
		:var(var), value(value) {}
};
/*
	Interprets the POST data sent to CGI Script
*/
class Cgicpphelper {
private:
	std::string postdata;
	std::vector <Varset> vars;
public:
	Cgicpphelper();
	void outputinit();
	bool interpret(std::string input);
	std::string getvalue(std::string var);
};
/*
	Read the POST request and send to the interpret() to eextract the data
*/
Cgicpphelper::Cgicpphelper()
{
	outputinit();
	std::cin >> postdata;
	interpret(postdata);
}
/*
	Takes the variable and value from a POST request
	var1=value1&var2=value2
	Recursive
*/
bool Cgicpphelper::interpret(std::string input)
{
	std::string var, value;
	//extract the variable and value
	var = input.substr(0,input.find("=",0));
	value = input.substr(input.find("=",0)+1, input.find("&",0)-input.find("=",0)-1);
	vars.push_back(Varset(var,value));
	//checks if it has reach the last variable, since at end there is no &
	if(input.find("&",0) == -1)
		return true;
	//get rid of used variable and value
	input.erase(0,input.find("&",0)+1);
	//recursively call the interpret function with the
	//trucated input for data extraction
	if(input.size() > 1)
		interpret(input);
}
/*
	function which returns if variable with asked name is sent
	returns the value if present, else returns a -1
*/
std::string Cgicpphelper::getvalue(string var)
{
	for(std::vector <Varset>::iterator it = vars.begin(); it != vars.end(); it++)
		if(it->var == var)
			return it->value;
	return "-1";
}
/*
	Sends the Content-Type: to client for browser usability
	include anything else you want to be included in the header
*/
void Cgicpphelper::outputinit()
{
	std::cout << "Content-Type: text/html; charset=ISO-8859-1" << endl << endl;
}

//eof
