From a10c4785586b5dba51cf24dfdbb75c840ce2e31f Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Wed, 16 Nov 2022 13:34:32 +0000 Subject: [PATCH] memory-stream: Factor out memory-stream related code to a new file Signed-off-by: Joshua Ashton --- include/memory-stream.h | 24 ++++++++++++++++++++++++ info.c | 34 +--------------------------------- memory-stream.c | 30 ++++++++++++++++++++++++++++++ meson.build | 1 + 4 files changed, 56 insertions(+), 33 deletions(-) create mode 100644 include/memory-stream.h create mode 100644 memory-stream.c diff --git a/include/memory-stream.h b/include/memory-stream.h new file mode 100644 index 0000000..d14e48f --- /dev/null +++ b/include/memory-stream.h @@ -0,0 +1,24 @@ +#ifndef MEMORY_STREAM_H +#define MEMORY_STREAM_H + +/** + * Utility functions for memory streams. + */ + +#include +#include +#include + +struct memory_stream { + FILE *fp; + char *str; + size_t str_len; +}; + +bool +memory_stream_open(struct memory_stream *m); + +char * +memory_stream_close(struct memory_stream *m); + +#endif diff --git a/info.c b/info.c index f89f413..3d58ab8 100644 --- a/info.c +++ b/info.c @@ -7,6 +7,7 @@ #include "edid.h" #include "info.h" #include "log.h" +#include "memory-stream.h" /* Generated file pnp-id-table.c: */ const char * @@ -76,39 +77,6 @@ di_info_get_failure_msg(const struct di_info *info) return info->failure_msg; } -struct memory_stream { - FILE *fp; - char *str; - size_t str_len; -}; - -static bool -memory_stream_open(struct memory_stream *m) -{ - *m = (struct memory_stream){ 0 }; - m->fp = open_memstream(&m->str, &m->str_len); - - return m->fp != NULL; -} - -static char * -memory_stream_close(struct memory_stream *m) -{ - char *str; - int ret; - - ret = fclose(m->fp); - str = m->str; - *m = (struct memory_stream){ 0 }; - - if (ret != 0) { - free(str); - str = NULL; - } - - return str; -} - static void encode_ascii_byte(FILE *out, char ch) { diff --git a/memory-stream.c b/memory-stream.c new file mode 100644 index 0000000..571fc69 --- /dev/null +++ b/memory-stream.c @@ -0,0 +1,30 @@ +#include + +#include "memory-stream.h" + +bool +memory_stream_open(struct memory_stream *m) +{ + *m = (struct memory_stream){ 0 }; + m->fp = open_memstream(&m->str, &m->str_len); + + return m->fp != NULL; +} + +char * +memory_stream_close(struct memory_stream *m) +{ + char *str; + int ret; + + ret = fclose(m->fp); + str = m->str; + *m = (struct memory_stream){ 0 }; + + if (ret != 0) { + free(str); + str = NULL; + } + + return str; +} diff --git a/meson.build b/meson.build index a44bd26..e9d7e57 100644 --- a/meson.build +++ b/meson.build @@ -57,6 +57,7 @@ di_lib = library( 'gtf.c', 'info.c', 'log.c', + 'memory-stream.c', pnp_id_table, ], include_directories: include_directories('include'),