Show key icon for encrypted mails in summary view.
[claws.git] / src / string_match.c
1 /*
2  * Sylpheed -- regexp pattern matching utilities
3  * Copyright (C) 2001 Thomas Link, Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <glib.h>
21 #include "string_match.h"
22
23
24 int string_match_regexp(char * txt, char * rexp,
25                         int size, regmatch_t matches[],
26                         int cflags, int eflags)
27 {
28         regex_t re;
29         int problem;
30
31         g_return_val_if_fail(txt, 0);
32         g_return_val_if_fail(rexp, 0);
33
34         problem = regcomp(&re, rexp, cflags);  
35         if (problem == 0) {
36                 problem = regexec(&re, txt, size, matches, eflags);
37         }
38         regfree(&re);
39         return problem == 0;
40 }
41
42 int string_remove_match(char * txt, char * rexp, int cflags, int eflags)
43 {
44         regmatch_t matches[STRING_MATCH_MR_SIZE];
45         int foundp;
46
47         g_return_val_if_fail(txt, -1);
48         g_return_val_if_fail(rexp, -1);
49
50         if (strlen(txt) > 0 && strlen(rexp) > 0) {
51                 foundp = string_match_regexp(txt, rexp, STRING_MATCH_MR_SIZE, 
52                                              matches, cflags, eflags);
53         } else {
54                 return -1;
55         }
56
57         if (foundp && matches[0].rm_so != -1 && matches[0].rm_eo != -1) {
58                 if (matches[0].rm_so == matches[0].rm_eo) {
59                         /* in case the match is an empty string */
60                         return matches[0].rm_so + 1;
61                 } else {
62                         strcpy(txt + matches[0].rm_so, txt + matches[0].rm_eo);
63                         return matches[0].rm_so;
64                 }
65         } else {
66                 return -1;
67         }
68 }
69
70 int string_remove_all_matches(char * txt, char * rexp, int cflags, int eflags)
71 {
72         int pos = 0;
73         int pos0 = pos;
74
75         g_return_val_if_fail(txt, 0);
76         g_return_val_if_fail(rexp, 0);
77
78         while (pos0 >= 0 && pos < strlen(txt) && strlen(txt) > 0) {
79                 /* printf("%s %d:%d\n", txt, pos0, pos); */
80                 pos0 = string_remove_match(txt + pos, rexp, cflags, eflags);
81                 if (pos0 >= 0) {
82                         pos = pos + pos0;
83                 }
84         }
85         return pos;
86 }