[PATCH] : More cleanups/improvments



	Hi all,
in libbalsa/address-book-vcard.c
- in extract_name : avoid useless g_strdup;
- in libbalsa_address_book_vcard_alias_complete : simplify test;

in libbalsa/address-book-ldif.c
- in load_ldif_file : delete FIXME comment (I think what was said has been
corrected), and use g_strconcat instead of mix of g_new, strcpy, strcat;
- in libbalsa_address_book_ldif_alias_complete : simplify test (same as in
vcard version);

in libbalsa/address-book-ldap.c
- in rfc_2254_escape : change it so it uses GString (I'm not sure this one
really improves something ;-)

in libbalsa/address.c
-in libbalsa/libbalsa_address_to_gchar : just small cleanup, I'm not sure
it really improves something else than readability;

in libbalsa/body.c
- in libbalsa_message_body_save_temporary : replace one occurence of
mutt_mktemp (see changes in libbalsa/misc.c);

in libbalsa/folders-scanners.c
-in libbalsa_scanner_mh_dir/libbalsa_scanner_local_dir : use
g_strdup_printf instead of snprintf in a (big) gchar array;

in libbalsa/misc.[c/h]
- add new function libbalsa_mktemp, replaces mutt_mktemp (but still uses
globals from libmutt, but these will just be moved to libbalsa when all
libmutt functions have their counter part in libbalsa); I have normally
replaced all mutt_mktemp calls by libbalsa_mktemp (moreover any forgotten
mutt_mktemp call is harmless because mutt version and libbalsa version
share same globals to build the name, so you could mix both versions
without ending with overlapping filenames);

in libbalsa/send.c
-in add_mutt_body_plain : make use of libbalsa_mktemp;
-in libbalsa_create_msg : change the calls to mutt_mktemp to
libbalsa_mktemp; to do this I had to change the tempfile parameter from
gchar * to gchar **, in order to be able to assign it the newly allocated
pointer, moreover I had to change the tempfile MessageQueueItem struct
field from a big gchar array to a gchar * (which will be allocated by
libbalsa_create_msg in general), so I had also to change the following
functions to make this coherent :
	* msg_queue_item_new : assign NULL to tempfile field;
	* msg_queue_item_destroy : add a call to g_free on tempfile field
	* libbalsa_message_queue : pass &mqi->tempfile as parameters
instead of mqi->tempfile
	* libbalsa_process_queue : same as above
	* libbalsa_message_queue : same as above

This should be OK because I tried to take care of all issues, but I think
it still needs careful review (particularly the changes in send.c)
Thanks
Bye
Manu
diff -u balsa-1.2.0/libbalsa/address-book-ldap.c test/balsa-1.2.0/libbalsa/address-book-ldap.c
--- balsa-1.2.0/libbalsa/address-book-ldap.c	Tue Aug 28 11:24:30 2001
+++ test/balsa-1.2.0/libbalsa/address-book-ldap.c	Sun Oct 14 10:55:30 2001
@@ -429,46 +429,34 @@
 static gchar*
 rfc_2254_escape(const gchar *raw)
 {
-    gchar *new;
     gchar *str;
-    gchar *step;
+    GString * new;
+    gchar * step;
 
-    new = (gchar *)malloc((strlen(raw) * 3) + 1);
-    str = new;
     for (step = (gchar *)raw;
          step[0] != '\0';
          step++) {
         switch (step[0]) {
             case '*':
-                str[0] = '\\'; str++;
-                str[0] = '2'; str++;
-                str[0] = 'a'; str++;
+		new=g_string_append(new,"\\2a");
                 break;
             case '(':
-                str[0] = '\\'; str++;
-                str[0] = '2'; str++;
-                str[0] = '8'; str++;
+		new=g_string_append(new,"\\28");
                 break;
             case ')':
-                str[0] = '\\'; str++;
-                str[0] = '2'; str++;
-                str[0] = '9'; str++;
+		new=g_string_append(new,"\\29");
                 break;
             case '\\':
-                str[0] = '\\'; str++;
-                str[0] = '5'; str++;
-                str[0] = 'c'; str++;
+		new=g_string_append(new,"\\5c");
                 break;
             default:
-                str[0] = step[0]; str++;
+		new=g_string_append_c(new,step);
         }
     }
-    str[0] = '\0';
-    str = g_strdup(new);
-    free(new);
+    str=new->str;
+    g_free(new,FALSE);
     return str;
 }
