initial Hungarian translation
[claws.git] / src / 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 2 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, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include <string.h>
26
27 #include "intl.h"
28 #include "string_match.h"
29
30 int string_match_precompile (gchar *rexp, regex_t *preg, int cflags)
31 {
32         int problem = 0;
33
34         g_return_val_if_fail(rexp, -1);
35         g_return_val_if_fail(*rexp, -1);
36
37         problem = regcomp(preg, rexp, cflags);  
38         
39         return problem;
40 }
41
42
43 gchar *string_remove_match(gchar *buf, gint buflen, gchar * txt, regex_t *preg)
44 {
45         regmatch_t match;
46         int notfound;
47         gint i, j ,k;
48
49         if (!preg)
50                 return txt;
51         if (*txt != 0x00) {
52                 i = 0;
53                 j = 0;
54                 do {
55                         notfound = regexec(preg, txt+j, 1, &match, (j ? REG_NOTBOL : 0));
56                         if (notfound) {
57                                 while (txt[j] && i < buflen -1)
58                                         buf[i++] = txt[j++];
59                         } else {
60                                 if ( match.rm_so == match.rm_eo)
61                                         buf[i++] = txt[j++];
62                                 else {
63                                         k = j;
64                                         while (txt[j] &&  j != k + match.rm_so) 
65                                                 buf[i++] = txt[j++];
66                                         if (txt[j])
67                                                 j = k + match.rm_eo;
68                                 }
69                         }
70                 } while (txt[j] && i < buflen - 1);
71                 buf[i] = 0x00;
72                 if (buf[0] == 0x00) {
73                         strncpy(buf, _("(Subject cleared by RegExp)"),
74                                         buflen - 1);
75                         buf[buflen - 1] = 0x00;
76                 }
77                 return buf;             
78         }
79         return txt;
80 }
81