diff options
-rw-r--r-- | hscript/CMakeLists.txt | 1 | ||||
-rw-r--r-- | hscript/key.cc | 33 | ||||
-rw-r--r-- | hscript/key.hh | 40 |
3 files changed, 70 insertions, 4 deletions
diff --git a/hscript/CMakeLists.txt b/hscript/CMakeLists.txt index da5ce1b..dcdb8ad 100644 --- a/hscript/CMakeLists.txt +++ b/hscript/CMakeLists.txt @@ -1,5 +1,6 @@ set(HSCRIPT_SOURCE script.cc + key.cc ) set(HSCRIPT_INCLUDE diff --git a/hscript/key.cc b/hscript/key.cc new file mode 100644 index 0000000..74406f6 --- /dev/null +++ b/hscript/key.cc @@ -0,0 +1,33 @@ +/* + * key.cc - Implementation of common routines for the base Key classes + * libhscript, the HorizonScript library for + * Project Horizon + * + * Copyright (c) 2019 Adélie Linux and contributors. All rights reserved. + * This code is licensed under the AGPL 3.0 license, as noted in the + * LICENSE-code file in the root directory of this repository. + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +#include <algorithm> +#include "key.hh" + +bool Horizon::Keys::BooleanKey::parse(const std::string what, bool *out) { + std::string lower; + std::transform(what.begin(), what.end(), lower.begin(), ::tolower); + + if(lower == "true" || lower == "yes" || lower == "1") { + *out = true; + } else if(lower == "false" || lower == "no" || lower == "0") { + *out = false; + } else { + return false; + } + return true; +} + +bool Horizon::Keys::BooleanKey::validate() { + /* Key will fail init if it is not valid, so this is always a no-op. */ + return true; +} diff --git a/hscript/key.hh b/hscript/key.hh index 5e09fab..4817ea5 100644 --- a/hscript/key.hh +++ b/hscript/key.hh @@ -1,5 +1,5 @@ /* - * key.hh - Definition of the base Key class + * key.hh - Definition of the base Key classes * libhscript, the HorizonScript library for * Project Horizon * @@ -24,7 +24,7 @@ namespace Keys { */ class Key { public: - virtual ~Key(); + virtual ~Key() {} /*! Create the Key object with the specified data as the entire value. * @param data The value associated with the key. @@ -33,8 +33,12 @@ public: * @param warnings Output variable: if not nullptr, ++ on each warning. * @returns nullptr if data is unparsable, otherwise a pointer to a Key. */ - static Key *parseFromData(std::string data, int lineno, int *errors, - int *warnings) { return nullptr; } +#define UNUSED __attribute__((unused)) + static Key *parseFromData(const std::string data UNUSED, int lineno UNUSED, + int *errors UNUSED, int *warnings UNUSED) { + return nullptr; + } +#undef UNUSED /*! Determines if the data associated with the Key is valid. */ virtual bool validate() = 0; @@ -45,6 +49,34 @@ public: virtual bool execute() = 0; }; + +/*! Base Key class that parses and handles Boolean values. + * All values passed in are lowercased. Delimiters are not allowed. + * Truthy values: "true" "yes" "1" + * Falsy values: "false" "no" "0" + * Any other values will be considered invalid. + */ +class BooleanKey : public Key { +protected: + BooleanKey(bool my_value) : value(my_value) {} + bool value; + + /*! Parse a string into a boolean. + * @param what The string to attempt parsing. + * @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); +public: + /*! Determines if the Key is set or not. + * @returns true if the Key is truthy, false otherwise. + */ + bool test() const { return this->value; } + + /*! Key will fail to init if valid is invalid. */ + bool validate() override; +}; + } } |