mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Parser: split out name-comparison from match() function
The match() function in parse-xml.c calls a very specific callback, which doesn't take a context-parameter. To be able to call other callbacks, split out the actual name-comparison. Moreover, remove the "plen" parameter, as this was called with strlen(pattern) in all cases anyway. Replace the old logic which potentially accessed a byte beyond the end of name with a simply classical C-style loop. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
2de8e70ab0
commit
59a68fe9b5
1 changed files with 13 additions and 11 deletions
|
@ -468,19 +468,21 @@ static void event_divemode(char *buffer, int *value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Compare a pattern with a name, whereby the name may end in '\0' or '.'. */
|
||||||
|
static int match_name(const char *pattern, const char *name)
|
||||||
|
{
|
||||||
|
while (*pattern == *name && *pattern) {
|
||||||
|
pattern++;
|
||||||
|
name++;
|
||||||
|
}
|
||||||
|
return *pattern == '\0' && (*name == '\0' || *name == '.');
|
||||||
|
}
|
||||||
|
|
||||||
typedef void (*matchfn_t)(char *buffer, void *);
|
typedef void (*matchfn_t)(char *buffer, void *);
|
||||||
static int match(const char *pattern, int plen,
|
static int match(const char *pattern, const char *name,
|
||||||
const char *name,
|
|
||||||
matchfn_t fn, char *buf, void *data)
|
matchfn_t fn, char *buf, void *data)
|
||||||
{
|
{
|
||||||
switch (name[plen]) {
|
if (!match_name(pattern, name))
|
||||||
case '\0':
|
|
||||||
case '.':
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if (memcmp(pattern, name, plen))
|
|
||||||
return 0;
|
return 0;
|
||||||
fn(buf, data);
|
fn(buf, data);
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -489,7 +491,7 @@ static int match(const char *pattern, int plen,
|
||||||
#define MATCH(pattern, fn, dest) ({ \
|
#define MATCH(pattern, fn, dest) ({ \
|
||||||
/* Silly type compatibility test */ \
|
/* Silly type compatibility test */ \
|
||||||
if (0) (fn)("test", dest); \
|
if (0) (fn)("test", dest); \
|
||||||
match(pattern, strlen(pattern), name, (matchfn_t) (fn), buf, dest); })
|
match(pattern, name, (matchfn_t) (fn), buf, dest); })
|
||||||
|
|
||||||
static void get_index(char *buffer, int *i)
|
static void get_index(char *buffer, int *i)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue