[go: up one dir, main page]

Menu

[r456]: / tags / 0.6.4 / src / match.c  Maximize  Restore  History

Download this file

151 lines (127 with data), 3.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* -------------------------------------------------------
* Copyright (C) 2003-2007 Tommi Saviranta <wnd@iki.fi>
* (C) 1998-2002 Sebastian Kienzl <zap@riot.org>
* -------------------------------------------------------
* 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.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* ifdef HAVE_CONFIG_H */
#include "match.h"
#include "tools.h"
#include "common.h"
#include <string.h>
/**
* match:
* @string: String to match
* @pattern: Pattern to match against
*
* Issues: This algorithm is non-greedy. It will not match string "foobar"
* against pattern "*ob*".
*
* Return value: 1 if string matches patters, 0 if not.
*/
int
match(const char *string, const char *pattern)
{
char *str, *pat;
int str_p, pat_p;
if (string == NULL || pattern == NULL) {
return 0;
}
str = xstrdup(string);
upcase(str);
pat = xstrdup(pattern);
upcase(pat);
str_p = pat_p = 0;
/* Then to the matching part. */
while (1) {
if (str[str_p] == '\0') {
if ((pat[pat_p] == '*' && pat[pat_p + 1] == '\0')
|| pat[pat_p] == '\0') {
xfree(str);
xfree(pat);
return 1;
}
}
if (str[str_p] == '\0' || pat[pat_p] == '\0') {
xfree(str);
xfree(pat);
return 0;
}
if (str[str_p] == pat[pat_p] || pat[pat_p] == '?') {
str_p++;
pat_p++;
} else if (pat[pat_p] == '*') {
if (str[str_p] == pat[pat_p + 1]) {
pat_p++;
} else if (pat[pat_p + 1] == str[str_p + 1]) {
str_p++;
pat_p++;
} else {
str_p++;
}
} else {
xfree(str);
xfree(pat);
return 0;
}
}
} /* int match(const char *string, const char *pattern) */
/**
* match:
* @string: String to match
* @pattern: Pattern to match against
* @foo: String to replace. Can be NULL.
* @foo_len: Length of foo or -1 if unknown
* @replace: String to replace foo with. Can be NULL.
*
* Return value: 1 if string matches patters, 0 if not.
*/
int
match_replace(const char *string, const char *pattern, const char *foo,
const int foo_len, const char *replace)
{
char *pat;
int pat_len;
int replace_len;
int foo_len_real;
int at;
int ret;
/* Nothing to replace? Don't waste time here. */
if (foo == NULL || replace == NULL) {
return match(string, pattern);
}
/* Replace "@@" in pattern with "replace" if it was given. */
replace_len = (int) strlen(replace);
foo_len_real = foo_len == -1 ? strlen(foo) : foo_len;
for (at = strlen(pattern) - foo_len_real; at >= 0; at--) {
if (xstrncmp(pattern + at, foo, foo_len_real) == 0) {
break;
}
}
/* No "foo" found. */
if (at == -1) {
return match(string, pattern);
}
pat_len = strlen(pattern) + replace_len - foo_len_real + 1;
pat = (char *) xmalloc(pat_len);
memcpy(pat, pattern, at);
memcpy(pat + at, replace, replace_len);
memcpy(pat + at + replace_len, pattern + at + foo_len_real,
strlen(pattern) - at - foo_len_real);
pat[pat_len - 1] = '\0';
ret = match(string, pat);
xfree(pat);
return ret;
} /* int match_replace(const char *string, const char *pattern,
const char *foo, const int foo_len, const char *replace) */