NeoMutt
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
slist.c File Reference

A separated list of strings. More...

#include "config.h"
#include <stddef.h>
#include "slist.h"
#include "buffer.h"
#include "list.h"
#include "memory.h"
#include "queue.h"
#include "string2.h"
+ Include dependency graph for slist.c:

Go to the source code of this file.

Functions

struct Slistslist_new (uint32_t flags)
 Create a new string list.
 
struct Slistslist_add_list (struct Slist *list, const struct Slist *add)
 Add a list to another list.
 
struct Slistslist_add_string (struct Slist *list, const char *str)
 Add a string to a list.
 
bool slist_compare (const struct Slist *a, const struct Slist *b)
 Compare two string lists.
 
struct Slistslist_dup (const struct Slist *list)
 Create a copy of an Slist object.
 
struct Slistslist_empty (struct Slist **list)
 Empty out an Slist object.
 
void slist_free (struct Slist **ptr)
 Free an Slist object.
 
bool slist_is_empty (const struct Slist *list)
 Is the slist empty?
 
bool slist_is_member (const struct Slist *list, const char *str)
 Is a string a member of a list?
 
struct Slistslist_parse (const char *str, uint32_t flags)
 Parse a list of strings into a list.
 
struct Slistslist_remove_string (struct Slist *list, const char *str)
 Remove a string from a list.
 
int slist_to_buffer (const struct Slist *list, struct Buffer *buf)
 Export an Slist to a Buffer.
 

Detailed Description

A separated list of strings.

Authors
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file slist.c.

Function Documentation

◆ slist_new()

struct Slist * slist_new ( uint32_t  flags)

Create a new string list.

Parameters
flagsFlag to set, e.g. SLIST_SEP_COMMA
Return values
ptrNew string list

Definition at line 43 of file slist.c.

44{
45 struct Slist *list = mutt_mem_calloc(1, sizeof(*list));
46 list->flags = flags;
47 STAILQ_INIT(&list->head);
48
49 return list;
50}
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define STAILQ_INIT(head)
Definition: queue.h:372
String list.
Definition: slist.h:47
struct ListHead head
List containing values.
Definition: slist.h:48
uint32_t flags
Flags controlling list, e.g. SLIST_SEP_SPACE.
Definition: slist.h:50
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ slist_add_list()

struct Slist * slist_add_list ( struct Slist list,
const struct Slist add 
)

Add a list to another list.

Parameters
listString list to add to
addString list to add
Return values
ptrModified list

Definition at line 58 of file slist.c.

59{
60 if (!add)
61 return list;
62 if (!list)
63 return slist_dup(add);
64
65 struct ListNode *np = NULL;
66 STAILQ_FOREACH(np, &add->head, entries)
67 {
68 mutt_list_insert_tail(&list->head, mutt_str_dup((char *) np->data));
69 list->count++;
70 }
71 return list;
72}
struct ListNode * mutt_list_insert_tail(struct ListHead *h, char *s)
Append a string to the end of a List.
Definition: list.c:64
struct Slist * slist_dup(const struct Slist *list)
Create a copy of an Slist object.
Definition: slist.c:120
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:251
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
A List node for strings.
Definition: list.h:35
char * data
String.
Definition: list.h:36
size_t count
Number of values in list.
Definition: slist.h:49
+ Here is the call graph for this function:

◆ slist_add_string()

struct Slist * slist_add_string ( struct Slist list,
const char *  str 
)

Add a string to a list.

Parameters
listList to modify
strString to add
Return values
ptrModified list

Definition at line 80 of file slist.c.

81{
82 if (!list)
83 return NULL;
84
85 if (str && (str[0] == '\0'))
86 str = NULL;
87
88 if (!str && !(list->flags & SLIST_ALLOW_EMPTY))
89 return list;
90
92 list->count++;
93
94 return list;
95}
#define SLIST_ALLOW_EMPTY
Definition: slist.h:40
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ slist_compare()

bool slist_compare ( const struct Slist a,
const struct Slist b 
)

Compare two string lists.

Parameters
aFirst list
bSecond list
Return values
trueThey are identical

Definition at line 103 of file slist.c.

104{
105 if (!a && !b) /* both empty */
106 return true;
107 if (!a ^ !b) /* one is empty, but not the other */
108 return false;
109 if (a->count != b->count)
110 return false;
111
112 return mutt_list_compare(&a->head, &b->head);
113}
bool mutt_list_compare(const struct ListHead *ah, const struct ListHead *bh)
Compare two string lists.
Definition: list.c:218
+ Here is the call graph for this function:

◆ slist_dup()

struct Slist * slist_dup ( const struct Slist list)

Create a copy of an Slist object.

Parameters
listSlist to duplicate
Return values
ptrNew Slist object

Definition at line 120 of file slist.c.

121{
122 if (!list)
123 return NULL;
124
125 struct Slist *list_new = slist_new(list->flags);
126
127 struct ListNode *np = NULL;
128 STAILQ_FOREACH(np, &list->head, entries)
129 {
131 }
132 list_new->count = list->count;
133 return list_new;
134}
struct Slist * slist_new(uint32_t flags)
Create a new string list.
Definition: slist.c:43
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ slist_empty()

struct Slist * slist_empty ( struct Slist **  list)

Empty out an Slist object.

Parameters
listSlist to empty
Return values
ptrNew Slist object

Definition at line 141 of file slist.c.

142{
143 if (!list || !*list)
144 return NULL;
145
146 mutt_list_free(&(*list)->head);
147
148 if ((*list)->flags & SLIST_ALLOW_EMPTY)
149 {
150 (*list)->count = 0;
151 return *list;
152 }
153
154 FREE(list);
155 return NULL;
156}
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition: list.c:122
#define FREE(x)
Definition: memory.h:45
+ Here is the call graph for this function:

