Add printf to IStream.

This commit is contained in:
H. Utku Maden 2024-03-24 12:37:33 +03:00
parent 66eb6d8f68
commit 1ae2735c62
3 changed files with 57 additions and 12 deletions

@ -4,6 +4,8 @@
#include "mx/base.h"
#include "mx/trait.h"
#include <stdarg.h>
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,

40
src/stream.c Normal file

@ -0,0 +1,40 @@
#include "mx/io/stream.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#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);
}

@ -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);