LmMessageNode: Expose attributes member
authorMykhailo Danylenko <risbea@gmail.com>
Sat, 19 Mar 2016 16:39:49 +0200
changeset 710 ea3137a59c2b
parent 709 bdcdb9b34992
child 711 f9e84b075afa
LmMessageNode: Expose attributes member * New typedef: LmMessageNodeAttribute
loudmouth/lm-message-node.c
loudmouth/lm-message-node.h
--- a/loudmouth/lm-message-node.c	Sat Mar 19 16:38:54 2016 +0200
+++ b/loudmouth/lm-message-node.c	Sat Mar 19 16:39:49 2016 +0200
@@ -28,19 +28,14 @@
 #include "lm-internals.h"
 #include "lm-message-node.h"
 
-typedef struct {
-    gchar *key;
-    gchar *value;
-} KeyValuePair;
-
 static void            message_node_free            (LmMessageNode    *node);
 static LmMessageNode * message_node_last_child      (LmMessageNode    *node);
 
 static void
 message_node_free (LmMessageNode *node)
 {
-    LmMessageNode *l;
-    GSList        *list;
+    LmMessageNode          *l;
+    LmMessageNodeAttribute *a;
 
     g_return_if_fail (node != NULL);
 
@@ -54,15 +49,16 @@
     g_free (node->name);
     g_free (node->value);
 
-    for (list = node->attributes; list; list = list->next) {
-        KeyValuePair *kvp = (KeyValuePair *) list->data;
+    for (a = node->attributes; a;) {
+        LmMessageNodeAttribute *next_a = a->next;
 
-        g_free (kvp->key);
-        g_free (kvp->value);
-        g_free (kvp);
+        g_free (a->name);
+        g_free (a->value);
+        g_free (a);
+
+        a = next_a;
     }
 
-    g_slist_free (node->attributes);
     g_free (node);
 }
 
@@ -219,7 +215,6 @@
         value = (const gchar *) va_arg (args, gpointer);
 
         lm_message_node_set_attribute (node, name, value);
-
     }
 
     va_end (args);
@@ -238,32 +233,29 @@
                                const gchar   *name,
                                const gchar   *value)
 {
-    gboolean  found = FALSE;
-    GSList   *l;
+    gboolean                found = FALSE;
+    LmMessageNodeAttribute *a;
 
     g_return_if_fail (node != NULL);
     g_return_if_fail (name != NULL);
     g_return_if_fail (value != NULL);
 
-    for (l = node->attributes; l; l = l->next) {
-        KeyValuePair *kvp = (KeyValuePair *) l->data;
-
-        if (strcmp (kvp->key, name) == 0) {
-            g_free (kvp->value);
-            kvp->value = g_strdup (value);
+    for (a = node->attributes; a; a = a->next) {
+        if (strcmp (a->name, name) == 0) {
+            g_free (a->value);
+            a->value = g_strdup (value);
             found = TRUE;
             break;
         }
     }
 
     if (!found) {
-        KeyValuePair *kvp;
+        a = g_new0 (LmMessageNodeAttribute, 1);
+        a->name = g_strdup (name);
+        a->value = g_strdup (value);
 
-        kvp = g_new0 (KeyValuePair, 1);
-        kvp->key = g_strdup (name);
-        kvp->value = g_strdup (value);
-
-        node->attributes = g_slist_prepend (node->attributes, kvp);
+        a->next = node->attributes;
+        node->attributes = a;
     }
 }
 
@@ -279,17 +271,16 @@
 const gchar *
 lm_message_node_get_attribute (LmMessageNode *node, const gchar *name)
 {
-    GSList      *l;
-    const gchar *ret_val = NULL;
+    LmMessageNodeAttribute *a;
+    const gchar            *ret_val = NULL;
 
     g_return_val_if_fail (node != NULL, NULL);
     g_return_val_if_fail (name != NULL, NULL);
 
-    for (l = node->attributes; l; l = l->next) {
-        KeyValuePair *kvp = (KeyValuePair *) l->data;
-
-        if (strcmp (kvp->key, name) == 0) {
-            ret_val = kvp->value;
+    for (a = node->attributes; a; a = a->next) {
+        if (strcmp (a->name, name) == 0) {
+            ret_val = a->value;
+            break;
         }
     }
 
@@ -442,9 +433,9 @@
 gchar *
 lm_message_node_to_string (LmMessageNode *node)
 {
-    GString       *ret;
-    GSList        *l;
-    LmMessageNode *child;
+    GString                *ret;
+    LmMessageNodeAttribute *a;
+    LmMessageNode          *child;
 
     g_return_val_if_fail (node != NULL, NULL);
 
@@ -455,21 +446,18 @@
     ret = g_string_new ("<");
     g_string_append (ret, node->name);
 
-    for (l = node->attributes; l; l = l->next) {
-        KeyValuePair *kvp = (KeyValuePair *) l->data;
-
+    for (a = node->attributes; a; a = a->next) {
         if (node->raw_mode == FALSE) {
             gchar *escaped;
 
-            escaped = g_markup_escape_text (kvp->value, -1);
+            escaped = g_markup_escape_text (a->value, -1);
             g_string_append_printf (ret, " %s=\"%s\"",
-                                    kvp->key, escaped);
+                                    a->name, escaped);
             g_free (escaped);
         } else {
             g_string_append_printf (ret, " %s=\"%s\"",
-                                    kvp->key, kvp->value);
+                                    a->name, a->value);
         }
-
     }
 
     g_string_append_c (ret, '>');
--- a/loudmouth/lm-message-node.h	Sat Mar 19 16:38:54 2016 +0200
+++ b/loudmouth/lm-message-node.h	Sat Mar 19 16:39:49 2016 +0200
@@ -28,6 +28,23 @@
 G_BEGIN_DECLS
 
 /**
+ * LmMessageNodeAttribute:
+ * @name: attribute name
+ * @value: attribute value
+ * @next: next attribute
+ *
+ * Linked list of message node attributes.
+ */
+typedef struct _LmMessageNodeAttribute LmMessageNodeAttribute;
+
+struct _LmMessageNodeAttribute {
+    gchar *name;
+    gchar *value;
+
+    LmMessageNodeAttribute *next;
+};
+
+/**
  * LmMessageNode:
  * @name: the name of the node
  * @value: value of the node, can be NULL
@@ -36,6 +53,7 @@
  * @prev: previous sibling
  * @parent: node parent
  * @children: pointing to first child
+ * @attributes: linked list of node attributes
  *
  * A struct representing a node in a message.
  */
@@ -51,8 +69,9 @@
     LmMessageNode     *parent;
     LmMessageNode     *children;
 
+    LmMessageNodeAttribute *attributes;
+
     /* < private > */
-    GSList     *attributes;
     gint        ref_count;
 };