]> git.ozlabs.org Git - ccan/commitdiff
Rename string to str, and split into three modules.
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 10 Nov 2008 05:50:42 +0000 (16:50 +1100)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 10 Nov 2008 05:50:42 +0000 (16:50 +1100)
29 files changed:
ccan/grab_file/_info.c [new file with mode: 0644]
ccan/grab_file/grab_file.c [new file with mode: 0644]
ccan/grab_file/grab_file.h [new file with mode: 0644]
ccan/grab_file/test/run-grab.c [new file with mode: 0644]
ccan/str/_info.c [new file with mode: 0644]
ccan/str/str.h [new file with mode: 0644]
ccan/str/test/run.c [new file with mode: 0644]
ccan/str_talloc/_info.c [new file with mode: 0644]
ccan/str_talloc/str_talloc.c [new file with mode: 0644]
ccan/str_talloc/str_talloc.h [new file with mode: 0644]
ccan/str_talloc/test/run.c [new file with mode: 0644]
ccan/string/_info.c [deleted file]
ccan/string/string.c [deleted file]
ccan/string/string.h [deleted file]
ccan/string/test/run-grab.c [deleted file]
ccan/string/test/run.c [deleted file]
ccan/talloc/talloc.h
tools/ccan_depends.c
tools/ccanlint/Makefile
tools/ccanlint/file_analysis.c
tools/ccanlint/has_main_header.c
tools/ccanlint/has_tests.c
tools/ccanlint/idempotent.c
tools/ccanlint/trailing_whitespace.c
tools/create_dep_tar.c
tools/depends.c
tools/doc_extract.c
tools/namespacize.c
tools/run_tests.c

