pidgin: 5097d288: Use one GString and pass it around inste...

markdoliner at pidgin.im markdoliner at pidgin.im
Fri Dec 12 21:45:56 EST 2008


-----------------------------------------------------------------
Revision: 5097d28816eecf123094d27602b14963619bbc8b
Ancestor: b777f3c2f3b9991830f31a5572d2bb0b030c2600
Author: markdoliner at pidgin.im
Date: 2008-12-13T02:41:12
Branch: im.pidgin.pidgin
URL: http://d.pidgin.im/viewmtn/revision/info/5097d28816eecf123094d27602b14963619bbc8b

Modified files:
        libpurple/protocols/myspace/markup.c

ChangeLog: 

Use one GString and pass it around instead of allocating and freeing a
bunch of smaller gchars when converting to and from html and myspace
im markup

-------------- next part --------------
============================================================
--- libpurple/protocols/myspace/markup.c	3a000c1bf671676ffd9e34201a396d4d7967eeaf
+++ libpurple/protocols/myspace/markup.c	854a43233ee85e63ed4e6d93c1edced418d48a33
@@ -76,8 +76,6 @@ static struct MSIM_EMOTICON
 	{ NULL, NULL }
 };
 
-
-
 /* Indexes of this array + 1 map HTML font size to scale of normal font size. *
  * Based on _point_sizes from libpurple/gtkimhtml.c
  *                                 1    2  3    4     5      6       7 */
@@ -100,7 +98,6 @@ static gdouble _font_scale[] = { .85, .9
  * in account options. */
 #define MSIM_DEFAULT_DPI                96
 
-
 /* round is part of C99, but sometimes is unavailable before then.
  * Based on http://forums.belution.com/en/cpp/000/050/13.shtml
  */
@@ -113,7 +110,6 @@ static double msim_round(double value)
 	}
 }
 
-
 /** Convert typographical font point size to HTML font size.
  * Based on libpurple/gtkimhtml.c */
 static guint
@@ -550,29 +546,26 @@ html_tag_to_msim_markup(MsimSession *ses
  *
  * @return An HTML string. Caller frees.
  */
-static gchar *
-msim_convert_xmlnode(MsimSession *session, xmlnode *root, MSIM_XMLNODE_CONVERT f, int nodes_processed)
+static void
+msim_convert_xmlnode(MsimSession *session, GString *out, xmlnode *root, MSIM_XMLNODE_CONVERT f, int nodes_processed)
 {
 	xmlnode *node;
 	gchar *begin, *inner, *end;
-	GString *final;
 	int descended = nodes_processed;
 
-	if (!root || !root->name) {
-		return g_strdup("");
-	}
+	if (!root || !root->name)
+		return;
 
 	purple_debug_info("msim", "msim_convert_xmlnode: got root=%s\n",
 			root->name);
 
 	begin = inner = end = NULL;
 
-	final = g_string_new("");
-
 	if (descended == 0) /* We've not formatted this yet.. :) */
 		descended = f(session, root, &begin, &end); /* Get the value that our format function has already descended for us */
 
-	g_string_append(final, begin);
+	g_string_append(out, begin);
+	g_free(begin);
 
 	/* Loop over all child nodes. */
 	for (node = root->child; node != NULL; node = node->next) {
@@ -583,45 +576,29 @@ msim_convert_xmlnode(MsimSession *sessio
 
 			case XMLNODE_TYPE_TAG:
 				/* A tag or tag with attributes. Recursively descend. */
-				inner = msim_convert_xmlnode(session, node, f, descended);
-				g_return_val_if_fail(inner != NULL, NULL);
+				msim_convert_xmlnode(session, out, node, f, descended);
 
 				purple_debug_info("msim", " ** node name=%s\n",
-						(node && node->name) ? node->name : "(NULL)");
+						node->name ? node->name : "(NULL)");
 				break;
 
 			case XMLNODE_TYPE_DATA:
 				/* Literal text. */
-				inner = g_strndup(node->data, node->data_sz);
-				purple_debug_info("msim", " ** node data=%s\n",
-						inner ? inner : "(NULL)");
+				g_string_append_len(out, node->data, node->data_sz);
 				break;
 
 			default:
-				purple_debug_info("msim",
-						"msim_convert_xmlnode: strange node\n");
+				purple_debug_warning("msim",
+						"msim_convert_xmlnode: unknown node type\n");
 		}
-
-		if (inner) {
-			g_string_append(final, inner);
-			g_free(inner);
-			inner = NULL;
-		}
 	}
 
 	/* TODO: Note that msim counts each piece of text enclosed by <f> as
 	 * a paragraph and will display each on its own line. You actually have
 	 * to _nest_ <f> tags to intersperse different text in one paragraph!
 	 * Comment out this line below to see. */
-	g_string_append(final, end);
-
-	g_free(begin);
+	g_string_append(out, end);
 	g_free(end);
-
-	purple_debug_info("msim", "msim_markup_xmlnode_to_gtkhtml: RETURNING %s\n",
-			(final && final->str) ? final->str : "(NULL)");
-
-	return g_string_free(final, FALSE);
 }
 
 /** Convert XML to something based on MSIM_XMLNODE_CONVERT. */
@@ -629,7 +606,7 @@ msim_convert_xml(MsimSession *session, c
 msim_convert_xml(MsimSession *session, const gchar *raw, MSIM_XMLNODE_CONVERT f)
 {
 	xmlnode *root;
-	gchar *str;
+	GString *str;
 	gchar *enclosed_raw;
 
 	g_return_val_if_fail(raw != NULL, NULL);
@@ -640,7 +617,7 @@ msim_convert_xml(MsimSession *session, c
 	root = xmlnode_from_str(enclosed_raw, -1);
 
 	if (!root) {
-		purple_debug_info("msim", "msim_markup_to_html: couldn't parse "
+		purple_debug_warning("msim", "msim_markup_to_html: couldn't parse "
 				"%s as XML, returning raw: %s\n", enclosed_raw, raw);
 		/* TODO: msim_unrecognized */
 		g_free(enclosed_raw);
@@ -649,13 +626,13 @@ msim_convert_xml(MsimSession *session, c
 
 	g_free(enclosed_raw);
 
-	str = msim_convert_xmlnode(session, root, f, 0);
-	g_return_val_if_fail(str != NULL, NULL);
-	purple_debug_info("msim", "msim_markup_to_html: returning %s\n", str);
+	str = g_string_new(NULL);
+	msim_convert_xmlnode(session, str, root, f, 0);
+	xmlnode_free(root);
 
-	xmlnode_free(root);
+	purple_debug_info("msim", "msim_markup_to_html: returning %s\n", str->str);
 
-	return str;
+	return g_string_free(str, FALSE);
 }
 
 /** Convert plaintext smileys to <i> markup tags.


More information about the Commits mailing list