2020-03-21 14:12:26 +01:00
# include "stdio.h"
# include "stdarg.h"
# include <Library/printf_lite.h>
extern " C " {
2020-04-04 15:50:13 +02:00
# include <Library / UefiLib.h>
# include <Library / PrintLib.h>
// UINTN
// EFIAPI
// AsciiSPrint (
// OUT CHAR8 *StartOfBuffer,
// IN UINTN BufferSize,
// IN CONST CHAR8 *FormatString,
// ...
// );
2020-03-21 14:12:26 +01:00
}
# include "../../cpp_foundation/XString.h"
2020-03-28 07:36:07 +01:00
2020-05-10 09:05:34 +02:00
static XString8 stdio_static_buf = XString8 ( ) . takeValueFrom ( " XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX " ) ; // prealloc stdio_static_buf. It has to be at least 2 chars because of 'while ( n > size - 2 )' in strguid and strerror
// = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX "_XS8 won't work because allocatedSize() will stay 0
static XStringW stdio_static_wbuf ;
2020-03-21 14:12:26 +01:00
2020-04-24 08:36:29 +02:00
int vprintf ( const char * format , VA_LIST va )
2020-03-21 14:12:26 +01:00
{
2020-03-28 07:36:07 +01:00
// AsciiPrint seems no to work with utf8 chars. We have to use Print instead
2020-03-31 18:10:30 +02:00
stdio_static_wbuf . vSWPrintf ( format , va ) ;
2020-08-16 15:21:12 +02:00
stdio_static_wbuf . replaceAll ( " \n " _XS8 , " \r \n " _XS8 ) ;
2020-03-28 07:36:07 +01:00
int ret = ( int ) Print ( L " %s " , stdio_static_wbuf . wc_str ( ) ) ;
2020-04-24 08:36:29 +02:00
return ret ;
}
int printf ( const char * format , . . . )
{
va_list va ;
va_start ( va , format ) ;
int ret = vprintf ( format , va ) ;
2020-03-21 14:12:26 +01:00
va_end ( va ) ;
return ret ;
}
2020-03-26 13:59:20 +01:00
const char * strerror ( EFI_STATUS Status )
2020-03-21 14:12:26 +01:00
{
2020-04-29 22:34:28 +02:00
size_t size = stdio_static_buf . allocatedSize ( ) ;
2020-03-21 14:12:26 +01:00
UINTN n = 0 ;
2020-05-10 08:49:46 +02:00
n = AsciiSPrint ( stdio_static_buf . dataSized ( size ) , size , " %r " , Status ) ;
2020-04-29 22:34:28 +02:00
while ( n > size - 2 )
{
size + = 10 ;
n = AsciiSPrint ( stdio_static_buf . dataSized ( size ) , size , " %r " , Status ) ;
}
2020-04-23 15:20:48 +02:00
return stdio_static_buf . s ( ) ;
2020-03-21 14:12:26 +01:00
}
2020-04-10 09:15:36 +02:00
//this function print guid in LittleEndian format while we need BigEndian as Apple do
2020-03-26 13:59:20 +01:00
const char * strguid ( EFI_GUID * guid )
2020-03-21 14:12:26 +01:00
{
2020-04-29 22:34:28 +02:00
size_t size = stdio_static_buf . allocatedSize ( ) ;
2020-03-21 14:12:26 +01:00
UINTN n = 0 ;
2020-04-29 22:34:28 +02:00
n = AsciiSPrint ( stdio_static_buf . dataSized ( size ) , size , " %g " , guid ) ;
while ( n > size - 2 )
{
size + = 10 ;
n = AsciiSPrint ( stdio_static_buf . dataSized ( size ) , size , " %g " , guid ) ;
}
2020-04-23 15:20:48 +02:00
return stdio_static_buf . s ( ) ;
2020-03-21 14:12:26 +01:00
}