This commit is contained in:
timerix 2022-10-24 18:06:51 +06:00
parent cb8f31d998
commit c70544ff97
4 changed files with 66 additions and 4 deletions

View File

@ -76,7 +76,8 @@ char* toString_float(float64 n, bool withPostfix, bool uppercase){
return cptr_copy("<float>"); return cptr_copy("<float>");
} }
char* toString_bin(char* bytes, uint32 size, bool withPrefix){ char* toString_bin(void* _bytes, uint32 size, bool withPrefix){
char* bytes=_bytes;
char* str=malloc(size*8 + (withPrefix?2:0) +1); char* str=malloc(size*8 + (withPrefix?2:0) +1);
uint32 cn=0; uint32 cn=0;
if(withPrefix){ if(withPrefix){
@ -104,7 +105,8 @@ char _4bitsHex(uint8 u, bool uppercase){
} }
} }
char* toString_hex(char* bytes, uint32 size, bool withPrefix, bool uppercase){ char* toString_hex(void* _bytes, uint32 size, bool withPrefix, bool uppercase){
char* bytes=_bytes;
char* str=malloc(size*2 + (withPrefix?2:0) + 1); char* str=malloc(size*2 + (withPrefix?2:0) + 1);
uint32 cn=0; uint32 cn=0;
if(withPrefix){ if(withPrefix){

View File

@ -1,5 +1,6 @@
#include "../String/StringBuilder.h" #include "../String/StringBuilder.h"
#include "kprint.h" #include "kprint.h"
#include "../base/type_system/base_toString.h"
ktId __typeFromFormat(kprint_format f){ ktId __typeFromFormat(kprint_format f){
ktId typeId=kprint_format_ktId(f); ktId typeId=kprint_format_ktId(f);
@ -166,3 +167,59 @@ void kprint_setColor(kprint_format f){
StringBuilder_append_char(strb, ' '); StringBuilder_append_char(strb, ' ');
StringBuilder_append_char(strb, ']'); StringBuilder_append_char(strb, ']');
} */ } */
void kprintf(char* format, ...){
va_list vl;
va_start(vl, format);
char c;
while((c=*format++)){
if(c=='%'){
c=*format++;
switch (c) {
case 'u':
toString_uint(va_arg(vl, uint64),0,0);
break;
case 'i':
toString_int(va_arg(vl, uint64));
break;
case 'f':
toString_float(va_arg(vl, float64),0,0);
break;
/* case 'l':
c=*format++;
switch (c) {
case 'u':
toString_uint(va_arg(vl, uint64),0,0);
break;
case 'i':
toString_int(va_arg(vl, uint64));
break;
case 'f':
toString_float(va_arg(vl, float64),0,0);
break;
default:
throw(ERR_FORMAT);
}
break; */
case 'p':
case 'x':
uint64 px=va_arg(vl, uint64);
toString_hex(&px,sizeof(px),1,0);
break;
default:
throw(ERR_FORMAT);
}
} else if(c=='\e'){
IFWIN(
({
}),
putc(c,stdout);
)
} else {
putc(c,stdout);
}
}
va_end(vl);
}

View File

@ -65,6 +65,9 @@ Maybe __kfprint(FILE* fd, uint8 n, kprint_format* formats, __kprint_value_union*
void __kprint(uint8 n, kprint_format* formats, __kprint_value_union* objects); void __kprint(uint8 n, kprint_format* formats, __kprint_value_union* objects);
#define kprint(ARGS...) __kprint(count_args(ARGS), __kprint_argsToArrs(count_args(ARGS),ARGS, __32zeroes)) #define kprint(ARGS...) __kprint(count_args(ARGS), __kprint_argsToArrs(count_args(ARGS),ARGS, __32zeroes))
// cross-platform printf analog
void kprintf(char* format, ...);
// can take (bgColor | fgColor) // can take (bgColor | fgColor)
void kprint_setColor(kprint_format f); void kprint_setColor(kprint_format f);

View File

@ -13,7 +13,7 @@
printf("\n"); printf("\n");
void test_kprint_colors(){ void test_kprint_colors(){
IFWIN( /* IFWIN(
({ ({
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
for(uint8 col=0; col<255; col++){ for(uint8 col=0; col<255; col++){
@ -26,7 +26,7 @@ void test_kprint_colors(){
printf("\e[%um%u ", col, col); printf("\e[%um%u ", col, col);
}) })
); );
printf("\n"); printf("\n"); */
testColor(Black); testColor(Black);
testColor(DarkRed); testColor(DarkRed);