You can use talloc_array_length() to get the length of a tallocated array.
* all = grab_fd(NULL, 0, NULL);
* if (!all)
* return NULL;
- * lines = strsplit(NULL, all, "\n", NULL);
+ * lines = strsplit(NULL, all, "\n");
* talloc_free(all);
* return lines;
* }
* all = grab_file(NULL, filename, NULL);
* if (!all)
* return NULL;
- * lines = strsplit(NULL, all, "\n", NULL);
+ * lines = strsplit(NULL, all, "\n");
* talloc_free(all);
* return lines;
* }
struct stat st;
str = grab_file(NULL, "test/run-grab.c", NULL);
- split = strsplit(NULL, str, "\n", NULL);
+ split = strsplit(NULL, str, "\n");
length = strlen(split[0]);
ok1(!strcmp(split[0], "/* This is test for grab_file() function"));
for (i = 1; split[i]; i++)
* textfile = grab_file(NULL, argv[1], NULL);
* if (!textfile)
* err(1, "Failed reading %s", argv[1]);
- * lines = strsplit(textfile, textfile, "\n", NULL);
+ * lines = strsplit(textfile, textfile, "\n");
*
* // Join them back together with two linefeeds.
* printf("%s", strjoin(textfile, lines, "\n\n"));
#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)
{
char **lines = NULL;
unsigned int max = 64, num = 0;
lines = talloc_realloc(ctx, lines, char *, max*=2 + 1);
}
lines[num] = NULL;
- if (nump)
- *nump = num;
- return lines;
+
+ /* Shrink, so talloc_get_size works */
+ return talloc_realloc(ctx, lines, char *, num+1);
}
char *strjoin(const void *ctx, char *strings[], const char *delim)
* @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)
* 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.
+ * The final char * in the array will be NULL, talloc_array_length() of the
+ * returned value is 1 greater than the number of valid elements in
+ * the array.
*
* Example:
* #include <ccan/talloc/talloc.h>
* unsigned int i, long_lines = 0;
*
* // Can only fail on out-of-memory.
- * lines = strsplit(NULL, string, "\n", NULL);
+ * lines = strsplit(NULL, string, "\n");
* for (i = 0; lines[i] != NULL; i++)
* if (strlen(lines[i]) > 80)
* long_lines++;
* return long_lines;
* }
*/
-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);
/**
* strjoin - Join an array of substrings into one long string
* {
* char **lines, *ret;
*
- * lines = strsplit(NULL, string, "\n", NULL);
+ * lines = strsplit(NULL, string, "\n");
* ret = strjoin(NULL, lines, "-- EOL\n");
* talloc_free(lines);
* return ret;
#include <ccan/str_talloc/str_talloc.c>
#include <ccan/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 };
int main(int argc, char *argv[])
{
- unsigned int n;
char **split, *str;
void *ctx;
- plan_tests(19);
- split = strsplit(NULL, "hello world", " ", &n);
- ok1(n == 3);
+ plan_tests(16);
+ split = strsplit(NULL, "hello world", " ");
+ ok1(talloc_array_length(split) == 4);
ok1(!strcmp(split[0], "hello"));
ok1(!strcmp(split[1], ""));
ok1(!strcmp(split[2], "world"));
ok1(split[3] == NULL);
talloc_free(split);
- split = strsplit(NULL, "hello world", " ", NULL);
- ok1(!strcmp(split[0], "hello"));
- ok1(!strcmp(split[1], ""));
- ok1(!strcmp(split[2], "world"));
- ok1(split[3] == NULL);
- talloc_free(split);
-
- split = strsplit(NULL, "hello world", "o ", NULL);
+ split = strsplit(NULL, "hello world", "o ");
+ ok1(talloc_array_length(split) == 6);
ok1(!strcmp(split[0], "hell"));
ok1(!strcmp(split[1], ""));
ok1(!strcmp(split[2], ""));
ok1(split[5] == NULL);
ctx = split;
- split = strsplit(ctx, "hello world", "o ", NULL);
+ split = strsplit(ctx, "hello world", "o ");
ok1(talloc_parent(split) == ctx);
talloc_free(ctx);
if (!contents)
err(1, "Reading %s", filename[file]);
- lines = strsplit(contents, contents, "\n", NULL);
+ lines = strsplit(contents, contents, "\n");
if (!lines[0])
errx(1, "%s is empty", filename[file]);
- words = strsplit(lines, lines[0], " ", NULL);
+ words = strsplit(lines, lines[0], " ");
if (!streq(words[1], "tdb_open"))
fail(filename[file], 1, "does not start with tdb_open");
for (i = 1; lines[i]; i++) {
const struct op_table *opt;
- words = strsplit(lines, lines[i], " ", NULL);
+ words = strsplit(lines, lines[i], " ");
if (!words[0] || !words[1])
fail(filename[file], i+1,
"Expected seqnum number and op");
/* Resolve dependencies. */
foreach_ptr(list, &compulsory_tests, &normal_tests) {
list_for_each(list, c, list) {
- char **deps = strsplit(NULL, c->needs, " ", NULL);
+ char **deps = strsplit(NULL, c->needs, " ");
unsigned int i;
for (i = 0; deps[i]; i++) {
continue;
for (i = 0; i < d->num_lines; i++) {
- char **words = collapse(strsplit(d, d->lines[i], " \t",
- NULL), NULL);
+ char **words = collapse(strsplit(d, d->lines[i], " \t"),
+ NULL);
if (!words[0])
continue;
char **get_ccan_file_lines(struct ccan_file *f)
{
if (!f->lines)
- f->lines = strsplit(f, get_ccan_file_contents(f),
- "\n", &f->num_lines);
+ f->lines = strsplit(f, get_ccan_file_contents(f), "\n");
+ /* FIXME: is f->num_lines necessary? */
+ f->num_lines = talloc_array_length(f->lines) - 1;
return f->lines;
}
{
if (!f->doc_sections) {
get_ccan_file_lines(f);
- f->doc_sections = extract_doc_sections(f->lines, f->num_lines);
+ f->doc_sections = extract_doc_sections(f->lines);
}
return f->doc_sections;
}
static void analyze_coverage(struct manifest *m, bool full_gcov,
const char *output, struct score *score)
{
- char **lines = strsplit(score, output, "\n", NULL);
+ char **lines = strsplit(score, output, "\n");
float covered_lines = 0.0;
unsigned int i, total_lines = 0;
bool lines_matter = false;
static char *get_leaks(const char *output, char **errs)
{
char *leaks = talloc_strdup(output, "");
- unsigned int i, num;
- char **lines = strsplit(output, output, "\n", &num);
+ unsigned int i;
+ char **lines = strsplit(output, output, "\n");
*errs = talloc_strdup(output, "");
- for (i = 0; i < num; i++) {
+ for (i = 0; i < talloc_array_length(lines) - 1; i++) {
if (strstr(lines[i], " lost ")) {
/* A leak... */
if (strstr(lines[i], " definitely lost ")) {
#include <unistd.h>
#include <errno.h>
-static char ** __attribute__((format(printf, 3, 4)))
-lines_from_cmd(const void *ctx, unsigned int *num, char *format, ...)
+static char ** __attribute__((format(printf, 2, 3)))
+lines_from_cmd(const void *ctx, char *format, ...)
{
va_list ap;
char *cmd, *buffer;
err(1, "Reading from '%s'", cmd);
pclose(p);
- return strsplit(ctx, buffer, "\n", num);
+ return strsplit(ctx, buffer, "\n");
}
/* Be careful about trying to compile over running programs (parallel make).
}
cmd = talloc_asprintf(ctx, "%s depends", *infofile);
- deps = lines_from_cmd(cmd, num, "%s", cmd);
+ deps = lines_from_cmd(cmd, "%s", cmd);
if (!deps)
err(1, "Could not run '%s'", cmd);
+ /* FIXME: Do we need num arg? */
+ *num = talloc_array_length(deps) - 1;
return deps;
}
char **infofile)
{
char **deps, **lines, *raw, *fname;
- unsigned int i, n = 0;
+ unsigned int i, n;
fname = talloc_asprintf(ctx, "%s/_info", dir);
raw = grab_file(fname, fname, NULL);
errx(1, "Could not open %s", fname);
/* Replace \n by actual line breaks, and split it. */
- lines = strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n", &n);
+ lines = strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n");
- deps = talloc_array(ctx, char *, n+1);
+ deps = talloc_array(ctx, char *, talloc_array_length(lines));
for (n = i = 0; lines[i]; i++) {
char *str;
}
cmd = talloc_asprintf(ctx, "%s libs", *infofile);
- libs = lines_from_cmd(cmd, num, "%s", cmd);
+ libs = lines_from_cmd(cmd, "%s", cmd);
if (!libs)
err(1, "Could not run '%s'", cmd);
+ /* FIXME: Do we need num arg? */
+ *num = talloc_array_length(libs) - 1;
return libs;
}
#include "doc_extract.h"
#include "tools.h"
-static char **grab_doc(char **lines, unsigned int num, unsigned int **linemap)
+static char **grab_doc(char **lines, unsigned int **linemap)
{
char **ret;
- unsigned int i;
+ unsigned int i, num;
bool printing = false;
- ret = talloc_array(NULL, char *, num+1);
- *linemap = talloc_array(ret, unsigned int, num);
+ ret = talloc_array(NULL, char *, talloc_array_length(lines));
+ *linemap = talloc_array(ret, unsigned int, talloc_array_length(lines));
num = 0;
for (i = 0; lines[i]; i++) {
curr->lines[curr->num_lines++] = talloc_strdup(curr->lines, line);
}
-struct list_head *extract_doc_sections(char **rawlines, unsigned int num)
+struct list_head *extract_doc_sections(char **rawlines)
{
unsigned int *linemap;
- char **lines = grab_doc(rawlines, num, &linemap);
+ char **lines = grab_doc(rawlines, &linemap);
const char *function = NULL;
struct doc_section *curr = NULL;
unsigned int i;
type = argv[1];
for (i = 2; i < argc; i++) {
char *file, **lines;
- unsigned int num;
struct list_head *list;
struct doc_section *d;
file = grab_file(NULL, argv[i], NULL);
if (!file)
err(1, "Reading file %s", argv[i]);
- lines = strsplit(file, file, "\n", &num);
+ lines = strsplit(file, file, "\n");
- list = extract_doc_sections(lines, num);
+ list = extract_doc_sections(lines);
if (list_empty(list))
errx(1, "No documentation in file %s", argv[i]);
talloc_free(file);
char **lines;
};
-struct list_head *extract_doc_sections(char **rawlines, unsigned int num);
+struct list_head *extract_doc_sections(char **rawlines);
#endif /* _DOC_EXTRACT_CORE_H */
return NULL;
}
- for (line = strsplit(file, file, "\n", NULL); *line; line++)
+ for (line = strsplit(file, file, "\n"); *line; line++)
add_replace(&repl, *line);
return repl;
}