2
* \file idlist.c -- string list handling
4
* For license terms, see the file COPYING in this directory.
13
#if defined(STDC_HEADERS)
17
#if defined(HAVE_UNISTD_H)
21
#include "fetchmail.h"
23
/** Save string \a str to idlist \a idl with status \a status.
24
* \return Pointer to the last element of the list to help the quick,
25
* constant-time addition to the list. */
27
struct idlist **save_str_quick(/*@shared@*/ struct idlist **idl,
28
/*@only@*/ char *str /** caller-allocated string */, flag status)
29
/* save a number/UID pair on the given UID list */
33
/* do it nonrecursively so the list is in the right order */
34
for (end = idl; *end; end = &(*end)->next)
37
*end = (struct idlist *)xmalloc(sizeof(struct idlist));
39
(*end)->val.status.mark = status;
40
(*end)->val.status.num = 0;
46
/** Save string \a str to idlist \a idl with status \a status.
47
* \return the end list element for direct modification. */
48
struct idlist *save_str(struct idlist **idl, const char *str /** implicitly strdup()ed */, flag status)
50
return *save_str_quick(idl, str ? xstrdup(str) : NULL, status);
53
/** Free string list \a idl and free each of the id members. */
54
void free_str_list(struct idlist **idl)
56
struct idlist *i = *idl;
59
struct idlist *t = i->next;
67
/** Save an ID pair made of \a str1 and \a str2 on the given idlist \a idl. */
68
void save_str_pair(struct idlist **idl, const char *str1, const char *str2)
72
/* do it nonrecursively so the list is in the right order */
73
for (end = idl; *end; end = &(*end)->next)
76
*end = (struct idlist *)xmalloc(sizeof(struct idlist));
77
(*end)->id = str1 ? xstrdup(str1) : (char *)NULL;
79
(*end)->val.id2 = xstrdup(str2);
81
(*end)->val.id2 = (char *)NULL;
82
(*end)->next = (struct idlist *)NULL;
86
void free_str_pair_list(struct idlist **idl)
87
/* free the given ID pair list */
89
if (*idl == (struct idlist *)NULL)
92
free_idpair_list(&(*idl)->next);
94
free ((*idl)->val.id2);
96
*idl = (struct idlist *)NULL;
100
/** Check if ID \a str is in idlist \a idl. \return idlist entry if found,
101
* NULL if not found. */
102
struct idlist *str_in_list(struct idlist **idl, const char *str,
103
const flag caseblind /** if true, use strcasecmp, if false, use strcmp */)
107
for( walk = *idl; walk; walk = walk->next )
108
if( strcasecmp( str, walk->id) == 0 )
111
for( walk = *idl; walk; walk = walk->next )
112
if( strcmp( str, walk->id) == 0 )
118
/** \return position of first occurrence of \a str in idlist \a idl */
119
int str_nr_in_list(struct idlist **idl, const char *str)
126
for (walk = *idl, nr = 0; walk; nr ++, walk = walk->next)
127
if (strcmp(str, walk->id) == 0)
132
/** \return position of last occurrence of \a str in idlist \a idl */
133
int str_nr_last_in_list( struct idlist **idl, const char *str)
139
for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
140
if( strcmp( str, walk->id) == 0 )
145
/** Update the mark of an id \a str in idlist \a idl to given value \a val. */
146
void str_set_mark( struct idlist **idl, const char *str, const flag val)
152
for(walk = *idl, nr = 0; walk; nr ++, walk = walk->next)
153
if (strcmp(str, walk->id) == 0)
154
walk->val.status.mark = val;
157
/** Count the number of elements in the idlist \a idl.
158
* \return number of elements */
159
int count_list(struct idlist **idl)
164
for (it = *idl ; it ; it = it->next)
170
/** return the \a number'th id string on idlist \a idl */
171
/*@null@*/ char *str_from_nr_list(struct idlist **idl, long number)
173
if( !*idl || number < 0)
177
return str_from_nr_list(&(*idl)->next, number-1);
181
/** Search idlist \a idl for entry with given \a number.
182
* \return id member of idlist entry. */
183
char *str_find(struct idlist **idl, long number)
185
if (*idl == (struct idlist *) 0)
187
else if (number == (*idl)->val.status.num)
190
return(str_find(&(*idl)->next, number));
193
/** Search idlist \a idl for entry with given \a number.
194
* \return idlist entry. */
195
struct idlist *id_find(struct idlist **idl, long number)
198
for (idp = *idl; idp; idp = idp->next)
199
if (idp->val.status.num == number)
204
/** Return the id of the given \a id in the given idlist \a idl, comparing
205
* case insensitively. \returns the respective other \a idlist member (the one
206
* that was not searched for). */
207
char *idpair_find(struct idlist **idl, const char *id)
209
if (*idl == (struct idlist *) 0)
211
else if (strcasecmp(id, (*idl)->id) == 0)
212
return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
214
return(idpair_find(&(*idl)->next, id));
217
/** Mark message number \a num on given idlist \a idl as deleted.
218
* \return 1 if found, 0 if not found. */
219
int delete_str(struct idlist **idl, long num)
223
for (idp = *idl; idp; idp = idp->next)
224
if (idp->val.status.num == num)
226
idp->val.status.mark = UID_DELETED;
232
/** Copy the given UID list \a idl. \return A newly malloc()ed copy of the list. */
233
struct idlist *copy_str_list(struct idlist *idl)
235
struct idlist *newnode ;
237
if (idl == (struct idlist *)NULL)
241
newnode = (struct idlist *)xmalloc(sizeof(struct idlist));
242
memcpy(newnode, idl, sizeof(struct idlist));
243
newnode->next = copy_str_list(idl->next);
248
/** Append \a nidl to \a idl (does not copy *) */
249
void append_str_list(struct idlist **idl, struct idlist **nidl)
251
if ((*nidl) == (struct idlist *)NULL || *nidl == *idl)
253
else if ((*idl) == (struct idlist *)NULL)
255
else if ((*idl)->next == (struct idlist *)NULL)
256
(*idl)->next = *nidl;
257
else if ((*idl)->next != *nidl)
258
append_str_list(&(*idl)->next, nidl);
261
/* idlist.c ends here */