NeoMutt  2018-07-16 +2481-68dcde
Teaching an old dog new tricks
DOXYGEN
list.h File Reference

Singly-linked list type. More...

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

Go to the source code of this file.

Data Structures

struct  ListNode
 A List node for strings. More...
 

Typedefs

typedef void(* list_free_t) (void **ptr)
 typedef list_free_t - Prototype for a function to free List data More...
 

Functions

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

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.h.

Typedef Documentation

◆ list_free_t

typedef void(* list_free_t) (void **ptr)

typedef list_free_t - Prototype for a function to free List data

Parameters
[out]ptrData to free

Definition at line 44 of file list.h.

Function Documentation

◆ STAILQ_HEAD()

STAILQ_HEAD ( ListHead  ,
ListNode   
)

◆ 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 168 of file list.c.

169 {
170  if (!h)
171  return;
172 
173  struct ListNode *np = NULL, *tmp = NULL;
174  STAILQ_FOREACH_SAFE(np, h, entries, tmp)
175  {
176  STAILQ_REMOVE(h, np, ListNode, entries);
177  FREE(&np);
178  }
179 
180  STAILQ_INIT(h);
181 }
#define STAILQ_REMOVE(head, elm, type, field)
Definition: queue.h:400
#define STAILQ_INIT(head)
Definition: queue.h:370
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:360
#define FREE(x)
Definition: memory.h:40
A List node for strings.
Definition: list.h:33
+ Here is the caller graph for this function:

◆ mutt_list_compare()

bool mutt_list_compare ( 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 219 of file list.c.

220 {
221  if (!ah || !bh)
222  return false;
223 
224  struct ListNode *a = STAILQ_FIRST(ah);
225  struct ListNode *b = STAILQ_FIRST(bh);
226 
227  while (a && b)
228  {
229  if (mutt_str_strcmp(a->data, b->data) != 0)
230  return false;
231 
232  a = STAILQ_NEXT(a, entries);
233  b = STAILQ_NEXT(b, entries);
234  }
235  if (a || b)
236  return false;
237 
238  return true;
239 }
#define STAILQ_NEXT(elm, field)
Definition: queue.h:398
char * data
String.
Definition: list.h:35
A List node for strings.
Definition: list.h:33
#define STAILQ_FIRST(head)
Definition: queue.h:348
int mutt_str_strcmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:615
+ Here is the call graph for this function:
+ 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
NULLif the 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 ((np->data == data) || (mutt_str_strcmp(np->data, data) == 0))
112  {
113  return np;
114  }
115  }
116  return NULL;
117 }
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:350
char * data
String.
Definition: list.h:35
A List node for strings.
Definition: list.h:33
int mutt_str_strcmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:615
+ 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 = STAILQ_FIRST(h);
129  struct ListNode *next = NULL;
130  while (np)
131  {
132  next = STAILQ_NEXT(np, entries);
133  FREE(&np->data);
134  FREE(&np);
135  np = next;
136  }
137  STAILQ_INIT(h);
138 }
#define STAILQ_INIT(head)
Definition: queue.h:370
#define STAILQ_NEXT(elm, field)
Definition: queue.h:398
char * data
String.
Definition: list.h:35
#define FREE(x)
Definition: memory.h:40
A List node for strings.
Definition: list.h:33
#define STAILQ_FIRST(head)
Definition: queue.h:348
+ 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 145 of file list.c.

146 {
147  if (!h || !fn)
148  return;
149 
150  struct ListNode *np = STAILQ_FIRST(h);
151  struct ListNode *next = NULL;
152  while (np)
153  {
154  next = STAILQ_NEXT(np, entries);
155  fn((void **) &np->data);
156  FREE(&np);
157  np = next;
158  }
159  STAILQ_INIT(h);
160 }
#define STAILQ_INIT(head)
Definition: queue.h:370
#define STAILQ_NEXT(elm, field)
Definition: queue.h:398
char * data
String.
Definition: list.h:35
#define FREE(x)
Definition: memory.h:40
A List node for strings.
Definition: list.h:33
#define STAILQ_FIRST(head)
Definition: queue.h:348
+ 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, sizeof(struct ListNode));
91  np->data = s;
92  STAILQ_INSERT_AFTER(h, n, np, entries);
93  return np;
94 }
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
char * data
String.
Definition: list.h:35
A List node for strings.
Definition: list.h:33
#define STAILQ_INSERT_AFTER(head, tqelm, elm, field)
Definition: queue.h:375
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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, sizeof(struct ListNode));
52  np->data = s;
53  STAILQ_INSERT_HEAD(h, np, entries);
54  return np;
55 }
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define STAILQ_INSERT_HEAD(head, elm, field)
Definition: queue.h:381
char * data
String.
Definition: list.h:35
A List node for strings.
Definition: list.h:33
+ Here is the call graph for this function:
+ 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, sizeof(struct ListNode));
71  np->data = s;
72  STAILQ_INSERT_TAIL(h, np, entries);
73  return np;
74 }
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:387
char * data
String.
Definition: list.h:35
A List node for strings.
Definition: list.h:33
+ Here is the call graph for this function:
+ 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 196 of file list.c.

197 {
198  if (!h)
199  return false;
200 
201  struct ListNode *np = NULL;
202  STAILQ_FOREACH(np, h, entries)
203  {
204  if ((*np->data == '*') || mutt_str_startswith(s, np->data, CASE_IGNORE))
205  return true;
206  }
207  return false;
208 }
Ignore case when comparing strings.
Definition: string2.h:68
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:350
size_t mutt_str_startswith(const char *str, const char *prefix, enum CaseSensitivity cs)
Check whether a string starts with a prefix.
Definition: string.c:168
char * data
String.
Definition: list.h:35
A List node for strings.
Definition: list.h:33
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_list_str_split()

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

Split a string into a list using a separator char.

Parameters
srcString to split
sepWord separator

Definition at line 246 of file list.c.

247 {
248  struct ListHead head = STAILQ_HEAD_INITIALIZER(head);
249 
250  if (!src || !*src)
251  return head;
252 
253  while (true)
254  {
255  const char *start = src;
256  while (*src && (*src != sep))
257  src++;
258 
259  mutt_list_insert_tail(&head, mutt_str_substr_dup(start, src));
260 
261  if (!*src)
262  break;
263 
264  src++;
265  }
266 
267  return head;
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
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:322
char * mutt_str_substr_dup(const char *begin, const char *end)
Duplicate a sub-string.
Definition: string.c:579
+ Here is the call graph for this function:
+ Here is the caller graph for this function: