forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.h
124 lines (111 loc) · 3.05 KB
/
list.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* @file
* Singly-linked list type
*
* @authors
* Copyright (C) 2017 Richard Russon <rich@flatcap.org>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* 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/>.
*/
#ifndef _MUTT_LIST_H
#define _MUTT_LIST_H
#include <string.h>
#include "lib/lib.h"
/**
* New implementation using macros from queue.h
*/
#include "queue.h"
STAILQ_HEAD(ListHead, ListNode);
struct ListNode
{
char *data;
STAILQ_ENTRY(ListNode) entries;
};
static inline struct ListNode* mutt_list_insert_head(struct ListHead *h,
char *s)
{
struct ListNode *np = safe_calloc(1, sizeof(struct ListNode));
np->data = s;
STAILQ_INSERT_HEAD(h, np, entries);
return np;
}
static inline struct ListNode* mutt_list_insert_tail(struct ListHead *h,
char *s)
{
struct ListNode *np = safe_calloc(1, sizeof(struct ListNode));
np->data = s;
STAILQ_INSERT_TAIL(h, np, entries);
return np;
}
static inline struct ListNode* mutt_list_insert_after(struct ListHead *h,
struct ListNode *n,
char *s)
{
struct ListNode *np = safe_calloc(1, sizeof(struct ListNode));
np->data = s;
STAILQ_INSERT_AFTER(h, n, np, entries);
return np;
}
static inline struct ListNode *mutt_list_find(struct ListHead *h,
const char *data)
{
struct ListNode *np;
STAILQ_FOREACH(np, h, entries)
{
if (np->data == data || mutt_strcmp(np->data, data) == 0)
{
return np;
}
}
return NULL;
}
static inline void mutt_list_free(struct ListHead *h)
{
struct ListNode *np = STAILQ_FIRST(h), *next = NULL;
while (np)
{
next = STAILQ_NEXT(np, entries);
FREE(&np->data);
FREE(&np);
np = next;
}
STAILQ_INIT(h);
}
static inline void mutt_list_clear(struct ListHead *h)
{
struct ListNode *np = STAILQ_FIRST(h), *next = NULL;
while (np)
{
next = STAILQ_NEXT(np, entries);
FREE(&np);
np = next;
}
STAILQ_INIT(h);
}
/**
* mutt_list_match - Is the string in the list
* @return true if the header contained in "s" is in list "h"
*/
static inline bool mutt_list_match(const char *s, struct ListHead *h)
{
struct ListNode *np;
STAILQ_FOREACH(np, h, entries)
{
if (*np->data == '*' || mutt_strncasecmp(s, np->data, strlen(np->data)) == 0)
return true;
}
return false;
}
#endif /* _MUTT_LIST_H */