Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:59

0001 // -*- C++ -*-
0002 //
0003 // Package:     PluginManager
0004 // Class  :     CacheParser
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Wed Apr  4 14:30:46 EDT 2007
0011 //
0012 
0013 // system include files
0014 #include <algorithm>
0015 #include <limits>
0016 #include "boost/version.hpp"
0017 
0018 // user include files
0019 #include "FWCore/PluginManager/interface/CacheParser.h"
0020 #include "FWCore/Utilities/interface/Exception.h"
0021 #include "FWCore/Utilities/interface/Algorithms.h"
0022 
0023 namespace edmplugin {
0024   //
0025   // constants, enums and typedefs
0026   //
0027 
0028   //
0029   // static data member definitions
0030   //
0031 
0032   //
0033   // constructors and destructor
0034   //
0035   //CacheParser::CacheParser(std::istream&)
0036   //{
0037   //}
0038 
0039   // CacheParser::CacheParser(const CacheParser& rhs)
0040   // {
0041   //    // do actual copying here;
0042   // }
0043 
0044   //CacheParser::~CacheParser()
0045   //{
0046   //}
0047 
0048   //
0049   // assignment operators
0050   //
0051   // const CacheParser& CacheParser::operator=(const CacheParser& rhs)
0052   // {
0053   //   //An exception safe implementation is
0054   //   CacheParser temp(rhs);
0055   //   swap(rhs);
0056   //
0057   //   return *this;
0058   // }
0059 
0060   //
0061   // member functions
0062   //
0063 
0064   //
0065   // const member functions
0066   //
0067 
0068   //
0069   // static member functions
0070   //
0071   static void checkForError(const std::istream& iIn, unsigned long iRecordNumber, const std::string& iContext) {
0072     if (iIn.eof()) {
0073       throw cms::Exception("PluginCacheParseFailed")
0074           << "Unexpectedly reached end of file for line " << iRecordNumber << " just after '" << iContext << "'";
0075     }
0076     if (iIn.bad()) {
0077       throw cms::Exception("PluginCacheParseFailed")
0078           << "Reading failed on line " << iRecordNumber << " just after '" << iContext << "'";
0079     }
0080   }
0081 
0082   bool CacheParser::readline(std::istream& iIn,
0083                              const std::filesystem::path& iDirectory,
0084                              unsigned long iRecordNumber,
0085                              PluginInfo& oInfo,
0086                              std::string& oPluginType) {
0087     static const std::string kNewLine("start of new line");
0088     std::string fileName;
0089     std::string pluginName;
0090     iIn >> fileName;
0091     if (iIn.eof()) {
0092       return false;
0093     }
0094     checkForError(iIn, iRecordNumber, kNewLine);
0095     CacheParser::restoreSpaces(fileName);
0096     iIn >> pluginName;
0097     checkForError(iIn, iRecordNumber, fileName);
0098     CacheParser::restoreSpaces(pluginName);
0099     iIn >> oPluginType;
0100     checkForError(iIn, iRecordNumber, oPluginType);
0101     CacheParser::restoreSpaces(oPluginType);
0102 
0103     oInfo.loadable_ = iDirectory / fileName;
0104     oInfo.name_ = pluginName;
0105 
0106     //ignore everything to the end of line
0107     iIn.ignore(std::numeric_limits<int>::max(), '\n');
0108     while (iIn.peek() == '\n') {
0109       iIn.get();
0110     }
0111     return true;
0112   }
0113 
0114   namespace {
0115     struct CompPluginInfos {
0116       bool operator()(const PluginInfo& iLHS, const PluginInfo& iRHS) const { return iLHS.name_ < iRHS.name_; }
0117     };
0118   }  // namespace
0119 
0120   void CacheParser::read(std::istream& iIn,
0121                          const std::filesystem::path& iDirectory,
0122                          CacheParser::CategoryToInfos& iOut) {
0123     unsigned long recordNumber = 0;
0124 
0125     std::string pluginType;
0126 
0127     PluginInfo info;
0128 
0129     while (iIn) {
0130       ++recordNumber;
0131       if (not readline(iIn, iDirectory, recordNumber, info, pluginType)) {
0132         break;
0133       }
0134       iOut[pluginType].push_back(info);
0135     }
0136     //now do a sort which preserves any previous order for files
0137     for (CacheParser::CategoryToInfos::iterator it = iOut.begin(), itEnd = iOut.end(); it != itEnd; ++it) {
0138       std::stable_sort(it->second.begin(), it->second.end(), CompPluginInfos());
0139     }
0140   }
0141 
0142   void CacheParser::write(const CategoryToInfos& iInfos, std::ostream& oOut) {
0143     //order the data more to our liking: library then object then type
0144     LoadableToPlugins ordered;
0145 
0146     for (CategoryToInfos::const_iterator it = iInfos.begin(); it != iInfos.end(); ++it) {
0147       std::string type(it->first);
0148       for (std::vector<PluginInfo>::const_iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2) {
0149         //remove any directory specification
0150 #if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 47
0151         std::string loadable(it2->loadable_.filename().string());
0152 #else
0153         std::string loadable(it2->loadable_.filename());
0154 #endif
0155         std::string name(it2->name_);
0156         ordered[loadable].push_back(NameAndType(name, type));
0157       }
0158     }
0159     write(ordered, oOut);
0160   }
0161 
0162   void CacheParser::write(LoadableToPlugins& iIn, std::ostream& oOut) {
0163     for (LoadableToPlugins::iterator it = iIn.begin(); it != iIn.end(); ++it) {
0164       std::string loadable(it->first.string());
0165       replaceSpaces(loadable);
0166       edm::sort_all(it->second);
0167 
0168       for (std::vector<std::pair<std::string, std::string> >::iterator it2 = it->second.begin();
0169            it2 != it->second.end();
0170            ++it2) {
0171         oOut << loadable << " " << replaceSpaces(it2->first) << " " << replaceSpaces(it2->second) << "\n";
0172       }
0173     }
0174   }
0175 
0176   void CacheParser::read(std::istream& iIn, LoadableToPlugins& oOut) {
0177     unsigned long recordNumber = 0;
0178 
0179     std::string pluginType;
0180 
0181     PluginInfo info;
0182     NameAndType pat;
0183     std::filesystem::path empty;
0184 
0185     while (iIn) {
0186       ++recordNumber;
0187       if (not readline(iIn, empty, recordNumber, info, pat.second)) {
0188         break;
0189       }
0190       pat.first = info.name_;
0191       oOut[info.loadable_].push_back(pat);
0192     }
0193   }
0194 
0195   std::string& CacheParser::replaceSpaces(std::string& io) {
0196     std::string::size_type index = 0;
0197     while (std::string::npos != (index = io.find_first_of(" \t\n", index))) {
0198       io[index] = '%';
0199     }
0200     return io;
0201   }
0202 
0203   std::string& CacheParser::restoreSpaces(std::string& io) {
0204     std::string::size_type index = 0;
0205     while (std::string::npos != (index = io.find_first_of('%', index))) {
0206       io[index] = ' ';
0207     }
0208     return io;
0209   }
0210 }  // namespace edmplugin