From: Joao Eduardo Luis Date: Mon, 28 Jul 2014 22:09:24 +0000 (+0100) Subject: common: str_map: add helper methods to get values from maps X-Git-Tag: v0.86~167^2~14 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=b1af4bd0a1adca1de5e39034f11b0c7138f138e5;p=ceph.git common: str_map: add helper methods to get values from maps Both methods obtain values for keys from a given map. Main distinction is that one method will return a default value if key is not present and if the default value is specified (i.e., not NULL), while the other method will return the value of a fallback key if the key is not present. Signed-off-by: Joao Eduardo Luis --- diff --git a/src/common/str_map.cc b/src/common/str_map.cc index ef9b7d41bc06..f8e60634ce21 100644 --- a/src/common/str_map.cc +++ b/src/common/str_map.cc @@ -66,3 +66,44 @@ int get_str_map(const string &str, } return 0; } + +string get_str_map_value( + const map &str_map, + const string &key, + const string *def_val) +{ + map::const_iterator p = str_map.find(key); + + // key exists in str_map + if (p != str_map.end()) { + // but value is empty + if (p->second.empty()) + return p->first; + // and value is not empty + return p->second; + } + + // key DNE in str_map and def_val was specified + if (def_val != NULL) + return *def_val; + + // key DNE in str_map, no def_val was specified + return string(); +} + +string get_str_map_key( + const map &str_map, + const string &key, + const string *fallback_key) +{ + map::const_iterator p = str_map.find(key); + if (p != str_map.end()) + return p->second; + + if (fallback_key != NULL) { + p = str_map.find(*fallback_key); + if (p != str_map.end()) + return p->second; + } + return string(); +} diff --git a/src/include/str_map.h b/src/include/str_map.h index eabe8d2023be..a695f3777152 100644 --- a/src/include/str_map.h +++ b/src/include/str_map.h @@ -56,4 +56,42 @@ extern int get_str_map(const std::string &str, std::ostream &ss, std::map *str_map); +/** + * Returns the value of **key** in **str_map** if available. + * + * If **key** is not available in **str_map**, and if **def_val** is + * not-NULL then returns **def_val**. Otherwise checks if the value of + * **key** is an empty string and if so will return **key**. + * If the map contains **key**, the function returns the value of **key**. + * + * @param[in] str_map Map to obtain **key** from + * @param[in] key The key to search for in the map + * @param[in] def_val The value to return in case **key** is not present + */ +extern std::string get_str_map_value( + const std::map &str_map, + const std::string &key, + const std::string *def_val = NULL); + +/** + * Returns the value of **key** in **str_map** if available. + * + * If **key** is available in **str_map** returns the value of **key**. + * + * If **key** is not available in **str_map**, and if **def_key** + * is not-NULL and available in **str_map**, then returns the value + * of **def_key**. + * + * Otherwise returns an empty string. + * + * @param[in] str_map Map to obtain **key** or **def_key** from + * @param[in] key Key to obtain the value of from **str_map** + * @param[in] def_key Key to fallback to if **key** is not present + * in **str_map** + */ +extern std::string get_str_map_key( + const std::map &str_map, + const std::string &key, + const std::string *fallback_key = NULL); + #endif