[PATCH] Remove loads of code and use GString

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

 



Much easier than a home-made function.

Cheers
>From 04c74b471bb2d6505206b31d87931c8a2c79373a Mon Sep 17 00:00:00 2001
From: Bastien Nocera <hadess@xxxxxxxxxx>
Date: Tue, 24 Mar 2009 12:04:32 +0000
Subject: [PATCH] Remove "append_and_grow" code and just use GString

Less code for us to maintain.
---
 src/device.c |   52 ++++++++--------------------------------------------
 1 files changed, 8 insertions(+), 44 deletions(-)

diff --git a/src/device.c b/src/device.c
index 48ac512..3734c29 100644
--- a/src/device.c
+++ b/src/device.c
@@ -987,41 +987,6 @@ static void iter_append_record(DBusMessageIter *dict, uint32_t handle,
 	dbus_message_iter_close_container(dict, &entry);
 }
 
-static void append_and_grow_string(void *data, const char *str)
-{
-	sdp_buf_t *buff = data;
-	int len;
-
-	len = strlen(str);
-
-	if (!buff->data) {
-		buff->data = malloc(DEFAULT_XML_BUF_SIZE);
-		if (!buff->data)
-			return;
-		buff->buf_size = DEFAULT_XML_BUF_SIZE;
-	}
-
-	/* Grow string */
-	while (buff->buf_size < (buff->data_size + len + 1)) {
-		void *tmp;
-		uint32_t new_size;
-
-		/* Grow buffer by a factor of 2 */
-		new_size = (buff->buf_size << 1);
-
-		tmp = realloc(buff->data, new_size);
-		if (!tmp)
-			return;
-
-		buff->data = tmp;
-		buff->buf_size = new_size;
-	}
-
-	/* Include the NULL character */
-	memcpy(buff->data + buff->data_size, str, len + 1);
-	buff->data_size += len;
-}
-
 static void discover_services_reply(struct browse_req *req, int err,
 							sdp_list_t *recs)
 {
@@ -1056,21 +1021,20 @@ static void discover_services_reply(struct browse_req *req, int err,
 
 	for (seq = recs; seq; seq = seq->next) {
 		sdp_record_t *rec = (sdp_record_t *) seq->data;
-		sdp_buf_t result;
+		GString *result;
 
 		if (!rec)
 			break;
 
-		memset(&result, 0, sizeof(sdp_buf_t));
+		result = g_string_new(NULL);
 
-		convert_sdp_record_to_xml(rec, &result,
-				append_and_grow_string);
+		convert_sdp_record_to_xml(rec, result,
+				(void *) g_string_append);
 
-		if (result.data) {
-			const char *val = (char *) result.data;
-			iter_append_record(&dict, rec->handle, val);
-			free(result.data);
-		}
+		if (result->len)
+			iter_append_record(&dict, rec->handle, result->str);
+
+		g_string_free(result, TRUE);
 	}
 
 	dbus_message_iter_close_container(&iter, &dict);
-- 
1.6.0.6


[Index of Archives]     [Bluez Devel]     [Linux Wireless Networking]     [Linux Wireless Personal Area Networking]     [Linux ATH6KL]     [Linux USB Devel]     [Linux Media Drivers]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux