changeset 815:7daea970a89a

collection_info_list_to_dnd_data(): simplify and optimize.
author zas_
date Tue, 10 Jun 2008 11:27:35 +0000
parents 721c2bbaceec
children cf5bbd0beaae
files src/collect.c
diffstat 1 files changed, 27 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/src/collect.c	Mon Jun 09 20:27:39 2008 +0000
+++ b/src/collect.c	Tue Jun 10 11:27:35 2008 +0000
@@ -472,63 +472,59 @@
 
 gchar *collection_info_list_to_dnd_data(CollectionData *cd, GList *list, gint *length)
 {
-	gchar *uri_text = NULL;
-	gint total;
 	GList *work;
-	gint n;
-	GList *temp;
+	GList *temp = NULL;
 	gchar *ptr;
-
-	n = collection_to_number(cd);
+	gchar *text;
+	gchar *uri_text;
+	gint collection_number;
 
-	if (!list || n < 0)
-		{
-		*length = 0;
-		return NULL;
-		}
+	*length = 0;
+	if (!list) return NULL;
 
-	temp = NULL;
-	temp = g_list_prepend(temp, g_strdup_printf("COLLECTION:%d\n", n));
+	collection_number = collection_to_number(cd);
+	if (collection_number < 0) return NULL;
+
+	text = g_strdup_printf("COLLECTION:%d\n", collection_number);
+	*length += strlen(text);
+	temp = g_list_prepend(temp, text);
+
 	work = list;
 	while (work)
 		{
-		n = g_list_index(cd->list, work->data);
-		if (n >= 0)
-			{
-			temp = g_list_prepend(temp, g_strdup_printf("%d\n", n));
-			}
+		gint item_number = g_list_index(cd->list, work->data);
+
 		work = work->next;
+
+		if (item_number < 0) continue;
+		
+		text = g_strdup_printf("%d\n", item_number);
+		temp = g_list_prepend(temp, text);
+		*length += strlen(text);
 		}
 
-	total = 0;
-	work = temp;
-	while (work)
-		{
-		total += strlen((gchar *)work->data);
-		work = work->next;
-		}
-	total += 1;
+	*length += 1; /* ending nul char */
 
-	uri_text = g_malloc(total);
+	uri_text = g_malloc(*length);
 	ptr = uri_text;
 
 	work = g_list_last(temp);
 	while (work)
 		{
+		gint len;
 		gchar *text = work->data;
 
 		work = work->prev;
 
-		strcpy(ptr, text);
-		ptr += strlen(text);
+		len = strlen(text);
+		memcpy(ptr, text, len);
+		ptr += len;
 		}
 
 	ptr[0] = '\0';
 
 	string_list_free(temp);
 
-	*length = total;
-
 	return uri_text;
 }