changeset 17301:641c96277fa6

Add msim_msg_insert_before(), needed since some elements must be in a certain order.
author Jeffrey Connelly <jaconnel@calpoly.edu>
date Mon, 04 Jun 2007 03:07:42 +0000
parents 176b1ba4bafe
children d69bcd74c240
files libpurple/protocols/myspace/message.c libpurple/protocols/myspace/message.h
diffstat 2 files changed, 67 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/myspace/message.c	Mon Jun 04 02:31:02 2007 +0000
+++ b/libpurple/protocols/myspace/message.c	Mon Jun 04 03:07:42 2007 +0000
@@ -26,6 +26,7 @@
 static void msim_msg_debug_string_element(gpointer data, gpointer user_data);
 static gchar *msim_msg_pack_using(MsimMessage *msg, GFunc gf, gchar *sep, gchar *begin, gchar *end);
 static gchar *msim_msg_element_pack(MsimMessageElement *elem);
+static GList *msim_msg_get_node(MsimMessage *msg, gchar *name);
 
 /** Create a new MsimMessage. */
 MsimMessage *msim_msg_new(void)
@@ -249,6 +250,23 @@
 	return success;
 }
 
+/** Create a new MsimMessageElement * - must be g_free()'d. 
+ *
+ * For internal use; users probably want msim_msg_append() or msim_msg_insert_before(). 
+ */
+static MsimMessageElement *msim_msg_element_new(gchar *name, MsimMessageType type, gpointer data)
+{
+	MsimMessageElement *elem;
+
+	elem = g_new0(MsimMessageElement, 1);
+
+	elem->name = name;
+	elem->type = type;
+	elem->data = data;
+
+	return elem;
+}
+
 
 /** Append a new element to a message. 
  *
@@ -279,15 +297,26 @@
  * */
 MsimMessage *msim_msg_append(MsimMessage *msg, gchar *name, MsimMessageType type, gpointer data)
 {
-	MsimMessageElement *elem;
-
-	elem = g_new0(MsimMessageElement, 1);
+	return g_list_append(msg, msim_msg_element_new(name, type, data));
+}
 
-	elem->name = name;
-	elem->type = type;
-	elem->data = data;
+/** Insert a new element into a message, before the given element name.
+ *
+ * @param name_before Name of the element to insert the new element before. If 
+ * 					  could not be found, new element will be inserted at end.
+ *
+ * See msim_msg_append() for usage of other parameters, and an important note about return value.
+ */
+MsimMessage *msim_msg_insert_before(MsimMessage *msg, gchar *name_before, gchar *name, MsimMessageType type, gpointer data)
+{
+	MsimMessageElement *new_elem;
+	GList *node_before;
 
-	return g_list_append(msg, elem);
+	new_elem = msim_msg_element_new(name, type, data);	
+
+	node_before = msim_msg_get_node(msg, name_before);
+
+	return g_list_insert_before(msg, node_before, new_elem);
 }
 
 /** Pack a string using the given GFunc and seperator.
@@ -657,6 +686,30 @@
     return table;
 }
 
+/** Search for and return the node in msg, matching name, or NULL. 
+ * For internal use - users probably want to use msim_msg_get() to
+ * access the MsimMessageElement *, instead of the GList * container.
+ */
+static GList *msim_msg_get_node(MsimMessage *msg, gchar *name)
+{
+	GList *i;
+
+	/* Linear search for the given name. O(n) but n is small. */
+	for (i = g_list_first(msg); i != NULL; i = g_list_next(i))
+	{
+		MsimMessageElement *elem;
+
+		elem = i->data;
+		g_return_val_if_fail(elem != NULL, NULL);
+
+		if (strcmp(elem->name, name) == 0)
+			return i;
+	}
+	return NULL;
+}
+
+
+
 /** Return the first MsimMessageElement * with given name in the MsimMessage *. 
  *
  * @param name Name to search for.
@@ -669,20 +722,13 @@
  */
 MsimMessageElement *msim_msg_get(MsimMessage *msg, gchar *name)
 {
-	GList *i;
-
-	/* Linear search for the given name. O(n) but n is small. */
-	for (i = g_list_first(msg); i != NULL; i = g_list_next(i))
-	{
-		MsimMessageElement *elem;
+	GList *node;
 
-		elem = i->data;
-		g_return_val_if_fail(elem != NULL, NULL);
-
-		if (strcmp(elem->name, name) == 0)
-			return elem;
-	}
-	return NULL;
+	node = msim_msg_get_node(msg, name);
+	if (node)
+		return (MsimMessageElement *)node->data;
+	else
+		return NULL;
 }
 
 /** Return the data of an element of a given name, as a string.
--- a/libpurple/protocols/myspace/message.h	Mon Jun 04 02:31:02 2007 +0000
+++ b/libpurple/protocols/myspace/message.h	Mon Jun 04 03:07:42 2007 +0000
@@ -48,6 +48,7 @@
 MsimMessage *msim_msg_clone(MsimMessage *old);
 void msim_msg_free(MsimMessage *msg);
 MsimMessage *msim_msg_append(MsimMessage *msg, gchar *name, MsimMessageType type, gpointer data);
+MsimMessage *msim_msg_insert_before(MsimMessage *msg, gchar *name_before, gchar *name, MsimMessageType type, gpointer data);
 void msim_msg_dump(char *fmt_string, MsimMessage *msg);
 gchar *msim_msg_pack(MsimMessage *msg);