Main Page | Namespace List | Class List | Directories | File List | Class Members | File Members

cgi_in2.H

Go to the documentation of this file.
00001 /******************************************************************************
00002 *                          cgi_in.h                            2000-10-31 AgF *
00003 *                CGI interface for C++ programs                               *
00004 *******************************************************************************
00005 
00006 This header file defines the class CGI_Interface and the object cgi to
00007 facilitate web interface through CGI (Comman Gateway Interface).
00008 
00009 The constructor outputs a cgi header and reads input parameters from both
00010 POST and GET methods.
00011 
00012 Instructions:
00013 -------------
00014 Write #include <cgi_in.h> in the beginning of your C++ program to get easy
00015 access to input parameters.
00016 
00017 The member functions cgi.GetString(char * key), cgi.GetInt(char * key),
00018 and cgi.GetFloat(char * key) are used for getting the values for any input
00019 parameter, where key is the name of the parameter (case sensitive).
00020 
00021 Using with multiple modules:
00022 ----------------------------
00023 If your C++ program consists of multiple modules compiled separately,
00024 then #define NOTMAIN in all but one of the modules to avoid multiple
00025 instances of the object cgi.
00026 
00027 Testing on a PC:
00028 ----------------
00029 If the program is compiled on a PC (DOS or Windows) it will not attempt to
00030 read CGI input, which only exists on a web server. Instead, it will read
00031 the input from a file named input.txt. This file should have the format:
00032 PARAMETER1=VALUE1
00033 PARAMETER2=VALUE2
00034 PARAMETER3=VALUE3
00035 
00036 modified by Dan Spinosa 2005
00037 
00038 *****************************************************************************/
00039 
00040 #include <iostream>
00041 #include <string.h>
00042 #include <stdlib.h>
00043 
00044 //const int MAXKEYS = 1000;          // max. number input parameters
00045 //const int MAXINPUTLEN = 13000;    // max. total length of all inputs and keys
00046 
00047 using namespace std;
00048 
00049 // class CGI_Interface facilitates input/output through CGI.
00051 class CGI_Interface2 {
00052 public:
00054    CGI_Interface2( char*, int);               // constructor
00056    char * GetString(char * key);  // get input parameter named 'key' as string
00058    int GetInt(char * key);        // get input parameter named 'key' as integer
00060    float GetFloat(char * key);    // get input parameter named 'key' as float
00062    int NumberOfKeys();            // get number of keys
00064    char * GetNextKey();           // call repeatedly to get all key names
00066    void BeginKey();               // reset GetNextKey
00067 
00069    char *InputBuffer; // stores all input
00071    int InputBufferLen;
00073    char * keys[MAXKEYS];          // all keys (= field names)
00075    char * values[MAXKEYS];        // corresponding values
00077    int NumKeys;                   // number of keys
00079    int KeyIndex;                  // index for GetNextKey
00080 };
00081 
00082 //#ifdef __unix__  // check if compiled under unix (this is normal use)
00083 // constructor
00084 CGI_Interface2::CGI_Interface2(char* inputStr, int inputLen) {
00085    //cout << "CGI INTERFACE CONSTRUCTOR" << endl;
00086    char * p; int len;
00087    // read input from cin
00088    InputBuffer = (char*)malloc( inputLen + 100 );
00089    memcpy( InputBuffer, inputStr, inputLen );
00090    //cin.get(InputBuffer, MAXINPUTLEN, 0);
00091    // append input from QUERY_STRING
00092    p = getenv("QUERY_STRING"); 
00093    //len = strlen(InputBuffer);
00094    len = inputLen;
00095    InputBufferLen = len;
00096 
00097      
00098    if (p && *p && len + 4 < MAXINPUTLEN) {
00099       if (len > 1) strcat(InputBuffer,"&");
00100       strncat(InputBuffer, p, MAXINPUTLEN-len-1);
00101    }
00102    // InputBuffer has the form:
00103    // key1=value1&key2=value2&key3=value3 ...
00104    // split the string into keys and values:
00105    NumKeys = KeyIndex = 0; p = InputBuffer;
00106    while (NumKeys < MAXKEYS) {
00107       keys[NumKeys] = p;                 // pointer to key
00108       p = strchr (p, '=');               // search for '='
00109       if (p==0) break;                   // stop if '=' not found
00110       *p = 0;                            // end key string
00111       values[NumKeys++] = ++p;           // point to value
00112       p = strchr (p, '&');               // search for '&' separator
00113       if (p==0) break;                   // stop if '&' not found
00114       *p = 0; p++;                       // end value string
00115    }
00116    
00117    // translate special characters in values:
00118    for (int i=0; i<NumKeys; i++) {
00119       // replace all '+' with space
00120       p = values[i];
00121       while (1) {
00122          p = strchr(p,'+');
00123          if (p==0) break;
00124          *p = ' ';
00125       }
00126       // replace %XX with character that has hexadecimal code XX
00127       p = values[i];
00128       while (1) {
00129          p = strchr(p,'%');
00130          if (p==0) break;
00131          int hi = p[1] | 0x20;
00132          int lo = p[2] | 0x20;
00133          if (hi > '9') hi -= 87; else hi -= 48;
00134          if (lo > '9') lo -= 87; else lo -= 48;
00135          *p = (char)(hi * 16 + lo);
00136          strcpy (p+1, p+3); p++;
00137       }
00138    }
00139    // write cgi header to tell that the output is html:
00140    //cout << "Content-type: text/html\n\n";
00141 }
00142 
00143 
00144 // get input as string
00145 char * CGI_Interface2::GetString(char * key) {
00146    for (int i=0; i<NumKeys; i++) {
00147       if (strcmp(keys[i],key)==0) {
00148          return values[i];
00149       }
00150    }
00151    return "";
00152 }
00153 
00154 // get input as integer
00155 int CGI_Interface2::GetInt(char * key) {
00156    for (int i=0; i<NumKeys; i++) {
00157       if (strcmp(keys[i],key)==0) {
00158          return atoi(values[i]);
00159       }
00160    }
00161    return 0;
00162 }
00163 
00164 // get input as float
00165 float CGI_Interface2::GetFloat(char * key) {
00166    for (int i=0; i<NumKeys; i++) {
00167       if (strcmp(keys[i],key)==0) {
00168          return float(atof(values[i]));
00169       }
00170    }
00171    return 0.;
00172 }
00173 
00174 // get number of keys
00175 int CGI_Interface2::NumberOfKeys() {
00176    return NumKeys;
00177 }
00178 
00179 // call repeatedly to get all keys
00180 char * CGI_Interface2::GetNextKey() {
00181    if (KeyIndex >= NumKeys) return 0;
00182    return keys[KeyIndex++];
00183 }
00184 
00185 // reset GetNextKey
00186 void CGI_Interface2::BeginKey() {
00187    KeyIndex = 0;
00188 }
00189 
00190 // define cgi interface object and call constructor
00191 #ifndef NOTMAIN
00192 //CGI_Interface cgi;
00193 #endif
00194 
00195 

Generated on Mon May 16 11:08:36 2005 for BikeQuest by  doxygen 1.4.0