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