File: sieve-stringlist.c

package info (click to toggle)
dovecot 1:2.2.13-12~deb8u4
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 38,792 kB
  • sloc: ansic: 341,472; sh: 16,920; makefile: 5,393; cpp: 1,474; perl: 265; xml: 44; python: 34; pascal: 27
file content (143 lines) | stat: -rw-r--r-- 3,223 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* Copyright (c) 2002-2013 Pigeonhole authors, see the included COPYING file
 */

#include "lib.h"
#include "array.h"
#include "str.h"

#include "sieve-common.h"
#include "sieve-stringlist.h"

/*
 * Default implementation
 */

int sieve_stringlist_read_all
(struct sieve_stringlist *strlist, pool_t pool,
	const char * const **list_r)
{
	if ( strlist->read_all == NULL ) {
		ARRAY(const char *) items;
		string_t *item;
		int ret;

		sieve_stringlist_reset(strlist);

		p_array_init(&items, pool, 4);

		item = NULL;
		while ( (ret=sieve_stringlist_next_item(strlist, &item)) > 0 ) {
			const char *stritem = p_strdup(pool, str_c(item));

			array_append(&items, &stritem, 1);
		}

		(void)array_append_space(&items);
		*list_r = array_idx(&items, 0);

		return ( ret < 0 ? -1 : 1 );
	}

	return strlist->read_all(strlist, pool, list_r);
}

int sieve_stringlist_get_length
(struct sieve_stringlist *strlist)
{
	if ( strlist->get_length == NULL ) {
		string_t *item;
		int count = 0;
		int ret;

		while ( (ret=sieve_stringlist_next_item(strlist, &item)) > 0 ) {
			count++;
		}

		return ( ret < 0 ? -1 : count );
	}

	return strlist->get_length(strlist);
}

/*
 * Single Stringlist
 */

/* Object */

static int sieve_single_stringlist_next_item
	(struct sieve_stringlist *_strlist, string_t **str_r);
static void sieve_single_stringlist_reset
	(struct sieve_stringlist *_strlist);
static int sieve_single_stringlist_get_length
	(struct sieve_stringlist *_strlist);

struct sieve_single_stringlist {
	struct sieve_stringlist strlist;

	string_t *value;

	unsigned int end:1;
	unsigned int count_empty:1;
};

struct sieve_stringlist *sieve_single_stringlist_create
(const struct sieve_runtime_env *renv, string_t *str, bool count_empty)
{
	struct sieve_single_stringlist *strlist;

	strlist = t_new(struct sieve_single_stringlist, 1);
	strlist->strlist.runenv = renv;
	strlist->strlist.exec_status = SIEVE_EXEC_OK;
	strlist->strlist.next_item = sieve_single_stringlist_next_item;
	strlist->strlist.reset = sieve_single_stringlist_reset;
	strlist->strlist.get_length = sieve_single_stringlist_get_length;
	strlist->count_empty = count_empty;
	strlist->value = str;

	return &strlist->strlist;
}

struct sieve_stringlist *sieve_single_stringlist_create_cstr
(const struct sieve_runtime_env *renv, const char *cstr, bool count_empty)
{
	string_t *str = t_str_new_const(cstr, strlen(cstr));

	return sieve_single_stringlist_create(renv, str, count_empty);
}

/* Implementation */

static int sieve_single_stringlist_next_item
(struct sieve_stringlist *_strlist, string_t **str_r)
{
	struct sieve_single_stringlist *strlist =
		(struct sieve_single_stringlist *)_strlist;

	if ( strlist->end ) {
		*str_r = NULL;
		return 0;
	}

	*str_r = strlist->value;
	strlist->end = TRUE;
	return 1;
}

static void sieve_single_stringlist_reset
(struct sieve_stringlist *_strlist)
{
	struct sieve_single_stringlist *strlist =
		(struct sieve_single_stringlist *)_strlist;

	strlist->end = FALSE;
}

static int sieve_single_stringlist_get_length
(struct sieve_stringlist *_strlist)
{
	struct sieve_single_stringlist *strlist =
		(struct sieve_single_stringlist *)_strlist;

	return ( strlist->count_empty || str_len(strlist->value) > 0 );
}