NeoMutt  2025-01-09-81-g753ae0
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
slist.h File Reference

A separated list of strings. More...

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "list.h"
+ Include dependency graph for slist.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  Slist
 String list. More...
 

Functions

struct Slistslist_add_string (struct Slist *list, const char *str)
 Add a string to a list.
 
struct Slistslist_dup (const struct Slist *list)
 Create a copy of an Slist object.
 
bool slist_equal (const struct Slist *a, const struct Slist *b)
 Compare two string lists.
 
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_new (uint32_t flags)
 Create a new string 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.h.

Function Documentation

◆ 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
Note
String will be copied

Definition at line 68 of file slist.c.

69{
70 if (!list)
71 return NULL;
72
73 if (str && (str[0] == '\0'))
74 str = NULL;
75
76 if (!str && !(list->flags & D_SLIST_ALLOW_EMPTY))
77 return list;
78
80 list->count++;
81
82 return list;
83}
struct ListNode * mutt_list_insert_tail(struct ListHead *h, char *s)
Append a string to the end of a List.
Definition: list.c:65
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:254
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
#define D_SLIST_ALLOW_EMPTY
Slist may be empty.
Definition: types.h:115
+ Here is the call graph for this function:
+ Here is the caller 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 108 of file slist.c.

109{
110 if (!list)
111 return NULL;
112
113 struct Slist *list_new = slist_new(list->flags);
114
115 mutt_list_copy_tail(&list_new->head, &list->head);
116 list_new->count = list->count;
117 return list_new;
118}
void mutt_list_copy_tail(struct ListHead *dst, const struct ListHead *src)
Copy a list into another list.
Definition: list.c:275
struct Slist * slist_new(uint32_t flags)
Create a new string list.
Definition: slist.c:51
String list.
Definition: slist.h:37
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ slist_equal()

bool slist_equal ( 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 91 of file slist.c.

92{
93 if (!a && !b) /* both empty */
94 return true;
95 if (!a ^ !b) /* one is empty, but not the other */
96 return false;
97 if (a->count != b->count)
98 return false;
99
100 return mutt_list_equal(&a->head, &b->head);
101}
bool mutt_list_equal(const struct ListHead *ah, const struct ListHead *bh)
Compare two string lists.
Definition: list.c:217
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ slist_free()

void slist_free ( struct Slist **  ptr)

Free an Slist object.

Parameters
ptrSlist to free

Definition at line 124 of file slist.c.

125{
126 if (!ptr || !*ptr)
127 return;
128
129 struct Slist *slist = *ptr;
130 mutt_list_free(&slist->head);
131
132 FREE(ptr);
133}
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition: list.c:123
#define FREE(x)
Definition: memory.h:55
+ 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 140 of file slist.c.

141{
142 if (!list)
143 return true;
144
145 return list->count == 0;
146}
+ 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 154 of file slist.c.

155{
156 if (!list)
157 return false;
158
159 if (!str && !(list->flags & D_SLIST_ALLOW_EMPTY))
160 return false;
161
162 struct ListNode *np = NULL;
163 STAILQ_FOREACH(np, &list->head, entries)
164 {
165 if (mutt_str_equal(np->data, str))
166 return true;
167 }
168 return false;
169}
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:661
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:390
A List node for strings.
Definition: list.h:37
char * data
String.
Definition: list.h:38
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ slist_new()

struct Slist * slist_new ( uint32_t  flags)

Create a new string list.

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

Definition at line 51 of file slist.c.

52{
53 struct Slist *list = MUTT_MEM_CALLOC(1, struct Slist);
54 list->flags = flags;
55 STAILQ_INIT(&list->head);
56
57 return list;
58}
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
#define STAILQ_INIT(head)
Definition: queue.h:410
+ 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. D_SLIST_ALLOW_EMPTY
Return values
ptrNew Slist object

Definition at line 177 of file slist.c.

178{
179 char *src = mutt_str_dup(str);
180 if (!src && !(flags & D_SLIST_ALLOW_EMPTY))
181 return NULL;
182
183 char sep = ' ';
184 if ((flags & D_SLIST_SEP_MASK) == D_SLIST_SEP_COMMA)
185 sep = ',';
186 else if ((flags & D_SLIST_SEP_MASK) == D_SLIST_SEP_COLON)
187 sep = ':';
188
189 struct Slist *list = MUTT_MEM_CALLOC(1, struct Slist);
190 list->flags = flags;
191 STAILQ_INIT(&list->head);
192
193 if (!src)
194 return list;
195
196 char *start = src;
197 for (char *p = start; *p; p++)
198 {
199 if ((p[0] == '\\') && (p[1] != '\0'))
200 {
201 p++;
202 continue;
203 }
204
205 if (p[0] == sep)
206 {
207 p[0] = '\0';
208 if (slist_is_member(list, start))
209 {
210 start = p + 1;
211 continue;
212 }
214 list->count++;
215 start = p + 1;
216 }
217 }
218
219 if (!slist_is_member(list, start))
220 {
222 list->count++;
223 }
224
225 FREE(&src);
226 return list;
227}
bool slist_is_member(const struct Slist *list, const char *str)
Is a string a member of a list?
Definition: slist.c:154
#define D_SLIST_SEP_COMMA
Slist items are comma-separated.
Definition: types.h:110
#define D_SLIST_SEP_COLON
Slist items are colon-separated.
Definition: types.h:111
#define D_SLIST_SEP_MASK
Definition: types.h:112
+ 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 235 of file slist.c.

236{
237 if (!list)
238 return NULL;
239 if (!str && !(list->flags & D_SLIST_ALLOW_EMPTY))
240 return list;
241
242 struct ListNode *prev = NULL;
243 struct ListNode *np = NULL;
244 struct ListNode *tmp = NULL;
245 STAILQ_FOREACH_SAFE(np, &list->head, entries, tmp)
246 {
247 if (mutt_str_equal(np->data, str))
248 {
249 if (prev)
250 STAILQ_REMOVE_AFTER(&list->head, prev, entries);
251 else
252 STAILQ_REMOVE_HEAD(&list->head, entries);
253 FREE(&np->data);
254 FREE(&np);
255 list->count--;
256 break;
257 }
258 prev = np;
259 }
260 return list;
261}
#define STAILQ_REMOVE_HEAD(head, field)
Definition: queue.h:461
#define STAILQ_REMOVE_AFTER(head, elm, field)
Definition: queue.h:455
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:400
+ 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 269 of file slist.c.

270{
271 if (!list || !buf || (list->count == 0))
272 return 0;
273
274 struct ListNode *np = NULL;
275 STAILQ_FOREACH(np, &list->head, entries)
276 {
277 buf_addstr(buf, np->data);
278 if (STAILQ_NEXT(np, entries))
279 {
280 const int sep = (list->flags & D_SLIST_SEP_MASK);
281 if (sep == D_SLIST_SEP_COMMA)
282 buf_addch(buf, ',');
283 else if (sep == D_SLIST_SEP_COLON)
284 buf_addch(buf, ':');
285 else
286 buf_addch(buf, ' ');
287 }
288 }
289
290 return list->count;
291}
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:241
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:226
#define STAILQ_NEXT(elm, field)
Definition: queue.h:439
+ Here is the call graph for this function:
+ Here is the caller graph for this function: