[PATCH] : cleanups Episode 5



	Hi all,
cleanups/improvements... Episode 5 (please note that all these patches are
incremental, ie you have to apply them in order)

in libbalsa/mailbox_local.c
-in libbalsa_mailbox_local_open : moved misplaced comment, correct little
typo

in libbalsa/mailbox_maildir.c
-in libbalsa_mailbox_maildir_create : use g_strdup_printf instead of big
gchar array

-in libbalsa_mailbox_maildir_get_message_stream : correct indentation

in libbalsa/mailbox_mbox.c
correct indentation

in libbalsa/mailbox_mh.c
-in libbalsa_mailbox_mh_create : make use of g_strdup_printf

in message.c
-in libbalsa_message_destroy : make use of g_list_foreach throughout the
func
-in libbalsa_message_user_hdrs : use g_list_prepend instead of
g_list_append (I do not use g_list_reverse, because I assume the headers
order does not matter, is that correct?)
-in libbalsa_messages_copy : use LIBBALSA_MESSAGE instead of brutal cast
-in libbalsa_message_size_to_gchar : simplifies a bit (use directly
g_strdup_printf)

in misc.c
-in libbalsa_escape_specials : use GString (this avoids us to scan twice
the input string, hope it will boost this function a bit)
-in libbalsa_deescape_specials : use GString (here I'm not sure it really
speeds it because of the GString overhead, but anyway it avoids copying
twice the input string so...)
-in libbalsa_make_string_from_list :
	make use of g_list_next, LIBBALSA_ADDRESS()
	speed it up using g_string_free(...,FALSE) to avoid the duplication
of result
-in libbalsa_readfile_nostat : use g_string_free(...,FALSE) to avoid the
duplication of result

