From f69ad4f8f38d87513c09a126ea3f9c5b0c20535f Mon Sep 17 00:00:00 2001 From: Anthony <62391911+ajwood1965@users.noreply.github.com> Date: Sat, 27 Apr 2024 21:00:42 -0500 Subject: [PATCH] add stdlib vsnprintf, fprintf, vfprintf --- include/stdio.h | 13 ++++++++- source/twr-crt/printf.c | 64 ++++++++++++++++++++++++++--------------- 2 files changed, 53 insertions(+), 24 deletions(-) diff --git a/include/stdio.h b/include/stdio.h index 8c25f54a..9001b2cd 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -6,17 +6,28 @@ #define __TINY_STDIO_H__ #include "stddef.h" +#include "assert.h" #ifdef __cplusplus extern "C" { #endif #define snprintf(x,y, ...) twr_snprintf(x,y, __VA_ARGS__) +static inline int vsnprintf(char *buffer, size_t bufsz, const char *format, va_list vlist) {return twr_vsnprintf(buffer, (twr_size_t)bufsz, format, vlist); } #define printf(...) twr_printf(__VA_ARGS__) +#define fprintf(x, ...) twr_fprintf(x, __VA_ARGS__) +#define vfprintf( x, y, z ) twr_vfprintf( x, y, z ) + + //void twr_vprintf(twr_cbprintf_callback out, void* cbdata, const char *format, va_list* args); +static inline int remove( const char* pathname ) {assert(0);return -1;} // not implemented; here to get libc++ to compile + +// EOF not implemented; here to get libc++ to compile +#define EOF (-1) + #ifdef __cplusplus } #endif -#endif \ No newline at end of file +#endif diff --git a/source/twr-crt/printf.c b/source/twr-crt/printf.c index 15d744f1..27f6ce35 100644 --- a/source/twr-crt/printf.c +++ b/source/twr-crt/printf.c @@ -78,7 +78,7 @@ void static do_width(const char* in, char* assembly, int size_assembly, bool pad twr_strcat_s(assembly, size_assembly, in); } -void twr_vprintf(twr_cbprintf_callback out, void* cbdata, const char *format, va_list* args) { +void twr_vprintf(twr_cbprintf_callback out, void* cbdata, const char *format, va_list vlist) { struct pformat pf; while (*format) { @@ -91,7 +91,7 @@ void twr_vprintf(twr_cbprintf_callback out, void* cbdata, const char *format, va char buffer[20]; char assembly[20]; int assemoff; - int val=va_arg(*args, int); + int val=va_arg(vlist, int); twr_itoa_s(val, buffer, sizeof(buffer), 10); if (val>=0 && pf.flag_space) { @@ -111,7 +111,7 @@ void twr_vprintf(twr_cbprintf_callback out, void* cbdata, const char *format, va { char buffer[16]; char assembly[16]; - twr_itoa_s(va_arg(*args, int), buffer, sizeof(buffer), 16); + twr_itoa_s(va_arg(vlist, int), buffer, sizeof(buffer), 16); do_width(buffer, assembly, sizeof(assembly), pf.flag_zero, pf.width); outstr(out, cbdata, assembly, sizeof(assembly)); @@ -123,7 +123,7 @@ void twr_vprintf(twr_cbprintf_callback out, void* cbdata, const char *format, va char buffer[30]; char assembly[30]; int assemoff; - double val=va_arg(*args, double); + double val=va_arg(vlist, double); twr_wasm_tofixed(buffer, sizeof(buffer), val, pf.precision); if (val>=0 && pf.flag_space) { @@ -144,7 +144,7 @@ void twr_vprintf(twr_cbprintf_callback out, void* cbdata, const char *format, va char buffer[30]; char assembly[30]; int assemoff; - double val=va_arg(*args, double); + double val=va_arg(vlist, double); twr_wasm_toexponential(buffer, sizeof(buffer), val, pf.precision); if (val>=0 && pf.flag_space) { @@ -164,7 +164,7 @@ void twr_vprintf(twr_cbprintf_callback out, void* cbdata, const char *format, va { char buffer[30]; char assembly[30]; - double val=va_arg(*args, double); + double val=va_arg(vlist, double); int assemoff; twr_dtoa(buffer, sizeof(buffer), val, pf.precision); if (val>=0 && pf.flag_space) { @@ -180,12 +180,12 @@ void twr_vprintf(twr_cbprintf_callback out, void* cbdata, const char *format, va break; case 's': - outstr(out, cbdata, va_arg(*args, char *), 100000); // arbitrary max of 100K string length + outstr(out, cbdata, va_arg(vlist, char *), 100000); // arbitrary max of 100K string length break; case 'c': { - const int c=va_arg(*args, int); + const int c=va_arg(vlist, int); out(cbdata, c); } break; @@ -203,9 +203,9 @@ void twr_vprintf(twr_cbprintf_callback out, void* cbdata, const char *format, va } struct snprintf_callback_data { - int size; - char* buffer; - int pos; + char *const buffer; + const twr_size_t size; + twr_size_t pos; }; static void snprintf_callback(void* datain, char ch) { @@ -215,29 +215,33 @@ static void snprintf_callback(void* datain, char ch) { } } -int twr_snprintf(char* buffer, int size, const char* format, ...) { - va_list args; - va_start(args, format); +int twr_snprintf(char *buffer, twr_size_t bufsz, const char *format, ... ) { + va_list vlist; + va_start(vlist, format); - struct snprintf_callback_data data; - data.buffer=buffer; - data.size=size; - data.pos=0; + const int rv=twr_vsnprintf(buffer, bufsz, format, vlist); - twr_vprintf(snprintf_callback, &data, format, &args); + va_end(vlist); - if (data.pos