summaryrefslogtreecommitdiff
path: root/shared
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2017-12-10 12:18:21 +0100
committerThomas Haller <thaller@redhat.com>2017-12-18 11:56:54 +0100
commitfe7b4641d6a91c85d5db3e32fdb4009fe5596ec4 (patch)
tree8ab8328cdac7082a872853938fff251a79741392 /shared
parent0735b35dd0fe87285f85c0040dfcb53cec2d1bd9 (diff)
downloadNetworkManager-fe7b4641d6a91c85d5db3e32fdb4009fe5596ec4.tar.gz
shared: add nm_utils_strdict_get_keys() helper
At various places we get the (string) keys of a GHashTable. Add a helper function that does that, including an argument for optional sorting. The helper function is there to get reduce code duplication.
Diffstat (limited to 'shared')
-rw-r--r--shared/nm-utils/nm-shared-utils.c27
-rw-r--r--shared/nm-utils/nm-shared-utils.h4
2 files changed, 31 insertions, 0 deletions
diff --git a/shared/nm-utils/nm-shared-utils.c b/shared/nm-utils/nm-shared-utils.c
index 732ce23bd7..8a7890b093 100644
--- a/shared/nm-utils/nm-shared-utils.c
+++ b/shared/nm-utils/nm-shared-utils.c
@@ -1148,3 +1148,30 @@ nm_utils_named_values_from_str_dict (GHashTable *hash, guint *out_len)
NM_SET_OUT (out_len, len);
return values;
}
+
+const char **
+nm_utils_strdict_get_keys (const GHashTable *hash,
+ gboolean sorted,
+ guint *out_length)
+{
+ const char **names;
+ guint length;
+
+ if ( !hash
+ || !g_hash_table_size ((GHashTable *) hash)) {
+ NM_SET_OUT (out_length, 0);
+ return NULL;
+ }
+
+ names = (const char **) g_hash_table_get_keys_as_array ((GHashTable *) hash, &length);
+ if ( sorted
+ && length > 1) {
+ g_qsort_with_data (names,
+ length,
+ sizeof (char *),
+ nm_strcmp_p_with_data,
+ NULL);
+ }
+ NM_SET_OUT (out_length, length);
+ return names;
+}
diff --git a/shared/nm-utils/nm-shared-utils.h b/shared/nm-utils/nm-shared-utils.h
index 17024d591f..81a66d5b28 100644
--- a/shared/nm-utils/nm-shared-utils.h
+++ b/shared/nm-utils/nm-shared-utils.h
@@ -443,6 +443,10 @@ typedef struct {
NMUtilsNamedValue *nm_utils_named_values_from_str_dict (GHashTable *hash, guint *out_len);
+const char **nm_utils_strdict_get_keys (const GHashTable *hash,
+ gboolean sorted,
+ guint *out_length);
+
/*****************************************************************************/
#define NM_UTILS_NS_PER_SECOND ((gint64) 1000000000)