More to come later
Bye
Manu
diff -u balsa-1.2.0/libbalsa/mailbox_local.c test/balsa-1.2.0/libbalsa/mailbox_local.c
--- balsa-1.2.0/libbalsa/mailbox_local.c	Wed Aug  1 11:22:50 2001
+++ test/balsa-1.2.0/libbalsa/mailbox_local.c	Tue Oct 16 18:36:02 2001
@@ -244,7 +244,7 @@
     path = libbalsa_mailbox_local_get_path(mailbox);
 
     if (CLIENT_CONTEXT_OPEN(mailbox)) {
-	/* incriment the reference count */
+	/* increment the reference count */
 	mailbox->open_ref++;
 	UNLOCK_MAILBOX(mailbox);
 	gdk_threads_enter();
@@ -271,12 +271,12 @@
     mailbox->total_messages = 0;
     mailbox->unread_messages = 0;
     mailbox->new_messages = CLIENT_CONTEXT(mailbox)->msgcount;
+    /* increment the reference count */
     mailbox->open_ref++;
     UNLOCK_MAILBOX(mailbox);
     gdk_threads_enter();
     libbalsa_mailbox_load_messages(mailbox);
     
-    /* increment the reference count */
 #ifdef DEBUG
     g_print(_("LibBalsaMailboxLocal: Opening %s Refcount: %d\n"),
 	    mailbox->name, mailbox->open_ref);
diff -u balsa-1.2.0/libbalsa/mailbox_maildir.c test/balsa-1.2.0/libbalsa/mailbox_maildir.c
--- balsa-1.2.0/libbalsa/mailbox_maildir.c	Tue Sep 18 15:28:58 2001
+++ test/balsa-1.2.0/libbalsa/mailbox_maildir.c	Tue Oct 16 18:47:13 2001
@@ -116,7 +116,7 @@
 	}
     } else {
 	if(create) {    
-	    char tmp[_POSIX_PATH_MAX];
+	    gchar * tmp1,* tmp2,* tmp3;
 	    
 	    if (mkdir (path, S_IRWXU)) {
 		libbalsa_information(LIBBALSA_INFORMATION_WARNING, 
@@ -124,35 +124,42 @@
 		return (-1);
 	    }
 	    
-	    snprintf (tmp, sizeof (tmp), "%s/cur", path);
-	    if (mkdir (tmp, S_IRWXU)) {
+	    tmp1=g_strdup_printf ("%s/cur", path);
+	    if (mkdir (tmp1, S_IRWXU)) {
 		libbalsa_information(LIBBALSA_INFORMATION_WARNING, 
 				     _("Could not create a MailDir at %s (%s)"), path, strerror(errno) );
 		rmdir (path);
+		g_free(tmp1);
 		return (-1);
 	    }
 	    
-	    snprintf (tmp, sizeof (tmp), "%s/new", path);
-	    if (mkdir (tmp, S_IRWXU)) {
+	    tmp2=g_strdup_printf ("%s/new", path);
+	    if (mkdir (tmp2, S_IRWXU)) {
 		libbalsa_information(LIBBALSA_INFORMATION_WARNING, 
 				     _("Could not create a MailDir at %s (%s)"), path, strerror(errno) );
-		snprintf (tmp, sizeof (tmp), "%s/cur", path);
-		rmdir (tmp);
+		rmdir (tmp1);
 		rmdir (path);
+		g_free(tmp1);
+		g_free(tmp2);
 		return (-1);
 	    }
 	    
-	    snprintf (tmp, sizeof (tmp), "%s/tmp", path);
-	    if (mkdir (tmp, S_IRWXU)) {
+	    tmp3=g_strdup_printf("%s/tmp", path);
+	    if (mkdir (tmp3, S_IRWXU)) {
 		libbalsa_information(LIBBALSA_INFORMATION_WARNING, 
 				     _("Could not create a MailDir at %s (%s)"), path, strerror(errno) );
-		snprintf (tmp, sizeof (tmp), "%s/cur", path);
-		rmdir (tmp);
-		snprintf (tmp, sizeof (tmp), "%s/new", path);
-		rmdir (tmp);
+		rmdir (tmp1);
+		rmdir (tmp2);
+		rmdir (tmp3);
 		rmdir (path);
+		g_free(tmp1);
+		g_free(tmp2);
+		g_free(tmp3);
 		return (-1);
 	    }
+	    g_free(tmp1);
+	    g_free(tmp2);
+	    g_free(tmp3);
 	} else 
 	    return(-1);
     }
@@ -192,27 +199,27 @@
 static FILE *
 libbalsa_mailbox_maildir_get_message_stream(LibBalsaMailbox *mailbox, LibBalsaMessage *message)
 {
-	FILE *stream = NULL;
-	gchar *filename;
-
-	g_return_val_if_fail (LIBBALSA_IS_MAILBOX_MAILDIR(mailbox), NULL);
-	g_return_val_if_fail (LIBBALSA_IS_MESSAGE(message), NULL);
-
-	filename = g_strdup_printf("%s/%s", 
-				   libbalsa_mailbox_local_get_path(mailbox),
-				   libbalsa_message_pathname(message));
-
-	stream = fopen(filename, "r");
-
-	if (!stream || ferror(stream)) {
-	    libbalsa_information(LIBBALSA_INFORMATION_ERROR, 
-				 _("Open of %s failed. Errno = %d, "),
-				 filename, errno);
-	    g_free(filename);
-	    return NULL;
-	}
+    FILE *stream = NULL;
+    gchar *filename;
+    
+    g_return_val_if_fail (LIBBALSA_IS_MAILBOX_MAILDIR(mailbox), NULL);
+    g_return_val_if_fail (LIBBALSA_IS_MESSAGE(message), NULL);
+    
+    filename = g_strdup_printf("%s/%s", 
+			       libbalsa_mailbox_local_get_path(mailbox),
+			       libbalsa_message_pathname(message));
+    
+    stream = fopen(filename, "r");
+    
+    if (!stream || ferror(stream)) {
+	libbalsa_information(LIBBALSA_INFORMATION_ERROR, 
+			     _("Open of %s failed. Errno = %d, "),
+			     filename, errno);
 	g_free(filename);
-	return stream;
+	return NULL;
+    }
+    g_free(filename);
+    return stream;
 }
 
 static void
diff -u balsa-1.2.0/libbalsa/mailbox_mbox.c test/balsa-1.2.0/libbalsa/mailbox_mbox.c
--- balsa-1.2.0/libbalsa/mailbox_mbox.c	Tue Sep 18 15:28:58 2001
+++ test/balsa-1.2.0/libbalsa/mailbox_mbox.c	Tue Oct 16 18:50:40 2001
@@ -161,14 +161,14 @@
 static FILE *
 libbalsa_mailbox_mbox_get_message_stream(LibBalsaMailbox *mailbox, LibBalsaMessage *message)
 {
-	FILE *stream = NULL;
-
-	g_return_val_if_fail (LIBBALSA_IS_MAILBOX_MBOX(mailbox), NULL);
-	g_return_val_if_fail (LIBBALSA_IS_MESSAGE(message), NULL);
-
-	stream = fopen(libbalsa_mailbox_local_get_path(mailbox), "r");
-
-	return stream;
+    FILE *stream = NULL;
+    
+    g_return_val_if_fail (LIBBALSA_IS_MAILBOX_MBOX(mailbox), NULL);
+    g_return_val_if_fail (LIBBALSA_IS_MESSAGE(message), NULL);
+    
+    stream = fopen(libbalsa_mailbox_local_get_path(mailbox), "r");
+    
+    return stream;
 }
 
 static void
diff -u balsa-1.2.0/libbalsa/mailbox_mh.c test/balsa-1.2.0/libbalsa/mailbox_mh.c
--- balsa-1.2.0/libbalsa/mailbox_mh.c	Tue Sep 18 15:28:58 2001
+++ test/balsa-1.2.0/libbalsa/mailbox_mh.c	Tue Oct 16 18:52:35 2001
@@ -114,7 +114,7 @@
 	}
     } else {
 	if(create) {
-	    char tmp[_POSIX_PATH_MAX];
+	    gchar * tmp;
 	    int i;
 
 	    if (mkdir (path, S_IRWXU)) {
@@ -122,14 +122,16 @@
 				     _("Could not create MH directory at %s (%s)"), path, strerror(errno) );
 		return (-1);
 	    } 
-	    snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", path);
-	    if ((i = creat (tmp, S_IRWXU)) == -1)
+	    tmp=g_strdup_printf ("%s/.mh_sequences", path);
+	    i = creat (tmp, S_IRWXU);
+	    g_free(tmp);
+	    if (i == -1)
 		{
 		    libbalsa_information(LIBBALSA_INFORMATION_WARNING, 
 					 _("Could not create MH structure at %s (%s)"), path, strerror(errno) );
 		    rmdir (path);
 		    return (-1);
-		}	    	    
+		}
 	} else 
 	    return(-1);
     }
