common/utils.h: add tal_arr_expand helper.

We do this a lot, and had boutique helpers in various places.  So add
a more generic one; for convenience it returns a pointer to the new
end element.

I prefer the name tal_arr_expand to tal_arr_append, since it's up to
the caller to populate the new array entry.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2018-09-27 09:49:24 +09:30
committed by Christian Decker
parent d590302523
commit 96f05549b2
23 changed files with 111 additions and 209 deletions

View File

@@ -47,7 +47,6 @@ static bool get_files(const char *dir, const char *subdir,
{
char *path = path_join(NULL, dir, subdir);
DIR *d = opendir(path);
size_t n = tal_count(*files);
struct dirent *e;
if (!d)
@@ -55,6 +54,7 @@ static bool get_files(const char *dir, const char *subdir,
while ((e = readdir(d)) != NULL) {
int preflen;
struct bolt_file *bf;
/* Must end in .md */
if (!strends(e->d_name, ".md"))
@@ -74,14 +74,12 @@ static bool get_files(const char *dir, const char *subdir,
if (verbose)
printf("Found bolt %.*s\n", preflen, e->d_name);
tal_resize(files, n+1);
(*files)[n].prefix = tal_strndup(*files,
e->d_name, preflen);
(*files)[n].contents
bf = tal_arr_expand(files);
bf->prefix = tal_strndup(*files, e->d_name, preflen);
bf->contents
= canonicalize(grab_file(*files,
path_join(path, path,
e->d_name)));
n++;
}
return true;
}