Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

dsCGI.h

Go to the documentation of this file.
00001 /* 
00002  * $Id: dsCGI.h,v 1.9 2004/07/29 07:55:11 dsamersoff Exp $
00003  */
00004 
00005 #ifndef dsCGI_h
00006 #define dsCGI_h 
00007 
00008 #ifdef HAVE_CONFIG_H
00009 # include <config.h>
00010 #endif
00011 
00012 
00013 #include <string.h>
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 
00017 #ifdef HAVE_UNISTD_H
00018 # include <unistd.h>
00019 #endif
00020 
00021 #ifdef HAVE_IO_H
00022 # include <io.h>
00023 #endif
00024 
00025 #include <dsSmartException.h>
00026 #include <dsutil.h>
00027 #include <dsHashTable.h>
00028 
00029 #define MS_MAGIC   "u04"
00030 #define MS_OFFSET  0xB0
00031 
00032 DECLARE_EXCEPTION(cgi);
00033 
00034 
00045 class dsCGI
00046 {
00047     dsHashTable *_ht;
00048     dsHashTable *_ht_cookie;
00049 
00050     char *_post_data;
00051 
00052 
00053     void decode(const char *src, int len, std::ostream& os);
00054 
00055 public: 
00056 
00057     dsCGI();
00058     dsCGI(const char *src);
00059     ~dsCGI();
00060 
00061     void parseQueryString( const char *query_string );
00062     void parseCookie( const char *cookie );
00063 
00064 
00070     void content_header(const char *mime = "text/html");
00071 
00077     const char *postData();
00078 
00085     char *seek(const char *key);
00086     char *seek_cookie(const char *key);
00087 
00096     char * getstring(const char *key);
00097     long getlong(const char *key);
00098 
00109     char * getstring(const char *key, char *defval);
00110     long getlong(const char *key, long defval);
00111 
00114     int operator !(){ return (int) !getenv( "GATEWAY_INTERFACE" ); }
00115 
00121     const char *pathInfo();
00122     
00127     const char *scriptName();
00128     
00129 };
00130 
00131 inline const char *dsCGI::postData()
00132 {
00133     return (const char *)_post_data;
00134 }
00135 
00136 inline  const char *dsCGI::pathInfo()
00137 {
00138     const char *s;
00139     if (! (s= getenv("PATH_INFO")))
00140             throw cgiException("Environment var PATH_INFO is not set");
00141 
00142    return s;
00143 }
00144 
00145 inline  const char *dsCGI::scriptName()
00146 {
00147     const char *s;
00148     if (! (s= getenv("SCRIPT_NAME")))
00149             throw cgiException("Environment var PATH_INFO is not set");
00150 
00151    return s;
00152 }
00153 
00154 inline char *dsCGI::seek(const char *key)
00155 {
00156     return _ht->seek(key);
00157 }
00158 
00159 inline char *dsCGI::seek_cookie(const char *key)
00160 {
00161     return _ht_cookie->seek(key);
00162 }
00163 
00164 inline char *dsCGI::getstring(const char *key)
00165 { 
00166     char *sp;
00167     if ( !(sp = _ht->seek(key)) && 
00168          !(sp = _ht_cookie->seek(key)) 
00169        )
00170        throw cgiException("Key '%s' not defined",key);
00171 
00172      return sp;
00173 }
00174 
00175 inline char *dsCGI::getstring(const char *key, char *defval)
00176 {  
00177     char *sp;
00178     if ( !(sp = _ht->seek(key)) )
00179          sp = _ht_cookie->seek(key);
00180 
00181     return (sp) ? sp : defval;
00182 }
00183 
00184 inline long dsCGI::getlong(const char *key)
00185 { 
00186     char *sp;
00187     if ( !(sp = _ht->seek(key)) && 
00188          !(sp = _ht_cookie->seek(key)) 
00189        )
00190        throw cgiException("Key '%s' not defined",key);
00191 
00192      return atol(sp);
00193 }
00194 
00195 inline long dsCGI::getlong(const char *key, long defval)
00196 {  
00197     char *sp;
00198     if ( !(sp = _ht->seek(key)) )
00199          sp = _ht_cookie->seek(key);
00200 
00201     return (sp) ? atol(sp) : defval;
00202 }
00203 
00220 class dsHtmlElements
00221 {
00222 public:
00223     void form(std::ostream& os, char *name, char *method, char *action);
00224     void formClose(std::ostream& os);
00225 
00226     void table(std::ostream& os, int spacing = 0, int padding = 0, int border = 0);
00227     void tableClose(std::ostream& os);
00228 
00229     void hidden(std::ostream& os, char *name, char *value);
00230     void input(std::ostream& os, char *label, char *name, char *invalue = "");
00231     void combo(std::ostream& os, char *label, char *name, char *optlist, char *selected="");
00232     void text(std::ostream& os, char *label, char *name, char *invalue = "");
00233 
00234     void submit(std::ostream& os, char *text, char *name="submit");
00235 };
00236 
00241 inline void dsHtmlElements::form(std::ostream& os, char *name, char *method, char *action)
00242 {
00243    os << "<FORM name=\"" << name << "\" method=\"" << method << "\" action=\"" << action << "\">" << std::endl;
00244 }
00245 
00246 inline void dsHtmlElements::formClose(std::ostream& os)
00247 {
00248    os << "</FORM>"<< std::endl;
00249 }
00250 
00251 inline void dsHtmlElements::table(std::ostream& os, int spacing, int padding, int border)
00252 {
00253    os << "<TABLE border=\"" << border << "\" cellspacing=\"" << spacing << "\" cellpadding=\"" << padding << "\">" << std::endl;
00254 }
00255 
00256 inline void dsHtmlElements::tableClose(std::ostream& os)
00257 {
00258    os << "</TABLE>"<< std::endl;
00259 }
00260 
00261 inline void dsHtmlElements::hidden(std::ostream& os, char *name, char *value)
00262 {
00263    os << "<INPUT type=\"hidden\" name=\"" << name << "\" value=\"" << value << "\">" << std::endl;
00264 }
00265 
00272 inline void dsHtmlElements::input(std::ostream& os, char *label, char *name, char *invalue)
00273 {
00274    os << "<TR><TD>" << label << "</TD><TD>";
00275    os << "<INPUT type=\"text\" name=\"" << name << "\" value=\"" << invalue << "\">";
00276    os << "</TD></TR>" << std::endl;
00277 }
00278 
00284 inline void dsHtmlElements::combo(std::ostream& os, char *label, char *name, 
00285                     char *optlist, char *selected)
00286 {
00287     os << "<TR><TD>" << label << "</TD><TD>";
00288     os << "<SELECT name=\"" << name << "\">" << std::endl;
00289 
00290     char *s = optlist, *e, *p;
00291 
00292     while(*s)
00293     {
00294         e = strchr(s,';');  
00295         if (!e)
00296             e = s + strlen(s)-1;
00297 
00298         if ( (p = ds_strnchr(s,'=',e-s)) )
00299         {
00300             os << "<OPTION value=\"";
00301             os.write(p+1, e-p-1); // value
00302             os << "\"";
00303 
00304             if ( strncmp(s, selected, p-s)  == 0)
00305             {
00306                 os << " selected ";
00307             }
00308 
00309             os << ">";
00310             os.write(s,p-s);  // key
00311             os << "</OPTION>" << std::endl; 
00312         }
00313         else
00314         {   
00315             os << "<OPTION value=\"";
00316             os.write(s, e-s); // key as option
00317             os << "\"";
00318 
00319             if ( strncmp(s, selected, e-s)  == 0)
00320             {
00321                 os << " selected ";
00322             }
00323 
00324             os << ">";
00325             os.write(s,e-s);     // key
00326             os << "</OPTION>" << std::endl; 
00327         }
00328           
00329          s = e+1;
00330     }
00331 
00332     os << "</TD></TR>" << std::endl;
00333 }
00334 
00335 inline void dsHtmlElements::text(std::ostream& os, char *label, char *name, char *invalue)
00336 {
00337    os << "<TR><TD>" << label << "</TD><TD>";
00338    os << "<TEXTAREA name=\"" << name << "\">" << invalue << "</TEXTAREA>";
00339    os << "</TD></TR>" << std::endl;
00340 }
00341 
00342 inline void dsHtmlElements::submit(std::ostream& os, char *text, char *name)
00343 {
00344    os << "<TR><TD colspan=2 align=center>";
00345    os << "<INPUT type=\"submit\" name=\"" << name << "\" value=\"" << text << "\">";
00346    os << "</TD><TD>&nbsp;</TD></TR>" << std::endl;
00347 }
00348 
00349 #endif

Generated on Mon May 16 18:26:57 2005 for libdms4 by doxygen1.3-rc2