From 1ae2735c628511490e7e8f4a5e54b3a8ec62f1fa Mon Sep 17 00:00:00 2001 From: "H. Utku Maden" Date: Sun, 24 Mar 2024 12:37:33 +0300 Subject: [PATCH] Add printf to IStream. --- include/mx/io/stream.h | 5 +++++ src/stream.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/stream.stdc.c | 24 ++++++++++++------------ 3 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 src/stream.c diff --git a/include/mx/io/stream.h b/include/mx/io/stream.h index fe03371..babf7c0 100644 --- a/include/mx/io/stream.h +++ b/include/mx/io/stream.h @@ -4,6 +4,8 @@ #include "mx/base.h" #include "mx/trait.h" +#include + typedef enum IStream_SeekOrigin { ISTREAM_SEEK_START, @@ -59,6 +61,9 @@ MX_INLINE void IStream_close(fatptr_t(IStream) str) fatptr_vcall(str, close); } +MX_API void IStream_printf(fatptr_t(IStream) str, const char *format, ...); +MX_API void IStream_vprintf(fatptr_t(IStream) str, const char *format, va_list va); + typedef enum mx_open_flags { MX_OPEN_READ = 1 << 0, diff --git a/src/stream.c b/src/stream.c new file mode 100644 index 0000000..51c9bda --- /dev/null +++ b/src/stream.c @@ -0,0 +1,40 @@ +#include "mx/io/stream.h" + +#include +#include +#include + +#include "mx/assert.h" + +MX_API void IStream_printf(fatptr_t(IStream) str, const char *format, ...) +{ + va_list va; + va_start(va, format); + IStream_vprintf(str, format, va); + va_end(va); +} + +MX_API void IStream_vprintf(fatptr_t(IStream) str, const char *format, va_list va) +{ + char buffer[260]; + va_list va2; + + va_copy(va2, va); + int size = vsnprintf(buffer, sizeof buffer, format, va2); + va_end(va2); + + if (size < sizeof(buffer)) + { + IStream_write(str, buffer, size); + return; + } + + char *buffer2 = malloc(size+1); + MX_ASSERT_OOM(buffer2); + + va_copy(va2, va); + vsnprintf(buffer2, size+1, format, va2); + IStream_write(str, buffer2, size); + free(buffer2); + va_end(va2); +} diff --git a/src/stream.stdc.c b/src/stream.stdc.c index 644d632..2ed3554 100644 --- a/src/stream.stdc.c +++ b/src/stream.stdc.c @@ -19,24 +19,24 @@ static mx_len_t mx_file_IStream_write(mx_file_t *self, const char *buffer, mx_le static void mx_file_IStream_close(mx_file_t *self); const IObject fat_vtable(mx_file_t, IObject) = { - .get_size = mx_file_IObject_get_size, + .get_size = (void*)mx_file_IObject_get_size, .get_type = NULL, - .to_string = mx_file_IObject_to_string, - .destruct = mx_file_IObject_destruct + .to_string = (void*)mx_file_IObject_to_string, + .destruct = (void*)mx_file_IObject_destruct }; const IStream fat_vtable(mx_file_t, IStream) = { .Object = { - .get_size = mx_file_IObject_get_size, + .get_size = (void*)mx_file_IObject_get_size, .get_type = NULL, - .to_string = mx_file_IObject_to_string, - .destruct = mx_file_IObject_destruct + .to_string = (void*)mx_file_IObject_to_string, + .destruct = (void*)mx_file_IObject_destruct }, - .get_flags = mx_file_IStream_get_flags, - .read = mx_file_IStream_read, - .seek = mx_file_IStream_seek, - .write = mx_file_IStream_write, - .close = mx_file_IStream_close, + .get_flags = (void*)mx_file_IStream_get_flags, + .read = (void*)mx_file_IStream_read, + .seek = (void*)mx_file_IStream_seek, + .write = (void*)mx_file_IStream_write, + .close = (void*)mx_file_IStream_close, }; static size_t mx_file_IObject_get_size(mx_file_t *self) @@ -167,7 +167,7 @@ MX_API fatptr_t(IStream) mx_open(const char *file, mx_open_flags flags) mx_file_t *self = malloc(sizeof(mx_file_t)); if (!self) { - return fat_new(NULL, *NULL, IStream); + return fat_new(NULL, *(IStream*)NULL, IStream); } self->f = fopen(file, options);