123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- // Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC")
- //
- // This Source Code Form is subject to the terms of the Mozilla Public
- // License, v. 2.0. If a copy of the MPL was not distributed with this
- // file, You can obtain one at http://mozilla.org/MPL/2.0/.
- #include <cc/simple_parser.h>
- #include <boost/foreach.hpp>
- #include <boost/lexical_cast.hpp>
- #include <cc/data.h>
- #include <string>
- using namespace std;
- namespace isc {
- namespace data {
- std::string
- SimpleParser::getString(isc::data::ConstElementPtr scope, const std::string& name) {
- ConstElementPtr x = scope->get(name);
- if (!x) {
- isc_throw(BadValue, "String parameter " << name << " not found"
- << "(" << scope->getPosition() << ")");
- }
- if (x->getType() != Element::string) {
- isc_throw(BadValue, "Element " << name << " found, but is not a string"
- << "(" << x->getPosition() << ")");
- }
- return (x->stringValue());
- }
- int64_t
- SimpleParser::getInteger(isc::data::ConstElementPtr scope, const std::string& name) {
- ConstElementPtr x = scope->get(name);
- if (!x) {
- isc_throw(BadValue, "Integer parameter " << name << " not found "
- << "(" << scope->getPosition() << ")");
- }
- if (x->getType() != Element::integer) {
- isc_throw(BadValue, "Element " << name << " found, but is not an integer"
- << "(" << x->getPosition() << ")");
- }
- return (x->intValue());
- }
- bool
- SimpleParser::getBoolean(isc::data::ConstElementPtr scope, const std::string& name) {
- ConstElementPtr x = scope->get(name);
- if (!x) {
- isc_throw(BadValue, "Boolean element " << name << " not found "
- << "(" << scope->getPosition() << ")");
- }
- if (x->getType() != Element::boolean) {
- isc_throw(BadValue, "Element " << name << " found, but is not a boolean"
- << "(" << x->getPosition() << ")");
- }
- return (x->boolValue());
- }
- const data::Element::Position&
- SimpleParser::getPosition(const std::string& name, const data::ConstElementPtr parent) {
- if (!parent) {
- return (data::Element::ZERO_POSITION());
- }
- ConstElementPtr elem = parent->get(name);
- if (!elem) {
- return (data::Element::ZERO_POSITION());
- }
- return (elem->getPosition());
- }
- size_t SimpleParser::setDefaults(isc::data::ElementPtr scope,
- const SimpleDefaults& default_values) {
- size_t cnt = 0;
- // This is the position representing a default value. As the values
- // we're inserting here are not present in whatever the config file
- // came from, we need to make sure it's clearly labeled as default.
- const Element::Position pos("<default-value>", 0, 0);
- // Let's go over all parameters we have defaults for.
- BOOST_FOREACH(SimpleDefault def_value, default_values) {
- // Try if such a parameter is there. If it is, let's
- // skip it, because user knows best *cough*.
- ConstElementPtr x = scope->get(string(def_value.name_));
- if (x) {
- // There is such a value already, skip it.
- continue;
- }
- // There isn't such a value defined, let's create the default
- // value...
- switch (def_value.type_) {
- case Element::string: {
- x.reset(new StringElement(def_value.value_, pos));
- break;
- }
- case Element::integer: {
- int int_value = boost::lexical_cast<int>(def_value.value_);
- x.reset(new IntElement(int_value, pos));
- break;
- }
- case Element::boolean: {
- bool bool_value;
- if (def_value.value_ == string("true")) {
- bool_value = true;
- } else if (def_value.value_ == string("false")) {
- bool_value = false;
- } else {
- isc_throw(BadValue, "Internal error. Boolean value specified as "
- << def_value.value_ << ", expected true or false");
- }
- x.reset(new BoolElement(bool_value, pos));
- break;
- }
- case Element::real: {
- double dbl_value = boost::lexical_cast<double>(def_value.value_);
- x.reset(new DoubleElement(dbl_value, pos));
- break;
- }
- default:
- // No default values for null, list or map
- isc_throw(BadValue, "Internal error. Incorrect default value type.");
- }
- // ... and insert it into the provided Element tree.
- scope->set(def_value.name_, x);
- ++cnt;
- }
- return (cnt);
- }
- size_t
- SimpleParser::setListDefaults(isc::data::ConstElementPtr list,
- const SimpleDefaults& default_values) {
- size_t cnt = 0;
- BOOST_FOREACH(ElementPtr entry, list->listValue()) {
- cnt += setDefaults(entry, default_values);
- }
- return (cnt);
- }
- size_t
- SimpleParser::deriveParams(isc::data::ConstElementPtr parent,
- isc::data::ElementPtr child,
- const ParamsList& params) {
- if ( (parent->getType() != Element::map) ||
- (child->getType() != Element::map)) {
- return (0);
- }
- size_t cnt = 0;
- BOOST_FOREACH(string param, params) {
- ConstElementPtr x = parent->get(param);
- if (!x) {
- // Parent doesn't define this parameter, so there's
- // nothing to derive from
- continue;
- }
- if (child->get(param)) {
- // Child defines this parameter already. There's
- // nothing to do here.
- continue;
- }
- // Copy the parameters to the child scope.
- child->set(param, x);
- cnt++;
- }
- return (cnt);
- }
- }; // end of isc::dhcp namespace
- }; // end of isc namespace
|