CMS 3D CMS Logo

CacheParser.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     PluginManager
00004 // Class  :     CacheParser
00005 // 
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  Chris Jones
00010 //         Created:  Wed Apr  4 14:30:46 EDT 2007
00011 // $Id: CacheParser.cc,v 1.3 2007/11/07 03:34:46 wmtan Exp $
00012 //
00013 
00014 // system include files
00015 #include <algorithm>
00016 
00017 // user include files
00018 #include "FWCore/PluginManager/interface/CacheParser.h"
00019 #include "FWCore/Utilities/interface/Exception.h"
00020 #include "FWCore/Utilities/interface/Algorithms.h"
00021 
00022 namespace edmplugin {
00023 //
00024 // constants, enums and typedefs
00025 //
00026 
00027 //
00028 // static data member definitions
00029 //
00030 
00031 //
00032 // constructors and destructor
00033 //
00034 //CacheParser::CacheParser(std::istream&)
00035 //{
00036 //}
00037 
00038 // CacheParser::CacheParser(const CacheParser& rhs)
00039 // {
00040 //    // do actual copying here;
00041 // }
00042 
00043 //CacheParser::~CacheParser()
00044 //{
00045 //}
00046 
00047 //
00048 // assignment operators
00049 //
00050 // const CacheParser& CacheParser::operator=(const CacheParser& rhs)
00051 // {
00052 //   //An exception safe implementation is
00053 //   CacheParser temp(rhs);
00054 //   swap(rhs);
00055 //
00056 //   return *this;
00057 // }
00058 
00059 //
00060 // member functions
00061 //
00062 
00063 //
00064 // const member functions
00065 //
00066 
00067 //
00068 // static member functions
00069 //
00070   static void checkForError(const std::istream& iIn,
00071                             unsigned long iRecordNumber,
00072                             const std::string& iContext)
00073 {
00074     if(iIn.eof()) {
00075       throw cms::Exception("PluginCacheParseFailed")<<"Unexpectedly reached end of file for line "
00076       <<iRecordNumber<<" just after '"<<iContext<<"'";
00077     }
00078     if(iIn.bad()) {
00079       throw cms::Exception("PluginCacheParseFailed")<<"Reading failed on line "<<iRecordNumber <<" just after '"<<iContext<<"'";
00080     }
00081 }
00082   
00083 bool
00084 CacheParser::readline(std::istream& iIn, const boost::filesystem::path& iDirectory,
00085          unsigned long iRecordNumber, PluginInfo &oInfo, std::string& oPluginType)
00086 {    
00087   static const std::string kNewLine("start of new line");
00088   std::string fileName;
00089   std::string pluginName;
00090   iIn >> fileName;
00091   if(iIn.eof()) { return false;}
00092   checkForError(iIn,iRecordNumber,kNewLine);
00093   CacheParser::restoreSpaces(fileName);
00094   iIn >> pluginName;
00095   checkForError(iIn,iRecordNumber,fileName);
00096   CacheParser::restoreSpaces(pluginName);
00097   iIn >> oPluginType;
00098   checkForError(iIn,iRecordNumber,oPluginType);
00099   CacheParser::restoreSpaces(oPluginType);
00100   
00101   oInfo.loadable_ = iDirectory / fileName;
00102   oInfo.name_ = pluginName;
00103   
00104   //ignore everything to the end of line
00105   iIn.ignore(std::numeric_limits<int>::max(),
00106              '\n');
00107   while(iIn.peek() == '\n') {
00108     iIn.get();
00109   }  
00110   return true;
00111 }
00112 
00113 namespace {
00114   struct CompPluginInfos {
00115     bool operator()(const PluginInfo& iLHS,
00116                     const PluginInfo& iRHS) const
00117   {
00118     return iLHS.name_ < iRHS.name_;
00119   }
00120   };
00121 }
00122 
00123 void
00124 CacheParser::read(std::istream& iIn, 
00125                   const boost::filesystem::path& iDirectory,
00126                   CacheParser::CategoryToInfos& iOut)
00127 {
00128   unsigned long recordNumber=0;
00129   
00130   std::string pluginType;
00131   
00132   PluginInfo info;
00133 
00134   while(iIn) {
00135     ++recordNumber;
00136     if( not readline(iIn,iDirectory,recordNumber,info,pluginType) ) {
00137       break;
00138     }
00139     iOut[pluginType].push_back(info);
00140   }
00141   //now do a sort which preserves any previous order for files
00142   for(CacheParser::CategoryToInfos::iterator it = iOut.begin(), itEnd=iOut.end();
00143       it != itEnd;
00144       ++it) {
00145     std::stable_sort(it->second.begin(),it->second.end(), CompPluginInfos());
00146   }
00147 }
00148 
00149 void
00150 CacheParser::write(const CategoryToInfos& iInfos, std::ostream& oOut)
00151 {
00152   //order the data more to our liking: library then object then type
00153   LoadableToPlugins ordered;
00154   
00155   for(CategoryToInfos::const_iterator it = iInfos.begin();
00156       it != iInfos.end();
00157       ++it) {
00158     std::string type(it->first);
00159     for(std::vector<PluginInfo>::const_iterator it2=it->second.begin();
00160         it2 != it->second.end();
00161         ++it2) {
00162       //remove any directory specification
00163       std::string loadable(it2->loadable_.leaf());
00164       std::string name(it2->name_);
00165       ordered[loadable].push_back(NameAndType(name,type));
00166     }
00167   }
00168   write(ordered,oOut);
00169 }
00170 
00171 void 
00172 CacheParser::write(LoadableToPlugins& iIn, std::ostream& oOut)
00173 {
00174   for( LoadableToPlugins::iterator it = iIn.begin();
00175        it!=iIn.end();
00176        ++it) {
00177     std::string loadable(it->first.string());
00178     replaceSpaces(loadable);
00179     edm::sort_all(it->second);
00180     
00181     for(std::vector<std::pair<std::string,std::string> >::iterator it2 = it->second.begin();
00182         it2 != it->second.end();
00183         ++it2) {
00184       oOut << loadable <<" "<<replaceSpaces(it2->first)<<" "<<replaceSpaces(it2->second)<<"\n";
00185     }
00186   }
00187 }
00188 
00189 void 
00190 CacheParser::read(std::istream& iIn, LoadableToPlugins& oOut)
00191 {
00192   unsigned long recordNumber=0;
00193   
00194   std::string pluginType;
00195   
00196   PluginInfo info;
00197   NameAndType pat;
00198   boost::filesystem::path empty;
00199   
00200   while(iIn) {
00201     ++recordNumber;
00202     if( not readline(iIn,empty,recordNumber,info,pat.second) ) {
00203       break;
00204     }
00205     pat.first = info.name_;
00206     oOut[info.loadable_].push_back(pat);
00207   }
00208 }
00209 
00210 std::string& 
00211 CacheParser::replaceSpaces(std::string& io)
00212 {
00213   std::string::size_type index=0;
00214   while(std::string::npos != (index = io.find_first_of(" \t\n",index))) {
00215     io[index]='%';
00216   }
00217   return io;
00218 }
00219 
00220 std::string& CacheParser::restoreSpaces(std::string& io)
00221 {
00222   std::string::size_type index=0;
00223   while(std::string::npos != (index = io.find_first_of("%",index))) {
00224     io[index]=' ';
00225   }
00226   return io;
00227 }
00228 }

Generated on Tue Jun 9 17:36:32 2009 for CMSSW by  doxygen 1.5.4