suggestion: notmuch-talloc.[ch]

Tomi Ollila tomi.ollila at iki.fi
Sat Sep 10 02:15:36 PDT 2011


In addition to notmuch_config_get_new_tags() I started to think
how to add notmuch_config_get_database_exclude() (for excluding
directories; needed by me and many others)

to copy and modify from notmuch_config_get_new_tags() would cause
lots of duplicate code so I started to look how to generalise.

Not so simple so I started to look "bigger picture" and this leads
to suggestion:

new c file called 'notmuch-talloc.c', with the following initial
content:

--8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<--

# include <add required includes...>

static void 
notmuch_talloc_g_string_list_destructor (void * ptr)
{
    g_strfreev ( (gchar **)ptr );
}

/* tallocify wrapper for g_key_file_get_string_list */

gchar **
void notmuch_talloc_g_key_file_get_string_list (const void * ctx,
					        GFile *key_file,
					        const gchar *group_name,
					        const gchar *key,
					        gsize *length,
					        GError **error)
{
    gchar ** tags = g_key_file_get_string_list (key_file,
						group_name, key,
						length, error);
    if (tags) {
	talloc_reference(ctx, tags);
	talloc_set_destructor(tags, notmuch_talloc_g_string_list_destructor);
    }
    return tags;
}
                                                                                        
--8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<--

Provided that the above is proven correct, this would simplify
notmuch_config_get_new_tags() as: 

const char **
notmuch_config_get_new_tags (notmuch_config_t *config,
			     size_t *length)
{
    if (config->new_tags == NULL) {
	config->new_tags = notmuch_talloc_g_key_file_get_string_list(
				config,
				config->key_file,
				"new", "tags",
				&config->tags_length, NULL);
	}
    }
    return config->new_tags;
}

Thus adding notmuch_config_get_database_exclude() avoids the
CopyPasteProgramming threshold. 

new code to the notmuch-talloc.c (and interface declarations to
notmuch-talloc.h) would be added on demand, per need basis. 

Note that this code is unproven code. For obvious reason i'm currently
hesitant to investigate further -- all of us have priorities -- but
if this idea gets good response I'm willing to do the proper patches;
for now, for my own use, I just hardcode excluded directories to
notmuch-new.c :D

tomi



More information about the notmuch mailing list