616d82452a4847277d40e71e15ff0ab76874d070
[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 <lib-lib/mem.h>
12
13 #include "rx.h"
14
15 #include "mem.h"
16 #include "str.h"
17
18 rx_t *rx_compile (const char *s, int flags) {
19   rx_t *pp = p_new(rx_t, 1);
20
21   pp->pattern = str_dup (s);
22   pp->rx = p_new(regex_t, 1);
23   if (REGCOMP(pp->rx, NONULL (s), flags) != 0)
24     rx_free (&pp);
25
26   return pp;
27 }
28
29 void rx_free (rx_t** p) {
30   p_delete(&(*p)->pattern);
31   regfree ((*p)->rx);
32   p_delete(&(*p)->rx);
33   p_delete(p);
34 }
35
36 int rx_compare (const rx_t* r1, const rx_t* r2) {
37   return (str_cmp (r1->pattern, r2->pattern));
38 }
39
40 int rx_list_match (list2_t* l, const char* pat) {
41   int i = 0;
42   if (!pat || !*pat || list_empty(l))
43     return (0);
44   for (i = 0; i < l->length; i++)
45     if (REGEXEC(((rx_t*) l->data[i])->rx, pat) == 0)
46       return (1);
47   return (0);
48 }
49
50 int rx_lookup (list2_t* l, const char* pat) {
51   int i = 0;
52   if (!pat || !*pat || list_empty(l))
53     return (-1);
54   for (i = 0; i < l->length; i++)
55     if (str_cmp (((rx_t*) l->data[i])->pattern, pat) == 0)
56       return (i);
57   return (-1);
58 }