示例#1
0
 static ConfigItem parseConfigItemFromYamlNode(const YAML::Node &n) {
   ConfigItem item;
   if(n.Type() == YAML::NodeType::Scalar) {
     std::string s;
     n.GetScalar(s);
     item.setUnparsedString(s);
   }
   return item;
 }
示例#2
0
文件: yaml.cpp 项目: jmlich/fawkes
void
YamlConfiguration::read_config_doc(const YAML::Node &doc, YamlConfigurationNode *&node)
{
  if (! node) {
    node = new YamlConfigurationNode("root");
  }

  if (doc.Type() == YAML::NodeType::Map) {
#ifdef HAVE_YAMLCPP_0_5
    for (YAML::const_iterator it = doc.begin(); it != doc.end(); ++it) {
      std::string key = it->first.as<std::string>();
#else
    for (YAML::Iterator it = doc.begin(); it != doc.end(); ++it) {
      std::string key;
      it.first() >> key;
#endif
      YamlConfigurationNode *in = node;
      if (key.find("/") != std::string::npos) {
	// we need to split and find the proper insertion node
	std::vector<std::string> pel = str_split(key);
	for (size_t i = 0; i < pel.size() - 1; ++i) {
	  YamlConfigurationNode *n = (*in)[pel[i]];
	  if (! n) {
	    n = new YamlConfigurationNode(pel[i]);
	    in->add_child(pel[i], n);
	  }
	  in = n;
	}

	key = pel.back();
      }

      YamlConfigurationNode *tmp = (*in)[key];
      if (tmp) {
#ifdef HAVE_YAMLCPP_0_5
	if (tmp->is_scalar() && it->second.Type() != YAML::NodeType::Scalar)
#else
	if (tmp->is_scalar() && it.second().Type() != YAML::NodeType::Scalar)
#endif
	{
	  throw Exception("YamlConfig: scalar %s cannot be overwritten by non-scalar",
			  tmp->name().c_str());
	}
#ifdef HAVE_YAMLCPP_0_5
	tmp->set_scalar(it->second.Scalar());
#else
	std::string s;
	if (it.second().GetScalar(s)) {
	  tmp->set_scalar(s);
	}
#endif
      } else {
#ifdef HAVE_YAMLCPP_0_5
	YamlConfigurationNode *tmp = new YamlConfigurationNode(key, it->second);
	in->add_child(key, tmp);
	read_config_doc(it->second, tmp);
#else
	YamlConfigurationNode *tmp = new YamlConfigurationNode(key, it.second());
	in->add_child(key, tmp);
	read_config_doc(it.second(), tmp);
#endif
      }
    }

  } else if (doc.Type() == YAML::NodeType::Scalar) {
    if (doc.Tag() == "tag:fawkesrobotics.org,cfg/tcp-port" ||
	doc.Tag() == "tag:fawkesrobotics.org,cfg/udp-port")
    {
      unsigned int p = 0;
      try {
	p = node->get_uint();
      } catch (Exception &e) {
	e.prepend("YamlConfig: Invalid TCP/UDP port number (not an unsigned int)");
	throw;
      }
      if (p <= 0 || p >= 65535) {
	throw Exception("YamlConfig: Invalid TCP/UDP port number "
			"(%u out of allowed range)", p);
      }
    } else if (doc.Tag() == "tag:fawkesrobotics.org,cfg/url") {
#ifdef HAVE_YAMLCPP_0_5
      std::string scalar = doc.Scalar();
#else
      std::string scalar;
      doc.GetScalar(scalar);
#endif
#ifdef USE_REGEX_CPP
      if (regex_search(scalar, __url_regex)) {
#  if 0
	// just for emacs auto-indentation
      }
#  endif
#else
      if (regexec(&__url_regex, scalar.c_str(), 0, NULL, 0) == REG_NOMATCH) {
	throw Exception("YamlConfig: %s is not a valid URL", scalar.c_str());
      }
#endif
    } else if (doc.Tag() == "tag:fawkesrobotics.org,cfg/frame") {
#ifdef HAVE_YAMLCPP_0_5
      std::string scalar = doc.Scalar();
#else
      std::string scalar;
      doc.GetScalar(scalar);
#endif
#ifdef USE_REGEX_CPP
      if (regex_search(scalar, __frame_regex)) {
#  if 0
	// just for emacs auto-indentation
      }
#  endif
#else
      if (regexec(&__frame_regex, scalar.c_str(), 0, NULL, 0) == REG_NOMATCH) {
	throw Exception("YamlConfig: %s is not a valid frame ID", scalar.c_str());
      }
#endif
    }

  }
}