/*
 *
 *
 *    cgi.h
 *
 *    Header file for classes to handle CGI posting input.
 *
 *    =========================================================================
 *
 *    Copyright (C) 1998-2003 by Matt Roberts, KK5JY.
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2.1 of the License, or (at your option) any later version.
 *
 *    This library 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
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    =========================================================================
 *
 *
 */


#ifndef __MY_CGI_H
#define __MY_CGI_H
	#include <iostream>
	#include <sstream>
	#include <string>
	#include <vector>

	#include <cstdlib>


	namespace my {

		/*
		 *
		 *    The 'my::cgi' class encapsulates an interface to CGI input.
		 *
		 */

		class cgi {
			private:
				class field {
					private:
						std::string cf_name;
						std::string cf_value;

					public:
						field (const char *in);
						field (const std::string &in);
						field (const field &cf);
						~field (void) { /* noop */ };

						std::string name (void) const { return cf_name; };
						std::string value (void) const { return cf_value; };
				};

			private:
				typedef std::vector<field*> list_type;
				list_type cf_vec;
				bool valid;

			public:
				cgi (std::istream &in = std::cin);
				~cgi (void);

				std::string getValue (const std::string &name);
				std::string getValue (const char *name);

				//CGI_Field &operator [](int i) { return cf_vec [i]; };
				unsigned size (void) { return cf_vec.size (); };

				operator bool() { return valid; };
				bool operator! () { return !valid; };
				bool isValid (void) { return valid; };
		};


		/*
		 *
		 *   cgi::field ctors
		 *
		 */

		inline cgi::field::field (const char *in) {
			std::istringstream input (in);
			getline (input, cf_name, '=');
			getline (input, cf_value, '\0');
		}

		inline cgi::field::field (const std::string &in) {
			std::istringstream input (in.c_str());
			getline (input, cf_name, '=');
			getline (input, cf_value, '\0');
		}

		inline cgi::field::field (const field &cf) {
			cf_name = cf.cf_name;
			cf_value = cf.cf_value;
		}
	}

#endif
