From: Rusty Russell Date: Tue, 10 Jun 2014 05:09:26 +0000 (+0930) Subject: grab_file: don't use str_talloc for tests. X-Git-Url: http://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=4c2395707d92b5891564adcc6ac2f8acb4d61f90 grab_file: don't use str_talloc for tests. Signed-off-by: Rusty Russell --- diff --git a/ccan/grab_file/_info b/ccan/grab_file/_info index 1f6e00d7..1db4c295 100644 --- a/ccan/grab_file/_info +++ b/ccan/grab_file/_info @@ -48,10 +48,6 @@ int main(int argc, char *argv[]) printf("ccan/noerr\n"); return 0; } - if (strcmp(argv[1], "testdepends") == 0) { - printf("ccan/str_talloc\n"); - return 0; - } return 1; } diff --git a/ccan/grab_file/grab_file.h b/ccan/grab_file/grab_file.h index 96381875..bcd728e6 100644 --- a/ccan/grab_file/grab_file.h +++ b/ccan/grab_file/grab_file.h @@ -15,20 +15,18 @@ * byte after the end of the content will always be NUL. * * Example: - * #include - * #include - * ... - * // Return all of standard input, as lines. - * static char **read_stdin_as_lines(void) + * // Return the first line. + * static char *read_stdin_firstline(void) * { - * char **lines, *all; + * char *all, *nl; * * all = grab_fd(NULL, 0, NULL); * if (!all) * return NULL; - * lines = strsplit(NULL, all, "\n"); - * talloc_free(all); - * return lines; + * nl = strchr(all, '\n'); + * if (nl) + * *nl = '\0'; + * return all; * } */ void *grab_fd(const void *ctx, int fd, size_t *size); @@ -45,17 +43,17 @@ void *grab_fd(const void *ctx, int fd, size_t *size); * after the end of the content will always be NUL. * * Example: - * // Return all of a given file, as lines. - * static char **read_file_as_lines(const char *filename) + * static char *read_file_firstline(const char *filename) * { - * char **lines, *all; + * char *nl, *all; * * all = grab_file(NULL, filename, NULL); * if (!all) * return NULL; - * lines = strsplit(NULL, all, "\n"); - * talloc_free(all); - * return lines; + * nl = strchr(all, '\n'); + * if (nl) + * *nl = '\0'; + * return all; * } */ void *grab_file(const void *ctx, const char *filename, size_t *size); diff --git a/ccan/grab_file/test/run-grab.c b/ccan/grab_file/test/run-grab.c index 060607d5..c9f24260 100644 --- a/ccan/grab_file/test/run-grab.c +++ b/ccan/grab_file/test/run-grab.c @@ -7,7 +7,30 @@ #include #include #include -#include +#include + +static char **strsplit(const void *ctx, const char *string, const char *delims) +{ + char **lines = NULL; + unsigned int max = 64, num = 0; + + lines = talloc_array(ctx, char *, max+1); + + while (*string != '\0') { + unsigned int len = strcspn(string, delims); + lines[num] = talloc_array(lines, char, len + 1); + memcpy(lines[num], string, len); + lines[num][len] = '\0'; + string += len; + string += strspn(string, delims) ? 1 : 0; + if (++num == max) + lines = talloc_realloc(ctx, lines, char *, max*=2 + 1); + } + lines[num] = NULL; + + /* Shrink, so talloc_get_size works */ + return talloc_realloc(ctx, lines, char *, num+1); +} int main(int argc, char *argv[])