The CGILib C++ Library

This is the README / FAQ / HOWTO file of the CGILib C++ library. It is a set of programming utilities which simplify the complexities of CGI programming using the C++ language. Don't worry if you aren't a C++ guru!: Everything here is explained and made easy for you.

Please, send feedback.

[Developer Zone]

Downloading the files

You can download the whole package compressed.

The current distribution (1.0) has been tested and compiled in the following systems:

The package is compound of six files:

Name Content
CGIlib.h Header to be included in your files. Contains the classes definitions for the library.
CGILib.cpp The methods' bodies.
test.cpp An example CGI program which uses the CGILib library.
Makefile Auto-compilation for gcc users.
test.html HTML page with forms to test the library.
CGILib.html This file.

[Index]

Programming: The basics

The simplest program you can write uses only one instance of the class CGIParser. Its methods provide you of an useful way of reading the CGI data form the server and manage it.
The parse() method must be called first and only once to retrieve the data. Then you can fetch it by using:

Function Description
getType() Returns one string of "GET", "POST" or "ISINDEX" indicating the type of transaction.
getText() Returns the whole decoded text transmited by the server.
getData() Returns a CGIData object (a STL map) which can be accesed using the subscript operator []. The keys are the NAME fields of the HTML FORM inputs and the values are the data typed by the user. All of them are standard strings.

It is recommended to enclose all the calls to these methods in an try/catch block in order to capture the CGIException that can be thrown if something goes wrong.

This is a simple example:

HTML code
...
<FORM METHOD=GET ACTION="http://site/cgi-bin/myprog">
<INPUT TYPE="text" NAME="name" SIZE=30>
<INPUT TYPE="submit>
</FORM>
...
myprog C++ code
# include <iostream.h>
# include "CGILib.h"
void main()
{
...
try
	{
	CGIParser parser;
	parser.parse();
	cout << "Type: " << parser.getType() << endl;
	CGIData data = parser.getData();
	cout << "Name: " << data["name"] << endl;
	}
	catch (CGIException e)
		{
		cerr << "CGILib error: " << e.getText() << endl;
		}
...
}

[Index]

Common questions

Q: How could I enumetate the keys received?
A: Renember that the CGIData is a STL map, so, if you want to enumetate the keys and values, you must use an iterator:

...
GCIData data = parser.getData();
CGIData::iterator itr;
for (itr = data.begin ; itr != data.end ; itr++)
	cout << "name = " << (*itr).first
	     << " value = " << (*itr).second << endl;
...

Q: In my Linux box I can't check somethig like if (data["name"] == "value")... Why?
A: Some string distributions can't handle the operator== correctly. Use: if (data["name"].compare("value") == 0)...

For further information, please check the comentaries in the source code.

[Index]


Last revised on Nov 29 1996 © Mack