2 * Sylpheed -- regexp pattern matching utilities
3 * Copyright (C) 2001 Thomas Link, Hiroyuki Yamamoto
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.
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.
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.
21 #include <sys/types.h>
23 #include "string_match.h"
25 /* the size used for regexec in string_remove_match */
26 #define STRING_MATCH_MR_SIZE 30
29 static int string_match_regexp (char * txt, char * rexp,
30 int size, regmatch_t matches[],
31 int cflags, int eflags);
33 int string_remove_match(char *txt, char *rexp, int cflags, int eflags)
35 regmatch_t matches[STRING_MATCH_MR_SIZE];
38 g_return_val_if_fail(txt, -1);
39 g_return_val_if_fail(rexp, -1);
41 if (strlen(txt) > 0 && strlen(rexp) > 0) {
42 foundp = string_match_regexp(txt, rexp, STRING_MATCH_MR_SIZE,
43 matches, cflags, eflags);
48 if (foundp && matches[0].rm_so != -1 && matches[0].rm_eo != -1) {
49 if (matches[0].rm_so == matches[0].rm_eo) {
50 /* in case the match is an empty string */
51 return matches[0].rm_so + 1;
53 strcpy(txt + matches[0].rm_so, txt + matches[0].rm_eo);
54 return matches[0].rm_so;
61 int string_remove_all_matches(char *txt, char *rexp, int cflags, int eflags)
66 g_return_val_if_fail(txt, 0);
67 g_return_val_if_fail(rexp, 0);
69 while (pos0 >= 0 && pos < strlen(txt) && strlen(txt) > 0) {
70 /* printf("%s %d:%d\n", txt, pos0, pos); */
71 pos0 = string_remove_match(txt + pos, rexp, cflags, eflags);
79 static int string_match_regexp(char * txt, char * rexp,
80 int size, regmatch_t matches[],
81 int cflags, int eflags)
86 g_return_val_if_fail(txt, 0);
87 g_return_val_if_fail(rexp, 0);
89 problem = regcomp(&re, rexp, cflags);
91 problem = regexec(&re, txt, size, matches, eflags);