diff --git a/ccan/grab_file/_info.c b/ccan/grab_file/_info.c
new file mode 100644 (file)
index 0000000..4aac31a
--- /dev/null
@@ -0,0 +1,51 @@
+#include <stdio.h>
+#include <string.h>
+#include "config.h"
+
+/**
+ * grab_file - file helper routines
+ *
+ * This contains simple functions for getting the contents of a file.
+ *
+ * Example:
+ *     #include "grab_file/grab_file.h"
+ *     #include <err.h>
+ *     #include <stdio.h>
+ *     #include <string.h>
+ *
+ *     int main(int argc, char *argv[])
+ *     {
+ *             size_t len;
+ *             char *file;
+ *
+ *             file = grab_file(NULL, argv[1], &len);
+ *             if (!file)
+ *                     err(1, "Could not read file %s", argv[1]);
+ *             if (strlen(file) != len)
+ *                     printf("File contains NUL characters\n");
+ *             else if (len == 0)
+ *                     printf("File contains nothing\n");
+ *             else if (strchr(file, '\n'))
+ *                     printf("File contains multiple lines\n");
+ *             else
+ *                     printf("File contains one line\n");
+ *             talloc_free(file);
+ *
+ *             return 0;
+ *     }
+ *
+ * Licence: LGPL (2 or any later version)
+ */
+int main(int argc, char *argv[])
+{
+       if (argc != 2)
+               return 1;
+
+       if (strcmp(argv[1], "depends") == 0) {
+               printf("ccan/talloc\n");
+               printf("ccan/noerr\n");
+               return 0;
+       }
+
+       return 1;
+}
diff --git a/ccan/grab_file/grab_file.c b/ccan/grab_file/grab_file.c
new file mode 100644 (file)
index 0000000..6f2c9fe
--- /dev/null
@@ -0,0 +1,50 @@
+#include "grab_file.h"
+#include "talloc/talloc.h"
+#include "noerr/noerr.h"
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+void *grab_fd(const void *ctx, int fd, size_t *size)
+{
+       int ret;
+       size_t max = 16384, s;
+       char *buffer;
+
+       if (!size)
+               size = &s;
+       *size = 0;
+
+       buffer = talloc_array(ctx, char, max+1);
+       while ((ret = read(fd, buffer + *size, max - *size)) > 0) {
+               *size += ret;
+               if (*size == max)
+                       buffer = talloc_realloc(ctx, buffer, char, max*=2 + 1);
+       }
+       if (ret < 0) {
+               talloc_free(buffer);
+               buffer = NULL;
+       } else
+               buffer[*size] = '\0';
+
+       return buffer;
+}
+
+void *grab_file(const void *ctx, const char *filename, size_t *size)
+{
+       int fd;
+       char *buffer;
+
+       if (!filename)
+               fd = dup(STDIN_FILENO);
+       else
+               fd = open(filename, O_RDONLY, 0);
+
+       if (fd < 0)
+               return NULL;
+
+       buffer = grab_fd(ctx, fd, size);
+       close_noerr(fd);
+       return buffer;
+}
diff --git a/ccan/grab_file/grab_file.h b/ccan/grab_file/grab_file.h
new file mode 100644 (file)
index 0000000..5f7e37c
--- /dev/null
@@ -0,0 +1,58 @@
+#ifndef CCAN_GRAB_FILE_H
+#define CCAN_GRAB_FILE_H
+#include <stdio.h> // For size_t
+
+/**
+ * grab_fd - read all of a file descriptor into memory
+ * @ctx: the context to tallocate from (often NULL)
+ * @fd: the file descriptor to read from
+ * @size: the (optional) size of the file
+ *
+ * This function reads from the given file descriptor until no more
+ * input is available.  The content is talloced off @ctx, and the size
+ * of the file places in @size if it's non-NULL.  For convenience, the
+ * byte after the end of the content will always be NUL.
+ *
+ * Example:
+ *     // Return all of standard input, as lines.
+ *     char **read_as_lines(void)
+ *     {
+ *             char **lines, *all;
+ *
+ *             all = grab_fd(NULL, 0, NULL);
+ *             if (!all)
+ *                     return NULL;
+ *             lines = strsplit(NULL, all, "\n", NULL);
+ *             talloc_free(all);
+ *             return lines;
+ *     }
+ */
+void *grab_fd(const void *ctx, int fd, size_t *size);
+
+/**
+ * grab_file - read all of a file (or stdin) into memory
+ * @ctx: the context to tallocate from (often NULL)
+ * @filename: the file to read (NULL for stdin)
+ * @size: the (optional) size of the file
+ *
+ * This function reads from the given file until no more input is
+ * available.  The content is talloced off @ctx, and the size of the
+ * file places in @size if it's non-NULL.  For convenience, the byte
+ * after the end of the content will always be NUL.
+ *
+ * Example:
+ *     // Return all of a given file, as lines.
+ *     char **read_as_lines(const char *filename)
+ *     {
+ *             char **lines, *all;
+ *
+ *             all = grab_file(NULL, filename, NULL);
+ *             if (!all)
+ *                     return NULL;
+ *             lines = strsplit(NULL, all, "\n", NULL);
+ *             talloc_free(all);
+ *             return lines;
+ *     }
+ */
+void *grab_file(const void *ctx, const char *filename, size_t *size);
+#endif /* CCAN_GRAB_FILE_H */
diff --git a/ccan/grab_file/test/run-grab.c b/ccan/grab_file/test/run-grab.c
new file mode 100644 (file)
index 0000000..2b36a0f
--- /dev/null
@@ -0,0 +1,34 @@
+/* This is test for grab_file() function
+ */
+
+#include       <stdlib.h>
+#include       <stdio.h>
+#include       <err.h>
+#include       <sys/stat.h>
+#include       "string/string.h"
+#include       "string/string.c"
+#include       "tap/tap.h"
+
+int 
+main(int argc, char *argv[])
+{
+       unsigned int    i;
+       char            **split, *str;
+       int             length;
+       struct          stat st;
+
+       str = grab_file(NULL, "ccan/string/test/run-grab.c", NULL);
+       split = strsplit(NULL, str, "\n", NULL);
+       length = strlen(split[0]);
+       ok1(streq(split[0], "/* This is test for grab_file() function"));
+       for (i = 1; split[i]; i++)      
+               length += strlen(split[i]);
+       ok1(streq(split[i-1], "/* End of grab_file() test */"));
+       if (stat("ccan/string/test/run-grab.c", &st) != 0) 
+               err(1, "Could not stat self");
+       ok1(st.st_size == length + i);
+       
+       return 0;
+}
+
+/* End of grab_file() test */
diff --git a/ccan/str/_info.c b/ccan/str/_info.c
new file mode 100644 (file)
index 0000000..4baea77
--- /dev/null
@@ -0,0 +1,37 @@
+#include <stdio.h>
+#include <string.h>
+#include "config.h"
+
+/**
+ * str - string helper routines
+ *
+ * This is a grab bag of functions for string operations, designed to enhance
+ * the standard string.h.
+ *
+ * Example:
+ *     #include "str/str.h"
+ *
+ *     int main(int argc, char *argv[])
+ *     {
+ *             if (argv[1] && streq(argv[1], "--verbose"))
+ *                     printf("verbose set\n");
+ *             if (argv[1] && strstarts(argv[1], "--"))
+ *                     printf("Some option set\n");
+ *             if (argv[1] && strends(argv[1], "cow-powers"))
+ *                     printf("Magic option set\n");
+ *             return 0;
+ *     }
+ *
+ * Licence: LGPL (2 or any later version)
+ */
+int main(int argc, char *argv[])
+{
+       if (argc != 2)
+               return 1;
+
+       if (strcmp(argv[1], "depends") == 0) {
+               return 0;
+       }
+
+       return 1;
+}
diff --git a/ccan/str/str.h b/ccan/str/str.h
new file mode 100644 (file)
index 0000000..f633bc7
--- /dev/null
@@ -0,0 +1,46 @@
+#ifndef CCAN_STR_H
+#define CCAN_STR_H
+#include <string.h>
+#include <stdbool.h>
+
+/**
+ * streq - Are two strings equal?
+ * @a: first string
+ * @b: first string
+ *
+ * This macro is arguably more readable than "!strcmp(a, b)".
+ *
+ * Example:
+ *     if (streq(str, ""))
+ *             printf("String is empty!\n");
+ */
+#define streq(a,b) (strcmp((a),(b)) == 0)
+
+/**
+ * strstarts - Does this string start with this prefix?
+ * @str: string to test
+ * @prefix: prefix to look for at start of str
+ *
+ * Example:
+ *     if (strstarts(str, "foo"))
+ *             printf("String %s begins with 'foo'!\n", str);
+ */
+#define strstarts(str,prefix) (strncmp((str),(prefix),strlen(prefix)) == 0)
+
+/**
+ * strends - Does this string end with this postfix?
+ * @str: string to test
+ * @postfix: postfix to look for at end of str
+ *
+ * Example:
+ *     if (strends(str, "foo"))
+ *             printf("String %s end with 'foo'!\n", str);
+ */
+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);
+}
+#endif /* CCAN_STR_H */
diff --git a/ccan/str/test/run.c b/ccan/str/test/run.c
new file mode 100644 (file)
index 0000000..20711ad
--- /dev/null
@@ -0,0 +1,83 @@
+#include "string/string.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include "string/string.c"
+#include "tap/tap.h"
+
+/* FIXME: ccanize */
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
+
+static char *substrings[] = { "far", "bar", "baz", "b", "ba", "z", "ar", NULL };
+
+#define NUM_SUBSTRINGS (ARRAY_SIZE(substrings) - 1)
+
+static char *strdup_rev(const char *s)
+{
+       char *ret = strdup(s);
+       unsigned int i;
+
+       for (i = 0; i < strlen(s); i++)
+               ret[i] = s[strlen(s) - i - 1];
+       return ret;
+}
+
+int main(int argc, char *argv[])
+{
+       unsigned int i, j, n;
+       char **split, *str;
+       void *ctx;
+       char *strings[NUM_SUBSTRINGS * NUM_SUBSTRINGS];
+       
+       n = 0;
+       for (i = 0; i < NUM_SUBSTRINGS; i++) {
+               for (j = 0; j < NUM_SUBSTRINGS; j++) {
+                       strings[n] = malloc(strlen(substrings[i])
+                                           + strlen(substrings[j]) + 1);
+                       sprintf(strings[n++], "%s%s",
+                               substrings[i], substrings[j]);
+               }
+       }
+
+       plan_tests(n * n * 5);
+       for (i = 0; i < n; i++) {
+               for (j = 0; j < n; j++) {
+                       unsigned int k, identical = 0;
+                       char *reva, *revb;
+
+                       /* Find first difference. */
+                       for (k = 0; strings[i][k]==strings[j][k]; k++) {
+                               if (k == strlen(strings[i])) {
+                                       identical = 1;
+                                       break;
+                               }
+                       }
+
+                       if (identical) 
+                               ok1(streq(strings[i], strings[j]));
+                       else
+                               ok1(!streq(strings[i], strings[j]));
+
+                       /* Postfix test should be equivalent to prefix
+                        * test on reversed string. */
+                       reva = strdup_rev(strings[i]);
+                       revb = strdup_rev(strings[j]);
+
+                       if (!strings[i][k]) {
+                               ok1(strstarts(strings[j], strings[i]));
+                               ok1(strends(revb, reva));
+                       } else {
+                               ok1(!strstarts(strings[j], strings[i]));
+                               ok1(!strends(revb, reva));
+                       }
+                       if (!strings[j][k]) {
+                               ok1(strstarts(strings[i], strings[j]));
+                               ok1(strends(reva, revb));
+                       } else {
+                               ok1(!strstarts(strings[i], strings[j]));
+                               ok1(!strends(reva, revb));
+                       }
+               }
+       }
+
+       return exit_status();
+}                              
diff --git a/ccan/str_talloc/_info.c b/ccan/str_talloc/_info.c
new file mode 100644 (file)
index 0000000..3186b2b
--- /dev/null
@@ -0,0 +1,52 @@
+#include <stdio.h>
+#include <string.h>
+#include "config.h"
+
+/**
+ * str_talloc - string helper routines which use talloc
+ *
+ * This is a grab bag of fnctions for string operations, designed to enhance
+ * the standard string.h; these are separated from the non-talloc-needing
+ * string utilities in "str.h".
+ *
+ * Example:
+ *     #include "str_talloc/str_talloc.h"
+ *     #include "talloc/talloc.h"
+ *     #include "grab_file/grab_file.h"
+ *     #include <err.h>
+ *     
+ *     // Dumb demo program to double-linespace a file.
+ *     int main(int argc, char *argv[])
+ *     {
+ *             char *textfile;
+ *             char **lines;
+ *     
+ *             // Grab lines in file.
+ *             textfile = grab_file(NULL, argv[1], NULL);
+ *             if (!textfile)
+ *                     err(1, "Failed reading %s", argv[1]);
+ *             lines = strsplit(textfile, textfile, "\n", NULL);
+ *     
+ *             // Join them back together with two linefeeds.
+ *             printf("%s", strjoin(textfile, lines, "\n\n"));
+ *
+ *             // Free everything, just because we can.
+ *             talloc_free(textfile);
+ *             return 0;
+ *     }
+ *
+ * Licence: LGPL (2 or any later version)
+ */
+int main(int argc, char *argv[])
+{
+       if (argc != 2)
+               return 1;
+
+       if (strcmp(argv[1], "depends") == 0) {
+               printf("ccan/talloc\n");
+               printf("ccan/noerr\n");
+               return 0;
+       }
+
+       return 1;
+}
diff --git a/ccan/str_talloc/str_talloc.c b/ccan/str_talloc/str_talloc.c
new file mode 100644 (file)
index 0000000..7438f48
--- /dev/null
@@ -0,0 +1,43 @@
+#include <unistd.h>
+#include <stdint.h>
+#include <string.h>
+#include <limits.h>
+#include <stdlib.h>
+#include "str_talloc.h"
+#include "talloc/talloc.h"
+
+char **strsplit(const void *ctx, const char *string, const char *delims,
+                unsigned int *nump)
+{
+       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;
+       if (nump)
+               *nump = num;
+       return lines;
+}
+
+char *strjoin(const void *ctx, char *strings[], const char *delim)
+{
+       unsigned int i;
+       char *ret = talloc_strdup(ctx, "");
+
+       for (i = 0; strings[i]; i++) {
+               ret = talloc_append_string(ret, strings[i]);
+               ret = talloc_append_string(ret, delim);
+       }
+       return ret;
+}
diff --git a/ccan/str_talloc/str_talloc.h b/ccan/str_talloc/str_talloc.h
new file mode 100644 (file)
index 0000000..9828cd8
--- /dev/null
@@ -0,0 +1,64 @@
+#ifndef CCAN_STR_TALLOC_H
+#define CCAN_STR_TALLOC_H
+#include <string.h>
+#include <stdbool.h>
+
+/**
+ * strsplit - Split string into an array of substrings
+ * @ctx: the context to tallocate from (often NULL)
+ * @string: the string to split
+ * @delims: delimiters where lines should be split.
+ * @nump: optional pointer to place resulting number of lines
+ *
+ * This function splits a single string into multiple strings.  The
+ * original string is untouched: an array is allocated (using talloc)
+ * pointing to copies of each substring.  Multiple delimiters result
+ * in empty substrings.  By definition, no delimiters will appear in
+ * the substrings.
+ *
+ * The final char * in the array will be NULL, so you can use this or
+ * @nump to find the array length.
+ *
+ * Example:
+ *     unsigned int count_long_lines(const char *text)
+ *     {
+ *             char **lines;
+ *             unsigned int i, long_lines = 0;
+ *
+ *             // Can only fail on out-of-memory.
+ *             lines = strsplit(NULL, string, "\n", NULL);
+ *             for (i = 0; lines[i] != NULL; i++)
+ *                     if (strlen(lines[i]) > 80)
+ *                             long_lines++;
+ *             talloc_free(lines);
+ *             return long_lines;
+ *     }
+ */
+char **strsplit(const void *ctx, const char *string, const char *delims,
+                unsigned int *nump);
+
+/**
+ * strjoin - Join an array of substrings into one long string
+ * @ctx: the context to tallocate from (often NULL)
+ * @strings: the NULL-terminated array of strings to join
+ * @delim: the delimiter to insert between the strings
+ *
+ * This function joins an array of strings into a single string.  The
+ * return value is allocated using talloc.  Each string in @strings is
+ * followed by a copy of @delim.
+ *
+ * Example:
+ *     // Append the string "--EOL" to each line.
+ *     char *append_to_all_lines(const char *string)
+ *     {
+ *             char **lines, *ret;
+ *             unsigned int i, num, newnum;
+ *
+ *             lines = strsplit(NULL, string, "\n", NULL);
+ *             ret = strjoin(NULL, lines, "-- EOL\n");
+ *             talloc_free(lines);
+ *             return ret;
+ *     }
+ */
+char *strjoin(const void *ctx, char *strings[], const char *delim);
+#endif /* CCAN_STR_TALLOC_H */
diff --git a/ccan/str_talloc/test/run.c b/ccan/str_talloc/test/run.c
new file mode 100644 (file)
index 0000000..6da48bd
--- /dev/null
@@ -0,0 +1,52 @@
+#include "str_talloc/str_talloc.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include "str_talloc/str_talloc.c"
+#include "tap/tap.h"
+
+/* FIXME: ccanize */
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
+
+int main(int argc, char *argv[])
+{
+       unsigned int i, j, n;
+
+       plan_tests(19);
+       split = strsplit(NULL, "hello  world", " ", &n);
+       ok1(n == 3);
+       ok1(streq(split[0], "hello"));
+       ok1(streq(split[1], ""));
+       ok1(streq(split[2], "world"));
+       ok1(split[3] == NULL);
+       talloc_free(split);
+
+       split = strsplit(NULL, "hello  world", " ", NULL);
+       ok1(streq(split[0], "hello"));
+       ok1(streq(split[1], ""));
+       ok1(streq(split[2], "world"));
+       ok1(split[3] == NULL);
+       talloc_free(split);
+
+       split = strsplit(NULL, "hello  world", "o ", NULL);
+       ok1(streq(split[0], "hell"));
+       ok1(streq(split[1], ""));
+       ok1(streq(split[2], ""));
+       ok1(streq(split[3], "w"));
+       ok1(streq(split[4], "rld"));
+       ok1(split[5] == NULL);
+
+       ctx = split;
+       split = strsplit(ctx, "hello  world", "o ", NULL);
+       ok1(talloc_parent(split) == ctx);
+       talloc_free(ctx);
+
+       str = strjoin(NULL, substrings, ", ");
+       ok1(streq(str, "far, bar, baz, b, ba, z, ar, "));
+       ctx = str;
+       str = strjoin(ctx, substrings, "");
+       ok1(streq(str, "farbarbazbbazar"));
+       ok1(talloc_parent(str) == ctx);
+       talloc_free(ctx);
+
+       return exit_status();
+}                              
diff --git a/ccan/string/_info.c b/ccan/string/_info.c
deleted file mode 100644 (file)
index 873d360..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-#include "config.h"
-
-/**
- * string - string helper routines
- *
- * This is a grab bag of modules for string operations, designed to enhance
- * the standard string.h.
- *
- * Example:
- *     #include "string/string.h"
- *
- *     int main(int argc, char *argv[])
- *     {
- *             if (argv[1] && streq(argv[1], "--verbose"))
- *                     printf("verbose set\n");
- *             if (argv[1] && strstarts(argv[1], "--"))
- *                     printf("Some option set\n");
- *             if (argv[1] && strends(argv[1], "cow-powers"))
- *                     printf("Magic option set\n");
- *             return 0;
- *     }
- *
- * Licence: LGPL (2 or any later version)
- */
-int main(int argc, char *argv[])
-{
-       if (argc != 2)
-               return 1;
-
-       if (strcmp(argv[1], "depends") == 0) {
-               printf("ccan/talloc\n");
-               printf("ccan/noerr\n");
-               return 0;
-       }
-
-       return 1;
-}
diff --git a/ccan/string/string.c b/ccan/string/string.c
deleted file mode 100644 (file)
index f0d0fd4..0000000
+++ /dev/null
@@ -1,92 +0,0 @@
-#include <unistd.h>
-#include <stdint.h>
-#include <string.h>
-#include <limits.h>
-#include <assert.h>
-#include <stdlib.h>
-#include "string.h"
-#include "talloc/talloc.h"
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <errno.h>
-#include "noerr/noerr.h"
-
-char **strsplit(const void *ctx, const char *string, const char *delims,
-                unsigned int *nump)
-{
-       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;
-       if (nump)
-               *nump = num;
-       return lines;
-}
-
-char *strjoin(const void *ctx, char *strings[], const char *delim)
-{
-       unsigned int i;
-       char *ret = talloc_strdup(ctx, "");
-
-       for (i = 0; strings[i]; i++) {
-               ret = talloc_append_string(ret, strings[i]);
-               ret = talloc_append_string(ret, delim);
-       }
-       return ret;
-}
-
-void *grab_fd(const void *ctx, int fd, size_t *size)
-{
-       int ret;
-       size_t max = 16384, s;
-       char *buffer;
-
-       if (!size)
-               size = &s;
-       *size = 0;
-
-       buffer = talloc_array(ctx, char, max+1);
-       while ((ret = read(fd, buffer + *size, max - *size)) > 0) {
-               *size += ret;
-               if (*size == max)
-                       buffer = talloc_realloc(ctx, buffer, char, max*=2 + 1);
-       }
-       if (ret < 0) {
-               talloc_free(buffer);
-               buffer = NULL;
-       } else
-               buffer[*size] = '\0';
-
-       return buffer;
-}
-
-void *grab_file(const void *ctx, const char *filename, size_t *size)
-{
-       int fd;
-       char *buffer;
-
-       if (!filename)
-               fd = dup(STDIN_FILENO);
-       else
-               fd = open(filename, O_RDONLY, 0);
-
-       if (fd < 0)
-               return NULL;
-
-       buffer = grab_fd(ctx, fd, size);
-       close_noerr(fd);
-       return buffer;
-}
diff --git a/ccan/string/string.h b/ccan/string/string.h
deleted file mode 100644 (file)
index 3bc7adf..0000000
+++ /dev/null
@@ -1,159 +0,0 @@
-#ifndef CCAN_STRING_H
-#define CCAN_STRING_H
-#include <string.h>
-#include <stdbool.h>
-
-/**
- * streq - Are two strings equal?
- * @a: first string
- * @b: first string
- *
- * This macro is arguably more readable than "!strcmp(a, b)".
- *
- * Example:
- *     if (streq(str, ""))
- *             printf("String is empty!\n");
- */
-#define streq(a,b) (strcmp((a),(b)) == 0)
-
-/**
- * strstarts - Does this string start with this prefix?
- * @str: string to test
- * @prefix: prefix to look for at start of str
- *
- * Example:
- *     if (strstarts(str, "foo"))
- *             printf("String %s begins with 'foo'!\n", str);
- */
-#define strstarts(str,prefix) (strncmp((str),(prefix),strlen(prefix)) == 0)
-
-/**
- * strends - Does this string end with this postfix?
- * @str: string to test
- * @postfix: postfix to look for at end of str
- *
- * Example:
- *     if (strends(str, "foo"))
- *             printf("String %s end with 'foo'!\n", str);
- */
-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);
-}
-
-/**
- * strsplit - Split string into an array of substrings
- * @ctx: the context to tallocate from (often NULL)
- * @string: the string to split
- * @delims: delimiters where lines should be split.
- * @nump: optional pointer to place resulting number of lines
- *
- * This function splits a single string into multiple strings.  The
- * original string is untouched: an array is allocated (using talloc)
- * pointing to copies of each substring.  Multiple delimiters result
- * in empty substrings.  By definition, no delimiters will appear in
- * the substrings.
- *
- * The final char * in the array will be NULL, so you can use this or
- * @nump to find the array length.
- *
- * Example:
- *     unsigned int count_long_lines(const char *text)
- *     {
- *             char **lines;
- *             unsigned int i, long_lines = 0;
- *
- *             // Can only fail on out-of-memory.
- *             lines = strsplit(NULL, string, "\n", NULL);
- *             for (i = 0; lines[i] != NULL; i++)
- *                     if (strlen(lines[i]) > 80)
- *                             long_lines++;
- *             talloc_free(lines);
- *             return long_lines;
- *     }
- */
-char **strsplit(const void *ctx, const char *string, const char *delims,
-                unsigned int *nump);
-
-/**
- * strjoin - Join an array of substrings into one long string
- * @ctx: the context to tallocate from (often NULL)
- * @strings: the NULL-terminated array of strings to join
- * @delim: the delimiter to insert between the strings
- *
- * This function joins an array of strings into a single string.  The
- * return value is allocated using talloc.  Each string in @strings is
- * followed by a copy of @delim.
- *
- * Example:
- *     // Append the string "--EOL" to each line.
- *     char *append_to_all_lines(const char *string)
- *     {
- *             char **lines, *ret;
- *             unsigned int i, num, newnum;
- *
- *             lines = strsplit(NULL, string, "\n", NULL);
- *             ret = strjoin(NULL, lines, "-- EOL\n");
- *             talloc_free(lines);
- *             return ret;
- *     }
- */
-char *strjoin(const void *ctx, char *strings[], const char *delim);
-
-/**
- * grab_fd - read all of a file descriptor into memory
- * @ctx: the context to tallocate from (often NULL)
- * @fd: the file descriptor to read from
- * @size: the (optional) size of the file
- *
- * This function reads from the given file descriptor until no more
- * input is available.  The content is talloced off @ctx, and the size
- * of the file places in @size if it's non-NULL.  For convenience, the
- * byte after the end of the content will always be NUL.
- *
- * Example:
- *     // Return all of standard input, as lines.
- *     char **read_as_lines(void)
- *     {
- *             char **lines, *all;
- *
- *             all = grab_fd(NULL, 0, NULL);
- *             if (!all)
- *                     return NULL;
- *             lines = strsplit(NULL, all, "\n", NULL);
- *             talloc_free(all);
- *             return lines;
- *     }
- */
-void *grab_fd(const void *ctx, int fd, size_t *size);
-
-/**
- * grab_file - read all of a file (or stdin) into memory
- * @ctx: the context to tallocate from (often NULL)
- * @filename: the file to read (NULL for stdin)
- * @size: the (optional) size of the file
- *
- * This function reads from the given file until no more input is
- * available.  The content is talloced off @ctx, and the size of the
- * file places in @size if it's non-NULL.  For convenience, the byte
- * after the end of the content will always be NUL.
- *
- * Example:
- *     // Return all of a given file, as lines.
- *     char **read_as_lines(const char *filename)
- *     {
- *             char **lines, *all;
- *
- *             all = grab_file(NULL, filename, NULL);
- *             if (!all)
- *                     return NULL;
- *             lines = strsplit(NULL, all, "\n", NULL);
- *             talloc_free(all);
- *             return lines;
- *     }
- */
-void *grab_file(const void *ctx, const char *filename, size_t *size);
-#endif /* CCAN_STRING_H */
diff --git a/ccan/string/test/run-grab.c b/ccan/string/test/run-grab.c
deleted file mode 100644 (file)
index 2b36a0f..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-/* This is test for grab_file() function
- */
-
-#include       <stdlib.h>
-#include       <stdio.h>
-#include       <err.h>
-#include       <sys/stat.h>
-#include       "string/string.h"
-#include       "string/string.c"
-#include       "tap/tap.h"
-
-int 
-main(int argc, char *argv[])
-{
-       unsigned int    i;
-       char            **split, *str;
-       int             length;
-       struct          stat st;
-
-       str = grab_file(NULL, "ccan/string/test/run-grab.c", NULL);
-       split = strsplit(NULL, str, "\n", NULL);
-       length = strlen(split[0]);
-       ok1(streq(split[0], "/* This is test for grab_file() function"));
-       for (i = 1; split[i]; i++)      
-               length += strlen(split[i]);
-       ok1(streq(split[i-1], "/* End of grab_file() test */"));
-       if (stat("ccan/string/test/run-grab.c", &st) != 0) 
-               err(1, "Could not stat self");
-       ok1(st.st_size == length + i);
-       
-       return 0;
-}
-
-/* End of grab_file() test */
diff --git a/ccan/string/test/run.c b/ccan/string/test/run.c
deleted file mode 100644 (file)
index c50854a..0000000
+++ /dev/null
@@ -1,119 +0,0 @@
-#include "string/string.h"
-#include <stdlib.h>
-#include <stdio.h>
-#include "string/string.c"
-#include "tap/tap.h"
-
-/* FIXME: ccanize */
-#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
-
-static char *substrings[] = { "far", "bar", "baz", "b", "ba", "z", "ar", NULL };
-
-#define NUM_SUBSTRINGS (ARRAY_SIZE(substrings) - 1)
-
-static char *strdup_rev(const char *s)
-{
-       char *ret = strdup(s);
-       unsigned int i;
-
-       for (i = 0; i < strlen(s); i++)
-               ret[i] = s[strlen(s) - i - 1];
-       return ret;
-}
-
-int main(int argc, char *argv[])
-{
-       unsigned int i, j, n;
-       char **split, *str;
-       void *ctx;
-       char *strings[NUM_SUBSTRINGS * NUM_SUBSTRINGS];
-       
-       n = 0;
-       for (i = 0; i < NUM_SUBSTRINGS; i++) {
-               for (j = 0; j < NUM_SUBSTRINGS; j++) {
-                       strings[n] = malloc(strlen(substrings[i])
-                                           + strlen(substrings[j]) + 1);
-                       sprintf(strings[n++], "%s%s",
-                               substrings[i], substrings[j]);
-               }
-       }
-
-       plan_tests(n * n * 5 + 19);
-       for (i = 0; i < n; i++) {
-               for (j = 0; j < n; j++) {
-                       unsigned int k, identical = 0;
-                       char *reva, *revb;
-
-                       /* Find first difference. */
-                       for (k = 0; strings[i][k]==strings[j][k]; k++) {
-                               if (k == strlen(strings[i])) {
-                                       identical = 1;
-                                       break;
-                               }
-                       }
-
-                       if (identical) 
-                               ok1(streq(strings[i], strings[j]));
-                       else
-                               ok1(!streq(strings[i], strings[j]));
-
-                       /* Postfix test should be equivalent to prefix
-                        * test on reversed string. */
-                       reva = strdup_rev(strings[i]);
-                       revb = strdup_rev(strings[j]);
-
-                       if (!strings[i][k]) {
-                               ok1(strstarts(strings[j], strings[i]));
-                               ok1(strends(revb, reva));
-                       } else {
-                               ok1(!strstarts(strings[j], strings[i]));
-                               ok1(!strends(revb, reva));
-                       }
-                       if (!strings[j][k]) {
-                               ok1(strstarts(strings[i], strings[j]));
-                               ok1(strends(reva, revb));
-                       } else {
-                               ok1(!strstarts(strings[i], strings[j]));
-                               ok1(!strends(reva, revb));
-                       }
-               }
-       }
-
-       split = strsplit(NULL, "hello  world", " ", &n);
-       ok1(n == 3);
-       ok1(streq(split[0], "hello"));
-       ok1(streq(split[1], ""));
-       ok1(streq(split[2], "world"));
-       ok1(split[3] == NULL);
-       talloc_free(split);
-
-       split = strsplit(NULL, "hello  world", " ", NULL);
-       ok1(streq(split[0], "hello"));
-       ok1(streq(split[1], ""));
-       ok1(streq(split[2], "world"));
-       ok1(split[3] == NULL);
-       talloc_free(split);
-
-       split = strsplit(NULL, "hello  world", "o ", NULL);
-       ok1(streq(split[0], "hell"));
-       ok1(streq(split[1], ""));
-       ok1(streq(split[2], ""));
-       ok1(streq(split[3], "w"));
-       ok1(streq(split[4], "rld"));
-       ok1(split[5] == NULL);
-
-       ctx = split;
-       split = strsplit(ctx, "hello  world", "o ", NULL);
-       ok1(talloc_parent(split) == ctx);
-       talloc_free(ctx);
-
-       str = strjoin(NULL, substrings, ", ");
-       ok1(streq(str, "far, bar, baz, b, ba, z, ar, "));
-       ctx = str;
-       str = strjoin(ctx, substrings, "");
-       ok1(streq(str, "farbarbazbbazar"));
-       ok1(talloc_parent(str) == ctx);
-       talloc_free(ctx);
-
-       return exit_status();
-}                              
index 2c55a12a987d4c0d4b2c83b993e85cf775d140bc..380009f380f8f16893019e33c178fe8eb575074a 100644 (file)
@@ -27,7 +27,7 @@
 #include <stdio.h>
 #include <stdarg.h>
 #include "config.h"
 #include <stdio.h>
 #include <stdarg.h>
 #include "config.h"
