/pidgin/main: cbeb76707f12: Better implementation of purple_esca...

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Fri Nov 23 11:30:45 EST 2012


Changeset: cbeb76707f125c801cd000249ab96eb3863d1c97
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2012-11-23 17:30 +0100
Branch:	 default
URL: http://hg.pidgin.im/pidgin/main/rev/cbeb76707f12

Description:

Better implementation of purple_escape_js

diffstat:

 libpurple/Makefile.am |   4 +++-
 libpurple/core.c      |   6 ++++--
 libpurple/util.c      |  39 ++++++++++++++++++++-------------------
 pidgin/gtkdebug.c     |   2 +-
 4 files changed, 28 insertions(+), 23 deletions(-)

diffs (127 lines):

diff --git a/libpurple/Makefile.am b/libpurple/Makefile.am
--- a/libpurple/Makefile.am
+++ b/libpurple/Makefile.am
@@ -313,6 +313,7 @@ libpurple_la_LIBADD = \
 	$(GSTVIDEO_LIBS) \
 	$(GSTINTERFACES_LIBS) \
 	$(IDN_LIBS) \
+	$(JSON_LIBS) \
 	ciphers/libpurple-ciphers.la \
 	-lm
 
@@ -330,7 +331,8 @@ AM_CPPFLAGS = \
 	$(GSTVIDEO_CFLAGS) \
 	$(GSTINTERFACES_CFLAGS) \
 	$(IDN_CFLAGS) \
-	$(NETWORKMANAGER_CFLAGS)
+	$(NETWORKMANAGER_CFLAGS) \
+	$(JSON_CFLAGS)
 
 # INSTALL_SSL_CERTIFICATES is true when SSL_CERTIFICATES_DIR is empty.
 # We want to use SSL_CERTIFICATES_DIR when it's not empty.
diff --git a/libpurple/core.c b/libpurple/core.c
--- a/libpurple/core.c
+++ b/libpurple/core.c
@@ -261,9 +261,11 @@ purple_core_quit(void)
 #endif
 
 	purple_cmds_uninit();
-	/* Everything after util_uninit cannot try to write things to the confdir */
+	purple_log_uninit();
+	/* Everything after util_uninit cannot try to write things to the
+	 * confdir nor use purple_escape_js
+	 */
 	purple_util_uninit();
-	purple_log_uninit();
 
 	purple_signals_uninit();
 
diff --git a/libpurple/util.c b/libpurple/util.c
--- a/libpurple/util.c
+++ b/libpurple/util.c
@@ -32,6 +32,8 @@
 #include "prefs.h"
 #include "util.h"
 
+#include <json-glib/json-glib.h>
+
 struct _PurpleMenuAction
 {
 	char *label;
@@ -43,6 +45,8 @@ struct _PurpleMenuAction
 static char *custom_user_dir = NULL;
 static char *user_dir = NULL;
 
+static JsonNode *escape_js_node = NULL;
+static JsonGenerator *escape_js_gen = NULL;
 
 PurpleMenuAction *
 purple_menu_action_new(const char *label, PurpleCallback callback, gpointer data,
@@ -124,8 +128,8 @@ void purple_menu_action_set_children(Pur
 void
 purple_util_init(void)
 {
-	/* This does nothing right now.  It exists for symmetry with
-	 * purple_util_uninit() and forwards compatibility. */
+	escape_js_node = json_node_new(JSON_NODE_VALUE);
+	escape_js_gen = json_generator_new();
 }
 
 void
@@ -138,6 +142,12 @@ purple_util_uninit(void)
 
 	g_free(user_dir);
 	user_dir = NULL;
+
+	json_node_free(escape_js_node);
+	escape_js_node = NULL;
+
+	g_object_unref(escape_js_gen);
+	escape_js_gen = NULL;
 }
 
 /**************************************************************************
@@ -4694,23 +4704,14 @@ purple_escape_filename(const char *str)
 
 gchar * purple_escape_js(const gchar *str)
 {
-	gchar *tmp, *esc;
-
-	esc = tmp = purple_utf8_try_convert(str);
-
-	esc = purple_strreplace(esc, "\\", "\\\\");
-	g_free(tmp); tmp = esc;
-
-	esc = purple_strreplace(esc, "'", "\\'");
-	g_free(tmp); tmp = esc;
-
-	esc = purple_strreplace(esc, "\n", "\\n");
-	g_free(tmp); tmp = esc;
-
-	esc = purple_strreplace(esc, "\r", "");
-	g_free(tmp); tmp = esc;
-
-	return esc;
+	gchar *escaped;
+
+	json_node_set_string(escape_js_node, str);
+	json_generator_set_root(escape_js_gen, escape_js_node);
+	escaped = json_generator_to_data(escape_js_gen, NULL);
+	json_node_set_boolean(escape_js_node, FALSE);
+
+	return escaped;
 }
 
 void purple_restore_default_signal_handlers(void)
diff --git a/pidgin/gtkdebug.c b/pidgin/gtkdebug.c
--- a/pidgin/gtkdebug.c
+++ b/pidgin/gtkdebug.c
@@ -1046,7 +1046,7 @@ pidgin_debug_print(PurpleDebugLevel leve
 
 	esc_s = purple_escape_js(arg_s);
 
-	js = g_strdup_printf("append(%d, '%s', '%s', '%s');",
+	js = g_strdup_printf("append(%d, '%s', '%s', %s);",
 		level, mdate, category ? category : "", esc_s);
 	g_free(esc_s);
 



More information about the Commits mailing list