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;
85 /** Check if ID \a str is in idlist \a idl. \return idlist entry if found,
86 * NULL if not found. */
87 struct idlist *str_in_list(struct idlist **idl, const char *str,
88 const flag caseblind /** if true, use strcasecmp, if false, use strcmp */)
92 for( walk = *idl; walk; walk = walk->next )
93 if( strcasecmp( str, walk->id) == 0 )
96 for( walk = *idl; walk; walk = walk->next )
97 if( strcmp( str, walk->id) == 0 )
103 /** \return position of first occurrence of \a str in idlist \a idl */
104 int str_nr_in_list(struct idlist **idl, const char *str)
111 for (walk = *idl, nr = 0; walk; nr ++, walk = walk->next)
112 if (strcmp(str, walk->id) == 0)
117 /** \return position of last occurrence of \a str in idlist \a idl */
118 int str_nr_last_in_list( struct idlist **idl, const char *str)
124 for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
125 if( strcmp( str, walk->id) == 0 )
130 /** Update the mark of an id \a str in idlist \a idl to given value \a val. */
131 void str_set_mark( struct idlist **idl, const char *str, const flag val)
137 for(walk = *idl, nr = 0; walk; nr ++, walk = walk->next)
138 if (strcmp(str, walk->id) == 0)
139 walk->val.status.mark = val;
142 /** Count the number of elements in the idlist \a idl.
143 * \return number of elements */
144 int count_list(struct idlist **idl)
149 for (it = *idl ; it ; it = it->next)
155 /** return the \a number'th id string on idlist \a idl */
156 /*@null@*/ char *str_from_nr_list(struct idlist **idl, long number)
158 if( !*idl || number < 0)
162 return str_from_nr_list(&(*idl)->next, number-1);
166 /** Search idlist \a idl for entry with given \a number.
167 * \return id member of idlist entry. */
168 char *str_find(struct idlist **idl, long number)
170 if (*idl == (struct idlist *) 0)
172 else if (number == (*idl)->val.status.num)
175 return(str_find(&(*idl)->next, number));
178 /** Search idlist \a idl for entry with given \a number.
179 * \return idlist entry. */
180 struct idlist *id_find(struct idlist **idl, long number)
183 for (idp = *idl; idp; idp = idp->next)
184 if (idp->val.status.num == number)
189 /** Return the id of the given \a id in the given idlist \a idl, comparing
190 * case insensitively. \returns the respective other \a idlist member (the one
191 * that was not searched for). */
192 char *idpair_find(struct idlist **idl, const char *id)
194 if (*idl == (struct idlist *) 0)
196 else if (strcasecmp(id, (*idl)->id) == 0)
197 return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
199 return(idpair_find(&(*idl)->next, id));
202 /** Mark message number \a num on given idlist \a idl as deleted.
203 * \return 1 if found, 0 if not found. */
204 int delete_str(struct idlist **idl, long num)
208 for (idp = *idl; idp; idp = idp->next)
209 if (idp->val.status.num == num)
211 idp->val.status.mark = UID_DELETED;
217 /** Copy the given UID list \a idl. \return A newly malloc()ed copy of the list. */
218 struct idlist *copy_str_list(struct idlist *idl)
220 struct idlist *newnode ;
222 if (idl == (struct idlist *)NULL)
226 newnode = (struct idlist *)xmalloc(sizeof(struct idlist));
227 memcpy(newnode, idl, sizeof(struct idlist));
228 newnode->next = copy_str_list(idl->next);
233 /** Append \a nidl to \a idl (does not copy *) */
234 void append_str_list(struct idlist **idl, struct idlist **nidl)
236 if ((*nidl) == (struct idlist *)NULL || *nidl == *idl)
238 else if ((*idl) == (struct idlist *)NULL)
240 else if ((*idl)->next == (struct idlist *)NULL)
241 (*idl)->next = *nidl;
242 else if ((*idl)->next != *nidl)
243 append_str_list(&(*idl)->next, nidl);
246 /* idlist.c ends here */