diff options
author | Thomas Haller <thaller@redhat.com> | 2017-12-08 17:27:39 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2017-12-08 18:47:37 +0100 |
commit | 097bd72e2ea6351108205eb4dbc35a6c4408207b (patch) | |
tree | 62d234c5a8f3914ea58c1d1a457506d8139ad9a5 | |
parent | e7d18548b9e15ff24b357fc182c1300901495466 (diff) | |
download | NetworkManager-097bd72e2ea6351108205eb4dbc35a6c4408207b.tar.gz |
shared: add nm_utils_named_values_from_str_dict()
NMUtilsNamedValue's purpose is precisely to create
a list and sort by entires.
Add nm_utils_named_values_from_str_dict() as helper
function to do that.
-rw-r--r-- | shared/nm-utils/nm-shared-utils.c | 33 | ||||
-rw-r--r-- | shared/nm-utils/nm-shared-utils.h | 2 |
2 files changed, 35 insertions, 0 deletions
diff --git a/shared/nm-utils/nm-shared-utils.c b/shared/nm-utils/nm-shared-utils.c index 0b343afdf8..732ce23bd7 100644 --- a/shared/nm-utils/nm-shared-utils.c +++ b/shared/nm-utils/nm-shared-utils.c @@ -1115,3 +1115,36 @@ nm_utils_fd_read_loop_exact (int fd, void *buf, size_t nbytes, bool do_poll) return 0; } + +NMUtilsNamedValue * +nm_utils_named_values_from_str_dict (GHashTable *hash, guint *out_len) +{ + GHashTableIter iter; + NMUtilsNamedValue *values; + guint i, len; + + if ( !hash + || !(len = g_hash_table_size (hash))) { + NM_SET_OUT (out_len, 0); + return NULL; + } + + i = 0; + values = g_new (NMUtilsNamedValue, len + 1); + g_hash_table_iter_init (&iter, hash); + while (g_hash_table_iter_next (&iter, + (gpointer *) &values[i].name, + (gpointer *) &values[i].value_ptr)) + i++; + nm_assert (i == len); + values[i].name = NULL; + values[i].value_ptr = NULL; + + if (len > 1) { + g_qsort_with_data (values, len, sizeof (values[0]), + nm_utils_named_entry_cmp_with_data, NULL); + } + + NM_SET_OUT (out_len, len); + return values; +} diff --git a/shared/nm-utils/nm-shared-utils.h b/shared/nm-utils/nm-shared-utils.h index d6d829cd57..89ad8b8d48 100644 --- a/shared/nm-utils/nm-shared-utils.h +++ b/shared/nm-utils/nm-shared-utils.h @@ -435,6 +435,8 @@ typedef struct { #define nm_utils_named_entry_cmp nm_strcmp_p #define nm_utils_named_entry_cmp_with_data nm_strcmp_p_with_data +NMUtilsNamedValue *nm_utils_named_values_from_str_dict (GHashTable *hash, guint *out_len); + /*****************************************************************************/ #define NM_UTILS_NS_PER_SECOND ((gint64) 1000000000) |