NeoMutt  2024-12-12-14-g7b49f7
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
list.c File Reference

Singly-linked list type. More...

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

Go to the source code of this file.

Functions

struct ListNodemutt_list_insert_head (struct ListHead *h, char *s)
 Insert a string at the beginning of a List.
 
struct ListNodemutt_list_insert_tail (struct ListHead *h, char *s)
 Append a string to the end of a List.
 
struct ListNodemutt_list_insert_after (struct ListHead *h, struct ListNode *n, char *s)
 Insert a string after a given ListNode.
 
struct ListNodemutt_list_find (const struct ListHead *h, const char *data)
 Find a string in a List.
 
void mutt_list_free (struct ListHead *h)
 Free a List AND its strings.
 
void mutt_list_free_type (struct ListHead *h, list_free_t fn)
 Free a List of type.
 
void mutt_list_clear (struct ListHead *h)
 Free a list, but NOT its strings.
 
bool mutt_list_match (const char *s, struct ListHead *h)
 Is the string in the list (see notes)
 
bool mutt_list_equal (const struct ListHead *ah, const struct ListHead *bh)
 Compare two string lists.
 
size_t mutt_list_str_split (struct ListHead *head, const char *src, char sep)
 Split a string into a list using a separator char.
 
void mutt_list_copy_tail (struct ListHead *dst, const struct ListHead *src)
 Copy a list into another list.
 
size_t mutt_list_write (const struct ListHead *h, struct Buffer *buf)
 Write a list to a buffer.
 

Detailed Description

Singly-linked list type.

Authors
  • Richard Russon
  • Pietro Cerutti

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 list.c.

Function Documentation

◆ mutt_list_insert_head()

struct ListNode * mutt_list_insert_head ( struct ListHead *  h,
char *  s 
)

Insert a string at the beginning of a List.

Parameters
hHead of the List
sString to insert
Return values
ptrNewly inserted ListNode containing the string
Note
The inserted string isn't strdup()d

Definition at line 46 of file list.c.

47{
48 if (!h)
49 return NULL;
50
51 struct ListNode *np = MUTT_MEM_CALLOC(1, struct ListNode);
52 np->data = s;
53 STAILQ_INSERT_HEAD(h, np, entries);
54 return np;
55}
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
#define STAILQ_INSERT_HEAD(head, elm, field)
Definition: queue.h:383
A List node for strings.
Definition: list.h:37
char * data
String.
Definition: list.h:38
+ Here is the caller graph for this function:

◆ mutt_list_insert_tail()

struct ListNode * mutt_list_insert_tail ( struct ListHead *  h,
char *  s 
)

Append a string to the end of a List.

Parameters
hHead of the List
sString to insert
Return values
ptrNewly appended ListNode containing the string
Note
The inserted string isn't strdup()d

Definition at line 65 of file list.c.

66{
67 if (!h)
68 return NULL;
69
70 struct ListNode *np = MUTT_MEM_CALLOC(1, struct ListNode);
71 np->data = s;
72 STAILQ_INSERT_TAIL(h, np, entries);
73 return np;
74}
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:389
+ Here is the caller graph for this function:

◆ mutt_list_insert_after()

struct ListNode * mutt_list_insert_after ( struct ListHead *  h,
struct ListNode n,
char *  s 
)

Insert a string after a given ListNode.

Parameters
hHead of the List
nListNode after which the string will be inserted
sString to insert
Return values
ptrNewly created ListNode containing the string
Note
The inserted string isn't strdup()d

Definition at line 85 of file list.c.

86{
87 if (!h || !n)
88 return NULL;
89
90 struct ListNode *np = MUTT_MEM_CALLOC(1, struct ListNode);
91 np->data = s;
92 STAILQ_INSERT_AFTER(h, n, np, entries);
93 return np;
94}
#define STAILQ_INSERT_AFTER(head, tqelm, elm, field)
Definition: queue.h:377
+ Here is the caller graph for this function:

◆ mutt_list_find()

struct ListNode * mutt_list_find ( const struct ListHead *  h,
const char *  data 
)

Find a string in a List.

Parameters
hHead of the List
dataString to find
Return values
ptrListNode containing the string
NULLThe string isn't found

Definition at line 103 of file list.c.

104{
105 if (!h)
106 return NULL;
107
108 struct ListNode *np = NULL;
109 STAILQ_FOREACH(np, h, entries)
110 {
111 if (mutt_str_equal(np->data, data))
112 {
113 return np;
114 }
115 }
116 return NULL;
117}
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
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_list_free()

void mutt_list_free ( struct ListHead *  h)

Free a List AND its strings.

Parameters
hHead of the List

Definition at line 123 of file list.c.

124{
125 if (!h)
126 return;
127
128 struct ListNode *np = NULL, *tmp = NULL;
129 STAILQ_FOREACH_SAFE(np, h, entries, tmp)
130 {
131 STAILQ_REMOVE(h, np, ListNode, entries);
132 FREE(&np->data);
133 FREE(&np);
134 }
135
136 STAILQ_INIT(h);
137}
#define FREE(x)
Definition: memory.h:55
#define STAILQ_REMOVE(head, elm, type, field)
Definition: queue.h:402
#define STAILQ_INIT(head)
Definition: queue.h:372
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:362
+ Here is the caller graph for this function:

