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