]> git.ozlabs.org Git - ccan/blobdiff - ccan/str_talloc/str_talloc.c
str_talloc: strreg
[ccan] / ccan / str_talloc / str_talloc.c
index 7438f48033052eb1ae4fd56df267632dc9594cef..e2d12df8791942744f3dec63f2e9a18284198a4d 100644 (file)
@@ -4,7 +4,12 @@
 #include <limits.h>
 #include <stdlib.h>
 #include "str_talloc.h"
 #include <limits.h>
 #include <stdlib.h>
 #include "str_talloc.h"
-#include "talloc/talloc.h"
+#include <sys/types.h>
+#include <regex.h>
+#include <stdarg.h>
+#include <unistd.h>
+#include <ccan/talloc/talloc.h>
+#include <ccan/str/str.h>
 
 char **strsplit(const void *ctx, const char *string, const char *delims,
                 unsigned int *nump)
 
 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;
 }
        }
        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;
+}