dovecot-2.0: lib-imap: Added imap_match_dup().

dovecot at dovecot.org dovecot at dovecot.org
Wed Apr 28 22:09:22 EEST 2010


details:   http://hg.dovecot.org/dovecot-2.0/rev/bb8ccf1ae2ac
changeset: 11194:bb8ccf1ae2ac
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Apr 28 17:36:25 2010 +0300
description:
lib-imap: Added imap_match_dup().

diffstat:

 src/lib-imap/imap-match.c      |  34 ++++++++++++++++++++++++++++++++++
 src/lib-imap/imap-match.h      |   4 +++-
 src/lib-imap/test-imap-match.c |  35 +++++++++++++++++++++++------------
 3 files changed, 60 insertions(+), 13 deletions(-)

diffs (124 lines):

diff -r f0d13439baea -r bb8ccf1ae2ac src/lib-imap/imap-match.c
--- a/src/lib-imap/imap-match.c	Wed Apr 28 17:35:41 2010 +0300
+++ b/src/lib-imap/imap-match.c	Wed Apr 28 17:36:25 2010 +0300
@@ -4,6 +4,7 @@
    rewritten. */
 
 #include "lib.h"
+#include "array.h"
 #include "imap-match.h"
 
 #include <ctype.h>
@@ -175,6 +176,39 @@
 	*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 -r f0d13439baea -r bb8ccf1ae2ac src/lib-imap/imap-match.h
--- a/src/lib-imap/imap-match.h	Wed Apr 28 17:35:41 2010 +0300
+++ b/src/lib-imap/imap-match.h	Wed Apr 28 17:36:25 2010 +0300
@@ -27,8 +27,10 @@
 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);
 
-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 -r f0d13439baea -r bb8ccf1ae2ac src/lib-imap/test-imap-match.c
--- a/src/lib-imap/test-imap-match.c	Wed Apr 28 17:35:41 2010 +0300
+++ b/src/lib-imap/test-imap-match.c	Wed Apr 28 17:36:25 2010 +0300
@@ -48,31 +48,42 @@
 		{ "%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);
 
-		test_out(t_strdup_printf("imap_match(inboxcase, %d)", i),
-			 result == inbox_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, inbox_test[i].input) == inbox_test[i].result);
+		imap_match_deinit(&glob2);
 	}
+	pool_unref(&pool);
+	test_end();
 }
 
 int main(void)


More information about the dovecot-cvs mailing list