Main Page   Class Hierarchy   File List  

cgiTemplates.cpp

00001 
00002 /*
00003   cgiTemplates implementation file
00004 
00005 members
00006   ocString unparsed;
00007   paragraphMap parsed;
00008 
00009 */
00010 #include "cgiTemplates.h"
00011 
00012 // default and only constructor
00013 cgiTemplates::cgiTemplates():comments(false)
00014 {
00015   ;
00016 }
00017 
00018 void cgiTemplates::commentsOn(void)
00019 {
00020   comments=true;
00021 }
00022 void cgiTemplates::commentsOff(void)
00023 {
00024   comments=false;
00025 }
00026 
00027 // non virtual destructor
00028 cgiTemplates::~cgiTemplates()
00029 {
00030   ;
00031 }
00032 
00033 // load and parse the file */
00034 bool cgiTemplates::load ( const char * filename )
00035 {
00036   bool bRet = false;
00037   unparsed = "";
00038   ifstream templateFile( filename );
00039   if( templateFile )
00040   {
00041     char buffer[1024];
00042     templateFile.getline( buffer, sizeof(buffer) );
00043     while( templateFile.rdstate() == ios::goodbit )
00044     {
00045       if( strlen(buffer) )
00046       {
00047         unparsed += buffer;
00048         unparsed += "\n";
00049       }
00050       templateFile.getline( buffer, sizeof(buffer) );
00051     }
00052     if( unparsed.length() )
00053     {
00054       bRet = parse();
00055     }
00056   }
00057   return bRet;
00058 }
00059 cgiTemplates & cgiTemplates::operator = ( const cgiTemplates & in )
00060 {
00061   parsed = in.parsed;
00062   return *this;
00063 }
00064 bool cgiTemplates::parse( void )
00065 {
00066   bool bRet = false;
00067   unparsed.parseInit();
00068   ocString element;
00069 
00070   for( element=unparsed.parse("<!--@");
00071        unparsed.endOfParse() == false;
00072        element=unparsed.parse("<!--@") )
00073   {
00074     /* we dont care about stuff between <!--/@elem1--> and <!--@elem2-->
00075        we just want the name for the paragraph
00076     */
00077     string name = unparsed.parse("-->");
00078 
00079     // now build the closing tag for the next parse
00080     string close = "<!--/@";
00081     close += name;
00082     close += "-->";
00083 
00084     /* now use the built tag to get the paragraph value
00085        to place in the associatie array
00086     */
00087     string value = "";
00088     if( comments )
00089     {
00090       value += "<!-- begin ";
00091       value += name;
00092       value += " -->";
00093     }
00094     value += unparsed.parse(close.c_str());
00095     if( comments )
00096     {
00097       value += "<!-- end ";
00098       value += name;
00099       value += " -->";
00100     }
00101     // add to paragraphs
00102     parsed[name] = value;
00103 
00104     bRet = true;
00105   }
00106   return bRet;
00107 }
00108 
00109 
00110 // return the paragraph labeled by 'key
00111 string & cgiTemplates::getParagraph( const string & key )
00112 {
00113   return parsed[key];
00114 }
00115 
00116 string & cgiTemplates::getParagraph( const char * key )
00117 {
00118   string strKey(key);
00119   return parsed[strKey];
00120 }
00121 
00122 // return the content of the html file unparsed
00123 string & cgiTemplates::getUnparsedHtml( void )
00124 {
00125   return unparsed;
00126 }

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