-
 
 static GList *
 libbalsa_address_book_ldap_alias_complete(LibBalsaAddressBook * ab,
diff -u balsa-1.2.0/libbalsa/address-book-ldif.c test/balsa-1.2.0/libbalsa/address-book-ldif.c
--- balsa-1.2.0/libbalsa/address-book-ldif.c	Sat Aug 25 01:22:41 2001
+++ test/balsa-1.2.0/libbalsa/address-book-ldif.c	Mon Oct 15 16:29:33 2001
@@ -243,7 +243,7 @@
 }
 
 /* address_new_prefill:
-   takes over the string ownership!
+   takes over the strings ownership!
 */
 static LibBalsaAddress*
 address_new_prefill(GList* address_list, gchar* nickn, gchar* givenn, 
@@ -267,9 +267,6 @@
     return address;
 }
     
-/* FIXME: Could stat the file to see if it has changed since last time 
-   we read it 
-*/
 static void
 load_ldif_file(LibBalsaAddressBook *ab)
 {
@@ -445,10 +442,7 @@
     gchar *name = NULL, *end = NULL;
 
     if(givenname && *givenname && surname && *surname) {
-	name = g_new (gchar, strlen(givenname) + strlen(surname) + 2);
-	strcpy(name, givenname);
-	strcat(name, " ");
-	strcat(name, surname);
+	name = g_strconcat (givenname," ",surname,NULL);
     } else if(givenname && *givenname) {
 	name = g_strdup(givenname);
     } else if(surname && *surname) {
@@ -637,8 +631,7 @@
 	    gtk_object_ref(GTK_OBJECT(addr1));
 	    resa = g_list_next(resa);
 	    resb = g_list_next(resb);
-	} else if (resa != NULL &&
-		   (resb == NULL || address_compare(addr1, addr2) > 0) ) {
+	} else if (address_compare(addr1, addr2) > 0) {
 	    res = g_list_prepend(res, addr1);
 	    gtk_object_ref(GTK_OBJECT(addr1));
 	    resa = g_list_next(resa);
diff -u balsa-1.2.0/libbalsa/address-book-vcard.c test/balsa-1.2.0/libbalsa/address-book-vcard.c
--- balsa-1.2.0/libbalsa/address-book-vcard.c	Wed Aug 15 13:30:02 2001
+++ test/balsa-1.2.0/libbalsa/address-book-vcard.c	Mon Oct 15 16:28:14 2001
@@ -316,7 +316,7 @@
 	 * fetch all e-mail fields
 	 */
 	if (g_strncasecmp(string, "EMAIL;", 6) == 0) {
-	    gchar *ptr = strchr(string, ':');
+	    gchar *ptr = strchr(string+6, ':');
 	    if (ptr) {
 		address_list =
 		    g_list_append(address_list, g_strdup(ptr + 1));
@@ -373,24 +373,40 @@
     name_arr = g_malloc((cpt + 1) * sizeof(gchar *));
 
     j = 0;
-    if (cpt > PREFIX && *fld[PREFIX] != '\0')
-	name_arr[j++] = g_strdup(fld[PREFIX]);
+    if (cpt > PREFIX) {
+	if (*fld[PREFIX] != '\0')
+	    name_arr[j++] = fld[PREFIX];
+	else g_free(fld[PREFIX]);
+    }
 
-    if (cpt > FIRST && *fld[FIRST] != '\0')
-	name_arr[j++] = g_strdup(fld[FIRST]);
+    if (cpt > FIRST) {
+	if (*fld[FIRST] != '\0')
+	    name_arr[j++] = fld[FIRST];
+	else g_free(fld[FIRST]);
+    }
 
-    if (cpt > MIDDLE && *fld[MIDDLE] != '\0')
-	name_arr[j++] = g_strdup(fld[MIDDLE]);
+    if (cpt > MIDDLE) {
+	if (*fld[MIDDLE] != '\0')
+	    name_arr[j++] = fld[MIDDLE];
+	else g_free(fld[MIDDLE]);
+    }
 
-    if (cpt > LAST && *fld[LAST] != '\0')
-	name_arr[j++] = g_strdup(fld[LAST]);
+    if (cpt > LAST) {
+	if (*fld[LAST] != '\0')
+	    name_arr[j++] = fld[LAST];
+	else g_free(fld[LAST]);
+    }
 
-    if (cpt > SUFFIX && *fld[SUFFIX] != '\0')
-	name_arr[j++] = g_strdup(fld[SUFFIX]);
+    if (cpt > SUFFIX) {
+	if (*fld[SUFFIX] != '\0')
+	    name_arr[j++] = fld[SUFFIX];
+	else g_free(fld[SUFFIX]);
+    }
 
     name_arr[j] = NULL;
 
-    g_strfreev(fld);
+    /* We only need to free the array, not its elements (they are now in name_arr, freed later */
+    g_free(fld);
 
     /* collect the data to one string */
     res = g_strjoinv(" ", name_arr);
@@ -540,8 +556,7 @@
 	    gtk_object_ref(GTK_OBJECT(addr1));
 	    resa = g_list_next(resa);
 	    resb = g_list_next(resb);
-	} else if (resa != NULL && 
-		   (resb == NULL || address_compare(addr1, addr2) > 0) ) {
+	} else if (address_compare(addr1, addr2) > 0) {
 	    res = g_list_prepend(res, addr1);
 	    gtk_object_ref(GTK_OBJECT(addr1));
 	    resa = g_list_next(resa);
diff -u balsa-1.2.0/libbalsa/address.c test/balsa-1.2.0/libbalsa/address.c
--- balsa-1.2.0/libbalsa/address.c	Wed Aug 15 13:30:02 2001
+++ test/balsa-1.2.0/libbalsa/address.c	Mon Oct 15 15:56:51 2001
@@ -184,11 +184,8 @@
 
 	nth_address = address->address_list;
 	while ( nth_address ) {
-	    address_string = (gchar *)nth_address->data;
-	    if ( str )
-		g_string_sprintfa(str, ", %s", address_string);
-	    else
-		str = g_string_new(address_string);
+	    str=g_string_append(str,", ");
+	    str=g_string_append(str,(gchar *)nth_address->data);
 	    nth_address = g_list_next(nth_address);
 	}
 
diff -u balsa-1.2.0/libbalsa/body.c test/balsa-1.2.0/libbalsa/body.c
--- balsa-1.2.0/libbalsa/body.c	Thu Mar 22 11:55:27 2001
+++ test/balsa-1.2.0/libbalsa/body.c	Mon Oct 15 16:34:55 2001
@@ -150,15 +150,8 @@
 libbalsa_message_body_save_temporary(LibBalsaMessageBody * body,
 				     gchar * prefix)
 {
-    /* FIXME: Role our own mktemp that doesn't need a large array (use g_strdup_printf) */
     if (body->temp_filename == NULL) {
-	gchar tmp_file_name[PATH_MAX + 1];
-
-	libbalsa_lock_mutt();
-	mutt_mktemp(tmp_file_name);
-	libbalsa_unlock_mutt();
-
-	body->temp_filename = g_strdup(tmp_file_name);
+	body->temp_filename = libbalsa_mktemp();
 	return libbalsa_message_body_save(body, prefix, body->temp_filename);
     } else {
 	/* the temporary name has been already allocated on previous
diff -u balsa-1.2.0/libbalsa/folder-scanners.c test/balsa-1.2.0/libbalsa/folder-scanners.c
--- balsa-1.2.0/libbalsa/folder-scanners.c	Mon Aug 20 16:07:19 2001
+++ test/balsa-1.2.0/libbalsa/folder-scanners.c	Mon Oct 15 17:04:30 2001
@@ -48,7 +48,7 @@
 {
     DIR *dpc;
     struct dirent *de;
-    char filename[PATH_MAX];
+    gchar * filename=NULL;
     struct stat st;
     GNode* parent_node = NULL;
 
@@ -63,7 +63,8 @@
     while ((de = readdir(dpc)) != NULL) {
 	if (de->d_name[0] == '.')
 	    continue;
-	snprintf(filename, PATH_MAX, "%s/%s", prefix, de->d_name);
+	g_free(filename);
+	filename=g_strdup_printf("%s/%s", prefix, de->d_name);
 	/* ignore file if it can't be read. */
 	if (stat(filename, &st) == -1 || access(filename, R_OK) == -1)
 	    continue;
@@ -81,6 +82,7 @@
 	}
 	/* ignore regular files */
     }
+    g_free(filename);
     closedir(dpc);
 }
 
@@ -91,8 +93,7 @@
 {
     DIR *dpc;
     struct dirent *de;
-    gchar * name;
-    char filename[PATH_MAX];
+    gchar * name,* filename=NULL;
     struct stat st;
     GtkType mailbox_type;
     GNode* current_node;
@@ -104,7 +105,8 @@
     while ((de = readdir(dpc)) != NULL) {
 	if (de->d_name[0] == '.')
 	    continue;
-	snprintf(filename, PATH_MAX, "%s/%s", prefix, de->d_name);
+	g_free(filename);
+	filename=g_strdup_printf("%s/%s", prefix, de->d_name);
 
 	/* ignore file if it can't be read. */
 	if (stat(filename, &st) == -1 || access(filename, R_OK) == -1)
@@ -131,6 +133,7 @@
 		mailbox_handler(rnode, de->d_name, filename);
 	}
     }
+    g_free(filename);
     closedir(dpc);
 }
 
@@ -181,7 +184,7 @@
     for(i=0; state.subfolders && i<depth; i++) {
 	list = state.subfolders;
 	state.subfolders = NULL;
-	printf("Deph: %i -------------------------------------------\n", i);
+	printf("Depth: %i -------------------------------------------\n", i);
 	for(el= g_list_first(list); el; el = g_list_next(el)) {
 	    if(*(char*)el->data)
 		imap_path = g_strdup_printf("imap%s://%s:%i/%s/", 
@@ -211,7 +214,6 @@
     g_list_free((GList*)state.subfolders);
     state_free (&state);
     libbalsa_unlock_mutt();
-    
 }
 
 /* this function ovverrides mutt's one. */
diff -u balsa-1.2.0/libbalsa/misc.c test/balsa-1.2.0/libbalsa/misc.c
--- balsa-1.2.0/libbalsa/misc.c	Fri Aug 24 18:27:00 2001
+++ test/balsa-1.2.0/libbalsa/misc.c	Mon Oct 15 16:33:43 2001
@@ -432,3 +432,15 @@
     closedir(d);
     return FALSE;
 }
+
+/*
+ * libbalsa_mktemp : returns a newly allocated string containing a (unique) temporary file name
+ * Replacement of mutt_mktemp
+ * FIXME Uses libmutt globals (specially Counter), we should move all these globals to libbalsa
+ * as soon as we can (ie when all functions needing them have their counter part into libbalsa)
+ */
+
+gchar * libbalsa_mktemp()
+{
+    return g_strdup_printf("%s/mutt-%s-%d-%d", NONULL(Tempdir), NONULL(Hostname), (int) getpid (), Counter++);
+}
diff -u balsa-1.2.0/libbalsa/misc.h test/balsa-1.2.0/libbalsa/misc.h
--- balsa-1.2.0/libbalsa/misc.h	Tue Aug 28 11:24:30 2001
+++ test/balsa-1.2.0/libbalsa/misc.h	Mon Oct 15 17:25:55 2001
@@ -62,5 +62,6 @@
 
 gboolean libbalsa_delete_directory_contents(const gchar *path);
 
+gchar * libbalsa_mktemp();
 
 #endif				/* __LIBBALSA_MISC_H__ */
diff -u balsa-1.2.0/libbalsa/send.c test/balsa-1.2.0/libbalsa/send.c
--- balsa-1.2.0/libbalsa/send.c	Thu Sep  6 15:37:38 2001
+++ test/balsa-1.2.0/libbalsa/send.c	Mon Oct 15 17:16:57 2001
@@ -56,7 +56,7 @@
     MessageQueueItem *next_message;
 #endif
     gchar *fcc;
-    char tempfile[_POSIX_PATH_MAX];
+    gchar * tempfile;
 #if !ENABLE_ESMTP
     enum {MQI_WAITING, MQI_FAILED,MQI_SENT} status;
 #else
@@ -106,15 +106,17 @@
 #if !ENABLE_ESMTP
     mqi->status = MQI_WAITING;
 #endif
-    mqi->tempfile[0] = '\0';
+    mqi->tempfile = NULL;
     return mqi;
 }
 
 static void
 msg_queue_item_destroy(MessageQueueItem * mqi)
 {
-    if (*mqi->tempfile)
+    if (mqi->tempfile) {
 	unlink(mqi->tempfile);
+	g_free(mqi->tempfile);
+    }
     if (mqi->message)
 	mutt_free_header(&mqi->message);
     if (mqi->fcc)
@@ -158,7 +160,7 @@
 static guint balsa_send_message_real(SendMessageInfo* info);
 static void encode_descriptions(BODY * b);
 static gboolean libbalsa_create_msg(LibBalsaMessage * message,
-				    HEADER * msg, char *tempfile,
+				    HEADER * msg, gchar **tempfile,
 				    gint encoding, int queu);
 gchar **libbalsa_lookup_mime_type(const gchar * path);
 
@@ -235,7 +237,6 @@
 add_mutt_body_plain(const gchar * charset, gint encoding_style)
 {
     BODY *body;
-    gchar buffer[PATH_MAX];
 
     g_return_val_if_fail(charset, NULL);
     libbalsa_lock_mutt();
@@ -252,8 +253,7 @@
     body->parameter->value = g_strdup(charset);
     body->parameter->next = NULL;
 
-    mutt_mktemp(buffer);
-    body->filename = g_strdup(buffer);
+    body->filename = libbalsa_mktemp();
     mutt_update_encoding(body);
 
     libbalsa_unlock_mutt();
@@ -291,7 +291,7 @@
     mqi = msg_queue_item_new(message);
     set_option(OPTWRITEBCC);
     if (libbalsa_create_msg(message, mqi->message,
-			    mqi->tempfile, encoding, 0)) {
+			    &mqi->tempfile, encoding, 0)) {
 	libbalsa_lock_mutt();
 	mutt_write_fcc(libbalsa_mailbox_local_get_path(outbox),
 		       mqi->message, NULL, 0, NULL);
@@ -480,7 +480,7 @@
 
 	new_message = msg_queue_item_new(queu);
 	if (!libbalsa_create_msg(queu, new_message->message,
-				 new_message->tempfile, encoding, 1)) {
+				 &new_message->tempfile, encoding, 1)) {
 	    msg_queue_item_destroy(new_message);
 	} else {
 	    total_messages_left++;
@@ -824,7 +824,7 @@
 	    
 	    new_message = msg_queue_item_new(queu);
 	    if (!libbalsa_create_msg(queu, new_message->message,
-				     new_message->tempfile, encoding, 1)) {
+				     &new_message->tempfile, encoding, 1)) {
 		msg_queue_item_destroy(new_message);
 	    } else {
 		if (mqi)
@@ -1296,7 +1296,7 @@
    PS: seems to be broken when queu == 1 - further execution of
    mutt_free_header(mgs) leads to crash.
 */ static gboolean
-libbalsa_create_msg(LibBalsaMessage * message, HEADER * msg, char *tmpfile,
+libbalsa_create_msg(LibBalsaMessage * message, HEADER * msg, gchar **tmpfile,
 		    gint encoding, int queu) {
     BODY *last, *newbdy;
     FILE *tempfp;
@@ -1421,8 +1421,8 @@
 /* We create the message in MIME format here, we use the same format 
  * for local delivery that for SMTP */
     if (queu == 0) {
-	mutt_mktemp(tmpfile);
-	if ((tempfp = safe_fopen(tmpfile, "w")) == NULL)
+	*tmpfile=libbalsa_mktemp();
+	if ((tempfp = safe_fopen(*tmpfile, "w")) == NULL)
 	    return (-1);
 
 	mutt_write_rfc822_header(tempfp, msg->env, msg->content, -1);
@@ -1430,14 +1430,18 @@
 
 	if ((mutt_write_mime_body(msg->content, tempfp) == -1)) {
 	    fclose(tempfp);
-	    unlink(tmpfile);
+	    unlink(*tmpfile);
+	    g_free(*tmpfile);
+	    *tmpfile=NULL;
 	    return (-1);
 	}
 	fputc('\n', tempfp);	/* tie off the body. */
 
 	if (fclose(tempfp) != 0) {
-	    mutt_perror(tmpfile);
-	    unlink(tmpfile);
+	    mutt_perror(*tmpfile);
+	    unlink(*tmpfile);
+	    g_free(*tmpfile);
+	    *tmpfile=NULL;
 	    return (-1);
 	}
 
@@ -1452,16 +1456,18 @@
 	    mx_open_message(CLIENT_CONTEXT(message->mailbox),
 			    msg_tmp->msgno);
 
-	mutt_mktemp(tmpfile);
-	if ((tempfp = safe_fopen(tmpfile, "w")) == NULL)
+	*tmpfile=libbalsa_mktemp();
+	if ((tempfp = safe_fopen(*tmpfile, "w")) == NULL)
 	    return FALSE;
 
 	_mutt_copy_message(tempfp, mensaje->fp, msg_tmp,
 			   msg_tmp->content, 0, CH_NOSTATUS);
 
 	if (fclose(tempfp) != 0) {
-	    mutt_perror(tmpfile);
-	    unlink(tmpfile);
+	    mutt_perror(*tmpfile);
+	    unlink(*tmpfile);
+	    g_free(*tmpfile);
+	    *tmpfile=NULL;
 	    if (message->mailbox)
 		libbalsa_message_body_unref(message);
 	    return FALSE;


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