From d7382fbe93c60dfe133e14f6289eb82c1303f76b Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Mon, 18 Jun 2018 18:46:52 +0200 Subject: [PATCH] nm-utils: add _nm_utils_format_variant_attributes_full() --- libnm-core/nm-utils-private.h | 6 +++ libnm-core/nm-utils.c | 80 ++++++++++++++++++++--------------- 2 files changed, 53 insertions(+), 33 deletions(-) diff --git a/libnm-core/nm-utils-private.h b/libnm-core/nm-utils-private.h index 5d0f83868..58539cb30 100644 --- a/libnm-core/nm-utils-private.h +++ b/libnm-core/nm-utils-private.h @@ -83,6 +83,12 @@ char * _nm_utils_hwaddr_canonical_or_invalid (const char *mac, gssize lengt GPtrArray * _nm_utils_team_link_watchers_from_variant (GVariant *value); GVariant * _nm_utils_team_link_watchers_to_variant (GPtrArray *link_watchers); +void _nm_utils_format_variant_attributes_full (GString *str, + const NMUtilsNamedValue *values, + guint num_values, + char attr_separator, + char key_value_separator); + /* JSON to GValue conversion macros */ typedef struct { diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index 62da974f1..9247d7f8a 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -6156,44 +6156,21 @@ next: return g_steal_pointer (&ht); } -/* - * nm_utils_format_variant_attributes: - * @attributes: (element-type utf8 GVariant): a #GHashTable mapping attribute names to #GVariant values - * @attr_separator: the attribute separator character - * @key_value_separator: character separating key and values - * - * Format attributes to a string. - * - * Returns: (transfer full): the string representing attributes, or %NULL - * in case there are no attributes - * - * Since: 1.8 - */ -char * -nm_utils_format_variant_attributes (GHashTable *attributes, - char attr_separator, - char key_value_separator) +void +_nm_utils_format_variant_attributes_full (GString *str, + const NMUtilsNamedValue *values, + guint num_values, + char attr_separator, + char key_value_separator) { - GString *str = NULL; - GVariant *variant; - char sep = 0; const char *name, *value; + GVariant *variant; char *escaped; char buf[64]; - gs_free NMUtilsNamedValue *values = NULL; - guint i, len; + char sep = 0; + guint i; - g_return_val_if_fail (attr_separator, NULL); - g_return_val_if_fail (key_value_separator, NULL); - - if (!attributes || !g_hash_table_size (attributes)) - return NULL; - - values = nm_utils_named_values_from_str_dict (attributes, &len); - - str = g_string_new (""); - - for (i = 0; i < len; i++) { + for (i = 0; i < num_values; i++) { name = values[i].name; variant = (GVariant *) values[i].value_ptr; value = NULL; @@ -6226,7 +6203,44 @@ nm_utils_format_variant_attributes (GHashTable *attributes, sep = attr_separator; } +} +/* + * nm_utils_format_variant_attributes: + * @attributes: (element-type utf8 GVariant): a #GHashTable mapping attribute names to #GVariant values + * @attr_separator: the attribute separator character + * @key_value_separator: character separating key and values + * + * Format attributes to a string. + * + * Returns: (transfer full): the string representing attributes, or %NULL + * in case there are no attributes + * + * Since: 1.8 + */ +char * +nm_utils_format_variant_attributes (GHashTable *attributes, + char attr_separator, + char key_value_separator) +{ + GString *str = NULL; + gs_free NMUtilsNamedValue *values = NULL; + guint len; + + g_return_val_if_fail (attr_separator, NULL); + g_return_val_if_fail (key_value_separator, NULL); + + if (!attributes || !g_hash_table_size (attributes)) + return NULL; + + values = nm_utils_named_values_from_str_dict (attributes, &len); + + str = g_string_new (""); + _nm_utils_format_variant_attributes_full (str, + values, + len, + attr_separator, + key_value_separator); return g_string_free (str, FALSE); }