* [PATCH] Remove loads of code and use GString
@ 2009-03-24 12:06 Bastien Nocera
2009-03-24 12:30 ` Johan Hedberg
0 siblings, 1 reply; 2+ messages in thread
From: Bastien Nocera @ 2009-03-24 12:06 UTC (permalink / raw)
To: BlueZ development
[-- Attachment #1: Type: text/plain, Size: 47 bytes --]
Much easier than a home-made function.
Cheers
[-- Attachment #2: 0001-Remove-append_and_grow-code-and-just-use-GString.patch --]
[-- Type: text/x-patch, Size: 2256 bytes --]
>From 04c74b471bb2d6505206b31d87931c8a2c79373a Mon Sep 17 00:00:00 2001
From: Bastien Nocera <hadess@hadess.net>
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
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2009-03-24 12:30 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-03-24 12:06 [PATCH] Remove loads of code and use GString Bastien Nocera
2009-03-24 12:30 ` Johan Hedberg
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox