diff options
author | Debarshi Ray <debarshir@gnome.org> | 2017-10-13 19:14:16 +0200 |
---|---|---|
committer | Debarshi Ray <debarshir@gnome.org> | 2017-10-17 16:45:25 +0200 |
commit | f184db2bff0618b99c4de3316082fe80439f124c (patch) | |
tree | c14f2c7f28ae35685bfe3f632e6c58ed8a0ec011 | |
parent | a34d02947c4f102e6d16b9d328941a4b2946c8e8 (diff) | |
download | librest-f184db2bff0618b99c4de3316082fe80439f124c.tar.gz |
xml-node: Define the order in which attributes & children are printed
The order in which GHashTable returns its key-value pairs is undefined.
Therefore the output of rest_xml_node_print can change based on the
GHashTable implementation. While not strictly necessary, it would be
nice to avoid that. Having a stable order, even if it is not
documented and depends on the current RestXmlNode code, is handy for
testing.
This was the main reason behind the tests/xml.c breakage.
https://bugzilla.gnome.org/show_bug.cgi?id=788960
-rw-r--r-- | rest/rest-xml-node.c | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/rest/rest-xml-node.c b/rest/rest-xml-node.c index d3a7c99..973ebcf 100644 --- a/rest/rest-xml-node.c +++ b/rest/rest-xml-node.c @@ -283,6 +283,9 @@ rest_xml_node_print (RestXmlNode *node) { GHashTableIter iter; gpointer key, value; + GList *attrs = NULL; + GList *children = NULL; + GList *l; GString *xml = g_string_new (NULL); RestXmlNode *n; @@ -291,13 +294,29 @@ rest_xml_node_print (RestXmlNode *node) g_hash_table_iter_init (&iter, node->attrs); while (g_hash_table_iter_next (&iter, &key, &value)) - g_string_append_printf (xml, " %s=\'%s\'", (char *)key, (char *)value); + { + char *attr = g_strdup_printf ("%s=\'%s\'", (char *)key, (char *)value); + attrs = g_list_prepend (attrs, attr); + } + + attrs = g_list_sort (attrs, (GCompareFunc) g_strcmp0); + for (l = attrs; l; l = l->next) + { + const char *attr = (const char *) l->data; + g_string_append_printf (xml, " %s", attr); + } g_string_append (xml, ">"); g_hash_table_iter_init (&iter, node->children); while (g_hash_table_iter_next (&iter, &key, &value)) + children = g_list_prepend (children, key); + + children = g_list_sort (children, (GCompareFunc) g_strcmp0); + for (l = children; l; l = l->next) { + const char *name = (const char *) l->data; + RestXmlNode *value = (RestXmlNode *) g_hash_table_lookup (node->children, name); char *child = rest_xml_node_print ((RestXmlNode *) value); g_string_append (xml, child); @@ -317,6 +336,8 @@ rest_xml_node_print (RestXmlNode *node) g_free (sibling); } + g_list_free_full (attrs, g_free); + g_list_free (children); return g_string_free (xml, FALSE); } |