Skip to contentSkip to conversation

Remove loads of code and use GString

PATCH In review Bastien Nocera 2 participants 2 messages Mar 24, 2009 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Remove loads of code and use GString

Much easier than a home-made function.

Cheers

Quoted 1 line

From 04c74b471bb2d6505206b31d87931c8a2c79373a Mon Sep 17 00:00:00 2001

From: Bastien Nocera <[email protected]>
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(-)
src/device.c +8 -44
index 48ac512..3734c29 100644
@@ -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);
Signature · 1 line
1.6.0.6
#1 lore raw

Hi Bastien,

Quoted 1 line · Bastien Nocera

Much easier than a home-made function.

Thanks. This one has also been pushed upstream.

Johan