◆ slist_free()

void slist_free ( struct Slist **  ptr)

Free an Slist object.

Parameters
ptrSlist to free

Definition at line 162 of file slist.c.

163{
164 if (!ptr || !*ptr)
165 return;
166
167 struct Slist *slist = *ptr;
168 mutt_list_free(&slist->head);
169
170 FREE(ptr);
171}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ slist_is_empty()

bool slist_is_empty ( const struct Slist list)

Is the slist empty?

Parameters
listList to check
Return values
trueList is empty

Definition at line 178 of file slist.c.

179{
180 if (!list)
181 return true;
182
183 return list->count == 0;
184}
+ Here is the caller graph for this function:

◆ slist_is_member()

bool slist_is_member ( const struct Slist list,
const char *  str 
)

Is a string a member of a list?

Parameters
listList to modify
strString to find
Return values
trueString is in the list

Definition at line 192 of file slist.c.

193{
194 if (!list)
195 return false;
196
197 if (!str && !(list->flags & SLIST_ALLOW_EMPTY))
198 return false;
199
200 struct ListNode *np = NULL;
201 STAILQ_FOREACH(np, &list->head, entries)
202 {
203 if (mutt_str_equal(np->data, str))
204 return true;
205 }
206 return false;
207}
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:798
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ slist_parse()

struct Slist * slist_parse ( const char *  str,
uint32_t  flags 
)

Parse a list of strings into a list.

Parameters
strString of strings
flagsFlags, e.g. SLIST_ALLOW_EMPTY
Return values
ptrNew Slist object

Definition at line 215 of file slist.c.

216{
217 char *src = mutt_str_dup(str);
218 if (!src && !(flags & SLIST_ALLOW_EMPTY))
219 return NULL;
220
221 char sep = ' ';
222 if ((flags & SLIST_SEP_MASK) == SLIST_SEP_COMMA)
223 sep = ',';
224 else if ((flags & SLIST_SEP_MASK) == SLIST_SEP_COLON)
225 sep = ':';
226
227 struct Slist *list = mutt_mem_calloc(1, sizeof(struct Slist));
228 list->flags = flags;
229 STAILQ_INIT(&list->head);
230
231 if (!src)
232 return list;
233
234 char *start = src;
235 for (char *p = start; *p; p++)
236 {
237 if ((p[0] == '\\') && (p[1] != '\0'))
238 {
239 p++;
240 continue;
241 }
242
243 if (p[0] == sep)
244 {
245 p[0] = '\0';
246 if (slist_is_member(list, start))
247 {
248 start = p + 1;
249 continue;
250 }
252 list->count++;
253 start = p + 1;
254 }
255 }
256
257 if (!slist_is_member(list, start))
258 {
260 list->count++;
261 }
262
263 FREE(&src);
264 return list;
265}
bool slist_is_member(const struct Slist *list, const char *str)
Is a string a member of a list?
Definition: slist.c:192
#define SLIST_SEP_COMMA
Definition: slist.h:34
#define SLIST_SEP_COLON
Definition: slist.h:35
#define SLIST_SEP_MASK
Definition: slist.h:37
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ slist_remove_string()

struct Slist * slist_remove_string ( struct Slist list,
const char *  str 
)

Remove a string from a list.

Parameters
listList to modify
strString to remove
Return values
ptrModified list

Definition at line 273 of file slist.c.

274{
275 if (!list)
276 return NULL;
277 if (!str && !(list->flags & SLIST_ALLOW_EMPTY))
278 return list;
279
280 struct ListNode *prev = NULL;
281 struct ListNode *np = NULL;
282 struct ListNode *tmp = NULL;
283 STAILQ_FOREACH_SAFE(np, &list->head, entries, tmp)
284 {
285 if (mutt_str_equal(np->data, str))
286 {
287 if (prev)
288 STAILQ_REMOVE_AFTER(&list->head, prev, entries);
289 else
290 STAILQ_REMOVE_HEAD(&list->head, entries);
291 FREE(&np->data);
292 FREE(&np);
293 list->count--;
294 break;
295 }
296 prev = np;
297 }
298 return list;
299}
#define STAILQ_REMOVE_HEAD(head, field)
Definition: queue.h:422
#define STAILQ_REMOVE_AFTER(head, elm, field)
Definition: queue.h:416
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:362
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ slist_to_buffer()

int slist_to_buffer ( const struct Slist list,
struct Buffer buf 
)

Export an Slist to a Buffer.

Parameters
listList to export
bufBuffer for the results
Return values
numNumber of strings written to Buffer

Definition at line 307 of file slist.c.

308{
309 if (!list || !buf || (list->count == 0))
310 return 0;
311
312 struct ListNode *np = NULL;
313 STAILQ_FOREACH(np, &list->head, entries)
314 {
315 buf_addstr(buf, np->data);
316 if (STAILQ_NEXT(np, entries))
317 {
318 const int sep = (list->flags & SLIST_SEP_MASK);
319 if (sep == SLIST_SEP_COMMA)
320 buf_addch(buf, ',');
321 else if (sep == SLIST_SEP_COLON)
322 buf_addch(buf, ':');
323 else
324 buf_addch(buf, ' ');
325 }
326 }
327
328 return list->count;
329}
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:253
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:238
#define STAILQ_NEXT(elm, field)
Definition: queue.h:400
+ Here is the call graph for this function:
+ Here is the caller graph for this function: