X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fstr_talloc%2Fstr_talloc.c;h=e2d12df8791942744f3dec63f2e9a18284198a4d;hp=d1d2f44de4b875b994b4cd8e5899981a759bf44e;hb=fa64b4599366818ea546c7db026f37d987d181a8;hpb=053f9398c5959d070b7092653ec8f222200ab463 diff --git a/ccan/str_talloc/str_talloc.c b/ccan/str_talloc/str_talloc.c index d1d2f44d..e2d12df8 100644 --- a/ccan/str_talloc/str_talloc.c +++ b/ccan/str_talloc/str_talloc.c @@ -4,7 +4,12 @@ #include #include #include "str_talloc.h" +#include +#include +#include +#include #include +#include char **strsplit(const void *ctx, const char *string, const char *delims, unsigned int *nump) @@ -41,3 +46,46 @@ char *strjoin(const void *ctx, char *strings[], const char *delim) } return ret; } + +bool strreg(const void *ctx, const char *string, const char *regex, ...) +{ + size_t nmatch = 1 + strcount(regex, "("); + regmatch_t *matches = talloc_array(ctx, regmatch_t, nmatch); + regex_t r; + bool ret; + + if (!matches || regcomp(&r, regex, REG_EXTENDED) != 0) + return false; + + if (regexec(&r, string, nmatch, matches, 0) == 0) { + unsigned int i; + va_list ap; + + ret = true; + va_start(ap, regex); + for (i = 1; i < nmatch; i++) { + char **arg; + arg = va_arg(ap, char **); + if (arg) { + /* eg. ([a-z])? can give "no match". */ + if (matches[i].rm_so == -1) + *arg = NULL; + else { + *arg = talloc_strndup(ctx, + string + matches[i].rm_so, + matches[i].rm_eo + - matches[i].rm_so); + if (!*arg) { + ret = false; + break; + } + } + } + } + va_end(ap); + } else + ret = false; + talloc_free(matches); + regfree(&r); + return ret; +}