Commit 097bd72e authored by Thomas Haller's avatar Thomas Haller

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.
parent e7d18548
......@@ -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;
}
......@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment