summaryrefslogtreecommitdiffstats
path: root/src/StringUtils.h
diff options
context:
space:
mode:
authorMattes D <github@xoft.cz>2015-12-03 13:07:23 +0100
committerMattes D <github@xoft.cz>2015-12-03 13:07:23 +0100
commit3fb356648453169758a9f5649f0ed4c5925d911f (patch)
tree57c90ff51aaeac4b1c2e05538a9f486da0a19f28 /src/StringUtils.h
parentMerge pull request #2696 from Gargaj/breeding (diff)
parentChanged Nether defaults to use PieceStructures. (diff)
downloadcuberite-3fb356648453169758a9f5649f0ed4c5925d911f.tar
cuberite-3fb356648453169758a9f5649f0ed4c5925d911f.tar.gz
cuberite-3fb356648453169758a9f5649f0ed4c5925d911f.tar.bz2
cuberite-3fb356648453169758a9f5649f0ed4c5925d911f.tar.lz
cuberite-3fb356648453169758a9f5649f0ed4c5925d911f.tar.xz
cuberite-3fb356648453169758a9f5649f0ed4c5925d911f.tar.zst
cuberite-3fb356648453169758a9f5649f0ed4c5925d911f.zip
Diffstat (limited to 'src/StringUtils.h')
-rw-r--r--src/StringUtils.h37
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