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