2009-10-21 [wwp] 3.7.3cvs7
[claws.git] / src / common / string_match.c
1 /*
2  * Sylpheed -- regexp pattern matching utilities
3  * Copyright (C) 2001 Thomas Link, Hiroyuki Yamamoto
4  *                    Modified by Melvin Hadasht.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  * 
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include <glib.h>
26 #ifdef ENABLE_NLS
27 #include <glib/gi18n.h>
28 #else
29 #define _(a) (a)
30 #define N_(a) (a)
31 #endif
32 #include <string.h>
33
34 #include "string_match.h"
35 #include "utils.h"
36
37 int string_match_precompile (gchar *rexp, regex_t *preg, int cflags)
38 {
39         int problem = 0;
40
41         cm_return_val_if_fail(rexp, -1);
42         cm_return_val_if_fail(*rexp, -1);
43
44         problem = regcomp(preg, rexp, cflags);  
45         
46         return problem;
47 }
48
49
50 gchar *string_remove_match(gchar *buf, gint buflen, gchar * txt, regex_t *preg)
51 {
52         regmatch_t match;
53         int notfound;
54         gint i, j ,k;
55 #ifdef G_OS_WIN32
56         return txt;
57 #else
58         if (!preg)
59                 return txt;
60         if (*txt != 0x00) {
61                 i = 0;
62                 j = 0;
63                 do {
64                         notfound = regexec(preg, txt+j, 1, &match, (j ? REG_NOTBOL : 0));
65                         if (notfound) {
66                                 while (txt[j] && i < buflen -1)
67                                         buf[i++] = txt[j++];
68                         } else {
69                                 if ( match.rm_so == match.rm_eo)
70                                         buf[i++] = txt[j++];
71                                 else {
72                                         k = j;
73                                         while (txt[j] &&  j != k + match.rm_so) 
74                                                 buf[i++] = txt[j++];
75                                         if (txt[j])
76                                                 j = k + match.rm_eo;
77                                 }
78                         }
79                 } while (txt[j] && i < buflen - 1);
80                 buf[i] = 0x00;
81                 if (buf[0] == 0x00) {
82                         strncpy(buf, _("(Subject cleared by RegExp)"),
83                                         buflen - 1);
84                         buf[buflen - 1] = 0x00;
85                 }
86                 return buf;             
87         }
88         return txt;
89 #endif
90 }
91