libFAUDES

Sections

Index

rti2html.cpp

Go to the documentation of this file.
00001 /** rti2hmtl.cpp  Utility to generate a html documents from rti files */
00002 
00003 /* FAU Discrete Event Systems Library (libfaudes)
00004 
00005 Copyright (C) 2009 Ruediger Berndt
00006 Copyright (C) 2009 Thomas Moor
00007 
00008 This library is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU Lesser General Public
00010 License as published by the Free Software Foundation; either
00011 version 2.1 of the License, or (at your option) any later version.
00012 
00013 This library is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 Lesser General Public License for more details.
00017 
00018 You should have received a copy of the GNU Lesser General Public
00019 License along with this library; if not, write to the Free Software
00020 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00021    
00022 
00023 #include <string>
00024 #include <cctype>
00025 #include <iostream>
00026 #include <fstream>
00027 #include "corefaudes.h"
00028 
00029 
00030 using namespace faudes;
00031 
00032 // ******************************************************************
00033 // error exit
00034 // ******************************************************************
00035 
00036 void Usage(const std::string& rMessage="") {
00037   // ui hints
00038   if(rMessage!="") {
00039     std::cerr << rMessage << std::endl;
00040     std::cout << "" << std::endl;
00041   }
00042   std::cerr << "rti2html: " << FDVersionString()  << std::endl;
00043   std::cout << "" << std::endl;
00044   std::cerr << "utility to generate an html snippets from an libFAUDES" << std::endl;
00045   std::cerr << "run-time interface definition file" << std::endl;
00046   std::cerr << std::endl << "usage: " << std::endl;
00047   std::cerr << " rti2html <rti input file> -dump" << std::endl;
00048   std::cerr << " rti2html <rti input file> -index <html output file>" << std::endl;
00049   std::cerr << " rti2html <rti input file> -section <plugin name> <html output file>" << std::endl;
00050   std::cerr << " rti2html <rti input file> -type <type name> <html output file>" << std::endl;
00051   std::cerr << " rti2html <rti input file> -function <function name> <html output file>" << std::endl;
00052   std::cerr << " rti2html <rti input file> -alltypes <html output file>" << std::endl;
00053   std::cerr << " rti2html <rti input file> -allfunctions <html output file>" << std::endl;
00054   std::cerr << " rti2html <rti input file> -allsections <html output file>" << std::endl;
00055   std::cerr << " rti2html <rti input file> -signature <function name> <html output file>" << std::endl;
00056   std::cerr << " rti2html <rti input file> -shortdoc <function/type name> <html output file>" << std::endl;
00057   std::cerr << std::endl << "note: use \"-\" as output file for console output" << std::endl;
00058   exit(1);
00059 }
00060 
00061 
00062 // ******************************************************************
00063 // helper: html for linked type name
00064 // ******************************************************************
00065 
00066 void TypeHtml(std::ostream* pStream, const std::string& rTypeName) {
00067 
00068   // just text if unknown
00069   if(!TypeRegistry::G()->Exists(rTypeName)) {
00070     *pStream << rTypeName;
00071     return;
00072   }
00073 
00074   // retrieve definition
00075   const TypeDefinition& tdef=TypeRegistry::G()->Definition(rTypeName);
00076   std::string tyname = tdef.Name();
00077   std::string tyhtml = tdef.HtmlDoc();
00078   if(tyhtml=="" || tyhtml=="none") {
00079     *pStream << tyname;
00080   } else {
00081     *pStream << "<a href=\"" << tyhtml << "\">" << tyname << "</a>";
00082   }
00083 }
00084 
00085 // ******************************************************************
00086 // helper: html for linked function name
00087 // ******************************************************************
00088 
00089 void FunctionHtml(std::ostream* pStream, const std::string& rFunctionName) {
00090 
00091   // just text if unknown
00092   if(!FunctionRegistry::G()->Exists(rFunctionName)) {
00093     *pStream << rFunctionName;
00094     return;
00095   }
00096 
00097   // retrieve definition
00098   const FunctionDefinition& fdef=FunctionRegistry::G()->Definition(rFunctionName);
00099   std::string fname = fdef.Name();
00100   std::string fhtml = fdef.HtmlDoc();
00101   if(fhtml=="" || fhtml=="none") {
00102     *pStream << fname;
00103   } else {
00104     *pStream << "<a href=\"" << fhtml << "\">" << fname << "</a>";
00105   }
00106 }
00107 
00108 // ******************************************************************
00109 // search for types
00110 // ******************************************************************
00111 
00112 void SearchTypesHtml(std::ostream* pIndexFile, const std::string& key="") {
00113 
00114   // traverse type registry
00115   bool found=false;
00116   for(TypeRegistry::Iterator tit=TypeRegistry::G()->Begin(); 
00117     tit!=TypeRegistry::G()->End(); tit++) 
00118   {
00119     // test for exact key
00120     if(key!="") {
00121       std::string section= tit->second->Name();
00122       std::transform(section.begin(), section.end(), section.begin(), tolower);
00123       if(section!=key) continue;
00124     }
00125     // test for user doc
00126     if(tit->second->TextDoc()=="") continue;
00127     // fine tune format
00128     if(found) *pIndexFile << "," << std::endl;
00129     // record success
00130     if(!found) found=true;
00131     // link for this type
00132     TypeHtml(pIndexFile,tit->second->Name());
00133   }
00134   // done
00135   if(!found) *pIndexFile << "<i>no matches found</i>" << std::endl;
00136 }
00137 
00138 // ******************************************************************
00139 // search for functions
00140 // ******************************************************************
00141 
00142 void SearchFunctionsHtml(std::ostream* pIndexFile, const std::string& key="") {
00143 
00144   // traverse function registry
00145   bool found=false;
00146   for(FunctionRegistry::Iterator fit=FunctionRegistry::G()->Begin(); 
00147     fit!=FunctionRegistry::G()->End(); fit++) 
00148   {
00149     // test for exact key
00150     if(key!="") {
00151       std::string section= fit->second->Name();
00152       std::transform(section.begin(), section.end(), section.begin(), tolower);
00153       if(section!=key) continue;
00154     }
00155     // test for user doc
00156     if(fit->second->TextDoc()=="") continue;
00157     // fine tune format
00158     if(found) *pIndexFile << "," << std::endl;
00159     // record success
00160     if(!found) found=true;
00161     // link for this type
00162     FunctionHtml(pIndexFile,fit->second->Name());
00163   }
00164   // done
00165   if(!found) *pIndexFile << "<i>no matches found</i>" << std::endl;
00166 }
00167 
00168 // ******************************************************************
00169 // search for sections
00170 // ******************************************************************
00171 
00172 void SearchSectionsHtml(std::ostream* pIndexFile, const std::string& key="") {
00173 
00174   // prepare list of all sections
00175   std::set< std::string > sections;
00176   for(TypeRegistry::Iterator tit=TypeRegistry::G()->Begin(); 
00177     tit!=TypeRegistry::G()->End(); tit++) 
00178   {
00179     std::string section= tit->second->KeywordAt(0);
00180     // bail out on trivial
00181     if(section=="") continue;
00182     // test for exact key
00183     if(key!="") {
00184       std::transform(section.begin(), section.end(), section.begin(), tolower);
00185       if(section!=key) continue;
00186     }
00187     // record
00188     sections.insert(tit->second->KeywordAt(0));
00189   }
00190   for(FunctionRegistry::Iterator fit=FunctionRegistry::G()->Begin(); 
00191     fit!=FunctionRegistry::G()->End(); fit++) 
00192   {
00193     std::string section=fit->second->KeywordAt(0);
00194     // bail out on trivial
00195     if(section=="") continue;
00196     // test for exact key
00197     if(key!="") {
00198       std::transform(section.begin(), section.end(), section.begin(), tolower);
00199       if(section!=key) continue;
00200     }
00201     // record
00202     sections.insert(fit->second->KeywordAt(0));
00203   }
00204 
00205   // list sections as links
00206   std::set< std::string >::iterator pit;
00207   for(pit=sections.begin(); pit != sections.end(); pit++) {
00208     // get nice name
00209     std::string sname = *pit;
00210     // have link
00211     std::string shtml = sname+"_index.html";
00212     std::transform(shtml.begin(),shtml.end(),shtml.begin(),tolower);
00213     // have entry as link
00214     if(pit!=sections.begin()) *pIndexFile << "," << std::endl;
00215     *pIndexFile << "<a href=\"" << shtml << "\">" << sname << "</a>";
00216   }
00217 }
00218 
00219 
00220 
00221 
00222 // ******************************************************************
00223 // type index by keyword (aka section name)
00224 // ******************************************************************
00225 
00226 void TypeIndexHtml(std::ostream* pIndexFile, const std::string& key="") {
00227 
00228   // traverse type registry
00229   bool head=false;
00230   for(TypeRegistry::Iterator tit=TypeRegistry::G()->Begin(); 
00231     tit!=TypeRegistry::G()->End(); tit++) 
00232   {
00233     // test for exact key
00234     if(key!="") {
00235       std::string section= tit->second->KeywordAt(0);
00236       std::transform(section.begin(), section.end(), section.begin(), tolower);
00237       if(section!=key) continue;
00238     }
00239     // test for user doc
00240     if(tit->second->TextDoc()=="") continue;
00241     // head line
00242     if(!head) {
00243       head=true;
00244       *pIndexFile << "<div class=\"registry_heading\"><strong>Types</strong></div>" << std::endl;
00245     }
00246     // index entry for this type
00247     *pIndexFile << "<div class=\"registry_index\">";
00248     TypeHtml(pIndexFile,tit->second->Name());
00249     *pIndexFile << "</div>" << std::endl;
00250   }
00251   // done
00252   if(head) *pIndexFile << "<br>" << std::endl;
00253 }
00254 
00255 // ******************************************************************
00256 // function index by keyword (aka plugin)
00257 // ******************************************************************
00258 
00259   void FunctionIndexHtml(std::ostream* pIndexFile, const std::string& key="") {
00260 
00261   // traverse function registry
00262   bool head=false;
00263   for(FunctionRegistry::Iterator fit=FunctionRegistry::G()->Begin(); 
00264     fit!=FunctionRegistry::G()->End(); fit++) 
00265   {
00266     // test for key
00267     if(key!="") {
00268       std::string section= fit->second->KeywordAt(0);
00269       std::transform(section.begin(), section.end(), section.begin(), tolower);
00270       if(section!=key) continue;
00271     }
00272     // test for user doc
00273     if(fit->second->TextDoc()=="") continue;
00274     // index entry for this function
00275     std::string fname = fit->second->Name();
00276     std::string fhtml = fit->second->HtmlDoc();
00277     if(fhtml=="") continue;
00278     // head line
00279     if(!head){
00280       head=true;
00281       *pIndexFile << "<div class=\"registry_heading\"><strong>Functions</strong></div>" << std::endl;
00282     }
00283     // list entry
00284     if(fhtml=="none") {
00285       *pIndexFile << "<div class=\"registry_index\"> "<< fhtml << "</div>" << std::endl;
00286       continue;
00287     }
00288     *pIndexFile << "<div class=\"registry_index\"><a href=\"" << fhtml << "\">" 
00289       << fname << "</a></div>" << std::endl;
00290   }
00291 
00292   // done
00293   if(head) *pIndexFile << "<br>" << std::endl;
00294 
00295 }
00296 
00297 // ******************************************************************
00298 // html index by keyword (aka section)
00299 // ******************************************************************
00300 
00301 void IndexHtml(std::ostream* pIndexFile, const std::string& key="") {
00302 
00303   // prepare list of all sections
00304   std::set< std::string > sections;
00305   for(TypeRegistry::Iterator tit=TypeRegistry::G()->Begin(); 
00306     tit!=TypeRegistry::G()->End(); tit++) 
00307   {
00308     std::string sec=tit->second->KeywordAt(0);
00309     if(sec!="") sections.insert(sec);
00310   }
00311   for(FunctionRegistry::Iterator fit=FunctionRegistry::G()->Begin(); 
00312     fit!=FunctionRegistry::G()->End(); fit++) 
00313   {
00314     std::string sec=fit->second->KeywordAt(0);
00315     if(sec!="") sections.insert(sec);
00316   }
00317 
00318   // sections headline
00319   if(sections.size()!=0) 
00320     *pIndexFile << "<div class=\"registry_heading\"><strong>Sections</strong></div>" << std::endl;
00321 
00322   // list sections
00323   std::set< std::string >::iterator pit;
00324   for(pit=sections.begin(); pit != sections.end(); pit++) {
00325     // get nice name
00326     std::string sname = *pit;
00327     // have link
00328     std::string shtml = sname+"_index.html";
00329     std::transform(shtml.begin(),shtml.end(),shtml.begin(),tolower);
00330     // have entry
00331     *pIndexFile << "<div class=\"registry_index\"><a href=\"" << shtml << "\">" 
00332       << sname << "</a></div>" << std::endl;
00333   }
00334 
00335   // sections done
00336   if(sections.size()!=0) 
00337     *pIndexFile << "<br>" << std::endl;
00338 
00339   // list types
00340   TypeIndexHtml(pIndexFile,key);
00341 
00342   // list functions
00343   FunctionIndexHtml(pIndexFile,key);
00344 }
00345 
00346 // ******************************************************************
00347 // signature
00348 // ******************************************************************
00349 
00350 void SignatureHtml(std::ostream* pOutFile, std::string function) {
00351 
00352   // bail out on non existence
00353   if(!FunctionRegistry::G()->Exists(function)) return;
00354 
00355   // function definition
00356   const FunctionDefinition& fdef=FunctionRegistry::G()->Definition(function);
00357   
00358   // bail out if there is no signature
00359   if(fdef.VariantsSize()==0) return;
00360 
00361   // start signature box
00362   *pOutFile << "<div class=\"registry_signature\">" << std::endl;
00363   *pOutFile << "<p><strong>Signature:</strong></p>" << std::endl;
00364   *pOutFile << "<p>" << std::endl;
00365 
00366   // loop signatures
00367   for(int i=0; i< fdef.VariantsSize(); i++) {
00368     const Signature& sigi=fdef.Variant(i);
00369     *pOutFile << fdef.Name() << "(";
00370     // loop params
00371     for(int j=0; j < sigi.Size(); j++) {
00372       if(j!=0) *pOutFile << ", ";
00373       const Parameter& parj=sigi.At(j);
00374       *pOutFile << "+" << Parameter::AStr(parj.mAttr) << "+ ";
00375       TypeHtml(pOutFile,parj.mTDName);
00376       *pOutFile << " <i>" << parj.mName << "</i>";
00377     }
00378     *pOutFile << ")<br>" << std::endl;
00379   }
00380   *pOutFile << "</p>" << std::endl;
00381 
00382 
00383   // close signature box
00384   *pOutFile << std::endl << "</div>" << std::endl;
00385 }
00386 
00387 // ******************************************************************
00388 // short doc
00389 // ******************************************************************
00390 
00391 void ShortdocHtml(std::ostream* pOutFile, std::string fname) {
00392 
00393   // its a function
00394   if(FunctionRegistry::G()->Exists(fname)) {
00395 
00396     // function definition
00397     const FunctionDefinition& fdef=FunctionRegistry::G()->Definition(fname);
00398   
00399     // stream doc
00400     *pOutFile << "<div class=\"registry_signature\">" << std::endl;
00401     *pOutFile << "<p>" << fdef.TextDoc() << "<p>" << std::endl;
00402     *pOutFile << "</div>" << std::endl;
00403 
00404     // stream signature
00405     SignatureHtml(pOutFile,fname);
00406   }
00407 
00408   // its a type
00409   if(TypeRegistry::G()->Exists(fname)) {
00410 
00411     // type definition
00412     const TypeDefinition& tdef=TypeRegistry::G()->Definition(fname);
00413   
00414     // stream doc
00415     *pOutFile << "<div class=\"registry_signature\">" << std::endl;
00416     *pOutFile << "<p>" << tdef.TextDoc() << "<p>" << std::endl;
00417     *pOutFile << "</div>" << std::endl;
00418 
00419   }
00420 
00421 
00422 }
00423 
00424 // ******************************************************************
00425 // command line ui
00426 // ******************************************************************
00427 
00428 
00429 int main(int argc, char *argv[]) {
00430 
00431   // min args
00432   if(argc < 3) Usage();
00433 
00434   // load registry 
00435   LoadRegistry(argv[1]);
00436 
00437   // dump registry
00438   if(std::string(argv[2])=="-dump") {
00439     // bail out
00440     if(argc != 3) Usage();
00441     // dump
00442     TypeRegistry::G()->Write();
00443     FunctionRegistry::G()->Write();
00444     return 0;
00445   } 
00446 
00447   // output file
00448   std::ostream* hout= &std::cout;
00449   std::ofstream fout;
00450   if(std::string(argv[argc-1]) != "-") {
00451     fout.open(argv[argc-1], std::ios::out);
00452     hout = &fout;
00453   }
00454   
00455   // generate global index
00456   if(std::string(argv[2])=="-index") {
00457     // bail out
00458     if(argc != 4) Usage();
00459     // run and done
00460     IndexHtml(hout);
00461     return 0;
00462   } 
00463 
00464   // generate section index
00465   if(std::string(argv[2])=="-section") {
00466     // bail out
00467     if(argc != 5) Usage();
00468     // have lower case plugin pattern
00469     std::string pattern=std::string(argv[3]);
00470     std::transform(pattern.begin(), pattern.end(), pattern.begin(), tolower);
00471     // run and done
00472     IndexHtml(hout,pattern);
00473     return 0;
00474   } 
00475 
00476   // generate linked function
00477   if(std::string(argv[2])=="-function") {
00478     // bail out
00479     if(argc != 5) Usage();
00480     // run and done
00481     FunctionHtml(hout,argv[3]);
00482     return 0;
00483   } 
00484 
00485   // generate linked function list
00486   if(std::string(argv[2])=="-allfunctions") {
00487     // bail out
00488     if(argc != 4) Usage();
00489     // run and done
00490     SearchFunctionsHtml(hout);
00491     return 0;
00492   } 
00493 
00494   // generate linked type
00495   if(std::string(argv[2])=="-type") {
00496     // bail out
00497     if(argc != 5) Usage();
00498     // run and done
00499     TypeHtml(hout,argv[3]);
00500     return 0;
00501   } 
00502 
00503   // generate all types
00504   if(std::string(argv[2])=="-alltypes") {
00505     // bail out
00506     if(argc != 4) Usage();
00507     // run and done
00508     SearchTypesHtml(hout);
00509     return 0;
00510   } 
00511 
00512   // generate all sections
00513   if(std::string(argv[2])=="-allsections") {
00514     // bail out
00515     if(argc != 4) Usage();
00516     // run and done
00517     SearchSectionsHtml(hout);
00518     return 0;
00519   } 
00520 
00521   // generate signature
00522   if(std::string(argv[2])=="-signature") {
00523     // bail out
00524     if(argc != 5) Usage();
00525     // run and done
00526     SignatureHtml(hout,argv[3]);
00527     return 0;
00528   } 
00529 
00530   // generate short doc
00531   if(std::string(argv[2])=="-shortdoc") {
00532     // bail out
00533     if(argc != 5) Usage();
00534     // run and done
00535     ShortdocHtml(hout,argv[3]);
00536     return 0;
00537   } 
00538 
00539   // error
00540   Usage();
00541   return(1);
00542 }

libFAUDES 2.14g --- 2009-12-3 --- c++ source docu by doxygen 1.5.6