If a CGI wants to write to the browser, it writes to standard out. If an Apache module wants to write to the browser, it needs to use an API call. Likewise, CGI's read environment variables while Apache modules make API calls to get the same information.
Since groundwork programs can be compiled as CGI's or Apache modules, the inputoutput class provides an abstraction layer for writing to the browser and reading environment variables.
The following program illustrates the use of the inputoutput class.
#include <groundwork/inputoutput.h>
#include <groundwork/cgimodule.h>
MAIN {
// initialize the APISTRUCT
inputoutput::initApiStruct(APISTRUCT);
// write the HTTP header to the browser
inputoutput::outputStatus(APISTRUCT,"");
// write the Content-type: text/plain header to the browser
inputoutput::outputHeader(APISTRUCT,"Content-type","text/plain");
// write some text to the browser
inputoutput::output(APISTRUCT,NULL,"Hello world.\n");
// write some individual characters to the browser
inputoutput::output(APISTRUCT,NULL,'\n');
inputoutput::output(APISTRUCT,NULL,'a');
inputoutput::output(APISTRUCT,NULL,'e');
inputoutput::output(APISTRUCT,NULL,'i');
inputoutput::output(APISTRUCT,NULL,'o');
inputoutput::output(APISTRUCT,NULL,'u');
inputoutput::output(APISTRUCT,NULL,'\n');
// write a file to the browser
ifstream file("myfile.txt");
if (file.good()) {
inputoutput::output(APISTRUCT,NULL,file.rdbuf());
}
file.close();
// get some environment variables
char *query_string=inputoutput::getEnv(APISTRUCT,"QUERY_STRING");
char *document_root=inputoutput::getEnv(APISTRUCT,"DOCUMENT_ROOT");
char *remote_addr=inputoutput::getEnv(APISTRUCT,"REMOTE_ADDR");
// write all environment variables to the browser
inputoutput::env(APISTRUCT);
}