NeoMutt  2024-12-12-14-g7b49f7
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 "config/types.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_string (struct Slist *list, const char *str)
 Add a string to a list.
 
bool slist_equal (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.
 
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
  • Pietro Cerutti
  • Yousef Akbar
  • наб

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. 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:372
String list.
Definition: slist.h:37
struct ListHead head
List containing values.
Definition: slist.h:38
uint32_t flags
Flags controlling list, e.g. D_SLIST_SEP_SPACE.
Definition: slist.h:40
+ Here is the caller 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 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:65
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
size_t count
Number of values in list.
Definition: slist.h:39
#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_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:217
+ 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 mutt_list_copy_tail(&list_new->head, &list->head);
114 list_new->count = list->count;
115 return list_new;
116}
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
+ 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 122 of file slist.c.

123{
124 if (!ptr || !*ptr)
125 return;
126
127 struct Slist *slist = *ptr;
128 mutt_list_free(&slist->head);
129
130 FREE(ptr);
131}
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 138 of file slist.c.

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

153{
154 if (!list)
155 return false;
156
157 if (!str && !(list->flags & D_SLIST_ALLOW_EMPTY))
158 return false;
159
160 struct ListNode *np = NULL;
161 STAILQ_FOREACH(np, &list->head, entries)
162 {
163 if (mutt_str_equal(np->data, str))
164 return true;
165 }
166 return false;
167}
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
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_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 175 of file slist.c.

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

234{
235 if (!list)
236 return NULL;
237 if (!str && !(list->flags & D_SLIST_ALLOW_EMPTY))
238 return list;
239
240 struct ListNode *prev = NULL;
241 struct ListNode *np = NULL;
242 struct ListNode *tmp = NULL;
243 STAILQ_FOREACH_SAFE(np, &list->head, entries, tmp)
244 {
245 if (mutt_str_equal(np->data, str))
246 {
247 if (prev)
248 STAILQ_REMOVE_AFTER(&list->head, prev, entries);
249 else
250 STAILQ_REMOVE_HEAD(&list->head, entries);
251 FREE(&np->data);
252 FREE(&np);
253 list->count--;
254 break;
255 }
256 prev = np;
257 }
258 return list;
259}
#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 267 of file slist.c.

268{
269 if (!list || !buf || (list->count == 0))
270 return 0;
271
272 struct ListNode *np = NULL;
273 STAILQ_FOREACH(np, &list->head, entries)
274 {
275 buf_addstr(buf, np->data);
276 if (STAILQ_NEXT(np, entries))
277 {
278 const int sep = (list->flags & D_SLIST_SEP_MASK);
279 if (sep == D_SLIST_SEP_COMMA)
280 buf_addch(buf, ',');
281 else if (sep == D_SLIST_SEP_COLON)
282 buf_addch(buf, ':');
283 else
284 buf_addch(buf, ' ');
285 }
286 }
287
288 return list->count;
289}
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:400
+ Here is the call graph for this function:
+ Here is the caller graph for this function: