CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FWConfigurationManager.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Core
4 // Class : FWConfigurationManager
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Chris Jones
10 // Created: Sun Feb 24 14:42:32 EST 2008
11 // $Id: FWConfigurationManager.cc,v 1.16 2011/02/22 18:37:31 amraktad Exp $
12 //
13 
14 // system include files
15 #include <fstream>
16 #include <iostream>
17 #include <memory>
18 #include <stdexcept>
19 #include "TROOT.h"
20 
21 // user include files
27 
28 //
29 // constants, enums and typedefs
30 //
31 
32 //
33 // static data member definitions
34 //
35 
36 //
37 // constructors and destructor
38 //
40 {
41 }
42 
43 // FWConfigurationManager::FWConfigurationManager(const FWConfigurationManager& rhs)
44 // {
45 // // do actual copying here;
46 // }
47 
49 {
50 }
51 
52 //
53 // assignment operators
54 //
55 // const FWConfigurationManager& FWConfigurationManager::operator=(const FWConfigurationManager& rhs)
56 // {
57 // //An exception safe implementation is
58 // FWConfigurationManager temp(rhs);
59 // swap(rhs);
60 //
61 // return *this;
62 // }
63 
64 //
65 // member functions
66 //
67 void
68 FWConfigurationManager::add(const std::string& iName, FWConfigurable* iConf)
69 {
70  assert(0!=iConf);
71  m_configurables[iName]=iConf;
72 }
73 
74 //
75 // const member functions
76 //
77 void
79 {
80  assert(0!=iConfig.keyValues());
81  for(FWConfiguration::KeyValues::const_iterator it = iConfig.keyValues()->begin(),
82  itEnd = iConfig.keyValues()->end();
83  it != itEnd;
84  ++it) {
85  std::map<std::string,FWConfigurable*>::const_iterator itFound = m_configurables.find(it->first);
86  assert(itFound != m_configurables.end());
87  itFound->second->setFrom(it->second);
88  }
89 }
90 
91 void
93 {
95  for(std::map<std::string,FWConfigurable*>::const_iterator it = m_configurables.begin(),
96  itEnd = m_configurables.end();
97  it != itEnd;
98  ++it) {
99  it->second->addTo(config);
100  oConfig.addKeyValue(it->first, config, true);
101  }
102 }
103 
104 
105 void
106 FWConfigurationManager::writeToFile(const std::string& iName) const
107 {
108  try
109  {
110  ofstream file(iName.c_str());
111  if(not file) {
112  std::string message("unable to open file %s ", iName.c_str());
113  fflush(stdout);
114  message += iName;
115  throw std::runtime_error(message.c_str());
116  }
117  FWConfiguration top;
118  to(top);
119  fwLog(fwlog::kInfo) << "Writing to file "<< iName.c_str() << "...\n";
120  fflush(stdout);
121 
122  streamTo(file, top, "top");
123  }
124  catch (std::runtime_error &e) { std::cout << e.what() << std::endl; }
125 }
126 
127 void
128 FWConfigurationManager::readFromOldFile(const std::string& iName) const
129 {
130  Int_t error=0;
131  // Int_t value =
132  gROOT->LoadMacro( iName.c_str(), &error );
133  if(0 != error) {
134  std::string message("unable to load macro file ");
135  message += iName;
136  throw std::runtime_error(message.c_str());
137  }
138 
139  const std::string command("(Long_t)(fwConfig() )");
140 
141  error = 0;
142  Long_t lConfig = gROOT->ProcessLineFast(command.c_str(),
143  &error);
144 
145  {
146  //need to unload this macro so that we can load a new configuration
147  // which uses the same function name in the macro
148  Int_t error = 0;
149  gROOT->ProcessLineSync((std::string(".U ")+iName).c_str(), &error);
150  }
151  if(0 != error) {
152  std::string message("unable to properly parse configuration file ");
153  message += iName;
154  throw std::runtime_error(message.c_str());
155  }
156  std::auto_ptr<FWConfiguration> config( reinterpret_cast<FWConfiguration*>(lConfig) );
157 
158  setFrom( *config);
159 }
160 
161 void
162 debug_config_state_machine(const char *where, const std::string &tag, int state)
163 {
164 #ifdef FW_CONFIG_PARSER_DEBUG
165  static char *debug_states[] = {
166  "IN_BEGIN_DOCUMENT",
167  "IN_PUSHED_CONFIG",
168  "IN_POPPED_CONFIG",
169  "IN_BEGIN_STRING",
170  "IN_STORED_STRING"
171  };
172 
173  std::cerr << " " << where << " tag/data " << tag << "in state " << debug_states[state] << std::endl;
174 #endif
175 }
176 
201 {
202  enum STATES {
208  };
209 
210 public:
211  FWXMLConfigParser(istream &f)
212  : SimpleSAXParser(f),
214  m_first(0)
215  {}
216 
219  {
220  std::string name;
221  int version = 0;
222  for (size_t i = 0, e = attributes.size(); i != e; ++i)
223  {
224  Attribute &attr = attributes[i];
225  if (attr.key == "name")
226  name = attr.value;
227  else if (attr.key == "version")
228  {
229  char *endptr;
230  version = strtol(attr.value.c_str(), &endptr, 10);
231  if (endptr == attr.value.c_str())
232  throw ParserError("Version must be an integer.");
233  }
234  else
235  throw ParserError("Unexpected attribute " + attr.key);
236  }
237  m_configs.push_back(std::make_pair(name, new FWConfiguration(version)));
238  }
239 
240 
244  virtual void startElement(const std::string &tag, Attributes &attributes)
245  {
246  debug_config_state_machine("start", tag, m_state);
247  if (m_state == IN_BEGIN_DOCUMENT)
248  {
249  if (tag != "config")
250  throw ParserError("Expecting toplevel <config> tag");
251  pushConfig(attributes);
252  m_first.reset(m_configs.back().second);
254  }
255  else if (m_state == IN_PUSHED_CONFIG)
256  {
257  if (tag == "config")
258  pushConfig(attributes);
259  else if (tag == "string")
261  else
262  throw ParserError("Unexpected element " + tag);
263  }
264  else if (m_state == IN_POPPED_CONFIG)
265  {
266  if (tag != "config")
267  throw ParserError("Unexpected element " + tag);
268  pushConfig(attributes);
270  }
271  else
272  throw ParserError("Wrong opening tag found " + tag);
273  }
274 
283  virtual void endElement(const std::string &tag)
284  {
285  debug_config_state_machine("end", tag, m_state);
287  {
288  if (tag != "config")
289  throw ParserError("Wrong closing tag found " + tag);
290 
291  FWConfiguration *current = m_configs.back().second;
292  std::string key = m_configs.back().first;
293  m_configs.pop_back();
294  if (!m_configs.empty())
295  m_configs.back().second->addKeyValue(key, *current);
297  }
298  else if (m_state == IN_BEGIN_STRING && tag == "string")
299  {
300  m_configs.back().second->addValue("");
302  }
303  else if (m_state == IN_STORED_STRING && tag == "string")
305  else
306  throw ParserError("Wrong closing tag found " + tag);
307  }
308 
314  virtual void data(const std::string &data)
315  {
316  debug_config_state_machine("data", data, m_state);
317  // We ignore whitespace but complain about any text which is not
318  // in the <string> tag.
319  if (m_state == IN_BEGIN_STRING)
320  {
321  m_configs.back().second->addValue(data);
323  }
324  else if (strspn(data.c_str(), " \t\n") != data.size())
325  throw ParserError("Unexpected text " + data);
326  }
327 
332  {
333  return m_first.get();
334  }
335 
336 private:
337  std::vector<std::pair<std::string, FWConfiguration *> > m_configs;
339  std::auto_ptr<FWConfiguration> m_first;
340  // unsigned int m_currentConfigVersion;
341  std::string m_currentConfigName;
342 };
343 
350 void
351 FWConfigurationManager::readFromFile(const std::string& iName) const
352 {
353  std::ifstream f(iName.c_str());
354  if (f.peek() != (int) '<')
355  return readFromOldFile(iName);
356 
357  // Check that the syntax is correct.
358  SimpleSAXParser syntaxTest(f);
359  syntaxTest.parse();
360  f.close();
361 
362  // Read again, this time actually parse.
363  std::ifstream g(iName.c_str());
364  // Actually parse the results.
366  parser.parse();
367  setFrom(*parser.config());
368 }
369 
370 //
371 // static member functions
372 //
int i
Definition: DBlmapReader.cc:9
std::vector< Attribute > Attributes
void to(FWConfiguration &) const
std::auto_ptr< FWConfiguration > m_first
const KeyValues * keyValues() const
std::vector< std::pair< std::string, FWConfiguration * > > m_configs
void streamTo(std::ostream &, const FWConfiguration &, const std::string &name)
void writeToFile(const std::string &) const
void debug_config_state_machine(const char *where, const std::string &tag, int state)
void readFromOldFile(const std::string &) const
void readFromFile(const std::string &) const
FWConfiguration * config(void)
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
Definition: Activities.doc:4
virtual void startElement(const std::string &tag, Attributes &attributes)
void pushConfig(Attributes &attributes)
void setFrom(const FWConfiguration &) const
list attributes
Definition: asciidump.py:415
double f[11][100]
FWConfiguration & addKeyValue(const std::string &, const FWConfiguration &)
virtual void data(const std::string &data)
tuple attr
Definition: asciidump.py:432
#define fwLog(_level_)
Definition: fwLog.h:51
char state
Definition: procUtils.cc:75
void add(const std::string &iName, FWConfigurable *)
does not take ownership
virtual void endElement(const std::string &tag)
list key
Definition: combine.py:13
tuple cout
Definition: gather_cfg.py:121
std::map< std::string, FWConfigurable * > m_configurables