mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2025-12-25 03:40:07 +01:00
glib-aux: add nm_utils_hash_to_array() helper
We effectively already have this function, with the name nm_utils_named_values_from_strdict(). Which is a decent name, if you have a strdict. But it seems odd to use for other dictionaries. Instead, add a variant with a different name. Naming is important, and just to have the better name, the function is effectively duplicated.
This commit is contained in:
parent
78aad6cf51
commit
de926723f0
2 changed files with 91 additions and 53 deletions
|
|
@ -3432,51 +3432,6 @@ nm_utils_named_value_clear_with_g_free(NMUtilsNamedValue *val)
|
|||
|
||||
G_STATIC_ASSERT(G_STRUCT_OFFSET(NMUtilsNamedValue, name) == 0);
|
||||
|
||||
NMUtilsNamedValue *
|
||||
nm_utils_named_values_from_strdict_full(GHashTable *hash,
|
||||
guint *out_len,
|
||||
GCompareDataFunc compare_func,
|
||||
gpointer user_data,
|
||||
NMUtilsNamedValue *provided_buffer,
|
||||
guint provided_buffer_len,
|
||||
NMUtilsNamedValue **out_allocated_buffer)
|
||||
{
|
||||
GHashTableIter iter;
|
||||
NMUtilsNamedValue *values;
|
||||
guint i, len;
|
||||
|
||||
nm_assert(provided_buffer_len == 0 || provided_buffer);
|
||||
nm_assert(!out_allocated_buffer || !*out_allocated_buffer);
|
||||
|
||||
if (!hash || !(len = g_hash_table_size(hash))) {
|
||||
NM_SET_OUT(out_len, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (provided_buffer_len >= len + 1) {
|
||||
/* the buffer provided by the caller is large enough. Use it. */
|
||||
values = provided_buffer;
|
||||
} else {
|
||||
/* allocate a new buffer. */
|
||||
values = g_new(NMUtilsNamedValue, len + 1);
|
||||
NM_SET_OUT(out_allocated_buffer, values);
|
||||
}
|
||||
|
||||
i = 0;
|
||||
g_hash_table_iter_init(&iter, hash);
|
||||
while (g_hash_table_iter_next(&iter, (gpointer *) &values[i].name, &values[i].value_ptr))
|
||||
i++;
|
||||
nm_assert(i == len);
|
||||
values[i].name = NULL;
|
||||
values[i].value_ptr = NULL;
|
||||
|
||||
if (compare_func)
|
||||
nm_utils_named_value_list_sort(values, len, compare_func, user_data);
|
||||
|
||||
NM_SET_OUT(out_len, len);
|
||||
return values;
|
||||
}
|
||||
|
||||
gssize
|
||||
nm_utils_named_value_list_find(const NMUtilsNamedValue *arr,
|
||||
gsize len,
|
||||
|
|
@ -3626,6 +3581,52 @@ nm_utils_hash_values_to_array(GHashTable *hash,
|
|||
return arr;
|
||||
}
|
||||
|
||||
NMUtilsNamedValue *
|
||||
nm_utils_hash_to_array_full(GHashTable *hash,
|
||||
guint *out_len,
|
||||
GCompareDataFunc compare_func,
|
||||
gpointer user_data,
|
||||
NMUtilsNamedValue *provided_buffer,
|
||||
guint provided_buffer_len,
|
||||
NMUtilsNamedValue **out_allocated_buffer)
|
||||
{
|
||||
GHashTableIter iter;
|
||||
NMUtilsNamedValue *values;
|
||||
guint len;
|
||||
guint i;
|
||||
|
||||
nm_assert(provided_buffer_len == 0 || provided_buffer);
|
||||
nm_assert(!out_allocated_buffer || !*out_allocated_buffer);
|
||||
|
||||
if (!hash || ((len = g_hash_table_size(hash)) == 0)) {
|
||||
NM_SET_OUT(out_len, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (provided_buffer_len >= len + 1) {
|
||||
/* the buffer provided by the caller is large enough. Use it. */
|
||||
values = provided_buffer;
|
||||
} else {
|
||||
/* allocate a new buffer. */
|
||||
values = g_new(NMUtilsNamedValue, len + 1);
|
||||
NM_SET_OUT(out_allocated_buffer, values);
|
||||
}
|
||||
|
||||
i = 0;
|
||||
g_hash_table_iter_init(&iter, hash);
|
||||
while (g_hash_table_iter_next(&iter, &values[i].name_ptr, &values[i].value_ptr))
|
||||
i++;
|
||||
nm_assert(i == len);
|
||||
values[i].name_ptr = NULL;
|
||||
values[i].value_ptr = NULL;
|
||||
|
||||
if (compare_func && len > 1)
|
||||
g_qsort_with_data(values, len, sizeof(NMUtilsNamedValue), compare_func, user_data);
|
||||
|
||||
NM_SET_OUT(out_len, len);
|
||||
return values;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1991,14 +1991,28 @@ typedef struct {
|
|||
.name = (n), .value_ptr = (v) \
|
||||
}
|
||||
|
||||
NMUtilsNamedValue *
|
||||
nm_utils_named_values_from_strdict_full(GHashTable *hash,
|
||||
guint *out_len,
|
||||
GCompareDataFunc compare_func,
|
||||
gpointer user_data,
|
||||
NMUtilsNamedValue *provided_buffer,
|
||||
guint provided_buffer_len,
|
||||
NMUtilsNamedValue **out_allocated_buffer);
|
||||
NMUtilsNamedValue *nm_utils_hash_to_array_full(GHashTable *hash,
|
||||
guint *out_len,
|
||||
GCompareDataFunc compare_func,
|
||||
gpointer user_data,
|
||||
NMUtilsNamedValue *provided_buffer,
|
||||
guint provided_buffer_len,
|
||||
NMUtilsNamedValue **out_allocated_buffer);
|
||||
|
||||
#define nm_utils_named_values_from_strdict_full(hash, \
|
||||
out_len, \
|
||||
compare_func, \
|
||||
user_data, \
|
||||
provided_buffer, \
|
||||
provided_buffer_len, \
|
||||
out_allocated_buffer) \
|
||||
nm_utils_hash_to_array_full((hash), \
|
||||
(out_len), \
|
||||
(compare_func), \
|
||||
(user_data), \
|
||||
(provided_buffer), \
|
||||
(provided_buffer_len), \
|
||||
(out_allocated_buffer))
|
||||
|
||||
#define nm_utils_named_values_from_strdict(hash, out_len, array, out_allocated_buffer) \
|
||||
nm_utils_named_values_from_strdict_full((hash), \
|
||||
|
|
@ -2039,6 +2053,29 @@ gpointer *nm_utils_hash_values_to_array(GHashTable *hash,
|
|||
gpointer user_data,
|
||||
guint *out_len);
|
||||
|
||||
static inline NMUtilsNamedValue *
|
||||
nm_utils_hash_to_array(GHashTable *hash,
|
||||
GCompareDataFunc compare_func,
|
||||
gpointer user_data,
|
||||
guint *out_len)
|
||||
{
|
||||
return nm_utils_hash_to_array_full(hash, out_len, compare_func, user_data, NULL, 0, NULL);
|
||||
}
|
||||
|
||||
#define nm_utils_hash_to_array_with_buffer(hash, \
|
||||
out_len, \
|
||||
compare_func, \
|
||||
user_data, \
|
||||
array, \
|
||||
out_allocated_buffer) \
|
||||
nm_utils_hash_to_array_full((hash), \
|
||||
(out_len), \
|
||||
(compare_func), \
|
||||
(user_data), \
|
||||
(array), \
|
||||
G_N_ELEMENTS(array), \
|
||||
(out_allocated_buffer))
|
||||
|
||||
static inline const char **
|
||||
nm_strdict_get_keys(const GHashTable *hash, gboolean sorted, guint *out_length)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue