CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CacheParser.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: PluginManager
4 // Class : CacheParser
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Chris Jones
10 // Created: Wed Apr 4 14:30:46 EDT 2007
11 //
12 
13 // system include files
14 #include <algorithm>
15 #include <limits>
16 #include "boost/version.hpp"
17 
18 // user include files
22 
23 namespace edmplugin {
24 //
25 // constants, enums and typedefs
26 //
27 
28 //
29 // static data member definitions
30 //
31 
32 //
33 // constructors and destructor
34 //
35 //CacheParser::CacheParser(std::istream&)
36 //{
37 //}
38 
39 // CacheParser::CacheParser(const CacheParser& rhs)
40 // {
41 // // do actual copying here;
42 // }
43 
44 //CacheParser::~CacheParser()
45 //{
46 //}
47 
48 //
49 // assignment operators
50 //
51 // const CacheParser& CacheParser::operator=(const CacheParser& rhs)
52 // {
53 // //An exception safe implementation is
54 // CacheParser temp(rhs);
55 // swap(rhs);
56 //
57 // return *this;
58 // }
59 
60 //
61 // member functions
62 //
63 
64 //
65 // const member functions
66 //
67 
68 //
69 // static member functions
70 //
71  static void checkForError(const std::istream& iIn,
72  unsigned long iRecordNumber,
73  const std::string& iContext)
74 {
75  if(iIn.eof()) {
76  throw cms::Exception("PluginCacheParseFailed")<<"Unexpectedly reached end of file for line "
77  <<iRecordNumber<<" just after '"<<iContext<<"'";
78  }
79  if(iIn.bad()) {
80  throw cms::Exception("PluginCacheParseFailed")<<"Reading failed on line "<<iRecordNumber <<" just after '"<<iContext<<"'";
81  }
82 }
83 
84 bool
85 CacheParser::readline(std::istream& iIn, const boost::filesystem::path& iDirectory,
86  unsigned long iRecordNumber, PluginInfo &oInfo, std::string& oPluginType)
87 {
88  static const std::string kNewLine("start of new line");
90  std::string pluginName;
91  iIn >> fileName;
92  if(iIn.eof()) { return false;}
93  checkForError(iIn,iRecordNumber,kNewLine);
95  iIn >> pluginName;
96  checkForError(iIn,iRecordNumber,fileName);
97  CacheParser::restoreSpaces(pluginName);
98  iIn >> oPluginType;
99  checkForError(iIn,iRecordNumber,oPluginType);
100  CacheParser::restoreSpaces(oPluginType);
101 
102  oInfo.loadable_ = iDirectory / fileName;
103  oInfo.name_ = pluginName;
104 
105  //ignore everything to the end of line
106  iIn.ignore(std::numeric_limits<int>::max(),
107  '\n');
108  while(iIn.peek() == '\n') {
109  iIn.get();
110  }
111  return true;
112 }
113 
114 namespace {
115  struct CompPluginInfos {
116  bool operator()(const PluginInfo& iLHS,
117  const PluginInfo& iRHS) const
118  {
119  return iLHS.name_ < iRHS.name_;
120  }
121  };
122 }
123 
124 void
125 CacheParser::read(std::istream& iIn,
126  const boost::filesystem::path& iDirectory,
128 {
129  unsigned long recordNumber=0;
130 
131  std::string pluginType;
132 
134 
135  while(iIn) {
136  ++recordNumber;
137  if( not readline(iIn,iDirectory,recordNumber,info,pluginType) ) {
138  break;
139  }
140  iOut[pluginType].push_back(info);
141  }
142  //now do a sort which preserves any previous order for files
143  for(CacheParser::CategoryToInfos::iterator it = iOut.begin(), itEnd=iOut.end();
144  it != itEnd;
145  ++it) {
146  std::stable_sort(it->second.begin(),it->second.end(), CompPluginInfos());
147  }
148 }
149 
150 void
151 CacheParser::write(const CategoryToInfos& iInfos, std::ostream& oOut)
152 {
153  //order the data more to our liking: library then object then type
154  LoadableToPlugins ordered;
155 
156  for(CategoryToInfos::const_iterator it = iInfos.begin();
157  it != iInfos.end();
158  ++it) {
159  std::string type(it->first);
160  for(std::vector<PluginInfo>::const_iterator it2=it->second.begin();
161  it2 != it->second.end();
162  ++it2) {
163  //remove any directory specification
164 #if (BOOST_VERSION / 100000) >= 1 && ((BOOST_VERSION / 100) % 1000) >= 47
165  std::string loadable(it2->loadable_.filename().string());
166 #else
167  std::string loadable(it2->loadable_.filename());
168 #endif
169  std::string name(it2->name_);
170  ordered[loadable].push_back(NameAndType(name,type));
171  }
172  }
173  write(ordered,oOut);
174 }
175 
176 void
177 CacheParser::write(LoadableToPlugins& iIn, std::ostream& oOut)
178 {
179  for( LoadableToPlugins::iterator it = iIn.begin();
180  it!=iIn.end();
181  ++it) {
182  std::string loadable(it->first.string());
183  replaceSpaces(loadable);
184  edm::sort_all(it->second);
185 
186  for(std::vector<std::pair<std::string,std::string> >::iterator it2 = it->second.begin();
187  it2 != it->second.end();
188  ++it2) {
189  oOut << loadable <<" "<<replaceSpaces(it2->first)<<" "<<replaceSpaces(it2->second)<<"\n";
190  }
191  }
192 }
193 
194 void
195 CacheParser::read(std::istream& iIn, LoadableToPlugins& oOut)
196 {
197  unsigned long recordNumber=0;
198 
199  std::string pluginType;
200 
202  NameAndType pat;
204 
205  while(iIn) {
206  ++recordNumber;
207  if( not readline(iIn,empty,recordNumber,info,pat.second) ) {
208  break;
209  }
210  pat.first = info.name_;
211  oOut[info.loadable_].push_back(pat);
212  }
213 }
214 
215 std::string&
217 {
219  while(std::string::npos != (index = io.find_first_of(" \t\n",index))) {
220  io[index]='%';
221  }
222  return io;
223 }
224 
226 {
228  while(std::string::npos != (index = io.find_first_of("%",index))) {
229  io[index]=' ';
230  }
231  return io;
232 }
233 }
type
Definition: HCALResponse.h:21
static std::string & restoreSpaces(std::string &io)
Definition: CacheParser.cc:225
static const TGPicture * info(bool iBackgroundIsBlack)
std::pair< std::string, std::string > NameAndType
Definition: CacheParser.h:45
std::map< std::string, std::vector< PluginInfo > > CategoryToInfos
Definition: CacheParser.h:44
uint16_t size_type
static bool readline(std::istream &iIn, const boost::filesystem::path &iDirectory, unsigned long iRecordNumber, PluginInfo &oInfo, std::string &oPluginType)
Definition: CacheParser.cc:85
std::map< boost::filesystem::path, NameAndTypes > LoadableToPlugins
Definition: CacheParser.h:47
static void checkForError(const std::istream &iIn, unsigned long iRecordNumber, const std::string &iContext)
Definition: CacheParser.cc:71
static void write(const CategoryToInfos &, std::ostream &)
Definition: CacheParser.cc:151
void sort_all(RandomAccessSequence &s)
wrappers for std::sort
Definition: Algorithms.h:120
static std::string & replaceSpaces(std::string &io)
Definition: CacheParser.cc:216
std::string name_
Definition: PluginInfo.h:29
boost::filesystem::path loadable_
Definition: PluginInfo.h:30
static void read(std::istream &, const boost::filesystem::path &iDirectory, CategoryToInfos &oOut)
Definition: CacheParser.cc:125