File indexing completed on 2024-04-06 12:13:26
0001 #include <ostream>
0002 #include <cstdlib>
0003 #include <vector>
0004 #include <string>
0005 #include <map>
0006
0007 #include "FWCore/Utilities/interface/EDMException.h"
0008 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0009
0010 #include "GeneratorInterface/Core/interface/ParameterCollector.h"
0011
0012 using namespace gen;
0013
0014 ParameterCollector::ParameterCollector() {}
0015
0016 ParameterCollector::ParameterCollector(const edm::ParameterSet &pset) {
0017 std::vector<std::string> names = pset.getParameterNamesForType<std::vector<std::string> >();
0018
0019 for (std::vector<std::string>::const_iterator it = names.begin(); it != names.end(); ++it)
0020 contents_[*it] = pset.getParameter<std::vector<std::string> >(*it);
0021 }
0022
0023 ParameterCollector::~ParameterCollector() {}
0024
0025 inline ParameterCollector::const_iterator::const_iterator(const ParameterCollector *collector,
0026 std::vector<std::string>::const_iterator begin,
0027 std::vector<std::string>::const_iterator end,
0028 bool special,
0029 std::ostream *dump)
0030 : collector_(collector), dump_(dump), special_(special) {
0031 if (begin != end)
0032 iter_.push_back(IterPair(begin, end));
0033
0034 next();
0035 }
0036
0037 void ParameterCollector::const_iterator::increment() {
0038 if (++iter_.back().first == iter_.back().second)
0039 iter_.pop_back();
0040
0041 next();
0042 }
0043
0044 void ParameterCollector::const_iterator::next() {
0045 if (iter_.empty()) {
0046 cache_.clear();
0047 return;
0048 }
0049
0050 for (;;) {
0051 const std::string &line = *iter_.back().first;
0052
0053 bool special = special_ && iter_.size() == 1;
0054 if ((!line.empty() && line[0] == '+') || special) {
0055 if (++iter_.back().first == iter_.back().second) {
0056 iter_.pop_back();
0057 if (iter_.empty())
0058 special_ = false;
0059 }
0060
0061 std::string block = special ? line : line.substr(1);
0062
0063 std::map<std::string, std::vector<std::string> >::const_iterator pos = collector_->contents_.find(block);
0064 if (pos == collector_->contents_.end())
0065 throw edm::Exception(edm::errors::Configuration) << "ParameterCollector could not find configuration lines "
0066 "block \""
0067 << block << "\", included via plus sign.";
0068
0069 if (dump_)
0070 *dump_ << "\n####### " << block << " #######" << std::endl;
0071
0072 if (!pos->second.empty())
0073 iter_.push_back(IterPair(pos->second.begin(), pos->second.end()));
0074 } else {
0075 cache_ = collector_->resolve(line);
0076 if (dump_)
0077 *dump_ << cache_ << std::endl;
0078 break;
0079 }
0080 }
0081 }
0082
0083 ParameterCollector::const_iterator ParameterCollector::begin() const {
0084 std::map<std::string, std::vector<std::string> >::const_iterator pos = contents_.find("parameterSets");
0085 if (pos == contents_.end())
0086 throw edm::Exception(edm::errors::Configuration) << "ParameterCollector could not find \"parameterSets\" block.";
0087
0088 return const_iterator(this, pos->second.begin(), pos->second.end(), true);
0089 }
0090
0091 ParameterCollector::const_iterator ParameterCollector::begin(std::ostream &dump) const {
0092 std::map<std::string, std::vector<std::string> >::const_iterator pos = contents_.find("parameterSets");
0093 if (pos == contents_.end())
0094 throw edm::Exception(edm::errors::Configuration) << "ParameterCollector could not find \"parameterSets\" block.";
0095
0096 return const_iterator(this, pos->second.begin(), pos->second.end(), true, &dump);
0097 }
0098
0099 ParameterCollector::const_iterator ParameterCollector::begin(const std::string &block) const {
0100 std::map<std::string, std::vector<std::string> >::const_iterator pos = contents_.find(block);
0101 if (pos == contents_.end())
0102 throw edm::Exception(edm::errors::Configuration) << "ParameterCollector could not find \"" << block << "\" block.";
0103
0104 return const_iterator(this, pos->second.begin(), pos->second.end());
0105 }
0106
0107 ParameterCollector::const_iterator ParameterCollector::begin(const std::string &block, std::ostream &dump) const {
0108 std::map<std::string, std::vector<std::string> >::const_iterator pos = contents_.find(block);
0109 if (pos == contents_.end())
0110 throw edm::Exception(edm::errors::Configuration) << "ParameterCollector could not find \"" << block << "\" block.";
0111
0112 dump << "\n####### " << block << " #######" << std::endl;
0113
0114 return const_iterator(this, pos->second.begin(), pos->second.end(), false, &dump);
0115 }
0116
0117 std::string ParameterCollector::resolve(const std::string &line) {
0118 std::string result(line);
0119
0120 for (;;) {
0121 std::string::size_type pos = result.find("${");
0122 if (pos == std::string::npos)
0123 break;
0124
0125 std::string::size_type endpos = result.find('}', pos);
0126 if (endpos == std::string::npos)
0127 break;
0128 else
0129 ++endpos;
0130
0131 std::string var = result.substr(pos + 2, endpos - pos - 3);
0132 const char *path = std::getenv(var.c_str());
0133
0134 result.replace(pos, endpos - pos, path ? path : "");
0135 }
0136
0137 return result;
0138 }