-#include "ccan/typesafe_cb/typesafe_cb.h"
+#include "typesafe_cb/typesafe_cb.h"
 
 /*
   this uses a little trick to allow __LINE__ to be stringified
 
 /*
   this uses a little trick to allow __LINE__ to be stringified
index ca8c4a610967402a9a8047e7e2b73172eabc81ad..0511648e45db045f416c3a9cf0f12ba74783e768 100644 (file)
@@ -2,7 +2,7 @@
 #include <err.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <err.h>
 #include <stdlib.h>
 #include <stdio.h>
-#include "string/string.h"
+#include "str/str.h"
 #include "talloc/talloc.h"
 
 int main(int argc, char *argv[])
 #include "talloc/talloc.h"
 
 int main(int argc, char *argv[])
index 4cc5044072d388c7a65ac477f587b663f887c06c..a084a2244dbb73c854cac36389b4c917c1aaf27b 100644 (file)
@@ -23,7 +23,8 @@ tools/ccanlint/ccanlint: \
        $(OBJS)                 \
        tools/ccanlint/ccanlint.o \
        tools/ccanlint/file_analysis.o \
        $(OBJS)                 \
        tools/ccanlint/ccanlint.o \
        tools/ccanlint/file_analysis.o \
-       ccan/string/string.o ccan/talloc/talloc.o ccan/noerr/noerr.o
+       ccan/str_talloc/str_talloc.o ccan/grab_file/grab_file.o \
+       ccan/talloc/talloc.o ccan/noerr/noerr.o
 
 ccanlint-clean:
        $(RM) tools/ccanlint/generated-init-tests
 
 ccanlint-clean:
        $(RM) tools/ccanlint/generated-init-tests
index bee99597310c346f5b8a291f40858de41b8605d5..d9f03dacfed94702e61a0ae0fa54e7b3f9c32e3a 100644 (file)
@@ -1,7 +1,9 @@
 #include "ccanlint.h"
 #include "get_file_lines.h"
 #include <talloc/talloc.h>
 #include "ccanlint.h"
 #include "get_file_lines.h"
 #include <talloc/talloc.h>
-#include <string/string.h>
+#include <str/str.h>
+#include <str_talloc/str_talloc.h>
+#include <grab_file/grab_file.h>
 #include <noerr/noerr.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <noerr/noerr.h>
 #include <unistd.h>
 #include <sys/types.h>
index 75e3f91dc51dc78007b621479433fdd7f0a2068c..0647f4346778fd67aac71ec428296e6ace541732 100644 (file)
@@ -8,7 +8,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <err.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <err.h>
-#include <string/string.h>
+#include <str/str.h>
 #include <talloc/talloc.h>
 #include <noerr/noerr.h>
 
 #include <talloc/talloc.h>
 #include <noerr/noerr.h>
 
index 7a187ab273a6187defb5e2269f9d0689758d6ad4..4a8d14b3287b3dc2b471e6308c830305bd45f4d7 100644 (file)
@@ -8,9 +8,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <err.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <err.h>
-#include <string/string.h>
 #include <talloc/talloc.h>
 #include <talloc/talloc.h>
-#include <noerr/noerr.h>
 
 static char test_is_not_dir[] = "test is not a directory";
 
 
 static char test_is_not_dir[] = "test is not a directory";
 
index 4456557516b6c414d36a57102a007311be9d2da0..78ebd2ce84da9b1f55ab91b655b55827e2cb897e 100644 (file)
@@ -1,6 +1,6 @@
 #include "ccanlint.h"
 #include <talloc/talloc.h>
 #include "ccanlint.h"
 #include <talloc/talloc.h>
-#include <string/string.h>
+#include <str/str.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
index 3aeec6e5d820f56db2bfddac3f17ed0a9d2aa3d8..35f37380c0e2f331f3ec37b7777e674a7a18df73 100644 (file)
@@ -1,7 +1,7 @@
 /* Trailing whitespace test.  Almost embarrassing, but trivial. */
 #include "ccanlint.h"
 #include <talloc/talloc.h>
 /* Trailing whitespace test.  Almost embarrassing, but trivial. */
 #include "ccanlint.h"
 #include <talloc/talloc.h>
