NeoMutt  2024-02-01-25-ga71e95
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
slist.c
Go to the documentation of this file.
1
36#include "config.h"
37#include <stddef.h>
38#include "config/types.h"
39#include "slist.h"
40#include "buffer.h"
41#include "list.h"
42#include "memory.h"
43#include "queue.h"
44#include "string2.h"
45
51struct Slist *slist_new(uint32_t flags)
52{
53 struct Slist *list = mutt_mem_calloc(1, sizeof(*list));
54 list->flags = flags;
55 STAILQ_INIT(&list->head);
56
57 return list;
58}
59
66struct Slist *slist_add_string(struct Slist *list, const char *str)
67{
68 if (!list)
69 return NULL;
70
71 if (str && (str[0] == '\0'))
72 str = NULL;
73
74 if (!str && !(list->flags & D_SLIST_ALLOW_EMPTY))
75 return list;
76
78 list->count++;
79
80 return list;
81}
82
89bool slist_equal(const struct Slist *a, const struct Slist *b)
90{
91 if (!a && !b) /* both empty */
92 return true;
93 if (!a ^ !b) /* one is empty, but not the other */
94 return false;
95 if (a->count != b->count)
96 return false;
97
98 return mutt_list_equal(&a->head, &b->head);
99}
100
106struct Slist *slist_dup(const struct Slist *list)
107{
108 if (!list)
109 return NULL;
110
111 struct Slist *list_new = slist_new(list->flags);
112
113 struct ListNode *np = NULL;
114 STAILQ_FOREACH(np, &list->head, entries)
115 {
117 }
118 list_new->count = list->count;
119 return list_new;
120}
121
126void slist_free(struct Slist **ptr)
127{
128 if (!ptr || !*ptr)
129 return;
130
131 struct Slist *slist = *ptr;
132 mutt_list_free(&slist->head);
133
134 FREE(ptr);
135}
136
142bool slist_is_empty(const struct Slist *list)
143{
144 if (!list)
145 return true;
146
147 return list->count == 0;
148}
149
156bool slist_is_member(const struct Slist *list, const char *str)
157{
158 if (!list)
159 return false;
160
161 if (!str && !(list->flags & D_SLIST_ALLOW_EMPTY))
162 return false;
163
164 struct ListNode *np = NULL;
165 STAILQ_FOREACH(np, &list->head, entries)
166 {
167 if (mutt_str_equal(np->data, str))
168 return true;
169 }
170 return false;
171}
172
179struct Slist *slist_parse(const char *str, uint32_t flags)
180{
181 char *src = mutt_str_dup(str);
182 if (!src && !(flags & D_SLIST_ALLOW_EMPTY))
183 return NULL;
184
185 char sep = ' ';
187 sep = ',';
189 sep = ':';
190
191 struct Slist *list = mutt_mem_calloc(1, sizeof(struct Slist));
192 list->flags = flags;
193 STAILQ_INIT(&list->head);
194
195 if (!src)
196 return list;
197
198 char *start = src;
199 for (char *p = start; *p; p++)
200 {
201 if ((p[0] == '\\') && (p[1] != '\0'))
202 {
203 p++;
204 continue;
205 }
206
207 if (p[0] == sep)
208 {
209 p[0] = '\0';
210 if (slist_is_member(list, start))
211 {
212 start = p + 1;
213 continue;
214 }
216 list->count++;
217 start = p + 1;
218 }
219 }
220
221 if (!slist_is_member(list, start))
222 {
224 list->count++;
225 }
226
227 FREE(&src);
228 return list;
229}
230
237struct Slist *slist_remove_string(struct Slist *list, const char *str)
238{
239 if (!list)
240 return NULL;
241 if (!str && !(list->flags & D_SLIST_ALLOW_EMPTY))
242 return list;
243
244 struct ListNode *prev = NULL;
245 struct ListNode *np = NULL;
246 struct ListNode *tmp = NULL;
247 STAILQ_FOREACH_SAFE(np, &list->head, entries, tmp)
248 {
249 if (mutt_str_equal(np->data, str))
250 {
251 if (prev)
252 STAILQ_REMOVE_AFTER(&list->head, prev, entries);
253 else
254 STAILQ_REMOVE_HEAD(&list->head, entries);
255 FREE(&np->data);
256 FREE(&np);
257 list->count--;
258 break;
259 }
260 prev = np;
261 }
262 return list;
263}
264
271int slist_to_buffer(const struct Slist *list, struct Buffer *buf)
272{
273 if (!list || !buf || (list->count == 0))
274 return 0;
275
276 struct ListNode *np = NULL;
277 STAILQ_FOREACH(np, &list->head, entries)
278 {
279 buf_addstr(buf, np->data);
280 if (STAILQ_NEXT(np, entries))
281 {
282 const int sep = (list->flags & D_SLIST_SEP_MASK);
283 if (sep == D_SLIST_SEP_COMMA)
284 buf_addch(buf, ',');
285 else if (sep == D_SLIST_SEP_COLON)
286 buf_addch(buf, ':');
287 else
288 buf_addch(buf, ' ');
289 }
290 }
291
292 return list->count;
293}
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:258
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:243
General purpose object for storing and parsing strings.
struct ListNode * mutt_list_insert_tail(struct ListHead *h, char *s)
Append a string to the end of a List.
Definition: list.c:64
bool mutt_list_equal(const struct ListHead *ah, const struct ListHead *bh)
Compare two string lists.
Definition: list.c:218
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition: list.c:122
Singly-linked list type.
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
Memory management wrappers.
#define FREE(x)
Definition: memory.h:45
struct Slist * slist_remove_string(struct Slist *list, const char *str)
Remove a string from a list.
Definition: slist.c:237
struct Slist * slist_parse(const char *str, uint32_t flags)
Parse a list of strings into a list.
Definition: slist.c:179
bool slist_is_empty(const struct Slist *list)
Is the slist empty?
Definition: slist.c:142
void slist_free(struct Slist **ptr)
Free an Slist object.
Definition: slist.c:126
bool slist_equal(const struct Slist *a, const struct Slist *b)
Compare two string lists.
Definition: slist.c:89
struct Slist * slist_add_string(struct Slist *list, const char *str)
Add a string to a list.
Definition: slist.c:66
bool slist_is_member(const struct Slist *list, const char *str)
Is a string a member of a list?
Definition: slist.c:156
struct Slist * slist_dup(const struct Slist *list)
Create a copy of an Slist object.
Definition: slist.c:106
int slist_to_buffer(const struct Slist *list, struct Buffer *buf)
Export an Slist to a Buffer.
Definition: slist.c:271
struct Slist * slist_new(uint32_t flags)
Create a new string list.
Definition: slist.c:51
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:709
#define STAILQ_REMOVE_HEAD(head, field)
Definition: queue.h:422
#define STAILQ_INIT(head)
Definition: queue.h:372
#define STAILQ_REMOVE_AFTER(head, elm, field)
Definition: queue.h:416
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:362
#define STAILQ_NEXT(elm, field)
Definition: queue.h:400
A separated list of strings.
String manipulation functions.
String manipulation buffer.
Definition: buffer.h:36
A List node for strings.
Definition: list.h:35
char * data
String.
Definition: list.h:36
String list.
Definition: slist.h:37
struct ListHead head
List containing values.
Definition: slist.h:38
size_t count
Number of values in list.
Definition: slist.h:39
uint32_t flags
Flags controlling list, e.g. D_SLIST_SEP_SPACE.
Definition: slist.h:40
Constants for all the config types.
#define D_SLIST_SEP_COMMA
Slist items are comma-separated.
Definition: types.h:108
#define D_SLIST_SEP_COLON
Slist items are colon-separated.
Definition: types.h:109
#define D_SLIST_ALLOW_EMPTY
Slist may be empty.
Definition: types.h:113
#define D_SLIST_SEP_MASK
Definition: types.h:110