libnmm,shared: extract and move nm_utils_strdict_to_variant_ass() to shared

This is a helper function that converts a string dictionary to an "a{ss}"
GVariant. It is generally useful, and should be independent from the
caller.
This commit is contained in:
Thomas Haller 2020-03-26 16:55:18 +01:00
parent 306414b93d
commit 55a058aeef
3 changed files with 59 additions and 47 deletions

View file

@ -802,53 +802,7 @@ _nm_utils_hash_values_to_slist (GHashTable *hash)
static GVariant *
_nm_utils_strdict_to_dbus (const GValue *prop_value)
{
GHashTable *hash;
GHashTableIter iter;
const char *key, *value;
GVariantBuilder builder;
guint i, len;
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}"));
hash = g_value_get_boxed (prop_value);
if (!hash)
goto out;
len = g_hash_table_size (hash);
if (!len)
goto out;
g_hash_table_iter_init (&iter, hash);
if (!g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value))
nm_assert_not_reached ();
if (len == 1)
g_variant_builder_add (&builder, "{ss}", key, value);
else {
gs_free NMUtilsNamedValue *idx_free = NULL;
NMUtilsNamedValue *idx;
if (len > 300 / sizeof (NMUtilsNamedValue)) {
idx_free = g_new (NMUtilsNamedValue, len);
idx = idx_free;
} else
idx = g_alloca (sizeof (NMUtilsNamedValue) * len);
i = 0;
do {
idx[i].name = key;
idx[i].value_str = value;
i++;
} while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value));
nm_assert (i == len);
nm_utils_named_value_list_sort (idx, len, NULL, NULL);
for (i = 0; i < len; i++)
g_variant_builder_add (&builder, "{ss}", idx[i].name, idx[i].value_str);
}
out:
return g_variant_builder_end (&builder);
return nm_utils_strdict_to_variant_ass (g_value_get_boxed (prop_value));
}
void

View file

@ -444,6 +444,62 @@ nm_utils_gbytes_to_variant_ay (GBytes *bytes)
/*****************************************************************************/
/* Convert a hash table with "char *" keys and values to an "a{ss}" GVariant.
* The keys will be sorted asciibetically.
* Returns a floating reference.
*/
GVariant *
nm_utils_strdict_to_variant_ass (GHashTable *strdict)
{
GHashTableIter iter;
const char *key, *value;
GVariantBuilder builder;
guint i, len;
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}"));
if (!strdict)
goto out;
len = g_hash_table_size (strdict);
if (!len)
goto out;
g_hash_table_iter_init (&iter, strdict);
if (!g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value))
nm_assert_not_reached ();
if (len == 1)
g_variant_builder_add (&builder, "{ss}", key, value);
else {
gs_free NMUtilsNamedValue *idx_free = NULL;
NMUtilsNamedValue *idx;
if (len > 300 / sizeof (NMUtilsNamedValue)) {
idx_free = g_new (NMUtilsNamedValue, len);
idx = idx_free;
} else
idx = g_alloca (sizeof (NMUtilsNamedValue) * len);
i = 0;
do {
idx[i].name = key;
idx[i].value_str = value;
i++;
} while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value));
nm_assert (i == len);
nm_utils_named_value_list_sort (idx, len, NULL, NULL);
for (i = 0; i < len; i++)
g_variant_builder_add (&builder, "{ss}", idx[i].name, idx[i].value_str);
}
out:
return g_variant_builder_end (&builder);
}
/*****************************************************************************/
/**
* nm_strquote:
* @buf: the output buffer of where to write the quoted @str argument.

View file

@ -393,6 +393,8 @@ gboolean nm_utils_gbytes_equal_mem (GBytes *bytes,
GVariant *nm_utils_gbytes_to_variant_ay (GBytes *bytes);
GVariant *nm_utils_strdict_to_variant_ass (GHashTable *strdict);
/*****************************************************************************/
GVariant *nm_utils_gvariant_vardict_filter (GVariant *src,