]> git.ozlabs.org Git - ccan/blob - ccan/tal/grab_file/grab_file.h
tal/grab_file: new module
[ccan] / ccan / tal / grab_file / grab_file.h
1 /* Licensed under LGPLv2+ - see LICENSE file for details */
2 #ifndef CCAN_TAL_GRAB_FILE_H
3 #define CCAN_TAL_GRAB_FILE_H
4 #include <stdio.h> // For size_t
5
6 /**
7  * grab_fd - read all of a file descriptor into memory
8  * @ctx: the context to tallocate from (often NULL)
9  * @fd: the file descriptor to read from
10  *
11  * This function reads from the given file descriptor until no more
12  * input is available.  The content is talloced off @ctx, and the
13  * tal_count() is the size in bytes plus one: for convenience, the
14  * byte after the end of the content will always be NUL.
15  *
16  * Example:
17  *      #include <ccan/tal/str/str.h>
18  *      #include <ccan/tal/tal.h>
19  *      ...
20  *      // Return all of standard input, as lines.
21  *      static char **read_stdin_as_lines(void)
22  *      {
23  *              char **lines, *all;
24  *
25  *              all = grab_fd(NULL, 0);
26  *              if (!all)
27  *                      return NULL;
28  *              lines = tal_strsplit(NULL, all, "\n", STR_EMPTY_OK);
29  *              tal_free(all);
30  *              return lines;
31  *      }
32  */
33 void *grab_fd(const void *ctx, int fd);
34
35 /**
36  * grab_file - read all of a file (or stdin) into memory
37  * @ctx: the context to tallocate from (often NULL)
38  * @filename: the file to read (NULL for stdin)
39  *
40  * This function reads from the given file until no more input is
41  * available.  The content is talloced off @ctx, and the tal_count()
42  * is the size in bytes plus one: for convenience, the byte after the
43  * end of the content will always be NUL.
44  *
45  * Example:
46  *      // Return all of a given file, as lines.
47  *      static char **read_file_as_lines(const char *filename)
48  *      {
49  *              char **lines, *all;
50  *
51  *              all = grab_file(NULL, filename);
52  *              if (!all)
53  *                      return NULL;
54  *              lines = tal_strsplit(NULL, all, "\n", STR_EMPTY_OK);
55  *              tal_free(all);
56  *              return lines;
57  *      }
58  */
59 void *grab_file(const void *ctx, const char *filename);
60 #endif /* CCAN_TAL_GRAB_FILE_H */