Browse Source

Use new string.h strsplit() everywhere.

Rusty Russell 17 years ago
parent
commit
779d83085d
8 changed files with 14 additions and 107 deletions
  1. 6 6
      tools/Makefile
  2. 1 1
      tools/ccanlint/Makefile
  3. 1 26
      tools/ccanlint/get_file_lines.c
  4. 1 1
      tools/depends.c
  5. 2 28
      tools/doc_extract.c
  6. 3 13
      tools/namespacize.c
  7. 0 29
      tools/split.c
  8. 0 3
      tools/tools.h

+ 6 - 6
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

+ 1 - 1
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

+ 1 - 26
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);
 }

+ 1 - 1
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)

+ 2 - 28
tools/doc_extract.c

@@ -9,12 +9,7 @@
 #include <fcntl.h>
 #include <stdbool.h>
 #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], "/**")) {

+ 3 - 13
tools/namespacize.c

@@ -10,8 +10,8 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
-#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;
 }

+ 0 - 29
tools/split.c

@@ -1,29 +0,0 @@
-#include "tools.h"
-#include "talloc/talloc.h"
-#include <string.h>
-
-/* 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;
-}
-

+ 0 - 3
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);