diff --git a/include/linphone/utils/utils.h b/include/linphone/utils/utils.h index 91897090c..ccf43a9de 100644 --- a/include/linphone/utils/utils.h +++ b/include/linphone/utils/utils.h @@ -49,6 +49,10 @@ namespace Utils { LINPHONE_PUBLIC int stoi (const std::string &str, size_t *idx = 0, int base = 10); + LINPHONE_PUBLIC std::string stringToLower (const std::string &str); + + LINPHONE_PUBLIC bool stringToBool (const std::string &str); + // Return a buffer allocated with new. LINPHONE_PUBLIC char *utf8ToChar (uint32_t ic); diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index 1fea89566..d3ad01afc 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +#include #include #include @@ -87,6 +88,17 @@ int Utils::stoi (const string &str, size_t *idx, int base) { return v; } +string Utils::stringToLower (const string &str) { + string result(str.size(), ' '); + transform(str.cbegin(), str.cend(), result.begin(), ::tolower); + return result; +} + +bool Utils::stringToBool (const string &str) { + const string lowerStr = stringToLower(str); + return !lowerStr.empty() && (lowerStr == "true" || lowerStr == "1"); +} + char *Utils::utf8ToChar (uint32_t ic) { char *result = new char[5]; int size = 0; diff --git a/src/variant/variant.cpp b/src/variant/variant.cpp index fa18701ed..8edfdcfb9 100644 --- a/src/variant/variant.cpp +++ b/src/variant/variant.cpp @@ -16,6 +16,8 @@ * along with this program. If not, see . */ +#include "linphone/utils/utils.h" + #include "variant.h" // ============================================================================= @@ -39,9 +41,12 @@ public: bool b; double d; float f; + string *str; + void *g; }; - Variant::Type type = Variant::Invalid; + // Integer, because type can be a custom type. + int type = Variant::Invalid; Value value = {}; }; @@ -182,6 +187,56 @@ void Variant::swap (const Variant &variant) { // TODO. } +// ----------------------------------------------------------------------------- +// Number helpers. +// ----------------------------------------------------------------------------- + +static inline long long getAssumedNumber (const VariantPrivate &p) { + L_ASSERT(p.type > Variant::Invalid && p.type < Variant::MaxDefaultTypes); + + switch (static_cast(p.type)) { + case Variant::Int: + return p.value.i; + case Variant::Short: + return p.value.s; + case Variant::Long: + return p.value.l; + case Variant::LongLong: + return p.value.ll; + case Variant::Char: + return p.value.c; + case Variant::Double: + return p.value.d; + case Variant::Float: + return p.value.f; + + default: + L_ASSERT(false); + } + + return 0; +} + +static inline unsigned long long getAssumedUnsignedNumber (const VariantPrivate &p) { + L_ASSERT(p.type > Variant::Invalid && p.type < Variant::MaxDefaultTypes); + + switch (static_cast(p.type)) { + case Variant::UnsignedInt: + return p.value.ui; + case Variant::UnsignedShort: + return p.value.us; + case Variant::UnsignedLong: + return p.value.ul; + case Variant::UnsignedLongLong: + return p.value.ull; + + default: + L_ASSERT(false); + } + + return 0; +} + // ----------------------------------------------------------------------------- // Number conversions. // ----------------------------------------------------------------------------- @@ -201,7 +256,39 @@ static inline unsigned long long getValueAsUnsignedNumber (const VariantPrivate // ----------------------------------------------------------------------------- static inline bool getValueAsBool (const VariantPrivate &p, bool *soFarSoGood) { - // TODO. + L_ASSERT(p.type > Variant::Invalid && p.type < Variant::MaxDefaultTypes); + + *soFarSoGood = true; + switch (static_cast(p.type)) { + case Variant::Int: + case Variant::Short: + case Variant::Long: + case Variant::LongLong: + case Variant::Char: + case Variant::Double: + case Variant::Float: + return static_cast(getAssumedNumber(p)); + + case Variant::UnsignedInt: + case Variant::UnsignedShort: + case Variant::UnsignedLong: + case Variant::UnsignedLongLong: + return static_cast(getAssumedUnsignedNumber(p)); + + case Variant::Bool: + return p.value.b; + + case Variant::String: + return Utils::stringToBool(*p.value.str); + + case Variant::Generic: + return static_cast(p.value.g); + + default: + *soFarSoGood = false; + break; + } + return false; } @@ -230,7 +317,7 @@ static inline void *getValueAsGeneric (const VariantPrivate &p, bool *soFarSoGoo void Variant::getValue (int type, void *value, bool *soFarSoGood) const { L_D(const Variant); - if (type <= 0 || type >= MaxDefaultTypes) { + if (type <= Invalid || type >= MaxDefaultTypes) { *soFarSoGood = false; // Unable to get value. It will be great to support custom types. return;