summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2019-08-23 18:08:44 +0200
committerThomas Haller <thaller@redhat.com>2019-09-22 16:05:50 +0200
commite688e70b372a2661dcad17d277ca7233ffad4f6d (patch)
tree9f4565eaa3a50574e725b63b29de0793a03298d0
parent2667a46874b7b853af0be2645cc38a4e965c3b9e (diff)
downloadNetworkManager-e688e70b372a2661dcad17d277ca7233ffad4f6d.tar.gz
shared: add nm_utils_hash_values_to_array() helper
-rw-r--r--shared/nm-glib-aux/nm-shared-utils.c38
-rw-r--r--shared/nm-glib-aux/nm-shared-utils.h5
2 files changed, 43 insertions, 0 deletions
diff --git a/shared/nm-glib-aux/nm-shared-utils.c b/shared/nm-glib-aux/nm-shared-utils.c
index f03c66ddae..43cab16b2c 100644
--- a/shared/nm-glib-aux/nm-shared-utils.c
+++ b/shared/nm-glib-aux/nm-shared-utils.c
@@ -2288,6 +2288,44 @@ nm_utils_hash_keys_to_array (GHashTable *hash,
return keys;
}
+gpointer *
+nm_utils_hash_values_to_array (GHashTable *hash,
+ GCompareDataFunc compare_func,
+ gpointer user_data,
+ guint *out_len)
+{
+ GHashTableIter iter;
+ gpointer value;
+ gpointer *arr;
+ guint i, len;
+
+ if ( !hash
+ || (len = g_hash_table_size (hash)) == 0u) {
+ NM_SET_OUT (out_len, 0);
+ return NULL;
+ }
+
+ arr = g_new (gpointer, ((gsize) len) + 1);
+ i = 0;
+ g_hash_table_iter_init (&iter, hash);
+ while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &value))
+ arr[i++] = value;
+
+ nm_assert (i == len);
+ arr[len] = NULL;
+
+ if ( len > 1
+ && compare_func) {
+ g_qsort_with_data (arr,
+ len,
+ sizeof (gpointer),
+ compare_func,
+ user_data);
+ }
+
+ return arr;
+}
+
gboolean
nm_utils_hashtable_same_keys (const GHashTable *a,
const GHashTable *b)
diff --git a/shared/nm-glib-aux/nm-shared-utils.h b/shared/nm-glib-aux/nm-shared-utils.h
index d3ec5ceb7d..75b5d387c5 100644
--- a/shared/nm-glib-aux/nm-shared-utils.h
+++ b/shared/nm-glib-aux/nm-shared-utils.h
@@ -955,6 +955,11 @@ gpointer *nm_utils_hash_keys_to_array (GHashTable *hash,
gpointer user_data,
guint *out_len);
+gpointer *nm_utils_hash_values_to_array (GHashTable *hash,
+ GCompareDataFunc compare_func,
+ gpointer user_data,
+ guint *out_len);
+
static inline const char **
nm_utils_strdict_get_keys (const GHashTable *hash,
gboolean sorted,