nm-utils: add _nm_utils_format_variant_attributes_full()

This commit is contained in:
Beniamino Galvani
2018-06-18 18:46:52 +02:00
parent 634b2d1ce2
commit d7382fbe93
2 changed files with 53 additions and 33 deletions

View File

@@ -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); GPtrArray * _nm_utils_team_link_watchers_from_variant (GVariant *value);
GVariant * _nm_utils_team_link_watchers_to_variant (GPtrArray *link_watchers); 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 */ /* JSON to GValue conversion macros */
typedef struct { typedef struct {

View File

@@ -6156,44 +6156,21 @@ next:
return g_steal_pointer (&ht); return g_steal_pointer (&ht);
} }
/* void
* nm_utils_format_variant_attributes: _nm_utils_format_variant_attributes_full (GString *str,
* @attributes: (element-type utf8 GVariant): a #GHashTable mapping attribute names to #GVariant values const NMUtilsNamedValue *values,
* @attr_separator: the attribute separator character guint num_values,
* @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 attr_separator,
char key_value_separator) char key_value_separator)
{ {
GString *str = NULL;
GVariant *variant;
char sep = 0;
const char *name, *value; const char *name, *value;
GVariant *variant;
char *escaped; char *escaped;
char buf[64]; char buf[64];
gs_free NMUtilsNamedValue *values = NULL; char sep = 0;
guint i, len; guint i;
g_return_val_if_fail (attr_separator, NULL); for (i = 0; i < num_values; i++) {
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++) {
name = values[i].name; name = values[i].name;
variant = (GVariant *) values[i].value_ptr; variant = (GVariant *) values[i].value_ptr;
value = NULL; value = NULL;
@@ -6226,7 +6203,44 @@ nm_utils_format_variant_attributes (GHashTable *attributes,
sep = attr_separator; 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); return g_string_free (str, FALSE);
} }