diff options
author | Damjan Marion <damarion@cisco.com> | 2023-08-06 20:39:38 +0200 |
---|---|---|
committer | Florin Coras <florin.coras@gmail.com> | 2023-08-07 17:33:09 +0000 |
commit | 8d0c0c68215a2972053ba7fd8fa0f82aa14f8624 (patch) | |
tree | 035f14012dfd939890cf360898be6d4e71021504 /src/vppinfra/unformat.c | |
parent | 696db20e332fcbecaaef9c5505cc2e132bfaa9e2 (diff) |
vppinfra: add unformat_init_path
More conveninet way to unformat file by providing filesystem path.
Takes format string for easier constuction of path...
Type: improvement
Change-Id: I433204fa20dc98e2b11c53914883d047a7fc62c6
Signed-off-by: Damjan Marion <damarion@cisco.com>
Diffstat (limited to 'src/vppinfra/unformat.c')
-rw-r--r-- | src/vppinfra/unformat.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/vppinfra/unformat.c b/src/vppinfra/unformat.c index a07f4e06df5..cea5b677dec 100644 --- a/src/vppinfra/unformat.c +++ b/src/vppinfra/unformat.c @@ -36,6 +36,7 @@ */ #include <vppinfra/format.h> +#include <fcntl.h> /* Call user's function to fill input buffer. */ __clib_export uword @@ -1045,6 +1046,13 @@ clib_file_fill_buffer (unformat_input_t * input) return input->index; } +static void +unformat_close_fd (unformat_input_t *input) +{ + int fd = pointer_to_uword (input->fill_buffer_arg); + close (fd); +} + __clib_export void unformat_init_clib_file (unformat_input_t * input, int file_descriptor) { @@ -1052,6 +1060,31 @@ unformat_init_clib_file (unformat_input_t * input, int file_descriptor) uword_to_pointer (file_descriptor, void *)); } +__clib_export uword +unformat_init_file (unformat_input_t *input, char *fmt, ...) +{ + va_list va; + u8 *path; + int fd; + + va_start (va, fmt); + path = va_format (0, fmt, &va); + va_end (va); + vec_add1 (path, 0); + + fd = open ((char *) path, 0); + vec_free (path); + + if (fd >= 0) + { + unformat_init (input, clib_file_fill_buffer, + uword_to_pointer (fd, void *)); + input->free = unformat_close_fd; + return 1; + } + return 0; +} + /* Take input from Unix environment variable. */ uword unformat_init_unix_env (unformat_input_t * input, char *var) |