[PATCH] : Final clean-up patch for address-entry



	Hi all,
just a janitorial work on address-entry to have an almost clean source + 
some improvment for libbalsa_strsplit (simpler, faster) and 
libbalsa_fill_input.
This is the last patch on address-entry, on my part.
On current CVS
Bye
Manu
--- ../balsa-cvs/balsa/libbalsa/address-entry.c	Mon Feb 11 13:54:24 2002
+++ balsa-cvs/balsa/libbalsa/address-entry.c	Mon Feb 11 14:37:03 2002
@@ -408,7 +408,7 @@
 {
     GList *list;
 
-    if (address_entry == NULL)
+    if (!address_entry)
         return;
 
     list = g_list_first(address_entry->active);
@@ -456,7 +456,8 @@
  *     Modifies the structure.
  *************************************************************/
 static void
-libbalsa_alias_accept_match(emailData *addy) {
+libbalsa_alias_accept_match(emailData *addy)
+{
     g_assert(addy->match != NULL);
     libbalsa_emailData_set_user(addy, addy->match);
     addy->match = NULL;
@@ -478,36 +479,26 @@
  *     Returns a newly allocated GList* with newly allocated
  *     data inside it.
  *************************************************************/
-static GList *
+GList *
 libbalsa_strsplit(const gchar *str, gchar delimiter)
 {
     GList *glist;
-    gchar *data;
     const gchar *old, *current;
-    gint i, previous;
     gboolean quoted;
 
-    g_return_val_if_fail(str != NULL, NULL);
+    if (!str) return NULL;
 
     quoted = FALSE;
     glist = NULL;
-    previous = 0;
-    old = current = str;
-    for (i = 0; str[i]; i++) {
-	if (str[i] == '"') quoted = !quoted;
-	if ( (!quoted) && (str[i] == delimiter) ) {
-	    data = g_strndup(old, i - previous);
-	    glist = g_list_append(glist, data);
-	    previous = i + 1;
-	    old = current;
-	    old++;
+    old = str;
+    for (current=str;*current;current++) {
+	if (*current == '"') quoted = !quoted;
+	else if ( (!quoted) && (*current == delimiter) ) {
+	    glist = g_list_append(glist, g_strndup(old, current-old));
+	    old=current+1;
 	}
-	current++;
-    }
-    if (str) {
-	data = g_strndup(old, i - previous);
-	glist = g_list_append(glist, data);
     }
+    glist = g_list_append(glist, g_strndup(old, current-old));
     return glist;
 }
 
@@ -633,20 +624,21 @@
     /*
      * Store it all in a glist.
      */
-    if (el != NULL) {
+    if (el) {
 	for (current = el;
-	     current != NULL;
+	     current;
 	     current = g_list_next(current)) {
 	    addy = libbalsa_emailData_new((gchar *)current->data);
             address_entry->active =
-                g_list_append(address_entry->active, addy);
+                g_list_prepend(address_entry->active, addy);
 	}
 	g_list_foreach(el, (GFunc)g_free, NULL);
         g_list_free(el);
     } else {
        addy = libbalsa_emailData_new("");
-       address_entry->active = g_list_append(address_entry->active, addy);
+       address_entry->active = g_list_prepend(address_entry->active, addy);
     }
+    address_entry->active = g_list_reverse(address_entry->active);
 
     /*
      * Search for the currently active list member.
@@ -654,17 +646,24 @@
      */
     g_assert(address_entry->active != NULL);
     size = prev = 0;
-    for (list = g_list_first(address_entry->active);
-	 list != NULL;
-	 list = g_list_next(list)) {
-	if (cursor >= size) {
-	    prev = size;
-	    address_entry->active = list;
-	}
+    for (list = address_entry->active;list;list = g_list_next(list)) {
+	prev = size;
+	size += strlen(addy->user) + 1;
 	addy = (emailData *)list->data;
-	size = size + strlen(addy->user) + 1;
 	addy->user = g_strchug(addy->user);
+
+	/* Break on the active address */
+	if (cursor < size) break;
     }
+    address_entry->active = list;
+
+    /* Don't forget to strip leading space returned by
+       strsplit (this also takes care of spaces added
+       by user via cut-and-paste */
+    for (list = g_list_next(list);list;list=g_list_next(list)) {
+	addy = (emailData *)list->data;
+	addy->user = g_strchug(addy->user);
+    }    
 
     addy = (emailData *)address_entry->active->data;
     addy->cursor = cursor - prev;
@@ -725,7 +724,7 @@
      */
     if (addy->user[addy->cursor] == '\0') {
 	list = g_list_next(address_entry->active);
-	if (list != NULL) {
+	if (list) {
 	    libbalsa_emailData_free(list->data);
 	    libbalsa_delete_link(list, list);
 	}
@@ -742,7 +741,7 @@
 	libbalsa_emailData_free(list->data);
         address_entry->active =
             libbalsa_delete_link(address_entry->active, list);
-	if (address_entry->active != NULL) {
+	if (address_entry->active) {
 	    addy = address_entry->active->data;
 	    g_assert(addy != NULL);
 	    addy->cursor = 0;
@@ -776,7 +775,7 @@
     g_return_if_fail(address_entry != NULL);
     g_return_if_fail(LIBBALSA_IS_ADDRESS_ENTRY(address_entry));
 
-    if (address_entry->active == NULL)
+    if (!address_entry->active)
 	return;
 
     addy = address_entry->active->data;
@@ -784,9 +783,9 @@
     /*
      * Lets see if the user is at the beginning of an e-mail entry.
      */
-    if (addy->cursor == 0) {
+    if (!addy->cursor) {
 	list = g_list_previous(address_entry->active);
-	if (list != NULL) {
+	if (list) {
 	    libbalsa_emailData_free(list->data);
 	    libbalsa_delete_link(list, list);
 	}
@@ -796,12 +795,12 @@
      */
     } else {
 	list = address_entry->active;
-	if (g_list_next(list) == NULL)
+	if (!g_list_next(list))
 	    address_entry->active = g_list_previous(list);
 	libbalsa_emailData_free(list->data);
         address_entry->active =
             libbalsa_delete_link(address_entry->active, list);
-	if (address_entry->active != NULL) {
+	if (address_entry->active) {
 	    addy = address_entry->active->data;
 	    g_assert(addy != NULL);
 	    addy->cursor = strlen(addy->user);
@@ -846,7 +845,7 @@
      * Free all the following data.
      */
     for (list = g_list_next(address_entry->active);
-	 list != NULL;
+	 list;
 	 list = g_list_next(address_entry->active)) {
 	 /*
 	  * Concatenate the two e-mails.
@@ -1014,18 +1013,18 @@
     /*
      * Lets see if the user is at the beginning of an e-mail entry.
      */
-    if (addy->cursor == 0) {
+    if (!addy->cursor) {
        list = g_list_previous(address_entry->active);
-       if (list != NULL) {
+       if (list) {
 	   /*
 	    * Concatenate the two e-mails.
 	    */
 	   extra = list->data;
 	   extra->cursor = strlen(extra->user);
-            libbalsa_emailData_set_user(addy,
-                                        g_strconcat(extra->user,
-                                                    addy->user,
-                                                    NULL));
+	   libbalsa_emailData_set_user(addy,
+				       g_strconcat(extra->user,
+						   addy->user,
+						   NULL));
 	   
 	   /*
 	    * Free a whole bunch of RAM.
@@ -1042,11 +1041,11 @@
         gchar *p;
         for (p = addy->user + addy->cursor - 1; *p; ++p)
             *p = *(p + 1);
-       addy->cursor--;
-       if (*addy->user == '\0')
-	   libbalsa_force_no_match(addy);
-       else if (address_entry->find_match)
-	   (*address_entry->find_match) (addy, TRUE);
+	addy->cursor--;
+	if (*addy->user == '\0')
+	    libbalsa_force_no_match(addy);
+	else if (address_entry->find_match)
+	    (*address_entry->find_match) (addy, TRUE);
     }
 }
 
@@ -1096,7 +1095,7 @@
         }
     
 	list = g_list_next(address_entry->active);
-	if (list != NULL) {
+	if (list) {
 	    /*
 	     * Concatenate the two e-mails.
 	     */
@@ -1140,13 +1139,13 @@
     g_return_val_if_fail(address_entry != NULL, FALSE);
     g_return_val_if_fail(LIBBALSA_IS_ADDRESS_ENTRY(address_entry), FALSE);
 	    
-    if (address_entry->active == NULL)
+    if (!address_entry->active)
 	return TRUE;
 
     g_assert(address_entry->active->data != NULL);
 
     addy = address_entry->active->data;
-    if (addy->match != NULL) {
+    if (addy->match) {
 	libbalsa_alias_accept_match(addy);
 	return TRUE;
     }
@@ -1184,7 +1183,7 @@
      * Else no match was found.  Check if there was a default
      * domain to add to e-mail addresses.
      */
-    if (address_entry->domain == NULL || *address_entry->domain == '\0')
+    if (!address_entry->domain || *address_entry->domain == '\0')
 	return;
 
     /*
@@ -1246,7 +1245,7 @@
     g_return_if_fail(LIBBALSA_IS_ADDRESS_ENTRY(address_entry));
 
     addy = address_entry->active->data;
-    if (addy->match != NULL)
+    if (addy->match)
 	libbalsa_alias_accept_match(addy);
     address_entry->active = g_list_first(address_entry->active);
     addy = address_entry->active->data;
@@ -1330,7 +1329,7 @@
     g_return_if_fail(LIBBALSA_IS_ADDRESS_ENTRY(address_entry));
 	    
     addy = address_entry->active->data;
-    if (addy->match != NULL)
+    if (addy->match)
 	libbalsa_alias_accept_match(addy);
     address_entry->active = g_list_last(address_entry->active);
     addy = address_entry->active->data;
@@ -1378,7 +1377,7 @@
     addy = address_entry->active->data;
     left = addy->user;
     right = NULL;
-    if (addy->match != NULL) {
+    if (addy->match) {
 	libbalsa_alias_accept_match(addy);
     } else {
 	if ((addy->cursor > 0) && (addy->user[addy->cursor] != '\0')) {
@@ -1394,7 +1393,7 @@
     /*
      * Now we add a new entry.
      */
-    if (right != NULL) {
+    if (right) {
         extra = libbalsa_emailData_new(right);
         libbalsa_emailData_set_user(addy, g_strndup(left, addy->cursor));
     } else
@@ -1410,7 +1409,7 @@
     /*
      * And we add the default domain to the original entry.
      */
-    if (address_entry->domain == NULL ||
+    if (!address_entry->domain ||
 	*address_entry->domain == '\0' ||
 	libbalsa_is_an_email(addy->user)) return;
 
@@ -1637,7 +1636,7 @@
     /*
      * Skip it if its not editable...
      */
-    if (editable->editable == FALSE) return FALSE;
+    if (!editable->editable) return FALSE;
 
     /*
      * Setup variables, and see if we need to select text.
@@ -1878,7 +1877,7 @@
 	 */
 	addy = (emailData *)list->data;
 	g_assert(addy != NULL);
-	if (addy->match != NULL) {
+	if (addy->match) {
 	    out = g_strconcat(addy->user, " (", addy->match, ")", NULL);
 	} else {
 	    out = g_strdup(addy->user);
@@ -1887,7 +1886,7 @@
 	 * Copy the string, adding a delimiter if need be.
 	 */
 	show = g_string_append(show, out);
-	if (g_list_next(list) != NULL)
+	if (g_list_next(list))
 	    show = g_string_append(show, ", ");
 
 	/*
@@ -1896,7 +1895,7 @@
 	if (!found) {
 	    if (list != address_entry->active) {
 		cursor += strlen(out);
-	    	if (g_list_next(list) != NULL) cursor += 2;
+	    	if (g_list_next(list)) cursor += 2;
 	    } else {
 		found = TRUE;
 		cursor += addy->cursor;


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