pidgin: e4c44c70: Don't put stuff into a linked list only ...

markdoliner at pidgin.im markdoliner at pidgin.im
Wed Aug 19 22:22:29 EDT 2009


-----------------------------------------------------------------
Revision: e4c44c708110388bc6b4646a63b5749285e0e820
Ancestor: 68bb82df03e74d8e33d5c334880a9b5bbab6a5d8
Author: markdoliner at pidgin.im
Date: 2009-08-20T02:15:00
Branch: im.pidgin.pidgin
URL: http://d.pidgin.im/viewmtn/revision/info/e4c44c708110388bc6b4646a63b5749285e0e820

Modified files:
        libpurple/protocols/yahoo/util.c
        libpurple/tests/test_yahoo_util.c

ChangeLog: 

Don't put stuff into a linked list only to iterate through the linked list
immediately afterward.  Instead, just deal with the three items on the
fly.  So much simpler.

-------------- next part --------------
============================================================
--- libpurple/protocols/yahoo/util.c	3e9effc420dcda9a622288613990ebd05c3cd323
+++ libpurple/protocols/yahoo/util.c	bf5b19ca42fdf5f2c96840c18389917aec47e23a
@@ -669,25 +669,8 @@ static const gint _point_sizes [] = { 8,
 #define POINT_SIZE(x) (_point_sizes [MIN ((x > 0 ? x : 1), MAX_FONT_SIZE) - 1])
 static const gint _point_sizes [] = { 8, 10, 12, 14, 20, 30, 40 };
 
-enum fontattr_type
-{
-	FATYPE_SIZE,
-	FATYPE_COLOR,
-	FATYPE_FACE
-};
-
 typedef struct
 {
-	enum fontattr_type type;
-	union {
-		int size;
-		char *color;
-		char *face;
-	} u;
-} fontattr;
-
-typedef struct
-{
 	gboolean bold;
 	gboolean italic;
 	gboolean underline;
@@ -697,15 +680,6 @@ typedef struct
 	char *font_color;
 } CurrentMsgState;
 
-static void fontattr_free(fontattr *f)
-{
-	if (f->type == FATYPE_COLOR)
-		g_free(f->u.color);
-	else if (f->type == FATYPE_FACE)
-		g_free(f->u.face);
-	g_free(f);
-}
-
 static void yahoo_htc_list_cleanup(GSList *l)
 {
 	while (l != NULL) {
@@ -721,80 +695,38 @@ static void parse_font_tag(const char *s
 	const char *end;
 	GData *attributes;
 	const char *attribute;
-	GSList *fontattrs = NULL;
 	gboolean needendtag;
-	fontattr *f;
 	GString *tmp;
 
 	purple_markup_find_tag(tag_name, tag, &start, &end, &attributes);
 
-	attribute = g_datalist_get_data(&attributes, "face");
-	if (attribute != NULL) {
-		f = g_new(fontattr, 1);
-		f->type = FATYPE_FACE;
-		f->u.face = g_strdup(attribute);
-		fontattrs = g_slist_prepend(fontattrs, f);
-	}
+	needendtag = FALSE;
+	tmp = g_string_new(NULL);
 
-	attribute = g_datalist_get_data(&attributes, "size");
+	attribute = g_datalist_get_data(&attributes, "color");
 	if (attribute != NULL) {
-		f = g_new(fontattr, 1);
-		f->type = FATYPE_SIZE;
-		f->u.size = POINT_SIZE(strtol(attribute, NULL, 10));
-		fontattrs = g_slist_prepend(fontattrs, f);
+		g_string_append(tmp, *colors ? (*colors)->data : "\033[#000000m");
+		g_string_append_printf(dest, "\033[%sm", attribute);
+		*colors = g_slist_prepend(*colors,
+				g_strdup_printf("\033[%sm", attribute));
 	}
 
-	attribute = g_datalist_get_data(&attributes, "color");
+	attribute = g_datalist_get_data(&attributes, "face");
 	if (attribute != NULL) {
-		f = g_new(fontattr, 1);
-		f->type = FATYPE_COLOR;
-		f->u.color = g_strdup(attribute);
-		fontattrs = g_slist_prepend(fontattrs, f);
+		needendtag = TRUE;
+		g_string_append(dest, "<font ");
+		g_string_append_printf(dest, "face=\"%s\" ", attribute);
 	}
 
-	g_datalist_clear(&attributes);
+	attribute = g_datalist_get_data(&attributes, "size");
+	if (attribute != NULL) {
+		if (!needendtag) {
+			needendtag = TRUE;
+			g_string_append(dest, "<font ");
+		}
 
-	if (fontattrs == NULL)
-		/* No recognized attributes in the font tag.  Nothing to do. */
-		return;
-
-	needendtag = FALSE;
-	tmp = g_string_new(NULL);
-
-	while (fontattrs != NULL) {
-		f = fontattrs->data;
-		fontattrs = g_slist_delete_link(fontattrs, fontattrs);
-
-		switch (f->type) {
-		case FATYPE_COLOR:
-			if (needendtag) {
-				g_string_append(tmp, "</font>");
-				dest->str[dest->len-1] = '>';
-			}
-
-			g_string_append(tmp, *colors ? (*colors)->data : "\033[#000000m");
-			g_string_append_printf(dest, "\033[%sm", f->u.color);
-			*colors = g_slist_prepend(*colors,
-					g_strdup_printf("\033[%sm", f->u.color));
-			break;
-		case FATYPE_FACE:
-			if (!needendtag) {
-				needendtag = TRUE;
-				g_string_append(dest, "<font ");
-			}
-
-			g_string_append_printf(dest, "face=\"%s\" ", f->u.face);
-			break;
-		case FATYPE_SIZE:
-			if (!needendtag) {
-				needendtag = TRUE;
-				g_string_append(dest, "<font ");
-			}
-
-			g_string_append_printf(dest, "size=\"%d\" ", f->u.size);
-			break;
-		}
-		fontattr_free(f);
+		g_string_append_printf(dest, "size=\"%d\" ",
+				POINT_SIZE(strtol(attribute, NULL, 10)));
 	}
 
 	if (needendtag) {
@@ -805,12 +737,21 @@ static void parse_font_tag(const char *s
 		*tags = g_slist_prepend(*tags, tmp->str);
 		g_string_free(tmp, FALSE);
 	}
+
+	g_datalist_clear(&attributes);
 }
 
 char *yahoo_html_to_codes(const char *src)
 {
 	GSList *colors = NULL;
+
+	/**
+	 * A stack of char*s where each char* is the string that should be
+	 * appended to dest in order to close all the tags that were opened
+	 * by a <font> tag.
+	 */
 	GSList *tags = NULL;
+
 	size_t src_len;
 	int i, j;
 	GString *dest;
============================================================
--- libpurple/tests/test_yahoo_util.c	b13a590fb74c5453f4430e89a523af35a407371c
+++ libpurple/tests/test_yahoo_util.c	90d3df4d677945e76b4040312778d4fbc390ca72
@@ -127,16 +127,6 @@ START_TEST(test_html_to_codes)
 	assert_string_equal_free("plain &",
 			yahoo_html_to_codes("plain &amp;"));
 
-	/* link */
-	assert_string_equal_free("http://pidgin.im/",
-			yahoo_html_to_codes("<A HREF=\"http://pidgin.im/\">http://pidgin.im/</A>"));
-	assert_string_equal_free("mark at example.com",
-			yahoo_html_to_codes("<A HREF=\"mailto:mark at example.com\">mark at example.com</A>"));
-#if 0
-	assert_string_equal_free("http://pidgin.im/",
-			yahoo_html_to_codes("<A HREF=\"http://pidgin.im/\">Pidgin</A>"));
-#endif
-
 	/* bold/italic/underline */
 	assert_string_equal_free("\x1B[1mbold\x1B[x1m",
 			yahoo_html_to_codes("<b>bold</b>"));
@@ -153,6 +143,24 @@ START_TEST(test_html_to_codes)
 	assert_string_equal_free("\x1B[1mbold \x1B[2mbolditalic\x1B[x2m\x1B[x1m\x1B[2m \x1B[4mitalicunderline\x1B[x4m\x1B[x2m",
 			yahoo_html_to_codes("<b>bold <i>bolditalic</i></b><i> <u>italicunderline</u></i>"));
 
+	/* link */
+	assert_string_equal_free("http://pidgin.im/",
+			yahoo_html_to_codes("<A HREF=\"http://pidgin.im/\">http://pidgin.im/</A>"));
+	assert_string_equal_free("mark at example.com",
+			yahoo_html_to_codes("<A HREF=\"mailto:mark at example.com\">mark at example.com</A>"));
+#if 0
+	assert_string_equal_free("http://pidgin.im/",
+			yahoo_html_to_codes("<A HREF=\"http://pidgin.im/\">Pidgin</A>"));
+#endif
+
+	/* font nothing */
+	assert_string_equal_free("nothing",
+			yahoo_html_to_codes("<font>nothing</font>"));
+
+	/* font color */
+	assert_string_equal_free("\x1B[#E71414mred\x1B[#000000m",
+			yahoo_html_to_codes("<font color=\"#E71414\">red</font>"));
+
 	/* font size */
 	assert_string_equal_free("<font size=\"10\">test</font>",
 			yahoo_html_to_codes("<font size=\"2\">test</font>"));


More information about the Commits mailing list