From b8fbba5eb92cda32b13d65f3704adf778da82f38 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Wed, 11 Nov 2015 10:32:42 +0100 Subject: Added PieceStructures generator. --- src/StringUtils.h | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'src/StringUtils.h') 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 AStringVector; typedef std::list AStringList; +/** A string dictionary, used for key-value pairs. */ +typedef std::map 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 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 +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(itr->second, res)) + { + return a_Default; + } + return res; +} + + + + + // If you have any other string helper functions, declare them here -- cgit v1.2.3