diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2018-08-19 16:25:36 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2018-08-30 13:32:09 -0700 |
commit | 59a68fe9b5bdeb2d557a26c58e43564169fa7746 (patch) | |
tree | 627d072ce13b19a6c0796e0cb884a975cab322a1 /core | |
parent | 2de8e70ab04b0cc254a1d04f31e7e02db57743fa (diff) | |
download | subsurface-59a68fe9b5bdeb2d557a26c58e43564169fa7746.tar.gz |
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>
Diffstat (limited to 'core')
-rw-r--r-- | core/parse-xml.c | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/core/parse-xml.c b/core/parse-xml.c index 5cdf577bc..9cc5270e7 100644 --- a/core/parse-xml.c +++ b/core/parse-xml.c @@ -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 *); -static int match(const char *pattern, int plen, - const char *name, +static int match(const char *pattern, const char *name, matchfn_t fn, char *buf, void *data) { - switch (name[plen]) { - case '\0': - case '.': - break; - default: - return 0; - } - if (memcmp(pattern, name, plen)) + if (!match_name(pattern, name)) return 0; fn(buf, data); return 1; @@ -489,7 +491,7 @@ static int match(const char *pattern, int plen, #define MATCH(pattern, fn, dest) ({ \ /* Silly type compatibility test */ \ 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) { |