From: Rusty Russell Date: Mon, 28 Jul 2008 04:21:23 +0000 (+1000) Subject: Use new string.h strsplit() everywhere. X-Git-Url: https://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=779d83085d6c6d37f6c4030130efe649a005696a Use new string.h strsplit() everywhere. --- diff --git a/tools/Makefile b/tools/Makefile index 4f6f699a..3c7fb7fa 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -1,15 +1,15 @@ -tools/ccan_depends: tools/ccan_depends.o tools/depends.o tools/split.o tools/grab_file.o ccan/talloc/talloc.o +tools/ccan_depends: tools/ccan_depends.o tools/depends.o tools/grab_file.o ccan/string/string.o ccan/talloc/talloc.o -tools/run_tests: tools/run_tests.o tools/depends.o tools/split.o tools/grab_file.o ccan/tap/tap.o ccan/talloc/talloc.o +tools/run_tests: tools/run_tests.o tools/depends.o tools/grab_file.o ccan/tap/tap.o ccan/string/string.o ccan/talloc/talloc.o -tools/doc_extract: tools/doc_extract.c ccan/talloc/talloc.o +tools/doc_extract: tools/doc_extract.o ccan/string/string.o ccan/talloc/talloc.o -tools/namespacize: tools/namespacize.c tools/split.o tools/grab_file.o tools/depends.o ccan/talloc/talloc.o +tools/namespacize: tools/namespacize.o tools/grab_file.o tools/depends.o ccan/string/string.o ccan/talloc/talloc.o -tools/run_tests.o tools/namespacize.o tools/split.o tools/grab_file.o tools/depends.o: tools/tools.h +tools/run_tests.o tools/namespacize.o tools/grab_file.o tools/depends.o: tools/tools.h tools-clean: ccanlint-clean - rm -f run_tests doc_extract namespacize + rm -f tools/ccan_depends tools/run_tests tools/doc_extract tools/namespacize include tools/ccanlint/Makefile include tools/_infotojson/Makefile diff --git a/tools/ccanlint/Makefile b/tools/ccanlint/Makefile index 60a35c12..40c26a83 100644 --- a/tools/ccanlint/Makefile +++ b/tools/ccanlint/Makefile @@ -24,7 +24,7 @@ tools/ccanlint/ccanlint: \ tools/ccanlint/ccanlint.o \ tools/ccanlint/get_file_lines.o \ tools/ccanlint/file_analysis.o \ - ccan/talloc/talloc.o ccan/noerr/noerr.o + ccan/string/string.o ccan/talloc/talloc.o ccan/noerr/noerr.o ccanlint-clean: $(RM) tools/ccanlint/generated-init-tests diff --git a/tools/ccanlint/get_file_lines.c b/tools/ccanlint/get_file_lines.c index e9ef302b..2f27a012 100644 --- a/tools/ccanlint/get_file_lines.c +++ b/tools/ccanlint/get_file_lines.c @@ -49,31 +49,6 @@ static void *grab_file(const void *ctx, const char *filename) return buffer; } -/* This is a dumb one which copies. We could mangle instead. */ -static char **split(const void *ctx, const char *text, const char *delims, - unsigned int *nump) -{ - char **lines = NULL; - unsigned int max = 64, num = 0; - - lines = talloc_array(ctx, char *, max+1); - - while (*text != '\0') { - unsigned int len = strcspn(text, delims); - lines[num] = talloc_array(lines, char, len + 1); - memcpy(lines[num], text, len); - lines[num][len] = '\0'; - text += len; - text += strspn(text, delims); - if (++num == max) - lines = talloc_realloc(ctx, lines, char *, max*=2 + 1); - } - lines[num] = NULL; - if (nump) - *nump = num; - return lines; -} - char **get_file_lines(void *ctx, const char *name, unsigned int *num_lines) { char *buffer = grab_file(ctx, name); @@ -81,5 +56,5 @@ char **get_file_lines(void *ctx, const char *name, unsigned int *num_lines) if (!buffer) err(1, "Getting file %s", name); - return split(buffer, buffer, "\n", num_lines); + return strsplit(buffer, buffer, "\n", num_lines); } diff --git a/tools/depends.c b/tools/depends.c index f416257a..1422c858 100644 --- a/tools/depends.c +++ b/tools/depends.c @@ -24,7 +24,7 @@ lines_from_cmd(const void *ctx, unsigned int *num, char *format, ...) err(1, "Reading from '%s'", cmd); pclose(p); - return split(ctx, buffer, "\n", num); + return strsplit(ctx, buffer, "\n", num); } static char **get_one_deps(const void *ctx, const char *dir, unsigned int *num) diff --git a/tools/doc_extract.c b/tools/doc_extract.c index b4ac0d3e..aa3f2206 100644 --- a/tools/doc_extract.c +++ b/tools/doc_extract.c @@ -9,12 +9,7 @@ #include #include #include "talloc/talloc.h" - -/* Is A == B ? */ -#define streq(a,b) (strcmp((a),(b)) == 0) - -/* Does A start with B ? */ -#define strstarts(a,b) (strncmp((a),(b),strlen(b)) == 0) +#include "string/string.h" /* This version adds one byte (for nul term) */ static void *grab_file(void *ctx, const char *filename) @@ -46,27 +41,6 @@ static void *grab_file(void *ctx, const char *filename) return buffer; } -/* This is a dumb one which copies. We could mangle instead. */ -static char **split(const char *text) -{ - char **lines = NULL; - unsigned int max = 64, num = 0; - - lines = talloc_array(text, char *, max+1); - - while (*text != '\0') { - unsigned int len = strcspn(text, "\n"); - lines[num] = talloc_array(lines, char, len + 1); - memcpy(lines[num], text, len); - lines[num][len] = '\0'; - text += len + 1; - if (++num == max) - lines = talloc_realloc(text, lines, char *, max*=2 + 1); - } - lines[num] = NULL; - return lines; -} - int main(int argc, char *argv[]) { unsigned int i, j; @@ -79,7 +53,7 @@ int main(int argc, char *argv[]) file = grab_file(NULL, argv[i]); if (!file) err(1, "Reading file %s", argv[i]); - lines = split(file); + lines = strsplit(file, file, "\n", NULL); for (j = 0; lines[j]; j++) { if (streq(lines[j], "/**")) { diff --git a/tools/namespacize.c b/tools/namespacize.c index 8c6c6632..759ef938 100644 --- a/tools/namespacize.c +++ b/tools/namespacize.c @@ -10,8 +10,8 @@ #include #include #include -#include "string/string.h" -#include "talloc/talloc.h" +#include "ccan/string/string.h" +#include "ccan/talloc/talloc.h" #include "tools.h" #define IDENT_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ @@ -30,16 +30,6 @@ static int indent = 0; #define verbose_indent() (indent += 2) #define verbose_unindent() (indent -= 2) -#define strstarts(str,prefix) (strncmp((str),(prefix),strlen(prefix)) == 0) - -static inline bool strends(const char *str, const char *postfix) -{ - if (strlen(str) < strlen(postfix)) - return false; - - return streq(str + strlen(str) - strlen(postfix), postfix); -} - static int unlink_no_errno(const char *filename) { int ret = 0, serrno = errno; @@ -466,7 +456,7 @@ static struct replace *read_replacement_file(const char *depdir) return NULL; } - for (line = split(file, file, "\n", NULL); *line; line++) + for (line = strsplit(file, file, "\n", NULL); *line; line++) add_replace(&repl, *line); return repl; } diff --git a/tools/split.c b/tools/split.c deleted file mode 100644 index f5d016a9..00000000 --- a/tools/split.c +++ /dev/null @@ -1,29 +0,0 @@ -#include "tools.h" -#include "talloc/talloc.h" -#include - -/* This is a dumb one which copies. We could mangle instead. */ -char **split(const void *ctx, const char *text, const char *delims, - unsigned int *nump) -{ - char **lines = NULL; - unsigned int max = 64, num = 0; - - lines = talloc_array(ctx, char *, max+1); - - while (*text != '\0') { - unsigned int len = strcspn(text, delims); - lines[num] = talloc_array(lines, char, len + 1); - memcpy(lines[num], text, len); - lines[num][len] = '\0'; - text += len; - text += strspn(text, delims); - if (++num == max) - lines = talloc_realloc(ctx, lines, char *, max*=2 + 1); - } - lines[num] = NULL; - if (nump) - *nump = num; - return lines; -} - diff --git a/tools/tools.h b/tools/tools.h index 4e8a48e6..fff962c7 100644 --- a/tools/tools.h +++ b/tools/tools.h @@ -3,9 +3,6 @@ #define CFLAGS "-O3 -Wall -Wundef -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations -Werror -Iccan/ -I." -char **split(const void *ctx, const char *text, const char *delims, - unsigned int *nump); - char **get_deps(const void *ctx, const char *dir); void *grab_fd(const void *ctx, int fd);