Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
#include <ostream>
#include <cstdlib>
#include <vector>
#include <string>
#include <map>

#include "FWCore/Utilities/interface/EDMException.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"

#include "GeneratorInterface/Core/interface/ParameterCollector.h"

using namespace gen;

ParameterCollector::ParameterCollector() {}

ParameterCollector::ParameterCollector(const edm::ParameterSet &pset) {
  std::vector<std::string> names = pset.getParameterNamesForType<std::vector<std::string> >();

  for (std::vector<std::string>::const_iterator it = names.begin(); it != names.end(); ++it)
    contents_[*it] = pset.getParameter<std::vector<std::string> >(*it);
}

ParameterCollector::~ParameterCollector() {}

inline ParameterCollector::const_iterator::const_iterator(const ParameterCollector *collector,
                                                          std::vector<std::string>::const_iterator begin,
                                                          std::vector<std::string>::const_iterator end,
                                                          bool special,
                                                          std::ostream *dump)
    : collector_(collector), dump_(dump), special_(special) {
  if (begin != end)
    iter_.push_back(IterPair(begin, end));

  next();
}

void ParameterCollector::const_iterator::increment() {
  if (++iter_.back().first == iter_.back().second)
    iter_.pop_back();

  next();
}

void ParameterCollector::const_iterator::next() {
  if (iter_.empty()) {
    cache_.clear();
    return;
  }

  for (;;) {
    const std::string &line = *iter_.back().first;

    bool special = special_ && iter_.size() == 1;
    if ((!line.empty() && line[0] == '+') || special) {
      if (++iter_.back().first == iter_.back().second) {
        iter_.pop_back();
        if (iter_.empty())
          special_ = false;
      }

      std::string block = special ? line : line.substr(1);

      std::map<std::string, std::vector<std::string> >::const_iterator pos = collector_->contents_.find(block);
      if (pos == collector_->contents_.end())
        throw edm::Exception(edm::errors::Configuration) << "ParameterCollector could not find configuration lines "
                                                            "block \""
                                                         << block << "\", included via plus sign.";

      if (dump_)
        *dump_ << "\n####### " << block << " #######" << std::endl;

      if (!pos->second.empty())
        iter_.push_back(IterPair(pos->second.begin(), pos->second.end()));
    } else {
      cache_ = collector_->resolve(line);
      if (dump_)
        *dump_ << cache_ << std::endl;
      break;
    }
  }
}

ParameterCollector::const_iterator ParameterCollector::begin() const {
  std::map<std::string, std::vector<std::string> >::const_iterator pos = contents_.find("parameterSets");
  if (pos == contents_.end())
    throw edm::Exception(edm::errors::Configuration) << "ParameterCollector could not find \"parameterSets\" block.";

  return const_iterator(this, pos->second.begin(), pos->second.end(), true);
}

ParameterCollector::const_iterator ParameterCollector::begin(std::ostream &dump) const {
  std::map<std::string, std::vector<std::string> >::const_iterator pos = contents_.find("parameterSets");
  if (pos == contents_.end())
    throw edm::Exception(edm::errors::Configuration) << "ParameterCollector could not find \"parameterSets\" block.";

  return const_iterator(this, pos->second.begin(), pos->second.end(), true, &dump);
}

ParameterCollector::const_iterator ParameterCollector::begin(const std::string &block) const {
  std::map<std::string, std::vector<std::string> >::const_iterator pos = contents_.find(block);
  if (pos == contents_.end())
    throw edm::Exception(edm::errors::Configuration) << "ParameterCollector could not find \"" << block << "\" block.";

  return const_iterator(this, pos->second.begin(), pos->second.end());
}

ParameterCollector::const_iterator ParameterCollector::begin(const std::string &block, std::ostream &dump) const {
  std::map<std::string, std::vector<std::string> >::const_iterator pos = contents_.find(block);
  if (pos == contents_.end())
    throw edm::Exception(edm::errors::Configuration) << "ParameterCollector could not find \"" << block << "\" block.";

  dump << "\n####### " << block << " #######" << std::endl;

  return const_iterator(this, pos->second.begin(), pos->second.end(), false, &dump);
}

std::string ParameterCollector::resolve(const std::string &line) {
  std::string result(line);

  for (;;) {
    std::string::size_type pos = result.find("${");
    if (pos == std::string::npos)
      break;

    std::string::size_type endpos = result.find('}', pos);
    if (endpos == std::string::npos)
      break;
    else
      ++endpos;

    std::string var = result.substr(pos + 2, endpos - pos - 3);
    const char *path = std::getenv(var.c_str());

    result.replace(pos, endpos - pos, path ? path : "");
  }

  return result;
}