diff --git a/ChangeLog b/ChangeLog index 662af8c7..9305dd34 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,9 @@ 2022-06-25 Alex Kotov + * configure.ac: Removed package "io" * include/kernaux/generic/file.h: Added + * include/kernaux/io.h: Removed + * include/kernaux/printf.h: Use new file API 2022-06-24 Alex Kotov diff --git a/Makefile.am b/Makefile.am index aa388ef1..5f42b2be 100644 --- a/Makefile.am +++ b/Makefile.am @@ -80,9 +80,6 @@ endif if WITH_FREE_LIST libkernaux_la_SOURCES += src/free_list.c endif -if WITH_IO -libkernaux_la_SOURCES += src/io.c -endif if WITH_MBR libkernaux_la_SOURCES += src/mbr.c endif diff --git a/README.md b/README.md index cf06b9a8..e2e7124d 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,6 @@ zero). Work-in-progress APIs can change at any time. * [Example: Assert](/examples/assert.c) * [Example: Panic](/examples/panic.c) * Stack trace *(planned)* - * [Input/output](/include/kernaux/io.h) (*work in progress*) * Architecture-specific code (*work in progress*) * [Declarations](/include/kernaux/arch/) * [Functions](/include/kernaux/asm/) @@ -76,7 +75,7 @@ zero). Work-in-progress APIs can change at any time. * Usual functions * [itoa/ftoa replacement](/include/kernaux/ntoa.h) (*non-breaking since* **0.4.0**) * [Example](/examples/ntoa.c) - * [printf replacement](/include/kernaux/printf.h.in) (*non-breaking since* **0.4.0**) + * [printf replacement](/include/kernaux/printf.h.in) (*non-breaking since* **?.?.?**) * Code from [https://github.com/mpaland/printf](https://github.com/mpaland/printf). Thank you! * [Example: fprintf](/examples/fprintf.c) * [Example: vfprintf](/examples/fprintf_va.c) @@ -166,7 +165,6 @@ explicitly included, use `--without-all`. * `--with[out]-asm` - kernel assembler helpers * `--with[out]-cmdline` - command line parser * `--with[out]-free-list` - free list memory allocator -* `--with[out]-io` - input/output * `--with[out]-memmap` - memory map * `--with[out]-ntoa` - itoa/ftoa * `--with[out]-printf` - printf diff --git a/configure.ac b/configure.ac index 65f97a85..a0191d86 100644 --- a/configure.ac +++ b/configure.ac @@ -65,7 +65,6 @@ AC_ARG_WITH( [asm], AS_HELP_STRING([--without-asm], [without AC_ARG_WITH( [cmdline], AS_HELP_STRING([--without-cmdline], [without command line parser])) AC_ARG_WITH( [elf], AS_HELP_STRING([--without-elf], [without ELF utils])) AC_ARG_WITH( [free-list], AS_HELP_STRING([--without-free-list], [without free list memory allocator])) -AC_ARG_WITH( [io], AS_HELP_STRING([--without-io], [without input/output])) AC_ARG_WITH( [mbr], AS_HELP_STRING([--without-mbr], [without Master Boot Record])) AC_ARG_WITH( [memmap], AS_HELP_STRING([--without-memmap], [without memory map])) AC_ARG_WITH( [multiboot2], AS_HELP_STRING([--without-multiboot2], [without Multiboot 2 information parser])) @@ -109,7 +108,6 @@ if test -z "$with_asm"; then with_asm=no; fi if test -z "$with_cmdline"; then with_cmdline=no; fi if test -z "$with_elf"; then with_elf=no; fi if test -z "$with_free_list"; then with_free_list=no; fi -if test -z "$with_io"; then with_io=no; fi if test -z "$with_mbr"; then with_mbr=no; fi if test -z "$with_memmap"; then with_memmap=no; fi if test -z "$with_multiboot2"; then with_multiboot2=no; fi @@ -149,7 +147,6 @@ AS_IF([test "$with_asm" = no ], [with_asm=no], [with_asm= AS_IF([test "$with_cmdline" = no ], [with_cmdline=no], [with_cmdline=yes]) AS_IF([test "$with_elf" = no ], [with_elf=no], [with_elf=yes]) AS_IF([test "$with_free_list" = no ], [with_free_list=no], [with_free_list=yes]) -AS_IF([test "$with_io" = no ], [with_io=no], [with_io=yes]) AS_IF([test "$with_mbr" = no ], [with_mbr=no], [with_mbr=yes]) AS_IF([test "$with_memmap" = no ], [with_memmap=no], [with_memmap=yes]) AS_IF([test "$with_multiboot2" = no ], [with_multiboot2=no], [with_multiboot2=yes]) @@ -208,7 +205,6 @@ AM_CONDITIONAL([WITH_ASM], [test "$with_asm" = yes]) AM_CONDITIONAL([WITH_CMDLINE], [test "$with_cmdline" = yes]) AM_CONDITIONAL([WITH_ELF], [test "$with_elf" = yes]) AM_CONDITIONAL([WITH_FREE_LIST], [test "$with_free_list" = yes]) -AM_CONDITIONAL([WITH_IO], [test "$with_io" = yes]) AM_CONDITIONAL([WITH_MBR], [test "$with_mbr" = yes]) AM_CONDITIONAL([WITH_MEMMAP], [test "$with_memmap" = yes]) AM_CONDITIONAL([WITH_MULTIBOOT2], [test "$with_multiboot2" = yes]) @@ -252,7 +248,6 @@ AS_IF([test "$with_asm" = yes], [AC_DEFINE([WITH_ASM], [1] AS_IF([test "$with_cmdline" = yes], [AC_DEFINE([WITH_CMDLINE], [1], [with command line parser])]) AS_IF([test "$with_elf" = yes], [AC_DEFINE([WITH_ELF], [1], [with ELF utils])]) AS_IF([test "$with_free_list" = yes], [AC_DEFINE([WITH_FREE_LIST], [1], [with free list memory allocator])]) -AS_IF([test "$with_io" = yes], [AC_DEFINE([WITH_IO], [1], [with input/output])]) AS_IF([test "$with_mbr" = yes], [AC_DEFINE([WITH_MBR], [1], [with Master Boot Record])]) AS_IF([test "$with_memmap" = yes], [AC_DEFINE([WITH_MEMMAP], [1], [with memory map])]) AS_IF([test "$with_multiboot2" = yes], [AC_DEFINE([WITH_MULTIBOOT2], [1], [with Multiboot 2 information parser])]) @@ -282,7 +277,6 @@ AS_IF([test "$with_arch_x86_64" = no], [AC_SUBST([comment_line_arch_x86_64], [ AS_IF([test "$with_cmdline" = no], [AC_SUBST([comment_line_cmdline], [//])]) AS_IF([test "$with_elf" = no], [AC_SUBST([comment_line_elf], [//])]) AS_IF([test "$with_free_list" = no], [AC_SUBST([comment_line_free_list], [//])]) -AS_IF([test "$with_io" = no], [AC_SUBST([comment_line_io], [//])]) AS_IF([test "$with_mbr" = no], [AC_SUBST([comment_line_mbr], [//])]) AS_IF([test "$with_memmap" = no], [AC_SUBST([comment_line_memmap], [//])]) AS_IF([test "$with_multiboot2" = no], [AC_SUBST([comment_line_multiboot2], [//])]) diff --git a/examples/Makefile.am b/examples/Makefile.am index bd33af09..3e699b38 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -26,24 +26,20 @@ endif ########### if WITH_PRINTF -if WITH_IO TESTS += fprintf fprintf_LDADD = $(top_builddir)/libkernaux.la fprintf_SOURCES = main.c fprintf.c endif -endif ############## # fprintf_va # ############## if WITH_PRINTF -if WITH_IO TESTS += fprintf_va fprintf_va_LDADD = $(top_builddir)/libkernaux.la fprintf_va_SOURCES = main.c fprintf_va.c endif -endif ################ # generic_file # diff --git a/examples/fprintf.c b/examples/fprintf.c index 58b59e5d..53e7ebcf 100644 --- a/examples/fprintf.c +++ b/examples/fprintf.c @@ -1,4 +1,6 @@ -#include +#define KERNAUX_ACCESS_PROTECTED + +#include #include #include @@ -8,24 +10,26 @@ #define BUFFER_SIZE 1024 -static const char *const data = "foobar"; - static char buffer[BUFFER_SIZE]; static size_t buffer_index = 0; -static void my_putchar(const char chr, void *arg) +static int my_putc(__attribute__((unused)) void *const file, unsigned char c) { - assert(arg == data); if (buffer_index >= BUFFER_SIZE) abort(); - buffer[buffer_index++] = chr; + buffer[buffer_index++] = c; + return 1; } +static const struct KernAux_File file = { + .putc = my_putc, + .puts = NULL, + .write = NULL, +}; + void example_main() { - struct KernAux_OldFile file = KernAux_OldFile_create(my_putchar); const int result = kernaux_fprintf( &file, - (char*)data, "Hello, %s! Session ID: %u.", "Alex", 123 diff --git a/examples/fprintf_va.c b/examples/fprintf_va.c index 0465b2bb..029a5c5c 100644 --- a/examples/fprintf_va.c +++ b/examples/fprintf_va.c @@ -1,4 +1,6 @@ -#include +#define KERNAUX_ACCESS_PROTECTED + +#include #include #include @@ -8,24 +10,27 @@ #define BUFFER_SIZE 1024 -static const char *const data = "foobar"; - static char buffer[BUFFER_SIZE]; static size_t buffer_index = 0; -static void my_putchar(const char chr, void *arg) +static int my_putc(__attribute__((unused)) void *const file, unsigned char c) { - assert(arg == data); if (buffer_index >= BUFFER_SIZE) abort(); - buffer[buffer_index++] = chr; + buffer[buffer_index++] = c; + return 1; } +static const struct KernAux_File file = { + .putc = my_putc, + .puts = NULL, + .write = NULL, +}; + static int my_printf(const char *const format, ...) { va_list va; va_start(va, format); - struct KernAux_OldFile file = KernAux_OldFile_create(my_putchar); - const int result = kernaux_vfprintf(&file, (char*)data, format, va); + const int result = kernaux_vfprintf(&file, format, va); va_end(va); return result; } diff --git a/include/Makefile.am b/include/Makefile.am index 0016e18c..a264a460 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -56,9 +56,6 @@ endif if WITH_FREE_LIST nobase_include_HEADERS += kernaux/free_list.h endif -if WITH_IO -nobase_include_HEADERS += kernaux/io.h -endif if WITH_MBR nobase_include_HEADERS += kernaux/mbr.h endif diff --git a/include/kernaux.h.in b/include/kernaux.h.in index f99ee296..3ce50c20 100644 --- a/include/kernaux.h.in +++ b/include/kernaux.h.in @@ -18,7 +18,6 @@ @comment_line_cmdline@#include @comment_line_elf@#include @comment_line_free_list@#include -@comment_line_io@#include @comment_line_mbr@#include @comment_line_memmap@#include @comment_line_multiboot2@#include diff --git a/include/kernaux/generic/file.h b/include/kernaux/generic/file.h index a44491d2..890d9f48 100644 --- a/include/kernaux/generic/file.h +++ b/include/kernaux/generic/file.h @@ -15,7 +15,7 @@ typedef int (*KernAux_File_Putc) (void *file, unsigned char c); typedef int (*KernAux_File_Puts) (void *file, const char *s); typedef int (*KernAux_File_Write)(void *file, const void *buffer, size_t count); -typedef struct KernAux_File { +typedef const struct KernAux_File { KernAux_File_Putc KERNAUX_PROTECTED_FIELD(putc); KernAux_File_Puts KERNAUX_PROTECTED_FIELD(puts); KernAux_File_Write KERNAUX_PROTECTED_FIELD(write); diff --git a/include/kernaux/generic/malloc.h b/include/kernaux/generic/malloc.h index 5a3bc741..ff1092f9 100644 --- a/include/kernaux/generic/malloc.h +++ b/include/kernaux/generic/malloc.h @@ -14,7 +14,7 @@ typedef void (*KernAux_Malloc_Free) (void *malloc, void *ptr); typedef void *(*KernAux_Malloc_Malloc) (void *malloc, size_t size); typedef void *(*KernAux_Malloc_Realloc)(void *malloc, void *ptr, size_t size); -typedef struct KernAux_Malloc { +typedef const struct KernAux_Malloc { KernAux_Malloc_Calloc KERNAUX_PROTECTED_FIELD(calloc); KernAux_Malloc_Free KERNAUX_PROTECTED_FIELD(free); KernAux_Malloc_Malloc KERNAUX_PROTECTED_FIELD(malloc); diff --git a/include/kernaux/generic/mutex.h b/include/kernaux/generic/mutex.h index bcd9786a..ad071e95 100644 --- a/include/kernaux/generic/mutex.h +++ b/include/kernaux/generic/mutex.h @@ -10,7 +10,7 @@ extern "C" { typedef void (*KernAux_Mutex_Lock )(void *mutex); typedef void (*KernAux_Mutex_Unlock)(void *mutex); -typedef struct KernAux_Mutex { +typedef const struct KernAux_Mutex { KernAux_Mutex_Lock KERNAUX_PROTECTED_FIELD(lock); KernAux_Mutex_Unlock KERNAUX_PROTECTED_FIELD(unlock); } *KernAux_Mutex; diff --git a/include/kernaux/io.h b/include/kernaux/io.h deleted file mode 100644 index b26e1c76..00000000 --- a/include/kernaux/io.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef KERNAUX_INCLUDED_IO -#define KERNAUX_INCLUDED_IO - -#ifdef __cplusplus -extern "C" { -#endif - -typedef void (*KernAux_OldFile_Out)(char c, void *arg); - -typedef struct KernAux_OldFile { - KernAux_OldFile_Out out; -} *KernAux_OldFile; - -struct KernAux_OldFile KernAux_OldFile_create(KernAux_OldFile_Out out); -void KernAux_OldFile_init(KernAux_OldFile file, KernAux_OldFile_Out out); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/include/kernaux/printf.h.in b/include/kernaux/printf.h.in index 394a860f..f89978fe 100644 --- a/include/kernaux/printf.h.in +++ b/include/kernaux/printf.h.in @@ -5,21 +5,20 @@ extern "C" { #endif +#include + #include #include -@comment_line_io@#include - /** * Tiny [v]fprintf implementation * \param file An output file - * \param arg An argument pointer for user data passed to output function * \param format A string that specifies the format of the output * \param va A value identifying a variable arguments list * \return The number of characters that are sent to the output function, not counting the terminating null character */ -@comment_line_io@int kernaux_fprintf(KernAux_OldFile file, void* arg, const char* format, ...); -@comment_line_io@int kernaux_vfprintf(KernAux_OldFile file, void* arg, const char* format, va_list va); +int kernaux_fprintf(KernAux_File file, const char* format, ...); +int kernaux_vfprintf(KernAux_File file, const char* format, va_list va); /** * Tiny [v]snprintf implementation diff --git a/include/kernaux/version.h.in b/include/kernaux/version.h.in index 0534d303..e41aee91 100644 --- a/include/kernaux/version.h.in +++ b/include/kernaux/version.h.in @@ -3,7 +3,6 @@ @comment_line_cmdline@#define KERNAUX_VERSION_WITH_CMDLINE @comment_line_elf@#define KERNAUX_VERSION_WITH_ELF -@comment_line_io@#define KERNAUX_VERSION_WITH_IO @comment_line_mbr@#define KERNAUX_VERSION_WITH_MBR @comment_line_multiboot2@#define KERNAUX_VERSION_WITH_MULTIBOOT2 @comment_line_ntoa@#define KERNAUX_VERSION_WITH_NTOA diff --git a/src/drivers/console.c b/src/drivers/console.c index 61144059..9d91fdae 100644 --- a/src/drivers/console.c +++ b/src/drivers/console.c @@ -4,6 +4,7 @@ #include #include +#include #ifdef ASM_I386 #include @@ -12,22 +13,20 @@ #include #endif -#ifdef WITH_IO -#include -#endif #ifdef WITH_PRINTF #include #endif #include -#if defined(WITH_IO) && defined(WITH_PRINTF) -static void kernaux_drivers_console_printf_putc( - const char c, - void *const arg __attribute__((unused)) -) { - kernaux_drivers_console_putc(c); -} +#ifdef WITH_PRINTF +static int file_putc(void *file, unsigned char c); + +static const struct KernAux_File file = { + .putc = file_putc, + .puts = NULL, + .write = NULL, +}; #endif void kernaux_drivers_console_putc(const char c __attribute__((unused))) @@ -49,16 +48,14 @@ void kernaux_drivers_console_print(const char *const s) } } -#if defined(WITH_IO) && defined(WITH_PRINTF) +#ifdef WITH_PRINTF void kernaux_drivers_console_printf(const char *format, ...) { KERNAUX_ASSERT(format); va_list va; va_start(va, format); - struct KernAux_OldFile file = - KernAux_OldFile_create(kernaux_drivers_console_printf_putc); - kernaux_vfprintf(&file, NULL, format, va); + kernaux_vfprintf(&file, format, va); va_end(va); } #endif @@ -79,3 +76,11 @@ void kernaux_drivers_console_write(const char *const data, const size_t size) kernaux_drivers_console_putc(data[i]); } } + +#ifdef WITH_PRINTF +int file_putc(__attribute__((unused)) void *const file, const unsigned char c) +{ + kernaux_drivers_console_putc(c); + return 1; +} +#endif diff --git a/src/generic/file.c b/src/generic/file.c index b09a017f..87865820 100644 --- a/src/generic/file.c +++ b/src/generic/file.c @@ -12,7 +12,7 @@ int KernAux_File_putc(const KernAux_File file, const int c) KERNAUX_ASSERT(file); KERNAUX_ASSERT(file->putc); - return file->putc(file, c); + return file->putc((void*)file, c); } int KernAux_File_puts(const KernAux_File file, const char *const s) @@ -23,7 +23,7 @@ int KernAux_File_puts(const KernAux_File file, const char *const s) if (!s) return 0; // Inherited implementation - if (file->puts) return file->puts(file, s); + if (file->puts) return file->puts((void*)file, s); // Default implementation size_t ccount = 0; @@ -45,7 +45,7 @@ int KernAux_File_write( if (count == 0 || !buffer) return 0; // Inherited implementation - if (file->write) return file->write(file, buffer, count); + if (file->write) return file->write((void*)file, buffer, count); // Default implementation size_t ccount = 0; diff --git a/src/generic/malloc.c b/src/generic/malloc.c index 8b777a98..809e489a 100644 --- a/src/generic/malloc.c +++ b/src/generic/malloc.c @@ -12,7 +12,7 @@ void *KernAux_Malloc_calloc(KernAux_Malloc malloc, size_t nmemb, size_t size) KERNAUX_ASSERT(malloc); KERNAUX_ASSERT(malloc->calloc); - return malloc->calloc(malloc, nmemb, size); + return malloc->calloc((void*)malloc, nmemb, size); } void KernAux_Malloc_free(KernAux_Malloc malloc, void *ptr) @@ -20,7 +20,7 @@ void KernAux_Malloc_free(KernAux_Malloc malloc, void *ptr) KERNAUX_ASSERT(malloc); KERNAUX_ASSERT(malloc->free); - malloc->free(malloc, ptr); + malloc->free((void*)malloc, ptr); } void *KernAux_Malloc_malloc(KernAux_Malloc malloc, size_t size) @@ -28,7 +28,7 @@ void *KernAux_Malloc_malloc(KernAux_Malloc malloc, size_t size) KERNAUX_ASSERT(malloc); KERNAUX_ASSERT(malloc->malloc); - return malloc->malloc(malloc, size); + return malloc->malloc((void*)malloc, size); } void *KernAux_Malloc_realloc(KernAux_Malloc malloc, void *ptr, size_t size) @@ -36,5 +36,5 @@ void *KernAux_Malloc_realloc(KernAux_Malloc malloc, void *ptr, size_t size) KERNAUX_ASSERT(malloc); KERNAUX_ASSERT(malloc->realloc); - return malloc->realloc(malloc, ptr, size); + return malloc->realloc((void*)malloc, ptr, size); } diff --git a/src/generic/mutex.c b/src/generic/mutex.c index 16f4e9a2..9b5eed6e 100644 --- a/src/generic/mutex.c +++ b/src/generic/mutex.c @@ -10,7 +10,7 @@ void KernAux_Mutex_lock(const KernAux_Mutex mutex) KERNAUX_ASSERT(mutex); KERNAUX_ASSERT(mutex->lock); - mutex->lock(mutex); + mutex->lock((void*)mutex); } void KernAux_Mutex_unlock(const KernAux_Mutex mutex) @@ -18,5 +18,5 @@ void KernAux_Mutex_unlock(const KernAux_Mutex mutex) KERNAUX_ASSERT(mutex); KERNAUX_ASSERT(mutex->unlock); - mutex->unlock(mutex); + mutex->unlock((void*)mutex); } diff --git a/src/io.c b/src/io.c deleted file mode 100644 index 0e36cc88..00000000 --- a/src/io.c +++ /dev/null @@ -1,23 +0,0 @@ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include -#include - -#include - -struct KernAux_OldFile KernAux_OldFile_create(const KernAux_OldFile_Out out) -{ - struct KernAux_OldFile file; - KernAux_OldFile_init(&file, out); - return file; -} - -void KernAux_OldFile_init(const KernAux_OldFile file, const KernAux_OldFile_Out out) -{ - KERNAUX_ASSERT(file); - KERNAUX_ASSERT(out); - - file->out = out; -} diff --git a/src/printf.c b/src/printf.c index 75e376f3..cc822a35 100644 --- a/src/printf.c +++ b/src/printf.c @@ -14,6 +14,7 @@ #endif #include +#include #include #include @@ -52,6 +53,8 @@ typedef struct { static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const char* format, va_list va); +static void file_putc(char c, void *arg); + static inline void _out_buffer(char character, void* buffer, size_t idx, size_t maxlen); static inline void _out_null(char character, void* buffer, size_t idx, size_t maxlen); static inline void _out_fct(char character, void* buffer, size_t idx, size_t maxlen); @@ -69,32 +72,28 @@ static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d * Implementations: main API * *****************************/ -#ifdef WITH_IO - -int kernaux_fprintf(const KernAux_OldFile file, void* arg, const char* format, ...) +int kernaux_fprintf(const KernAux_File file, const char* format, ...) { KERNAUX_ASSERT(file); KERNAUX_ASSERT(format); va_list va; va_start(va, format); - const out_fct_wrap_type out_fct_wrap = { file->out, arg }; + const out_fct_wrap_type out_fct_wrap = { file_putc, (void*)file }; const int ret = _vsnprintf(_out_fct, (char*)(uintptr_t)&out_fct_wrap, (size_t)-1, format, va); va_end(va); return ret; } -int kernaux_vfprintf(const KernAux_OldFile file, void* arg, const char* format, va_list va) +int kernaux_vfprintf(const KernAux_File file, const char* format, va_list va) { KERNAUX_ASSERT(file); KERNAUX_ASSERT(format); - const out_fct_wrap_type out_fct_wrap = { file->out, arg }; + const out_fct_wrap_type out_fct_wrap = { file_putc, (void*)file }; return _vsnprintf(_out_fct, (char*)(uintptr_t)&out_fct_wrap, (size_t)-1, format, va); } -#endif // WITH_IO - int kernaux_snprintf(char* buffer, size_t count, const char* format, ...) { KERNAUX_ASSERT(buffer); @@ -127,6 +126,16 @@ int kernaux_sprintf(char* buffer, const char* format, ...) return ret; } +/*************************** + * Implementation: file IO * + ***************************/ + +void file_putc(const char c, void *const arg) +{ + KernAux_File file = arg; + KernAux_File_putc(file, c); +} + /****************************************** * Implementation: main internal function * ******************************************/ diff --git a/tests/printf_gen.jinja b/tests/printf_gen.jinja index ff0f4112..22eb21f7 100644 --- a/tests/printf_gen.jinja +++ b/tests/printf_gen.jinja @@ -2,13 +2,13 @@ #include "config.h" #endif +#define KERNAUX_ACCESS_PROTECTED + +#include #include -#ifdef WITH_IO -#include -#endif - #include +#include #include #include #include @@ -18,39 +18,35 @@ static char buffer[BUFFER_SIZE]; static size_t buffer_index; -#ifdef WITH_IO - -static const char *const data = "foobar"; - -static void test_putchar(const char chr, void *const arg) +static int test_putc(__attribute__((unused)) void *const file, unsigned char c) { - assert(arg == data); - if (buffer_index >= BUFFER_SIZE) { printf("Buffer overflow!\n"); abort(); } - buffer[buffer_index++] = chr; + buffer[buffer_index++] = c; + return 1; } -#endif // WITH_IO +static const struct KernAux_File file = { + .putc = test_putc, + .puts = NULL, + .write = NULL, +}; static void test(const char *const expected, const char *const format, ...) { va_list va; int result; -#ifdef WITH_IO memset(buffer, '\0', sizeof(buffer)); buffer_index = 0; va_start(va, format); - struct KernAux_OldFile file = KernAux_OldFile_create(test_putchar); - result = kernaux_vfprintf(&file, (char*)data, format, va); + result = kernaux_vfprintf(&file, format, va); va_end(va); assert((size_t)result == strlen(expected)); assert(strcmp(expected, buffer) == 0); -#endif memset(buffer, '\0', sizeof(buffer)); buffer_index = 0; @@ -63,13 +59,10 @@ static void test(const char *const expected, const char *const format, ...) void test_main() { -#ifdef WITH_IO memset(buffer, '\0', sizeof(buffer)); buffer_index = 0; - struct KernAux_OldFile file = KernAux_OldFile_create(test_putchar); - kernaux_fprintf(&file, (char*)data, "Hello, World!"); + kernaux_fprintf(&file, "Hello, World!"); assert(strcmp("Hello, World!", buffer) == 0); -#endif {% for case in cases %} {% if case.float %}