]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common: str_map: add helper methods to get values from maps
authorJoao Eduardo Luis <joao.luis@inktank.com>
Mon, 28 Jul 2014 22:09:24 +0000 (23:09 +0100)
committerJoao Eduardo Luis <joao.luis@inktank.com>
Wed, 27 Aug 2014 17:21:47 +0000 (18:21 +0100)
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 <joao.luis@inktank.com>
src/common/str_map.cc
src/include/str_map.h

index ef9b7d41bc06f5b1dba09020fb496d1bbc638f5d..f8e60634ce21b4cadabef376e40153f6b21672e3 100644 (file)
@@ -66,3 +66,44 @@ int get_str_map(const string &str,
   }
   return 0;
 }
+
+string get_str_map_value(
+    const map<string,string> &str_map,
+    const string &key,
+    const string *def_val)
+{
+  map<string,string>::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<string,string> &str_map,
+    const string &key,
+    const string *fallback_key)
+{
+  map<string,string>::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();
+}
index eabe8d2023bec1cbc8eac60c4ea69481dadac32c..a695f3777152a0962905c2c8c722d5c4ea29cc7d 100644 (file)
@@ -56,4 +56,42 @@ extern int get_str_map(const std::string &str,
                       std::ostream &ss,
                       std::map<std::string,std::string> *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<std::string,std::string> &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<std::string,std::string> &str_map,
+    const std::string &key,
+    const std::string *fallback_key = NULL);
+
 #endif