-#include <string/string.h>
+#include <str/str.h>
 
 static char *report_on_trailing_whitespace(const char *line)
 {
 
 static char *report_on_trailing_whitespace(const char *line)
 {
index 78bdea6c9031f833cc4da09f1b518a18956a288a..ecf043c56fbddf3df03773deabb5719ca1c62c62 100644 (file)
@@ -4,7 +4,8 @@
 #include <stdio.h>
 #include <string.h>
 #include <sqlite3.h>
 #include <stdio.h>
 #include <string.h>
 #include <sqlite3.h>
-#include "ccan/string/string.h"
+#include "ccan/grab_file/grab_file.h"
+#include "ccan/str_talloc/str_talloc.h"
 #include "ccan/talloc/talloc.h"
 #include "tools/_infotojson/database.h"
 
 #include "ccan/talloc/talloc.h"
 #include "tools/_infotojson/database.h"
 
index d215441eead6acebe84b629d2b3c855b4191098c..c631b9bc82968747147a1fd7328297a5d9fff3bc 100644 (file)
@@ -1,5 +1,7 @@
 #include "talloc/talloc.h"
 #include "talloc/talloc.h"
-#include "string/string.h"
+#include "str/str.h"
+#include "grab_file/grab_file.h"
+#include "str_talloc/str_talloc.h"
 #include "tools.h"
 #include <err.h>
 #include <stdbool.h>
 #include "tools.h"
 #include <err.h>
 #include <stdbool.h>
index 0a5752c1db2d725094c99400bc84ab8a3c37afe3..fb7024a75daccb6baff61b0947ef8876ec24c19b 100644 (file)
@@ -9,7 +9,9 @@
 #include <fcntl.h>
 #include <stdbool.h>
 #include "talloc/talloc.h"
 #include <fcntl.h>
 #include <stdbool.h>
 #include "talloc/talloc.h"
-#include "string/string.h"
+#include "str/str.h"
+#include "str_talloc/str_talloc.h"
+#include "grab_file/grab_file.h"
 
 static char **grab_doc(const char *fname)
 {
 
 static char **grab_doc(const char *fname)
 {
index 6393d72e3d4a03e97255325d7e2d6f420a03242a..3fa040282b13022c546f8d3e8ecac452a3fccd47 100644 (file)
@@ -10,7 +10,9 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
-#include "ccan/string/string.h"
+#include "ccan/str/str.h"
+#include "ccan/str_talloc/str_talloc.h"
+#include "ccan/grab_file/grab_file.h"
 #include "ccan/talloc/talloc.h"
 #include "tools.h"
 
 #include "ccan/talloc/talloc.h"
 #include "tools.h"
 
index b9a6f66c4cd910acf6d4db0ff6f13e16be86b726..3ae752373de1b0e9c55e61647f82c43bbfcb2271 100644 (file)
@@ -6,7 +6,7 @@
 #include <unistd.h>
 #include "ccan/tap/tap.h"
 #include "ccan/talloc/talloc.h"
 #include <unistd.h>
 #include "ccan/tap/tap.h"
 #include "ccan/talloc/talloc.h"
-#include "ccan/string/string.h"
+#include "ccan/str/str.h"
 #include "tools.h"
 
 /* FIXME: Use build bug later. */
 #include "tools.h"
 
 /* FIXME: Use build bug later. */