◆ mutt_list_free_type()

void mutt_list_free_type ( struct ListHead *  h,
list_free_t  fn 
)

Free a List of type.

Parameters
hHead of the List
fnFunction to free contents of ListNode

Definition at line 144 of file list.c.

145{
146 if (!h || !fn)
147 return;
148
149 struct ListNode *np = NULL, *tmp = NULL;
150 STAILQ_FOREACH_SAFE(np, h, entries, tmp)
151 {
152 STAILQ_REMOVE(h, np, ListNode, entries);
153 fn((void **) &np->data);
154 FREE(&np);
155 }
156
157 STAILQ_INIT(h);
158}
+ Here is the caller graph for this function:

◆ mutt_list_clear()

void mutt_list_clear ( struct ListHead *  h)

Free a list, but NOT its strings.

Parameters
hHead of the List

This can be used when the strings have a different lifetime to the List.

Definition at line 166 of file list.c.

167{
168 if (!h)
169 return;
170
171 struct ListNode *np = NULL, *tmp = NULL;
172 STAILQ_FOREACH_SAFE(np, h, entries, tmp)
173 {
174 STAILQ_REMOVE(h, np, ListNode, entries);
175 FREE(&np);
176 }
177
178 STAILQ_INIT(h);
179}
+ Here is the caller graph for this function:

◆ mutt_list_match()

bool mutt_list_match ( const char *  s,
struct ListHead *  h 
)

Is the string in the list (see notes)

Parameters
sString to match
hHead of the List
Return values
trueString matches a List item (or List contains "*")

This is a very specific function. It searches a List of strings looking for a match. If the list contains a string "*", then it match any input string.

Note
The strings are compared to the length of the List item, e.g. List: "Red" matches Param: "Redwood", but not the other way around.
The case of the strings is ignored.

Definition at line 194 of file list.c.

195{
196 if (!h)
197 return false;
198
199 struct ListNode *np = NULL;
200 STAILQ_FOREACH(np, h, entries)
201 {
202 if ((*np->data == '*') || mutt_istr_startswith(s, np->data))
203 return true;
204 }
205 return false;
206}
size_t mutt_istr_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix, ignoring case.
Definition: string.c:242
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_list_equal()

bool mutt_list_equal ( const struct ListHead *  ah,
const struct ListHead *  bh 
)

Compare two string lists.

Parameters
ahFirst string list
bhSecond string list
Return values
trueLists are identical

To be identical, the lists must both be the same length and contain the same strings. Two empty lists are identical.

Definition at line 217 of file list.c.

218{
219 if (!ah || !bh)
220 return false;
221
222 struct ListNode *a = STAILQ_FIRST(ah);
223 struct ListNode *b = STAILQ_FIRST(bh);
224
225 while (a && b)
226 {
227 if (!mutt_str_equal(a->data, b->data))
228 return false;
229
230 a = STAILQ_NEXT(a, entries);
231 b = STAILQ_NEXT(b, entries);
232 }
233 if (a || b)
234 return false;
235
236 return true;
237}
#define STAILQ_FIRST(head)
Definition: queue.h:350
#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:

◆ mutt_list_str_split()

size_t mutt_list_str_split ( struct ListHead *  head,
const char *  src,
char  sep 
)

Split a string into a list using a separator char.

Parameters
headList to add to
srcString to split
sepWord separator
Return values
numNumber of items in list

Definition at line 246 of file list.c.

247{
248 if (!src || (*src == '\0'))
249 return 0;
250
251 size_t count = 0;
252 while (true)
253 {
254 const char *start = src;
255 while ((*src != '\0') && (*src != sep))
256 src++;
257
258 mutt_list_insert_tail(head, mutt_strn_dup(start, src - start));
259 count++;
260
261 if ((*src == '\0'))
262 break;
263
264 src++;
265 }
266
267 return count;
268}
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_strn_dup(const char *begin, size_t len)
Duplicate a sub-string.
Definition: string.c:380
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_list_copy_tail()

void mutt_list_copy_tail ( struct ListHead *  dst,
const struct ListHead *  src 
)

Copy a list into another list.

Parameters
dstDestination list
srcSource list

Definition at line 275 of file list.c.

276{
277 const struct ListNode *np = NULL;
278
279 STAILQ_FOREACH(np, src, entries)
280 {
282 }
283}
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_list_write()

size_t mutt_list_write ( const struct ListHead *  h,
struct Buffer buf 
)

Write a list to a buffer.

Parameters
hList to write
bufBuffer for the list

Elements separated by a space. References, and In-Reply-To, use this format.

Definition at line 293 of file list.c.

294{
295 if (!buf || !h)
296 return 0;
297
298 struct ListNode *np = NULL;
299 STAILQ_FOREACH(np, h, entries)
300 {
301 buf_addstr(buf, np->data);
302 if (STAILQ_NEXT(np, entries))
303 buf_addstr(buf, " ");
304 }
305
306 return buf_len(buf);
307}
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:491
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:226
+ Here is the call graph for this function:
+ Here is the caller graph for this function: