]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-imap: Added imap_match_dup().
authorTimo Sirainen <tss@iki.fi>
Wed, 28 Apr 2010 14:36:25 +0000 (17:36 +0300)
committerTimo Sirainen <tss@iki.fi>
Wed, 28 Apr 2010 14:36:25 +0000 (17:36 +0300)
--HG--
branch : HEAD

src/lib-imap/imap-match.c
src/lib-imap/imap-match.h
src/lib-imap/test-imap-match.c

index 95b7d541eff70ee6454538c1391baa94b00a3e7d..23775956b99eb74332878d67866a34986ce6a5cb 100644 (file)
@@ -4,6 +4,7 @@
    rewritten. */
 
 #include "lib.h"
+#include "array.h"
 #include "imap-match.h"
 
 #include <ctype.h>
@@ -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)) && \
index 620483faa4b7e52070507360db881721c73cf76e..5cc5f2efb5eddbe998f0c9f1cf47d0f5d1b0d01e 100644 (file)
@@ -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);
 
index 4cbeaf51e3e7b91488c4df5172c323574b3744dc..d509a69f6da47bd616943b6d8aae2a6c61bb5bc1 100644 (file)
@@ -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)