[empathy] Create API for generic string parser



commit 6ea436c8b313974d94eb9fe97974edcf4f47e46d
Author: Xavier Claessens <xclaesse gmail com>
Date:   Sun Nov 1 11:04:00 2009 +0100

    Create API for generic string parser

 libempathy-gtk/empathy-theme-adium.c |   41 +++++++++++++++++++++++----------
 libempathy-gtk/empathy-ui-utils.c    |   14 +++++++++++
 libempathy-gtk/empathy-ui-utils.h    |   12 ++++++++++
 3 files changed, 54 insertions(+), 13 deletions(-)
---
diff --git a/libempathy-gtk/empathy-theme-adium.c b/libempathy-gtk/empathy-theme-adium.c
index a83b8da..014cd87 100644
--- a/libempathy-gtk/empathy-theme-adium.c
+++ b/libempathy-gtk/empathy-theme-adium.c
@@ -194,7 +194,8 @@ theme_adium_open_address_cb (GtkMenuItem *menuitem,
 static void
 theme_adium_parser_escape (GString *string,
 			   const gchar *text,
-			   gssize len)
+			   gssize len,
+			   gpointer user_data)
 {
 	gchar *escaped;
 
@@ -206,7 +207,8 @@ theme_adium_parser_escape (GString *string,
 static void
 theme_adium_parser_newline (GString *string,
 			    const gchar *text,
-			    gssize len)
+			    gssize len,
+			    gpointer user_data)
 {
 	gint i;
 	gint prev = 0;
@@ -218,18 +220,20 @@ theme_adium_parser_newline (GString *string,
 	/* Replace \n by <br/> */
 	for (i = 0; i < len && text[i] != '\0'; i++) {
 		if (text[i] == '\n') {
-			theme_adium_parser_escape (string, text + prev, i - prev);
+			empathy_string_parser_substr (string, text + prev,
+						      i - prev, user_data);
 			g_string_append (string, "<br/>");
 			prev = i + 1;
 		}
 	}
-	theme_adium_parser_escape (string, text + prev, i - prev);
+	empathy_string_parser_substr (string, text + prev, i - prev, user_data);
 }
 
 static void
 theme_adium_parser_smiley (GString *string,
 			   const gchar *text,
-			   gssize len)
+			   gssize len,
+			   gpointer user_data)
 {
 	gboolean  use_smileys = FALSE;
 	gint      last = 0;
@@ -251,8 +255,9 @@ theme_adium_parser_smiley (GString *string,
 			if (hit->start > last) {
 				/* Append the text between last smiley (or the
 				 * start of the message) and this smiley */
-				theme_adium_parser_newline (string, text + last,
-							    hit->start - last);
+				empathy_string_parser_substr (string, text + last,
+							      hit->start - last,
+							      user_data);
 			}
 
 			/* Replace smileys by a <img/> tag */
@@ -273,13 +278,14 @@ theme_adium_parser_smiley (GString *string,
 		g_object_unref (smiley_manager);
 	}
 
-	theme_adium_parser_newline (string, text + last, len - last);
+	empathy_string_parser_substr (string, text + last, len - last, user_data);
 }
 
 static void
 theme_adium_parser_url (GString *string,
 			const gchar *text,
-			gssize len)
+			gssize len,
+			gpointer user_data)
 {
 	GRegex     *uri_regex;
 	GMatchInfo *match_info;
@@ -300,8 +306,9 @@ theme_adium_parser_url (GString *string,
 			if (s > last) {
 				/* Append the text between last link (or the
 				 * start of the message) and this link */
-				theme_adium_parser_smiley (string, text + last,
-							   s - last);
+				empathy_string_parser_substr (string, text + last,
+							      s - last,
+							      user_data);
 			}
 
 			/* Append the link inside <a href=""></a> tag */
@@ -317,12 +324,20 @@ theme_adium_parser_url (GString *string,
 		} while (g_match_info_next (match_info, NULL));
 	}
 
-	theme_adium_parser_smiley (string, text + last, len - last);
+	empathy_string_parser_substr (string, text + last, len - last, user_data);
 
 	g_match_info_free (match_info);
 	g_regex_unref (uri_regex);
 }
 
+static EmpathyStringParser string_parsers[] = {
+	theme_adium_parser_url,
+	theme_adium_parser_smiley,
+	theme_adium_parser_newline,
+	theme_adium_parser_escape,
+	NULL,
+};
+
 static gchar *
 theme_adium_parse_body (const gchar *text)
 {
@@ -343,7 +358,7 @@ theme_adium_parse_body (const gchar *text)
 	 */
 
 	string = g_string_sized_new (strlen (text));
-	theme_adium_parser_url (string, text, -1);
+	empathy_string_parser_substr (string, text, -1, string_parsers);
 
 	return g_string_free (string, FALSE);
 }
diff --git a/libempathy-gtk/empathy-ui-utils.c b/libempathy-gtk/empathy-ui-utils.c
index 97fd95c..376d1f9 100644
--- a/libempathy-gtk/empathy-ui-utils.c
+++ b/libempathy-gtk/empathy-ui-utils.c
@@ -1569,3 +1569,17 @@ empathy_receive_file_with_file_chooser (EmpathyFTHandler *handler)
 
 	gtk_widget_show (widget);
 }
+
+void
+empathy_string_parser_substr (GString *string,
+			      const gchar *text,
+			      gssize len,
+			      EmpathyStringParser *parsers)
+{
+	if (parsers != NULL && parsers[0] != NULL) {
+		parsers[0] (string, text, len, parsers + 1);
+	} else {
+		g_string_append_len (string, text, len);
+	}
+}
+
diff --git a/libempathy-gtk/empathy-ui-utils.h b/libempathy-gtk/empathy-ui-utils.h
index 7bec088..58960c3 100644
--- a/libempathy-gtk/empathy-ui-utils.h
+++ b/libempathy-gtk/empathy-ui-utils.h
@@ -117,6 +117,18 @@ gchar *     empathy_make_absolute_url                   (const gchar *url);
 gchar *     empathy_make_absolute_url_len               (const gchar *url,
 							 guint len);
 
+/* String parser */
+typedef void (*EmpathyStringParser) (GString *string,
+				     const gchar *text,
+				     gssize len,
+				     gpointer user_data);
+
+void
+empathy_string_parser_substr (GString *string,
+			      const gchar *text,
+			      gssize len,
+			      EmpathyStringParser *parsers);
+
 G_END_DECLS
 
 #endif /*  __EMPATHY_UI_UTILS_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]