e7c4f3cc43aed8cab7a155e72baca9320959c33c
[apps/madmutt.git] / lib / rx.c
1 /*
2  * This file is part of mutt-ng, see http://www.muttng.org/.
3  * It's licensed under the GNU General Public License,
4  * please see the file GPL in the top level source directory.
5  */
6
7 #if HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include "rx.h"
12
13 #include "mem.h"
14 #include "str.h"
15
16 rx_t *rx_compile (const char *s, int flags) {
17   rx_t *pp = safe_calloc (1, sizeof (rx_t));
18
19   pp->pattern = str_dup (s);
20   pp->rx = safe_calloc (1, sizeof (regex_t));
21   if (REGCOMP(pp->rx, NONULL (s), flags) != 0)
22     rx_free (&pp);
23
24   return pp;
25 }
26
27 void rx_free (rx_t** p) {
28   FREE(&(*p)->pattern);
29   regfree ((*p)->rx);
30   FREE(&(*p)->rx);
31   FREE(p);
32 }
33
34 int rx_compare (const rx_t* r1, const rx_t* r2) {
35   return (str_cmp (r1->pattern, r2->pattern));
36 }
37
38 int rx_list_match (list2_t* l, const char* pat) {
39   int i = 0;
40   if (!pat || !*pat || list_empty(l))
41     return (0);
42   for (i = 0; i < l->length; i++)
43     if (REGEXEC(((rx_t*) l->data[i])->rx, pat) == 0)
44       return (1);
45   return (0);
46 }
47
48 int rx_lookup (list2_t* l, const char* pat) {
49   int i = 0;
50   if (!pat || !*pat || list_empty(l))
51     return (-1);
52   for (i = 0; i < l->length; i++)
53     if (str_cmp (((rx_t*) l->data[i])->pattern, pat) == 0)
54       return (i);
55   return (-1);
56 }