diff -u balsa-1.2.0/libbalsa/mailbox_pop3.c test/balsa-1.2.0/libbalsa/mailbox_pop3.c
--- balsa-1.2.0/libbalsa/mailbox_pop3.c	Mon Sep 10 12:22:25 2001
+++ test/balsa-1.2.0/libbalsa/mailbox_pop3.c	Wed Oct 17 06:45:33 2001
@@ -285,13 +285,13 @@
     gdk_threads_enter();
 
     tmp_mailbox = (LibBalsaMailbox *)libbalsa_mailbox_local_new((const gchar *)tmp_path, FALSE);
-    if(!tmp_mailbox)  {
+    if(!tmp_mailbox) {
 	libbalsa_information(LIBBALSA_INFORMATION_WARNING,
 			     _("POP3 mailbox %s temp mailbox error:\n"), 
 			     mailbox->name);
 	g_free(tmp_path);
 	return;
-    }	
+    }
     libbalsa_mailbox_open(tmp_mailbox);
     if((m->inbox) && (tmp_mailbox->messages) &&
 	!libbalsa_messages_move(tmp_mailbox->message_list, m->inbox)) {    
diff -u balsa-1.2.0/libbalsa/message.c test/balsa-1.2.0/libbalsa/message.c
--- balsa-1.2.0/libbalsa/message.c	Tue Sep  4 08:41:10 2001
+++ test/balsa-1.2.0/libbalsa/message.c	Wed Oct 17 07:24:12 2001
@@ -213,7 +213,6 @@
 libbalsa_message_destroy(GtkObject * object)
 {
     LibBalsaMessage *message;
-    GList *list;
 
     g_return_if_fail(object != NULL);
     g_return_if_fail(LIBBALSA_IS_MESSAGE(object));
@@ -256,12 +255,8 @@
     g_free(message->subj);
     message->subj = NULL;
 #endif
-    for (list = message->references; list; list = list->next) {
-	if (list->data)
-	    g_free(list->data);
-    }
+    g_list_foreach(message->references,(GFunc) g_free,NULL);
     g_list_free(message->references);
-
     message->references = NULL;
 
     g_free(message->in_reply_to);
@@ -269,19 +264,12 @@
     g_free(message->message_id);
     message->message_id = NULL;
 
-
     libbalsa_message_body_free(message->body_list);
     message->body_list = NULL;
 
-    if(message->references_for_threading!=NULL) {
-	GList *list=message->references_for_threading;
-	for(; list; list=g_list_next(list)){
-	    if(list->data)
-		g_free(list->data);
-	}
-	g_list_free (message->references_for_threading);
-	message->references_for_threading=NULL;
-    }
+    g_list_foreach(message->references_for_threading,(GFunc) g_free,NULL);
+    g_list_free (message->references_for_threading);
+    message->references_for_threading=NULL;
 
     if (GTK_OBJECT_CLASS(parent_class)->destroy)
 	(*GTK_OBJECT_CLASS(parent_class)->destroy) (GTK_OBJECT(object));
@@ -372,42 +360,42 @@
 
     if (cur->env->return_path)
 	res =
-	    g_list_append(res,
+	    g_list_prepend(res,
 			  create_hdr_pair("Return-Path",
 					  ADDRESS_to_gchar(cur->env->
 							   return_path)));
 
     if (cur->env->sender)
 	res =
-	    g_list_append(res,
+	    g_list_prepend(res,
 			  create_hdr_pair("Sender",
 					  ADDRESS_to_gchar(cur->env->
 							   sender)));
 
     if (cur->env->mail_followup_to)
 	res =
-	    g_list_append(res,
+	    g_list_prepend(res,
 			  create_hdr_pair("Mail-Followup-To",
 					  ADDRESS_to_gchar(cur->env->
 							   mail_followup_to)));
 
     if (cur->env->message_id)
 	res =
-	    g_list_append(res,
+	    g_list_prepend(res,
 			  create_hdr_pair("Message-ID",
 					  g_strdup(cur->env->message_id)));
 
     for (tmp = cur->env->references; tmp; tmp = tmp->next) {
 	res =
-	    g_list_append(res,
+	    g_list_prepend(res,
 			  create_hdr_pair("References",
 					  g_strdup(tmp->data)));
     }
 
     for (tmp = cur->env->userhdrs; tmp; tmp = tmp->next) {
-	pair = g_strsplit(tmp->data, ":", 1);
+	pair = g_strsplit((gchar *)tmp->data, ":", 1);
 	g_strchug(pair[1]);
-	res = g_list_append(res, pair);
+	res = g_list_prepend(res, pair);
     }
 
     return res;
@@ -509,7 +497,7 @@
 
     libbalsa_lock_mutt();
     for(p=messages; p; 	p=g_list_next(p)) {
-	message=(LibBalsaMessage *)(p->data);
+	message=LIBBALSA_MESSAGE(p->data);
 	cur = CLIENT_CONTEXT(message->mailbox)->hdrs[message->msgno];
 	mutt_parse_mime_message(CLIENT_CONTEXT(message->mailbox), cur);
 	mutt_append_message(handle->context,
@@ -744,7 +732,7 @@
     g_return_if_fail(messages != NULL);
 
     while(messages){
-      message=(LibBalsaMessage *)(messages->data);
+      message=LIBBALSA_MESSAGE(messages->data);
       gtk_signal_emit(GTK_OBJECT(message),
   	  	      libbalsa_message_signals[SET_DELETED], TRUE);
       messages=g_list_next(messages);
@@ -937,7 +925,7 @@
 gchar *
 libbalsa_message_size_to_gchar (LibBalsaMessage * message, gboolean lines)
 {
-    gchar retsize[32];
+    gchar * retsize;
     glong length;   /* byte len */
     gint lines_len; /* line len */
 
@@ -946,22 +934,22 @@
     lines_len = LIBBALSA_MESSAGE_GET_LINES(message);
     /* lines is int, length is long */
     if (lines)
-        g_snprintf (retsize, sizeof(retsize), "%d", lines_len);
+        retsize=g_strdup_printf ("%d", lines_len);
     else {
         if (length <= 32768) {
-            g_snprintf (retsize, sizeof(retsize), "%ld", length);
+            retsize=g_strdup_printf ("%ld", length);
         } else if (length <= (100*1024)) {
             float tmp = (float)length/1024.0;
-            g_snprintf (retsize, sizeof(retsize), "%.1fK", tmp);
+            retsize=g_strdup_printf ("%.1fK", tmp);
         } else if (length <= (1024*1024)) {
-            g_snprintf (retsize, sizeof(retsize), "%ldK", length/1024);
+            retsize=g_strdup_printf ("%ldK", length/1024);
         } else {
             float tmp = (float)length/(1024.0*1024.0);
-            g_snprintf (retsize, sizeof(retsize), "%.1fM", tmp);
+            retsize=g_strdup_printf ("%.1fM", tmp);
         }
     }
 
-    return g_strdup(retsize);
+    return retsize;
 }
 
 static gchar *
diff -u balsa-1.2.0/libbalsa/misc.c test/balsa-1.2.0/libbalsa/misc.c
--- balsa-1.2.0/libbalsa/misc.c	Mon Oct 15 18:40:22 2001
+++ test/balsa-1.2.0/libbalsa/misc.c	Wed Oct 17 08:12:06 2001
@@ -68,33 +68,29 @@
 gchar *
 libbalsa_escape_specials(const gchar* str)
 {
-    int special_cnt = 0, length = 0;
-    unsigned i;
-    const gchar *str_ptr;
-    gchar *res, *res_ptr;
+    gint length = 0;
+    guint i;
+    const gchar * str_ptr;
+    gchar * p;
+    GString * res;
     
     g_return_val_if_fail(str, NULL);
-    for(str_ptr = str; *str_ptr; str_ptr++, length++)
-	for(i = 0; i<ELEMENTS(char_translations); i++)
-	    if(*str_ptr == char_translations[i].escaped_char) {
-		special_cnt++;
-		break;
-	    }
     
-    res = res_ptr = g_new(gchar, length+special_cnt+1);
+    res = g_string_new("");
     
     for(str_ptr = str; *str_ptr; str_ptr++) {
 	for(i = 0; i<ELEMENTS(char_translations); i++)
 	    if(*str_ptr == char_translations[i].escaped_char) 
 		break;
 	if(i<ELEMENTS(char_translations)) {
-	    *res_ptr++ = '\\';
-	    *res_ptr++ = char_translations[i].replacement;
-	} else 
-	    *res_ptr++ = *str_ptr;
+	    res = g_string_append_c(res,'\\');
+	    res = g_string_append_c(res,char_translations[i].replacement);
+	} else
+	    res = g_string_append_c(res,*str_ptr);
     }
-    *res_ptr = '\0';
-    return res;
+    p=res->str;
+    g_string_free(res,FALSE);
+    return p;
 }
 	    
 gchar *
@@ -103,25 +99,27 @@
     const gchar *src;
     gchar *dest;
     unsigned i;
-    gchar* res = g_strdup(str);
+    GString * res;
 
     g_return_val_if_fail(str, NULL);
-    src = dest = res;
+    res = g_string_new("");
+    src = str;
     while(*src) {
 	if(*src == '\\') {
 	    for(i = 0; i<ELEMENTS(char_translations); i++)
 		if(src[1] == char_translations[i].replacement) 
 		    break;
 	    if(i<ELEMENTS(char_translations)) {
-		*dest++ = char_translations[i].escaped_char;
+		res=g_string_append_c(res,char_translations[i].escaped_char);
 		src += 2;
 		continue;
 	    }
 	}
-	*dest++ = *src++;
+	res=g_string_append_c(res,*src++);
     }
-    *dest = '\0';
-    return res;
+    dest=res->str;
+    g_string_free(res,FALSE);
+    return dest;
 }
 
 /* FIXME: Move to address.c and change name to
@@ -137,21 +135,21 @@
     list = g_list_first((GList *) the_list);
 
     while (list) {
-	addy = list->data;
+	addy = LIBBALSA_ADDRESS(list->data);
 	str = libbalsa_address_to_gchar(addy, 0);
 	if (str)
 	    gs = g_string_append(gs, str);
 
 	g_free(str);
 
-	if (list->next)
+	if (g_list_next(list))
 	    gs = g_string_append(gs, ", ");
 
-	list = list->next;
+	list = g_list_next(list);
     }
 
-    retc = g_strdup(gs->str);
-    g_string_free(gs, 1);
+    retc = gs->str;
+    g_string_free(gs, FALSE);
 
     return retc;
 }
@@ -248,16 +246,8 @@
     } while( r != 0 );
 
     size = gstr->len;
-    *buf = (char *) g_malloc(size + 1);
-    if (*buf == NULL) {
-	g_string_free(gstr, TRUE);
-	return -1;
-    }
-
-    strncpy(*buf, gstr->str, size);
-    (*buf)[size] = '\0';
-
-    g_string_free(gstr, TRUE);
+    *buf = gstr->str;
+    g_string_free(gstr, FALSE);
 
     return size;
 }
@@ -270,7 +260,7 @@
 gboolean libbalsa_find_word(const gchar * word, const gchar * str)
 {
     const gchar *ptr = str;
-    int len = strlen(word);
+    gint len = strlen(word);
 
     while (*ptr) {
 	if (g_strncasecmp(word, ptr, len) == 0)
@@ -421,7 +411,7 @@
 	    if ( unlink( new_path ) == -1 )
 		goto error;
 	}
-	g_free(new_path); new_path = 0;
+	g_free(new_path); new_path = NULL;
     }
 
     closedir(d);


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