Main Page   Class Hierarchy   File List  

cgiExtend.h

00001 /*
00002   cgiExtend.h
00003 
00004   Common Gateway Interface Class Definitions.
00005      Extended classes for cgi support
00006      -- generic tag container
00007   copyright (c) 2001
00008 
00009   David McCombs davidmc@newcottage.com
00010 
00011   Nuclear Core Class Library
00012 
00013   freely re-usable, redistributable.
00014 
00015 */
00016 #ifndef cgiEXTEND_H
00017 #define cgiEXTEND_H
00018 #include "cgiClass.h"
00019 
00020 /*
00021   A generic html tag container
00022 */
00023 
00024 class cgiCan : public cgiBase
00025 {
00026 protected:
00027   bool isClosed;
00028 public:
00029 
00030   cgiCan( const char * name, const char * attr = NULL)
00031   {
00032     isClosed = false;
00033     id = generic;
00034     opening = "<";
00035     opening += name;
00036     if( attr && strlen(attr) )
00037     {
00038       opening += " ";
00039       opening += attr;
00040     }
00041     opening += ">";
00042     cgiBase::close = "</";
00043     cgiBase::close += name;
00044     cgiBase::close += ">";
00045     *this << opening;
00046   }
00047 
00048   virtual cgiCan & open()
00049   {
00050     if( isClosed )
00051     {
00052       *this << cgiBase::opening << endl;
00053       flush();
00054       isClosed = false;
00055     }
00056     return *this;
00057   }
00058   virtual cgiCan & close()
00059   {
00060     if( !isClosed )
00061     {
00062       *this << cgiBase::close << endl;
00063       flush();
00064       isClosed = true;
00065     }
00066     return *this;
00067   }
00068   virtual ~cgiCan()
00069   {
00070     if( !isClosed )
00071     {
00072       *this << cgiBase::close << endl;
00073       flush();
00074     }
00075   }
00076   cgiCan & noClose( void )
00077   {
00078     isClosed = true;
00079     return *this;
00080   }
00081 };
00082 
00083 class cgiTextbox: public cgiBase
00084 {
00085 public:
00086   cgiTextbox( const char * name,
00087               const char * value = NULL,
00088               const char * size = NULL,
00089               const char * max = NULL,
00090               const char * attr = NULL )
00091   {
00092     id = generic;
00093 
00094     *this << "<input type='text'";
00095     if ( name && strlen(name) )
00096     {
00097       *this << " name='" << name << "'";
00098     }
00099     if ( value && strlen(value) )
00100     {
00101       *this << " value=\"" << value << "\"";
00102     }
00103     if ( size && strlen(size) )
00104     {
00105       *this << " size='" << size << "'";
00106     }
00107     if ( max && strlen(max) )
00108     {
00109       *this << " maxlength='" << max << "'";
00110     }
00111     if ( attr && strlen(attr) )
00112     {
00113       *this << " " << attr;
00114     }
00115     *this << ">";
00116     flush();
00117   }
00118 
00119   // unary tag - not a container
00120   virtual ~cgiTextbox(){;}
00121 };
00122 
00123 class cgiPassword: public cgiBase
00124 {
00125 public:
00126   cgiPassword( const char * name,
00127               const char * value = NULL,
00128               const char * size = NULL,
00129               const char * max = NULL,
00130               const char * attr = NULL )
00131   {
00132     id = generic;
00133 
00134     *this << "<input type='password'";
00135     if ( name && strlen(name) )
00136     {
00137       *this << " name='" << name << "'";
00138     }
00139     if ( value && strlen(value) )
00140     {
00141       *this << " value=\"" << value << "\"";
00142     }
00143     if ( size && strlen(size) )
00144     {
00145       *this << " size='" << size << "'";
00146     }
00147     if ( max && strlen(max) )
00148     {
00149       *this << " maxlength='" << max << "'";
00150     }
00151     if ( attr && strlen(attr) )
00152     {
00153       *this << " " << attr;
00154     }
00155     *this << ">";
00156     flush();
00157   }
00158   // unary tag - not a container
00159   virtual ~cgiPassword(){;}
00160 };
00161 
00162 class cgiSelect: public cgiCan
00163 {
00164 protected:
00165   string m_selected;
00166 public:
00167   cgiSelect(const char * attr = NULL):cgiCan( "select", attr )
00168   {
00169     ;
00170   }
00171   void setSelected( const char * selected )
00172   {
00173     m_selected = selected;
00174   }
00175   void addOption( const char * label, const char * value, bool selected=false, const char * attr = NULL )
00176   {
00177     *this << "<option";
00178     if( attr ) *this << " " << attr;
00179     if( (m_selected == value) || selected ) *this << " selected";
00180     *this << " value='" << value << "'>" << label << "</option>";
00181   }
00182   ~cgiSelect(){;}
00183 };
00184 
00185 class cgiTextarea: public cgiCan
00186 {
00187 
00188 public:
00189   cgiTextarea(const char * attr = NULL):cgiCan( "textarea", attr?attr:" rows='10' cols='80'" )
00190   {
00191     ;
00192   }
00193   ~cgiTextarea(){;}
00194 };
00195 
00196 class cgitr
00197 {
00198 private:
00199   cgiCan & m_parent;
00200   cgitr & t( char type, const char * text, const char * attr = NULL )
00201   {
00202     m_parent << "<t" << type;
00203     if( attr && strlen(attr) )
00204     {
00205       m_parent << " " << attr;
00206     }
00207     m_parent << ">";
00208     if( text && strlen(text) )
00209     {
00210       m_parent << text;
00211     }
00212     m_parent << "</t" << type << ">";
00213     return *this;
00214   }
00215 public:
00216   cgitr( cgiCan & parent ): m_parent(parent){;}
00217   ~cgitr(){;}
00218 
00219   // char * overloads
00220   cgitr & td( const char * text, const char * attr = NULL )
00221   {
00222     return t( 'd', text, attr );
00223   }
00224   cgitr & th( const char * text, const char * attr = NULL )
00225   {
00226     return t( 'h', text, attr );
00227   }
00228   // string overloads
00229   cgitr & td( const string & text )
00230   {
00231     return t( 'd', text.c_str() );
00232   }
00233   cgitr & th( const string & text )
00234   {
00235     return t( 'h', text.c_str() );
00236   }
00237   // string with attribute overloads
00238   cgitr & td( const string & text, const string & attr )
00239   {
00240     return t( 'd', text.c_str(), attr.c_str() );
00241   }
00242   cgitr & th( const string & text, const string & attr )
00243   {
00244     return t( 'h', text.c_str(), attr.c_str() );
00245   }
00246   void begin(const char * attr = "")
00247   {
00248     m_parent << "<tr";
00249     if( attr && strlen(attr) )
00250     {
00251       m_parent << " " << attr;
00252     }
00253     m_parent << ">";
00254   }
00255   void end()
00256   {
00257     m_parent << "</tr>" << endl;
00258   }
00259 };
00260 
00261 class cgiTable: public cgiCan
00262 {
00263 private:
00264    cgitr m_tr;
00265 
00266 public:
00267   cgiTable (const char * attr = ""):cgiCan( "table", attr ),m_tr(*this){;}
00268   cgitr & tr( const char * attr = "" )
00269   {
00270     m_tr.begin(attr);
00271     return m_tr;
00272   }
00273 };
00274 
00275 #endif

Generated on Tue Jan 20 09:03:27 2004 for OpenTools by doxygen1.2.18