changeset 12741:2b61e6ed85c3

[gaim-migrate @ 15088] use GStrings to avoid unnecessary allocations and freeings committer: Tailor Script <tailor@pidgin.im>
author Daniel Atallah <daniel.atallah@gmail.com>
date Fri, 06 Jan 2006 00:32:19 +0000
parents a7ad7fa611e6
children 258871a62600
files src/protocols/simple/simple.c
diffstat 1 files changed, 19 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/src/protocols/simple/simple.c	Thu Jan 05 22:11:06 2006 +0000
+++ b/src/protocols/simple/simple.c	Fri Jan 06 00:32:19 2006 +0000
@@ -426,47 +426,37 @@
 }
 
 static void sendout_sipmsg(struct simple_account_data *sip, struct sipmsg *msg) {
-	gchar *oldstr;
-	gchar *outstr = g_strdup_printf("%s %s SIP/2.0\r\n", msg->method, msg->target);
+	GSList *tmp = msg->headers;
 	gchar *name;
 	gchar *value;
-	GSList *tmp = msg->headers;
+	GString *outstr = g_string_new("");
+	g_string_append_printf(outstr, "%s %s SIP/2.0\r\n", msg->method, msg->target);
 	while(tmp) {
-		oldstr = outstr;
-		name = ((struct siphdrelement*)(tmp->data))->name;
-		value = ((struct siphdrelement*)(tmp->data))->value;
-		outstr = g_strdup_printf("%s%s: %s\r\n",oldstr, name, value);
-		g_free(oldstr);
+		name = ((struct siphdrelement*) (tmp->data))->name;
+		value = ((struct siphdrelement*) (tmp->data))->value;
+		g_string_append_printf(outstr, "%s: %s\r\n", name, value);
 		tmp = g_slist_next(tmp);
 	}
-	oldstr = outstr;
-	if(msg->body) outstr = g_strdup_printf("%s\r\n%s", outstr, msg->body);
-	else outstr = g_strdup_printf("%s\r\n", outstr);
-	g_free(oldstr);
-	sendout_pkt(sip->gc, outstr);
-	g_free(outstr);
+	g_string_append_printf(outstr, "\r\n%s", msg->body ? msg->body : "");
+	sendout_pkt(sip->gc, outstr->str);
+	g_string_free(outstr, TRUE);
 }
 
 static void send_sip_response(GaimConnection *gc, struct sipmsg *msg, int code, char *text, char *body) {
 	GSList *tmp = msg->headers;
-	char *oldstr;
-	char *name;
-	char *value;
-	char *outstr = g_strdup_printf("SIP/2.0 %d %s\r\n",code, text);
+	gchar *name;
+	gchar *value;
+	GString *outstr = g_string_new("");
+	g_string_append_printf(outstr, "SIP/2.0 %d %s\r\n", code, text);
 	while(tmp) {
-		oldstr = outstr;
-		name = ((struct siphdrelement*)(tmp->data))->name;
-		value = ((struct siphdrelement*)(tmp->data))->value;
-		outstr = g_strdup_printf("%s%s: %s\r\n",oldstr, name, value);
-		g_free(oldstr);
+		name = ((struct siphdrelement*) (tmp->data))->name;
+		value = ((struct siphdrelement*) (tmp->data))->value;
+		g_string_append_printf(outstr, "%s: %s\r\n", name, value);
 		tmp = g_slist_next(tmp);
 	}
-	oldstr = outstr;
-	if(body) outstr = g_strdup_printf("%s\r\n%s",outstr,body);
-	else outstr = g_strdup_printf("%s\r\n",outstr);
-	g_free(oldstr);
-	sendout_pkt(gc, outstr);
-	g_free(outstr);
+	g_string_append_printf(outstr, "\r\n%s", body ? body : "");
+	sendout_pkt(gc, outstr->str);
+	g_string_free(outstr, TRUE);
 }
 
 static void transactions_remove(struct simple_account_data *sip, struct transaction *trans) {