From: Timo Sirainen Date: Wed, 28 Apr 2010 14:36:25 +0000 (+0300) Subject: lib-imap: Added imap_match_dup(). X-Git-Tag: 2.0.beta5~80 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6d783e9d5249612ccdd8d1c97c26e326a2b3b4fe;p=thirdparty%2Fdovecot%2Fcore.git lib-imap: Added imap_match_dup(). --HG-- branch : HEAD --- diff --git a/src/lib-imap/imap-match.c b/src/lib-imap/imap-match.c index 95b7d541ef..23775956b9 100644 --- a/src/lib-imap/imap-match.c +++ b/src/lib-imap/imap-match.c @@ -4,6 +4,7 @@ rewritten. */ #include "lib.h" +#include "array.h" #include "imap-match.h" #include @@ -175,6 +176,39 @@ void imap_match_deinit(struct imap_match_glob **glob) *glob = NULL; } +static struct imap_match_glob * +imap_match_dup_real(pool_t pool, const struct imap_match_glob *glob) +{ + ARRAY_TYPE(const_string) patterns; + const struct imap_match_pattern *p; + bool inboxcase = FALSE; + + t_array_init(&patterns, 8); + for (p = glob->patterns; p->pattern != NULL; p++) { + if (p->inboxcase) + inboxcase = TRUE; + array_append(&patterns, &p->pattern, 1); + } + (void)array_append_space(&patterns); + return imap_match_init_multiple_real(pool, array_idx(&patterns, 0), + inboxcase, glob->sep); +}; + +struct imap_match_glob * +imap_match_dup(pool_t pool, const struct imap_match_glob *glob) +{ + struct imap_match_glob *new_glob; + + if (pool->datastack_pool) { + return imap_match_dup_real(pool, glob); + } else { + T_BEGIN { + new_glob = imap_match_dup_real(pool, glob); + } T_END; + return new_glob; + } +}; + #define CMP_CUR_CHR(ctx, data, pattern) \ (*(data) == *(pattern) || \ (i_toupper(*(data)) == i_toupper(*(pattern)) && \ diff --git a/src/lib-imap/imap-match.h b/src/lib-imap/imap-match.h index 620483faa4..5cc5f2efb5 100644 --- a/src/lib-imap/imap-match.h +++ b/src/lib-imap/imap-match.h @@ -27,9 +27,11 @@ imap_match_init(pool_t pool, const char *pattern, struct imap_match_glob * imap_match_init_multiple(pool_t pool, const char *const *patterns, bool inboxcase, char separator); - void imap_match_deinit(struct imap_match_glob **glob); +struct imap_match_glob * +imap_match_dup(pool_t pool, const struct imap_match_glob *glob); + enum imap_match_result imap_match(struct imap_match_glob *glob, const char *data); diff --git a/src/lib-imap/test-imap-match.c b/src/lib-imap/test-imap-match.c index 4cbeaf51e3..d509a69f6d 100644 --- a/src/lib-imap/test-imap-match.c +++ b/src/lib-imap/test-imap-match.c @@ -48,31 +48,42 @@ static void test_imap_match(void) { "%I%N%B%O%X%/foo", "inbox/foo", IMAP_MATCH_YES }, { "i%X/foo", "inbx/foo", IMAP_MATCH_NO } }; - struct imap_match_glob *glob; + struct imap_match_glob *glob, *glob2; unsigned int i; - enum imap_match_result result; + pool_t pool; + + pool = pool_alloconly_create_clean("test", 1024); /* first try tests without inboxcasing */ + test_begin("imap match"); for (i = 0; i < N_ELEMENTS(test); i++) { - glob = imap_match_init(default_pool, test[i].pattern, + glob = imap_match_init(pool, test[i].pattern, FALSE, '/'); - result = imap_match(glob, test[i].input); - imap_match_deinit(&glob); + test_assert(imap_match(glob, test[i].input) == test[i].result); - test_out(t_strdup_printf("imap_match(%d)", i), - result == test[i].result); + glob2 = imap_match_dup(default_pool, glob); + p_clear(pool); + + /* test the dup after clearing first one's memory */ + test_assert(imap_match(glob2, test[i].input) == test[i].result); + imap_match_deinit(&glob2); } /* inboxcasing tests */ for (i = 0; i < N_ELEMENTS(inbox_test); i++) { - glob = imap_match_init(default_pool, inbox_test[i].pattern, + glob = imap_match_init(pool, inbox_test[i].pattern, TRUE, '/'); - result = imap_match(glob, inbox_test[i].input); - imap_match_deinit(&glob); + test_assert(imap_match(glob, inbox_test[i].input) == inbox_test[i].result); + + glob2 = imap_match_dup(default_pool, glob); + p_clear(pool); - test_out(t_strdup_printf("imap_match(inboxcase, %d)", i), - result == inbox_test[i].result); + /* test the dup after clearing first one's memory */ + test_assert(imap_match(glob2, inbox_test[i].input) == inbox_test[i].result); + imap_match_deinit(&glob2); } + pool_unref(&pool); + test_end(); } int main(void)