changeset 17892:9cb771adbdea

Refactor msim_escape() & msim_unescape().
author Jeffrey Connelly <jaconnel@calpoly.edu>
date Wed, 20 Jun 2007 03:28:05 +0000
parents 23f57d36cb65
children 0d799da3b893
files libpurple/protocols/myspace/myspace.c libpurple/protocols/myspace/myspace.h
diffstat 2 files changed, 34 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/myspace/myspace.c	Wed Jun 20 03:17:46 2007 +0000
+++ b/libpurple/protocols/myspace/myspace.c	Wed Jun 20 03:28:05 2007 +0000
@@ -112,23 +112,42 @@
     return "myspace";
 }
 
+/* Replacement codes to be replaced with associated replacement text,
+ * used for protocol message escaping / unescaping. */
 static gchar* msim_replacement_code[] = { "/1", "/2", NULL };
 static gchar* msim_replacement_text[] = { "/", "\\", NULL };
 
 /**
- * Unescape a protocol message.
+ * Unescape or escape a protocol message.
  *
- * @return The unescaped message. Caller must g_free().
+ * @param msg The message to be unescaped or escaped. WILL BE FREED.
+ * @param escape TRUE to escape, FALSE to unescape.
+ *
+ * @return The unescaped or escaped message. Caller must g_free().
  */
-gchar *msim_unescape(const gchar *msg)
+gchar *msim_unescape_or_escape(gchar *msg, gboolean escape)
 {
-	/* TODO: make more elegant, refactor with msim_escape */
-	gchar *tmp, *ret;
+	gchar *tmp, *code, *text;
+	guint i;
+
+	/* Replace each code in msim_replacement_code with
+	 * corresponding entry in msim_replacement_text. */
+	for (i = 0; (code = msim_replacement_code[i])
+		   	&& (text = msim_replacement_text[i]); ++i)
+	{
+		if (escape)
+		{
+			tmp = str_replace(msg, text, code);
+		}
+		else
+		{
+			tmp = str_replace(msg, code, text);
+		}
+		g_free(msg);
+		msg = tmp;
+	}
 	
-	tmp = str_replace(msg, "/1", "/");
-	ret = str_replace(tmp, "/2", "\\");
-	g_free(tmp);
-	return ret;
+	return msg;
 }
 
 /**
@@ -138,14 +157,12 @@
  */
 gchar *msim_escape(const gchar *msg)
 {
-	/* TODO: make more elegant, refactor with msim_unescape */
-	gchar *tmp, *ret;
-	
-	tmp = str_replace(msg, "/", "/1");
-	ret = str_replace(tmp, "\\", "/2");
-	g_free(tmp);
+	return msim_unescape_or_escape(g_strdup(msg), TRUE);
+}
 
-	return ret;
+gchar *msim_unescape(const gchar *msg)
+{
+	return msim_unescape_or_escape(g_strdup(msg), FALSE);
 }
 
 /**
--- a/libpurple/protocols/myspace/myspace.h	Wed Jun 20 03:17:46 2007 +0000
+++ b/libpurple/protocols/myspace/myspace.h	Wed Jun 20 03:28:05 2007 +0000
@@ -141,6 +141,7 @@
 const gchar *msim_list_icon(PurpleAccount *acct, PurpleBuddy *buddy);
 
 /* TODO: move these three functions to message.c/h */
+gchar *msim_unescape_or_escape(gchar *msg, gboolean escape);
 gchar *msim_unescape(const gchar *msg);
 gchar *msim_escape(const gchar *msg);
 gchar *str_replace(const gchar *str, const gchar *old, const gchar *new);