dovecot-2.0: Added p_strarray_dup().

dovecot at dovecot.org dovecot at dovecot.org
Sat Dec 12 01:38:38 EET 2009


details:   http://hg.dovecot.org/dovecot-2.0/rev/c2386c4594d5
changeset: 10457:c2386c4594d5
user:      Timo Sirainen <tss at iki.fi>
date:      Fri Dec 11 18:36:04 2009 -0500
description:
Added p_strarray_dup().

diffstat:

6 files changed, 61 insertions(+)
src/lib/Makefile.am     |    1 +
src/lib/strfuncs.c      |   22 ++++++++++++++++++++++
src/lib/strfuncs.h      |    3 +++
src/lib/test-lib.c      |    1 +
src/lib/test-lib.h      |    1 +
src/lib/test-strfuncs.c |   33 +++++++++++++++++++++++++++++++++

diffs (115 lines):

diff -r f0fd3bd7c122 -r c2386c4594d5 src/lib/Makefile.am
--- a/src/lib/Makefile.am	Fri Dec 11 16:05:38 2009 -0500
+++ b/src/lib/Makefile.am	Fri Dec 11 18:36:04 2009 -0500
@@ -232,6 +232,7 @@ test_lib_SOURCES = \
 	test-priorityq.c \
 	test-seq-range-array.c \
 	test-strescape.c \
+	test-strfuncs.c \
 	test-str-find.c \
 	test-str-sanitize.c \
 	test-time-util.c \
diff -r f0fd3bd7c122 -r c2386c4594d5 src/lib/strfuncs.c
--- a/src/lib/strfuncs.c	Fri Dec 11 16:05:38 2009 -0500
+++ b/src/lib/strfuncs.c	Fri Dec 11 18:36:04 2009 -0500
@@ -559,6 +559,28 @@ bool str_array_icase_find(const char *co
 	return FALSE;
 }
 
+const char **p_strarray_dup(pool_t pool, const char *const *arr)
+{
+	unsigned int i;
+	const char **ret;
+	char *p;
+	size_t len, size = sizeof(const char *);
+
+	for (i = 0; arr[i] != NULL; i++)
+		size += sizeof(const char *) + strlen(arr[i]) + 1;
+
+	ret = p_malloc(pool, size);
+	p = PTR_OFFSET(ret, sizeof(const char *) * (i + 1));
+	for (i = 0; arr[i] != NULL; i++) {
+		len = strlen(arr[i]) + 1;
+		memcpy(p, arr[i], len);
+		ret[i] = p;
+		p += len;
+	}
+	i_assert(PTR_OFFSET(ret, size) == (void *)p);
+	return ret;
+}
+
 const char *dec2str(uintmax_t number)
 {
 	char *buffer;
diff -r f0fd3bd7c122 -r c2386c4594d5 src/lib/strfuncs.h
--- a/src/lib/strfuncs.h	Fri Dec 11 16:05:38 2009 -0500
+++ b/src/lib/strfuncs.h	Fri Dec 11 18:36:04 2009 -0500
@@ -84,6 +84,9 @@ bool str_array_find(const char *const *a
 bool str_array_find(const char *const *arr, const char *value);
 /* Like str_array_find(), but use strcasecmp(). */
 bool str_array_icase_find(const char *const *arr, const char *value);
+/* Duplicate array of strings. The memory can be freed by freeing the
+   return value. */
+const char **p_strarray_dup(pool_t pool, const char *const *arr);
 
 /* INTERNAL */
 char *t_noalloc_strdup_vprintf(const char *format, va_list args,
diff -r f0fd3bd7c122 -r c2386c4594d5 src/lib/test-lib.c
--- a/src/lib/test-lib.c	Fri Dec 11 16:05:38 2009 -0500
+++ b/src/lib/test-lib.c	Fri Dec 11 18:36:04 2009 -0500
@@ -22,6 +22,7 @@ int main(void)
 		test_priorityq,
 		test_seq_range_array,
 		test_strescape,
+		test_strfuncs,
 		test_str_find,
 		test_str_sanitize,
 		test_time_util,
diff -r f0fd3bd7c122 -r c2386c4594d5 src/lib/test-lib.h
--- a/src/lib/test-lib.h	Fri Dec 11 16:05:38 2009 -0500
+++ b/src/lib/test-lib.h	Fri Dec 11 18:36:04 2009 -0500
@@ -21,6 +21,7 @@ void test_priorityq(void);
 void test_priorityq(void);
 void test_seq_range_array(void);
 void test_strescape(void);
+void test_strfuncs(void);
 void test_str_find(void);
 void test_str_sanitize(void);
 void test_time_util(void);
diff -r f0fd3bd7c122 -r c2386c4594d5 src/lib/test-strfuncs.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/test-strfuncs.c	Fri Dec 11 18:36:04 2009 -0500
@@ -0,0 +1,33 @@
+/* Copyright (c) 2009 Dovecot authors, see the included COPYING file */
+
+#include "test-lib.h"
+
+static void test_p_strarray_dup(void)
+{
+	const char *input[][3] = {
+		{ NULL },
+		{ "a", NULL },
+		{ "foobar", NULL },
+		{ "a", "foo", NULL }
+	};
+	const char **ret;
+	unsigned int i, j;
+
+	test_begin("p_strarray_dup");
+
+	for (i = 0; i < N_ELEMENTS(input); i++) {
+		ret = p_strarray_dup(default_pool, input[i]);
+		for (j = 0; input[i][j] != NULL; j++) {
+			test_assert(strcmp(input[i][j], ret[j]) == 0);
+			test_assert(input[i][j] != ret[j]);
+		}
+		test_assert(ret[j] == NULL);
+		i_free(ret);
+	}
+	test_end();
+}
+
+void test_strfuncs(void)
+{
+	test_p_strarray_dup();
+}


More information about the dovecot-cvs mailing list