X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftal%2Fgrab_file%2Fgrab_file.h;fp=ccan%2Ftal%2Fgrab_file%2Fgrab_file.h;h=03bf32a73c265975ae58bc36a55264340e000f12;hb=fbad46cd37018923bf0a7de2df37e35e90792c32;hp=0000000000000000000000000000000000000000;hpb=db4e16e71cd1bb474307c971c6312389bb254053;p=ccan diff --git a/ccan/tal/grab_file/grab_file.h b/ccan/tal/grab_file/grab_file.h new file mode 100644 index 00000000..03bf32a7 --- /dev/null +++ b/ccan/tal/grab_file/grab_file.h @@ -0,0 +1,60 @@ +/* Licensed under LGPLv2+ - see LICENSE file for details */ +#ifndef CCAN_TAL_GRAB_FILE_H +#define CCAN_TAL_GRAB_FILE_H +#include // 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 + * + * This function reads from the given file descriptor until no more + * input is available. The content is talloced off @ctx, and the + * tal_count() is the size in bytes plus one: for convenience, the + * byte after the end of the content will always be NUL. + * + * Example: + * #include + * #include + * ... + * // Return all of standard input, as lines. + * static char **read_stdin_as_lines(void) + * { + * char **lines, *all; + * + * all = grab_fd(NULL, 0); + * if (!all) + * return NULL; + * lines = tal_strsplit(NULL, all, "\n", STR_EMPTY_OK); + * tal_free(all); + * return lines; + * } + */ +void *grab_fd(const void *ctx, int fd); + +/** + * 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) + * + * This function reads from the given file until no more input is + * available. The content is talloced off @ctx, and the tal_count() + * is the size in bytes plus one: for convenience, the byte after the + * end of the content will always be NUL. + * + * Example: + * // Return all of a given file, as lines. + * static char **read_file_as_lines(const char *filename) + * { + * char **lines, *all; + * + * all = grab_file(NULL, filename); + * if (!all) + * return NULL; + * lines = tal_strsplit(NULL, all, "\n", STR_EMPTY_OK); + * tal_free(all); + * return lines; + * } + */ +void *grab_file(const void *ctx, const char *filename); +#endif /* CCAN_TAL_GRAB_FILE_H */