From 476301116a3ce5818c157ff7efda83ce79b16b81 Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Mon, 7 Oct 2019 19:10:34 -0500 Subject: hscript: Generalise BooleanKey failure mode for all keys --- hscript/key.cc | 7 ++++++- hscript/key.hh | 5 ++++- hscript/network.cc | 6 ++---- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/hscript/key.cc b/hscript/key.cc index 74406f6..a677670 100644 --- a/hscript/key.cc +++ b/hscript/key.cc @@ -12,8 +12,11 @@ #include #include "key.hh" +#include "util/output.hh" -bool Horizon::Keys::BooleanKey::parse(const std::string what, bool *out) { +bool Horizon::Keys::BooleanKey::parse(const std::string what, + const std::string where, + const std::string key, bool *out) { std::string lower; std::transform(what.begin(), what.end(), lower.begin(), ::tolower); @@ -22,6 +25,8 @@ bool Horizon::Keys::BooleanKey::parse(const std::string what, bool *out) { } else if(lower == "false" || lower == "no" || lower == "0") { *out = false; } else { + output_error(where, key + ": expected 'true' or 'false'", + "'" + what + "' is not a valid Boolean value"); return false; } return true; diff --git a/hscript/key.hh b/hscript/key.hh index 4817ea5..9c7755b 100644 --- a/hscript/key.hh +++ b/hscript/key.hh @@ -63,10 +63,13 @@ protected: /*! Parse a string into a boolean. * @param what The string to attempt parsing. + * @param where The location of the key. + * @param key The name of the key. * @param out Output variable: will contain the value. * @returns true if value is parsed successfully, false otherwise. */ - static bool parse(const std::string what, bool *out); + static bool parse(const std::string what, const std::string where, + const std::string key, bool *out); public: /*! Determines if the Key is set or not. * @returns true if the Key is truthy, false otherwise. diff --git a/hscript/network.cc b/hscript/network.cc index 8f79d96..9456d09 100644 --- a/hscript/network.cc +++ b/hscript/network.cc @@ -17,10 +17,8 @@ using namespace Horizon::Keys; Key *Network::parseFromData(const std::string data, int lineno, int *errors, int *warnings) { bool value; - if(!BooleanKey::parse(data, &value)) { - output_error("installfile:" + std::to_string(lineno), - "network: expected 'true' or 'false'", - "'" + data + "' is not a valid Boolean value"); + if(!BooleanKey::parse(data, "installfile:" + std::to_string(lineno), + "network", &value)) { if(errors) *errors += 1; return nullptr; } -- cgit v1.2.3-60-g2f50