[dovecot-cvs] dovecot/src/lib strfuncs.c, 1.48, 1.49 strfuncs.h, 1.25, 1.26

cras at dovecot.org cras at dovecot.org
Sun Sep 25 13:24:12 EEST 2005


Update of /var/lib/cvs/dovecot/src/lib
In directory talvi:/tmp/cvs-serv7467

Modified Files:
	strfuncs.c strfuncs.h 
Log Message:
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()



Index: strfuncs.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/strfuncs.c,v
retrieving revision 1.48
retrieving revision 1.49
diff -u -d -r1.48 -r1.49
--- strfuncs.c	7 Aug 2005 11:41:20 -0000	1.48
+++ strfuncs.c	25 Sep 2005 10:24:09 -0000	1.49
@@ -495,34 +495,36 @@
 	return strcasecmp(*s1, *s2);
 }
 
-static const char **_strsplit(const char *data, const char *separators,
-			      int spaces)
+static char **
+_strsplit(pool_t pool, const char *data, const char *separators, int spaces)
 {
-        const char **array;
+        char **array;
 	char *str;
-        size_t alloc_len, len;
+        unsigned int count, alloc_count, new_alloc_count;
 
 	i_assert(*separators != '\0');
 
 	if (spaces)
 		while (*data == ' ') data++;
 	if (*data == '\0')
-		return t_new(const char *, 1);
+		return p_new(pool, char *, 1);
 
-	str = t_strdup_noconst(data);
+	str = p_strdup(pool, data);
 
-        alloc_len = 32;
-        array = t_buffer_get(sizeof(const char *) * alloc_len);
+	alloc_count = 32;
+	array = p_new(pool, char *, alloc_count);
 
-	array[0] = str; len = 1;
+	array[0] = str; count = 1;
 	while (*str != '\0') {
 		if (strchr(separators, *str) != NULL) {
 			/* separator found */
-			if (len+1 >= alloc_len) {
-                                alloc_len = nearest_power(alloc_len+1);
-				array = t_buffer_reget(array,
-						       sizeof(const char *) *
-						       alloc_len);
+			if (count+1 >= alloc_count) {
+                                new_alloc_count = nearest_power(alloc_count+1);
+				array = p_realloc(pool, array,
+						  sizeof(char *) * alloc_count,
+						  sizeof(char *) *
+						  new_alloc_count);
+				alloc_count = new_alloc_count;
 			}
 
 			if (*str != ' ' || !spaces)
@@ -532,27 +534,39 @@
 				while (str[1] == ' ') str++;
 			}
 
-			array[len++] = str+1;
+			array[count++] = str+1;
 		}
 
                 str++;
 	}
 
-	i_assert(len < alloc_len);
-        array[len] = NULL;
+	i_assert(count < alloc_count);
+        array[count] = NULL;
 
-	t_buffer_alloc(sizeof(const char *) * (len+1));
         return array;
 }
 
 const char **t_strsplit(const char *data, const char *separators)
 {
-	return _strsplit(data, separators, FALSE);
+	return (const char **)_strsplit(unsafe_data_stack_pool, data,
+					separators, FALSE);
 }
 
 const char **t_strsplit_spaces(const char *data, const char *separators)
 {
-	return _strsplit(data, separators, TRUE);
+	return (const char **)_strsplit(unsafe_data_stack_pool, data,
+					separators, TRUE);
+}
+
+char **p_strsplit(pool_t pool, const char *data, const char *separators)
+{
+	return _strsplit(pool, data, separators, FALSE);
+}
+
+char **p_strsplit_spaces(pool_t pool, const char *data,
+			 const char *separators)
+{
+	return _strsplit(pool, data, separators, TRUE);
 }
 
 unsigned int strarray_length(const char *const *arr)
@@ -598,6 +612,22 @@
 	return str;
 }
 
+int strarray_remove(const char **arr, const char *value)
+{
+	const char **dest;
+
+	for (; *arr != NULL; arr++) {
+		if (strcmp(*arr, value) == 0) {
+			/* found it. now move the rest. */
+			for (dest = arr, arr++; *arr != NULL; arr++, dest++)
+				*dest = *arr;
+			*dest = NULL;
+			return TRUE;
+		}
+	}
+	return FALSE;
+}
+
 const char *dec2str(uintmax_t number)
 {
 	char *buffer;

Index: strfuncs.h
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/strfuncs.h,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -d -r1.25 -r1.26
--- strfuncs.h	16 May 2005 21:00:45 -0000	1.25
+++ strfuncs.h	25 Sep 2005 10:24:09 -0000	1.26
@@ -58,9 +58,13 @@
 int bsearch_strcasecmp(const void *p1, const void *p2);
 int strcasecmp_p(const void *p1, const void *p2);
 
-/* seprators is an array of separator characters, not a separator string. */
+/* separators is an array of separator characters, not a separator string. */
+char **p_strsplit(pool_t pool, const char *data, const char *separators);
 const char **t_strsplit(const char *data, const char *separators);
-/* like t_strsplit(), but treats multiple spaces as a single separator. */
+/* like p_strsplit(), but treats multiple adjacent separators as a single
+   separator. */
+char **p_strsplit_spaces(pool_t pool, const char *data,
+			 const char *separators);
 const char **t_strsplit_spaces(const char *data, const char *separators);
 
 const char *dec2str(uintmax_t number);
@@ -69,6 +73,8 @@
 unsigned int strarray_length(const char *const *arr);
 /* Return all strings from array joined into one string. */
 const char *t_strarray_join(const char *const *arr, const char *separator);
+/* Removes a value from NULL-terminated string array. Returns TRUE if found. */
+int strarray_remove(const char **arr, const char *value);
 
 /* INTERNAL */
 char *_vstrconcat(const char *str1, va_list args, size_t *ret_len);



More information about the dovecot-cvs mailing list