comparison libpurple/protocols/myspace/myspace.c @ 17649: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
comparison
equal deleted inserted replaced
17648:23f57d36cb65 17649:9cb771adbdea
110 /* Use a MySpace icon submitted by hbons at 110 /* Use a MySpace icon submitted by hbons at
111 * http://developer.pidgin.im/wiki/MySpaceIM. */ 111 * http://developer.pidgin.im/wiki/MySpaceIM. */
112 return "myspace"; 112 return "myspace";
113 } 113 }
114 114
115 /* Replacement codes to be replaced with associated replacement text,
116 * used for protocol message escaping / unescaping. */
115 static gchar* msim_replacement_code[] = { "/1", "/2", NULL }; 117 static gchar* msim_replacement_code[] = { "/1", "/2", NULL };
116 static gchar* msim_replacement_text[] = { "/", "\\", NULL }; 118 static gchar* msim_replacement_text[] = { "/", "\\", NULL };
117 119
118 /** 120 /**
119 * Unescape a protocol message. 121 * Unescape or escape a protocol message.
120 * 122 *
121 * @return The unescaped message. Caller must g_free(). 123 * @param msg The message to be unescaped or escaped. WILL BE FREED.
122 */ 124 * @param escape TRUE to escape, FALSE to unescape.
123 gchar *msim_unescape(const gchar *msg) 125 *
124 { 126 * @return The unescaped or escaped message. Caller must g_free().
125 /* TODO: make more elegant, refactor with msim_escape */ 127 */
126 gchar *tmp, *ret; 128 gchar *msim_unescape_or_escape(gchar *msg, gboolean escape)
129 {
130 gchar *tmp, *code, *text;
131 guint i;
132
133 /* Replace each code in msim_replacement_code with
134 * corresponding entry in msim_replacement_text. */
135 for (i = 0; (code = msim_replacement_code[i])
136 && (text = msim_replacement_text[i]); ++i)
137 {
138 if (escape)
139 {
140 tmp = str_replace(msg, text, code);
141 }
142 else
143 {
144 tmp = str_replace(msg, code, text);
145 }
146 g_free(msg);
147 msg = tmp;
148 }
127 149
128 tmp = str_replace(msg, "/1", "/"); 150 return msg;
129 ret = str_replace(tmp, "/2", "\\");
130 g_free(tmp);
131 return ret;
132 } 151 }
133 152
134 /** 153 /**
135 * Escape a protocol message. 154 * Escape a protocol message.
136 * 155 *
137 * @return The escaped message. Caller must g_free(). 156 * @return The escaped message. Caller must g_free().
138 */ 157 */
139 gchar *msim_escape(const gchar *msg) 158 gchar *msim_escape(const gchar *msg)
140 { 159 {
141 /* TODO: make more elegant, refactor with msim_unescape */ 160 return msim_unescape_or_escape(g_strdup(msg), TRUE);
142 gchar *tmp, *ret; 161 }
143 162
144 tmp = str_replace(msg, "/", "/1"); 163 gchar *msim_unescape(const gchar *msg)
145 ret = str_replace(tmp, "\\", "/2"); 164 {
146 g_free(tmp); 165 return msim_unescape_or_escape(g_strdup(msg), FALSE);
147
148 return ret;
149 } 166 }
150 167
151 /** 168 /**
152 * Replace 'old' with 'new' in 'str'. 169 * Replace 'old' with 'new' in 'str'.
153 * 170 *