diff options
author | Mattes D <github@xoft.cz> | 2015-11-11 10:32:42 +0100 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2015-12-01 10:35:07 +0100 |
commit | b8fbba5eb92cda32b13d65f3704adf778da82f38 (patch) | |
tree | 0c6e8f47ae152e5ffade6f0a7457505101764753 /src/StringUtils.h | |
parent | Merge pull request #2696 from Gargaj/breeding (diff) | |
download | cuberite-b8fbba5eb92cda32b13d65f3704adf778da82f38.tar cuberite-b8fbba5eb92cda32b13d65f3704adf778da82f38.tar.gz cuberite-b8fbba5eb92cda32b13d65f3704adf778da82f38.tar.bz2 cuberite-b8fbba5eb92cda32b13d65f3704adf778da82f38.tar.lz cuberite-b8fbba5eb92cda32b13d65f3704adf778da82f38.tar.xz cuberite-b8fbba5eb92cda32b13d65f3704adf778da82f38.tar.zst cuberite-b8fbba5eb92cda32b13d65f3704adf778da82f38.zip |
Diffstat (limited to '')
-rw-r--r-- | src/StringUtils.h | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/StringUtils.h b/src/StringUtils.h index 00504d358..8c1925115 100644 --- a/src/StringUtils.h +++ b/src/StringUtils.h @@ -17,6 +17,10 @@ typedef std::string AString; typedef std::vector<AString> AStringVector; typedef std::list<AString> AStringList; +/** A string dictionary, used for key-value pairs. */ +typedef std::map<AString, AString> AStringMap; + + @@ -129,6 +133,10 @@ extern AStringVector MergeStringVectors(const AStringVector & a_Strings1, const /** Concatenates the specified strings into a single string, separated by the specified separator. */ extern AString StringsConcat(const AStringVector & a_Strings, char a_Separator); + + + + /** Parses any integer type. Checks bounds and returns errors out of band. */ template <class T> bool StringToInteger(const AString & a_str, T & a_Num) @@ -197,6 +205,35 @@ bool StringToInteger(const AString & a_str, T & a_Num) return true; } + + + + +/** Returns an integer from a key-value string map. +Returns a_Default if the key is not present or the value is not an int. */ +template <typename T> +int GetStringMapInteger(const AStringMap & a_Map, const AString & a_Key, T a_Default) +{ + // Try to locate the key: + auto itr = a_Map.find(a_Key); + if (itr == a_Map.end()) + { + return a_Default; + } + + // Try to convert the value to a number: + T res = a_Default; + if (!StringToInteger<T>(itr->second, res)) + { + return a_Default; + } + return res; +} + + + + + // If you have any other string helper functions, declare them here |