]> git.notmuchmail.org Git - notmuch/blobdiff - lib/message-file.c
lib: refactor _notmuch_messsage_file_get_combined_header
[notmuch] / lib / message-file.c
index 8ac96e8e06a5dd9a132f241469690c773777fdb4..7d38ebd0fc04c6c6621c14492516599161599b3d 100644 (file)
@@ -13,7 +13,7 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see http://www.gnu.org/licenses/ .
+ * along with this program.  If not, see https://www.gnu.org/licenses/ .
  *
  * Author: Carl Worth <cworth@cworth.org>
  */
@@ -37,27 +37,6 @@ struct _notmuch_message_file {
     GMimeMessage *message;
 };
 
-static int
-strcase_equal (const void *a, const void *b)
-{
-    return strcasecmp (a, b) == 0;
-}
-
-static unsigned int
-strcase_hash (const void *ptr)
-{
-    const char *s = ptr;
-
-    /* This is the djb2 hash. */
-    unsigned int hash = 5381;
-    while (s && *s) {
-       hash = ((hash << 5) + hash) + tolower (*s);
-       s++;
-    }
-
-    return hash;
-}
-
 static int
 _notmuch_message_file_destructor (notmuch_message_file_t *message)
 {
@@ -222,6 +201,37 @@ _notmuch_message_file_get_mime_message (notmuch_message_file_t *message,
  *
  * Return NULL on errors, empty string for non-existing headers.
  */
+
+static char *
+_extend_header (char *combined, const char *value) {
+    char *decoded;
+
+    decoded = g_mime_utils_header_decode_text (value);
+    if (! decoded) {
+       if (combined) {
+           g_free (combined);
+           combined = NULL;
+       }
+       goto DONE;
+    }
+
+    if (combined) {
+       char *tmp = g_strdup_printf ("%s %s", combined, decoded);
+       g_free (decoded);
+       g_free (combined);
+       if (! tmp) {
+           combined = NULL;
+           goto DONE;
+       }
+
+       combined = tmp;
+    } else {
+       combined = decoded;
+    }
+ DONE:
+    return combined;
+}
+
 static char *
 _notmuch_message_file_get_combined_header (notmuch_message_file_t *message,
                                           const char *header)
@@ -243,37 +253,13 @@ _notmuch_message_file_get_combined_header (notmuch_message_file_t *message,
 
     do {
        const char *value;
-       char *decoded;
-
        if (strcasecmp (g_mime_header_iter_get_name (iter), header) != 0)
            continue;
 
        /* Note that GMime retains ownership of value... */
        value = g_mime_header_iter_get_value (iter);
 
-       /* ... while decoded needs to be freed with g_free(). */
-       decoded = g_mime_utils_header_decode_text (value);
-       if (! decoded) {
-           if (combined) {
-               g_free (combined);
-               combined = NULL;
-           }
-           goto DONE;
-       }
-
-       if (combined) {
-           char *tmp = g_strdup_printf ("%s %s", combined, decoded);
-           g_free (decoded);
-           g_free (combined);
-           if (! tmp) {
-               combined = NULL;
-               goto DONE;
-           }
-
-           combined = tmp;
-       } else {
-           combined = decoded;
-       }
+       combined = _extend_header (combined, value);
     } while (g_mime_header_iter_next (iter));
 
     /* Return empty string for non-existing headers. */