move rx.[hc].
[apps/madmutt.git] / lib-lib / rx.c
diff --git a/lib-lib/rx.c b/lib-lib/rx.c
new file mode 100644 (file)
index 0000000..6413f69
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ *  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, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ *  MA 02110-1301, USA.
+ *
+ *  Copyright © 2006 Pierre Habouzit
+ */
+/*
+ * This file is part of mutt-ng, see http://www.muttng.org/.
+ * It's licensed under the GNU General Public License,
+ * please see the file GPL in the top level source directory.
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <lib-lib/mem.h>
+#include <lib-lib/str.h>
+#include <lib-lib/rx.h>
+
+rx_t *rx_compile(const char *s, int flags)
+{
+    rx_t *pp = p_new(rx_t, 1);
+
+    pp->pattern = m_strdup(s);
+    pp->rx = p_new(regex_t, 1);
+
+    if (REGCOMP(pp->rx, NONULL(s), flags) != 0) {
+        rx_delete(&pp);
+    }
+
+    return pp;
+}
+
+void rx_delete(rx_t **p)
+{
+    p_delete(&(*p)->pattern);
+    regfree((*p)->rx);
+    p_delete(&(*p)->rx);
+    p_delete(p);
+}
+
+int rx_list_match(list2_t *l, const char *pat)
+{
+    int i;
+
+    if (!pat || !*pat || list_empty(l))
+        return 0;
+
+    for (i = 0; i < l->length; i++) {
+        if (!REGEXEC(((rx_t*)l->data[i])->rx, pat))
+            return 1;
+    }
+
+    return 0;
+}
+
+int rx_lookup (list2_t *l, const char *pat)
+{
+    int i;
+
+    if (!pat || !*pat || list_empty(l))
+        return -1;
+
+    for (i = 0; i < l->length; i++) {
+        if (!strcmp(((rx_t*)l->data[i])->pattern, pat))
+            return i;
+    }
+
+    return -1;
+}