356a11
From 19760e5b97136de3474743db8f2ae86380658f82 Mon Sep 17 00:00:00 2001
356a11
From: Ken Gaillot <kgaillot@redhat.com>
356a11
Date: Wed, 12 Jul 2017 15:53:20 -0500
356a11
Subject: [PATCH] Refactor: libcrmcommon,etc.: convenience functions for string
356a11
 tables
356a11
356a11
A simple hash table of dynamically allocated strings is the most common type in
356a11
the code base. This adds convenience functions for regular and case-insensitive
356a11
table creation, which makes the code less ugly, hopefully without sacrificing
356a11
readability.
356a11
356a11
As a side effect, this gets rid of a lot of usage of g_hash_destroy_str() when
356a11
we only need free().
356a11
---
356a11
 include/crm/common/util.h | 32 ++++++++++++++++++++++++++++++++
356a11
 include/crm/crm.h         |  5 -----
356a11
 lib/common/strings.c      | 20 ++++++++++++++++++++
356a11
 3 files changed, 52 insertions(+), 5 deletions(-)
356a11
356a11
diff --git a/include/crm/common/util.h b/include/crm/common/util.h
356a11
index 904b40c..04f7c31 100644
356a11
--- a/include/crm/common/util.h
356a11
+++ b/include/crm/common/util.h
356a11
@@ -30,6 +30,7 @@
356a11
 #  include <limits.h>
356a11
 #  include <signal.h>
356a11
 #  include <sysexits.h>
356a11
+#  include <glib.h>
356a11
 
356a11
 #  include <crm/lrmd.h>
356a11
 
356a11
@@ -60,8 +61,11 @@ int crm_parse_int(const char *text, const char *default_text);
356a11
 char * crm_strip_trailing_newline(char *str);
356a11
 gboolean crm_str_eq(const char *a, const char *b, gboolean use_case);
356a11
 gboolean safe_str_neq(const char *a, const char *b);
356a11
+guint crm_strcase_hash(gconstpointer v);
356a11
+guint g_str_hash_traditional(gconstpointer v);
356a11
 
356a11
 #  define safe_str_eq(a, b) crm_str_eq(a, b, FALSE)
356a11
+#  define crm_str_hash g_str_hash_traditional
356a11
 
356a11
 /* used with hash tables where case does not matter */
356a11
 static inline gboolean
356a11
@@ -70,6 +74,34 @@ crm_strcase_equal(gconstpointer a, gconstpointer b)
356a11
     return crm_str_eq((const char *) a, (const char *) b, FALSE);
356a11
 }
356a11
 
356a11
+/*!
356a11
+ * \brief Create hash table with dynamically allocated string keys/values
356a11
+ *
356a11
+ * \return Newly hash table
356a11
+ * \note It is the caller's responsibility to free the result, using
356a11
+ *       g_hash_table_destroy().
356a11
+ */
356a11
+static inline GHashTable *
356a11
+crm_str_table_new()
356a11
+{
356a11
+    return g_hash_table_new_full(crm_str_hash, g_str_equal, free, free);
356a11
+}
356a11
+
356a11
+/*!
356a11
+ * \brief Create hash table with case-insensitive dynamically allocated string keys/values
356a11
+ *
356a11
+ * \return Newly hash table
356a11
+ * \note It is the caller's responsibility to free the result, using
356a11
+ *       g_hash_table_destroy().
356a11
+ */
356a11
+static inline GHashTable *
356a11
+crm_strcase_table_new()
356a11
+{
356a11
+    return g_hash_table_new_full(crm_strcase_hash, crm_strcase_equal, free, free);
356a11
+}
356a11
+
356a11
+GHashTable *crm_str_table_dup(GHashTable *old_table);
356a11
+
356a11
 #  define crm_atoi(text, default_text) crm_parse_int(text, default_text)
356a11
 
356a11
 /* public I/O functions (from io.c) */
356a11
diff --git a/include/crm/crm.h b/include/crm/crm.h
356a11
index d03bcf2..0da6bfe 100644
356a11
--- a/include/crm/crm.h
356a11
+++ b/include/crm/crm.h
356a11
@@ -208,11 +208,6 @@ typedef GList *GListPtr;
356a11
 #  include <crm/common/util.h>
356a11
 #  include <crm/error.h>
356a11
 
356a11
-#  define crm_str_hash g_str_hash_traditional
356a11
-
356a11
-guint crm_strcase_hash(gconstpointer v);
356a11
-guint g_str_hash_traditional(gconstpointer v);
356a11
-
356a11
 static inline const char *crm_action_str(const char *task, int interval) {
356a11
     if(safe_str_eq(task, RSC_STATUS) && !interval) {
356a11
         return "probe";
356a11
diff --git a/lib/common/strings.c b/lib/common/strings.c
356a11
index 3df41f6..c624b4b 100644
356a11
--- a/lib/common/strings.c
356a11
+++ b/lib/common/strings.c
356a11
@@ -288,6 +288,26 @@ crm_strcase_hash(gconstpointer v)
356a11
     return h;
356a11
 }
356a11
 
356a11
+static void
356a11
+copy_str_table_entry(gpointer key, gpointer value, gpointer user_data)
356a11
+{
356a11
+    if (key && value && user_data) {
356a11
+        g_hash_table_insert((GHashTable*)user_data, strdup(key), strdup(value));
356a11
+    }
356a11
+}
356a11
+
356a11
+GHashTable *
356a11
+crm_str_table_dup(GHashTable *old_table)
356a11
+{
356a11
+    GHashTable *new_table = NULL;
356a11
+
356a11
+    if (old_table) {
356a11
+        new_table = crm_str_table_new();
356a11
+        g_hash_table_foreach(old_table, copy_str_table_entry, new_table);
356a11
+    }
356a11
+    return new_table;
356a11
+}
356a11
+
356a11
 char *
356a11
 add_list_element(char *list, const char *value)
356a11
 {
356a11
-- 
356a11
1.8.3.1
356a11