std/stdio: support asprintf() and vasprintf()

This commit is contained in:
Lephe 2021-02-15 09:48:10 +01:00
parent 3885f10ee1
commit 553982a445
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
3 changed files with 44 additions and 1 deletions

View File

@ -45,6 +45,7 @@ set(SOURCES_COMMON
src/rtc/rtc.c
src/rtc/rtc_ticks.c
src/spu/spu.c
src/std/aprint.c
src/std/memcmp.s
src/std/memcpy.s
src/std/memmove.s

View File

@ -17,7 +17,7 @@
* Parameter length (hh, h, l, ll, z)
* Limiting the size of the output and still returning the whole length
* If kprint_enable_fp() from <gint/kprint.h> is called: floating-point
formats (%e, %E, %f, %F, %g, %G) (disabled by default for space)
formats (%e, %E, %f, %F, %g, %G) (disabled by default to save space)
They do not support:
* Hexadecimal floating-point (%a, %A)
@ -41,5 +41,9 @@ int vsprintf(char *str, char const *format, va_list args);
int snprintf(char *str, size_t n, char const *format, ...);
/* Print to string with limited size from va_list */
int vsnprintf(char *str, size_t n, char const *format, va_list args);
/* Print to auto-allocated string */
int asprintf(char **strp, char const *format, ...);
/* Print to auto-allocated string from va_list */
int vasprintf(char **strp, char const *format, va_list args);
#endif /* GINT_STD_STDIO */

38
src/std/aprint.c Normal file
View File

@ -0,0 +1,38 @@
//---
// gint:src:aprint - Allocating extensions to formatted printing
//---
#include <gint/std/stdio.h>
#include <gint/std/stdlib.h>
#include <gint/kprint.h>
#include <stdarg.h>
/* asprintf() */
GWEAK int asprintf(char **strp, char const *format, ...)
{
va_list args;
va_start(args, format);
int count = vasprintf(strp, format, args);
va_end(args);
return count;
}
/* vasprintf() */
GWEAK int vasprintf(char **strp, char const *format, va_list args1)
{
va_list args2;
va_copy(args2, args1);
int count = vsnprintf(NULL, 0, format, args1);
va_end(args1);
char *str = malloc(count + 1);
if(str) count = vsnprintf(str, count + 1, format, args2);
va_end(args2);
if(!str) return -1;
*strp = str;
return count;
}