dovecot-2.2-pigeonhole: lib-sieve: Implemented the index stringl...

pigeonhole at rename-it.nl pigeonhole at rename-it.nl
Wed Nov 12 21:11:21 UTC 2014


details:   http://hg.rename-it.nl/dovecot-2.2-pigeonhole/rev/519972b17360
changeset: 1960:519972b17360
user:      Stephan Bosch <stephan at rename-it.nl>
date:      Wed Nov 12 22:10:23 2014 +0100
description:
lib-sieve: Implemented the index stringlist class which selects an item of the parent at a particular index.

diffstat:

 src/lib-sieve/sieve-stringlist.c |  132 +++++++++++++++++++++++++++++++++++++++
 src/lib-sieve/sieve-stringlist.h |    8 ++
 2 files changed, 140 insertions(+), 0 deletions(-)

diffs (165 lines):

diff -r c8e8321f2891 -r 519972b17360 src/lib-sieve/sieve-stringlist.c
--- a/src/lib-sieve/sieve-stringlist.c	Wed Nov 12 22:09:44 2014 +0100
+++ b/src/lib-sieve/sieve-stringlist.c	Wed Nov 12 22:10:23 2014 +0100
@@ -49,9 +49,11 @@
 		int count = 0;
 		int ret;
 
+		sieve_stringlist_reset(strlist);
 		while ( (ret=sieve_stringlist_next_item(strlist, &item)) > 0 ) {
 			count++;
 		}
+		sieve_stringlist_reset(strlist);
 
 		return ( ret < 0 ? -1 : count );
 	}
@@ -141,3 +143,133 @@
 
 	return ( strlist->count_empty || str_len(strlist->value) > 0 );
 }
+
+/*
+ * Index Stringlist
+ */
+
+/* Object */
+
+static int sieve_index_stringlist_next_item
+	(struct sieve_stringlist *_strlist, string_t **str_r);
+static void sieve_index_stringlist_reset
+	(struct sieve_stringlist *_strlist);
+static int sieve_index_stringlist_get_length
+	(struct sieve_stringlist *_strlist);
+static void sieve_index_stringlist_set_trace
+	(struct sieve_stringlist *strlist, bool trace);
+
+struct sieve_index_stringlist {
+	struct sieve_stringlist strlist;
+
+	struct sieve_stringlist *source;
+
+	int index;
+	unsigned int end:1;
+};
+
+struct sieve_stringlist *sieve_index_stringlist_create
+(const struct sieve_runtime_env *renv, struct sieve_stringlist *source,
+	int index)
+{
+	struct sieve_index_stringlist *strlist;
+
+	strlist = t_new(struct sieve_index_stringlist, 1);
+	strlist->strlist.runenv = renv;
+	strlist->strlist.exec_status = SIEVE_EXEC_OK;
+	strlist->strlist.next_item = sieve_index_stringlist_next_item;
+	strlist->strlist.reset = sieve_index_stringlist_reset;
+	strlist->strlist.get_length = sieve_index_stringlist_get_length;
+	strlist->strlist.set_trace = sieve_index_stringlist_set_trace;
+	strlist->source = source;
+	strlist->index = index;
+
+	return &strlist->strlist;
+}
+
+/* Implementation */
+
+static int sieve_index_stringlist_next_item
+(struct sieve_stringlist *_strlist, string_t **str_r)
+{
+	struct sieve_index_stringlist *strlist =
+		(struct sieve_index_stringlist *)_strlist;
+	int index, ret;
+
+	if ( strlist->end ) {
+		*str_r = NULL;
+		return 0;
+	}
+
+	if ( strlist->index < 0 ) {
+		int len = sieve_stringlist_get_length(strlist->source);
+		if (len < 0) {
+			_strlist->exec_status = strlist->source->exec_status;
+			return -1;
+		}
+
+		if (len < -strlist->index) {
+			*str_r = NULL;
+			strlist->end = TRUE;
+			return 0;
+		}
+		index = len + 1 + strlist->index;
+	} else {
+		index = strlist->index;
+	}
+
+	while ( index > 0 ) {
+		if ( (ret=sieve_stringlist_next_item(strlist->source, str_r)) <= 0 ) {
+			if (ret < 0)
+				_strlist->exec_status = strlist->source->exec_status;
+			return ret;
+		}
+	
+		index--;
+	}
+		
+	strlist->end = TRUE;
+	return 1;
+}
+
+static void sieve_index_stringlist_reset
+(struct sieve_stringlist *_strlist)
+{
+	struct sieve_index_stringlist *strlist =
+		(struct sieve_index_stringlist *)_strlist;
+
+	sieve_stringlist_reset(strlist->source);
+	strlist->end = FALSE;
+}
+
+static int sieve_index_stringlist_get_length
+(struct sieve_stringlist *_strlist)
+{
+	struct sieve_index_stringlist *strlist =
+		(struct sieve_index_stringlist *)_strlist;
+	int len;
+
+	len = sieve_stringlist_get_length(strlist->source);
+	if (len < 0) {
+		_strlist->exec_status = strlist->source->exec_status;
+		return -1;
+	}
+
+	if ( strlist->index < 0 ) {
+		if ( -strlist->index >= len )
+			return 0;
+	} else if ( strlist->index >= len ) {
+			return 0;
+	}
+
+	return 1;
+}
+
+static void sieve_index_stringlist_set_trace
+(struct sieve_stringlist *_strlist, bool trace)
+{
+	struct sieve_index_stringlist *strlist =
+		(struct sieve_index_stringlist *)_strlist;
+
+	sieve_stringlist_set_trace(strlist->source, trace);
+}
diff -r c8e8321f2891 -r 519972b17360 src/lib-sieve/sieve-stringlist.h
--- a/src/lib-sieve/sieve-stringlist.h	Wed Nov 12 22:09:44 2014 +0100
+++ b/src/lib-sieve/sieve-stringlist.h	Wed Nov 12 22:10:23 2014 +0100
@@ -66,4 +66,12 @@
 struct sieve_stringlist *sieve_single_stringlist_create_cstr
 (const struct sieve_runtime_env *renv, const char *cstr, bool count_empty);
 
+/*
+ * Index Stringlist
+ */
+
+struct sieve_stringlist *sieve_index_stringlist_create
+	(const struct sieve_runtime_env *renv, struct sieve_stringlist *source,
+		int index);
+
 #endif /* __SIEVE_STRINGLIST_H */


More information about the dovecot-cvs mailing list