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 = true; 00055 parse(); 00056 } 00057 } 00058 return bRet; 00059 } 00060 cgiTemplates & cgiTemplates::operator = ( const cgiTemplates & in ) 00061 { 00062 parsed = in.parsed; 00063 return *this; 00064 } 00065 bool cgiTemplates::parse( void ) 00066 { 00067 bool bRet = false; 00068 unparsed.parseInit(); 00069 ocString element; 00070 00071 for( element=unparsed.parse("<!--@"); 00072 unparsed.endOfParse() == false; 00073 element=unparsed.parse("<!--@") ) 00074 { 00075 /* we dont care about stuff between <!--/@elem1--> and <!--@elem2--> 00076 we just want the name for the paragraph 00077 */ 00078 string name = unparsed.parse("-->"); 00079 00080 // now build the closing tag for the next parse 00081 string close = "<!--/@"; 00082 close += name; 00083 close += "-->"; 00084 00085 /* now use the built tag to get the paragraph value 00086 to place in the associatie array 00087 */ 00088 string value = ""; 00089 if( comments ) 00090 { 00091 value += "<!-- begin "; 00092 value += name; 00093 value += " -->"; 00094 } 00095 value += unparsed.parse(close.c_str()); 00096 if( comments ) 00097 { 00098 value += "<!-- end "; 00099 value += name; 00100 value += " -->"; 00101 } 00102 // add to paragraphs 00103 parsed[name] = value; 00104 00105 bRet = true; 00106 } 00107 return bRet; 00108 } 00109 00110 00111 // return the paragraph labeled by 'key 00112 string & cgiTemplates::getParagraph( const string & key ) 00113 { 00114 return parsed[key]; 00115 } 00116 00117 string & cgiTemplates::getParagraph( const char * key ) 00118 { 00119 string strKey(key); 00120 return parsed[strKey]; 00121 } 00122 00123 // return the content of the html file unparsed 00124 string & cgiTemplates::getUnparsedHtml( void ) 00125 { 00126 return unparsed; 00127 }
1.5.5