NeoMutt  2024-04-16-36-g75b6fb
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
slist.h File Reference

A separated list of strings. More...

#include <stddef.h>
#include <stdbool.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

Definition at line 66 of file slist.c.

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}
struct ListNode * mutt_list_insert_tail(struct ListHead *h, char *s)
Append a string to the end of a List.
Definition: list.c:64
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
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:116
+ 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 106 of file slist.c.

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}
struct Slist * slist_new(uint32_t flags)
Create a new string list.
Definition: slist.c:51
#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
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 89 of file slist.c.

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}
bool mutt_list_equal(const struct ListHead *ah, const struct ListHead *bh)
Compare two string lists.
Definition: list.c:218
+ 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 126 of file slist.c.

127{
128 if (!ptr || !*ptr)
129 return;
130
131 struct Slist *slist = *ptr;
132 mutt_list_free(&slist->head);
133
134 FREE(ptr);
135}
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:
+ 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 142 of file slist.c.

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

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}
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:654
+ 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, sizeof(*list));
54 list->flags = flags;
55 STAILQ_INIT(&list->head);
56
57 return list;
58}
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
+ 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. D_SLIST_ALLOW_EMPTY
Return values
ptrNew Slist object

Definition at line 179 of file slist.c.

180{
181 char *src = mutt_str_dup(str);
182 if (!src && !(flags & D_SLIST_ALLOW_EMPTY))
183 return NULL;
184
185 char sep = ' ';
186 if ((flags & D_SLIST_SEP_MASK) == D_SLIST_SEP_COMMA)
187 sep = ',';
188 else if ((flags & D_SLIST_SEP_MASK) == D_SLIST_SEP_COLON)
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}
bool slist_is_member(const struct Slist *list, const char *str)
Is a string a member of a list?
Definition: slist.c:156
#define D_SLIST_SEP_COMMA
Slist items are comma-separated.
Definition: types.h:111
#define D_SLIST_SEP_COLON
Slist items are colon-separated.
Definition: types.h:112
#define D_SLIST_SEP_MASK
Definition: types.h:113
+ 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 237 of file slist.c.

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}
#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 271 of file slist.c.

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:240
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
#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: