]> Pileus Git - ~andy/fetchmail/blob - idlist.c
8a7a7c79cb91fb4d97df55978cf5630668d42749
[~andy/fetchmail] / idlist.c
1 /**
2  * \file idlist.c -- string list handling
3  *
4  * For license terms, see the file COPYING in this directory.
5  */
6
7 #include "config.h"
8
9 #include <sys/stat.h>
10 #include <errno.h>
11 #include <stdio.h>
12 #include <limits.h>
13 #if defined(STDC_HEADERS)
14 #include <stdlib.h>
15 #include <string.h>
16 #endif
17 #if defined(HAVE_UNISTD_H)
18 #include <unistd.h>
19 #endif
20
21 #include "fetchmail.h"
22
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. */
26 /*@shared@*/ static
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 */
30 {
31     struct idlist **end;
32
33     /* do it nonrecursively so the list is in the right order */
34     for (end = idl; *end; end = &(*end)->next)
35         continue;
36
37     *end = (struct idlist *)xmalloc(sizeof(struct idlist));
38     (*end)->id = str;
39     (*end)->val.status.mark = status;
40     (*end)->val.status.num = 0;
41     (*end)->next = NULL;
42
43     return end;
44 }
45
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)
49 {
50     return *save_str_quick(idl, str ? xstrdup(str) : NULL, status);
51 }
52
53 /** Free string list \a idl and free each of the id members. */
54 void free_str_list(struct idlist **idl)
55 {
56     struct idlist *i = *idl;
57
58     while(i) {
59         struct idlist *t = i->next;
60         free(i->id);
61         free(i);
62         i = t;
63     }
64     *idl = 0;
65 }
66
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)
69 {
70     struct idlist **end;
71
72     /* do it nonrecursively so the list is in the right order */
73     for (end = idl; *end; end = &(*end)->next)
74         continue;
75
76     *end = (struct idlist *)xmalloc(sizeof(struct idlist));
77     (*end)->id = str1 ? xstrdup(str1) : (char *)NULL;
78     if (str2)
79         (*end)->val.id2 = xstrdup(str2);
80     else
81         (*end)->val.id2 = (char *)NULL;
82     (*end)->next = (struct idlist *)NULL;
83 }
84
85 #ifdef __UNUSED__
86 void free_str_pair_list(struct idlist **idl)
87 /* free the given ID pair list */
88 {
89     if (*idl == (struct idlist *)NULL)
90         return;
91
92     free_idpair_list(&(*idl)->next);
93     free ((*idl)->id);
94     free ((*idl)->val.id2);
95     free(*idl);
96     *idl = (struct idlist *)NULL;
97 }
98 #endif
99
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 */)
104 {
105     struct idlist *walk;
106     if (caseblind) {
107         for( walk = *idl; walk; walk = walk->next )
108             if( strcasecmp( str, walk->id) == 0 )
109                 return walk;
110     } else {
111         for( walk = *idl; walk; walk = walk->next )
112             if( strcmp( str, walk->id) == 0 )
113                 return walk;
114     }
115     return NULL;
116 }
117
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)
120 {
121     int nr;
122     struct idlist *walk;
123
124     if (!str)
125         return -1;
126     for (walk = *idl, nr = 0; walk; nr ++, walk = walk->next)
127         if (strcmp(str, walk->id) == 0)
128             return nr;
129     return -1;
130 }
131
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)
134 {
135     int nr, ret = -1;
136     struct idlist *walk;
137     if ( !str )
138         return -1;
139     for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
140         if( strcmp( str, walk->id) == 0 )
141             ret = nr;
142     return ret;
143 }
144
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)
147 {
148     int nr;
149     struct idlist *walk;
150     if (!str)
151         return;
152     for(walk = *idl, nr = 0; walk; nr ++, walk = walk->next)
153         if (strcmp(str, walk->id) == 0)
154             walk->val.status.mark = val;
155 }
156
157 /** Count the number of elements in the idlist \a idl. 
158  * \return number of elements */
159 int count_list( struct idlist **idl)
160 {
161   if( !*idl )
162     return 0;
163   return 1 + count_list( &(*idl)->next );
164 }
165
166 /** return the \a number'th id string on idlist \a idl */
167 /*@null@*/ char *str_from_nr_list(struct idlist **idl, long number)
168 {
169     if( !*idl  || number < 0)
170         return 0;
171     if( number == 0 )
172         return (*idl)->id;
173     return str_from_nr_list(&(*idl)->next, number-1);
174 }
175
176
177 /** Search idlist \a idl for entry with given \a number.
178  * \return id member of idlist entry. */
179 char *str_find(struct idlist **idl, long number)
180 {
181     if (*idl == (struct idlist *) 0)
182         return((char *) 0);
183     else if (number == (*idl)->val.status.num)
184         return((*idl)->id);
185     else
186         return(str_find(&(*idl)->next, number));
187 }
188
189 /** Search idlist \a idl for entry with given \a number.
190  * \return idlist entry. */
191 struct idlist *id_find(struct idlist **idl, long number)
192 {
193     struct idlist       *idp;
194     for (idp = *idl; idp; idp = idp->next)
195         if (idp->val.status.num == number)
196             return(idp);
197     return(0);
198 }
199
200 /** Return the id of the given \a id in the given idlist \a idl, comparing
201  * case insensitively. \returns the respective other \a idlist member (the one
202  * that was not searched for). */
203 char *idpair_find(struct idlist **idl, const char *id)
204 {
205     if (*idl == (struct idlist *) 0)
206         return((char *) 0);
207     else if (strcasecmp(id, (*idl)->id) == 0)
208         return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
209     else
210         return(idpair_find(&(*idl)->next, id));
211 }
212
213 /** Mark message number \a num on given idlist \a idl as deleted.
214  * \return 1 if found, 0 if not found. */
215 int delete_str(struct idlist **idl, long num)
216 {
217     struct idlist       *idp;
218
219     for (idp = *idl; idp; idp = idp->next)
220         if (idp->val.status.num == num)
221         {
222             idp->val.status.mark = UID_DELETED;
223             return(1);
224         }
225     return(0);
226 }
227
228 /** Copy the given UID list \a idl. \return A newly malloc()ed copy of the list. */
229 struct idlist *copy_str_list(struct idlist *idl)
230 {
231     struct idlist *newnode ;
232
233     if (idl == (struct idlist *)NULL)
234         return(NULL);
235     else
236     {
237         newnode = (struct idlist *)xmalloc(sizeof(struct idlist));
238         memcpy(newnode, idl, sizeof(struct idlist));
239         newnode->next = copy_str_list(idl->next);
240         return(newnode);
241     }
242 }
243
244 /** Append \a nidl to \a idl (does not copy *) */
245 void append_str_list(struct idlist **idl, struct idlist **nidl)
246 {
247     if ((*nidl) == (struct idlist *)NULL || *nidl == *idl)
248         return;
249     else if ((*idl) == (struct idlist *)NULL)
250         *idl = *nidl;
251     else if ((*idl)->next == (struct idlist *)NULL)
252         (*idl)->next = *nidl;
253     else if ((*idl)->next != *nidl)
254         append_str_list(&(*idl)->next, nidl);
255 }
256
257 /* idlist.c ends here */