diff --git a/src/utils/math-helpers.cpp b/src/utils/math-helpers.cpp index f46d61ba..b8177c31 100644 --- a/src/utils/math-helpers.cpp +++ b/src/utils/math-helpers.cpp @@ -44,4 +44,14 @@ std::optional GetDouble(const std::string &str) return {}; } +std::optional GetInt(const std::string &str) +{ + char *end = nullptr; + int value = std::strtol(str.c_str(), &end, 10); + if (end != str.c_str() && *end == '\0') { + return value; + } + return {}; +} + } // namespace advss diff --git a/src/utils/math-helpers.hpp b/src/utils/math-helpers.hpp index 736cf123..c919b6bf 100644 --- a/src/utils/math-helpers.hpp +++ b/src/utils/math-helpers.hpp @@ -9,5 +9,6 @@ std::variant EvalMathExpression(const std::string &expression); bool IsValidNumber(const std::string &); std::optional GetDouble(const std::string &); +std::optional GetInt(const std::string &); } // namespace advss diff --git a/src/utils/variable.cpp b/src/utils/variable.cpp index baeaddf7..7632291a 100644 --- a/src/utils/variable.cpp +++ b/src/utils/variable.cpp @@ -53,6 +53,11 @@ std::optional Variable::DoubleValue() const return GetDouble(_value); } +std::optional Variable::IntValue() const +{ + return GetInt(_value); +} + void Variable::SetValue(const std::string &val) { _value = val; diff --git a/src/utils/variable.hpp b/src/utils/variable.hpp index cf7a5ece..9b9a87bf 100644 --- a/src/utils/variable.hpp +++ b/src/utils/variable.hpp @@ -20,6 +20,7 @@ public: void Save(obs_data_t *obj) const; std::string Value() const { return _value; } std::optional DoubleValue() const; + std::optional IntValue() const; void SetValue(const std::string &val); void SetValue(double); static std::shared_ptr Create()