some changes in kprint
This commit is contained in:
parent
e9d736e95f
commit
0e6a0f6482
@ -12,6 +12,7 @@ char* errname(ErrorId err){
|
|||||||
case ERR_NULLPTR: return "ERR_NULLPTR";
|
case ERR_NULLPTR: return "ERR_NULLPTR";
|
||||||
case ERR_ENDOFSTR: return "ERR_ENDOFSTR";
|
case ERR_ENDOFSTR: return "ERR_ENDOFSTR";
|
||||||
case ERR_KEYNOTFOUND: return "ERR_KEYNOTFOUND";
|
case ERR_KEYNOTFOUND: return "ERR_KEYNOTFOUND";
|
||||||
|
case ERR_FORMAT: return "ERR_FORMAT";
|
||||||
default: return "UNKNOWN_ERROR";
|
default: return "UNKNOWN_ERROR";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,7 +10,7 @@ I don't really like printf function (and its variants), so i made safer and more
|
|||||||
## how to use it:
|
## how to use it:
|
||||||
+ **format construction:**
|
+ **format construction:**
|
||||||
```
|
```
|
||||||
kprint_format fmt= kprint_fgColor | kprint_fbgColor | kprint_fdataFmt | flags | ktId;
|
kprint_format fmt= kprint_fgColor | kprint_bgColor | kprint_fdataFmt | flags | ktId;
|
||||||
```
|
```
|
||||||
[more about `kprint_format`](kprint_format.md)
|
[more about `kprint_format`](kprint_format.md)
|
||||||
+ fgColor and bgColor can be set to change console output color
|
+ fgColor and bgColor can be set to change console output color
|
||||||
|
|||||||
@ -1,29 +1,37 @@
|
|||||||
/* #include "../base.h"
|
#include "../base.h"
|
||||||
#include "../../String/StringBuilder.h"
|
#include "../../String/StringBuilder.h"
|
||||||
|
|
||||||
Maybe __next_toString(kprint_format format, void* object){
|
ktId __typeFromFormat(kprint_format f){
|
||||||
// detecting type
|
ktId typeId=kprint_format_ktId(f);
|
||||||
if(!format.typeId) switch((kprint_dataFormat)((uint32)0 | format.dataFmt)){
|
if(typeId)
|
||||||
|
return typeId;
|
||||||
|
switch(kprint_format_dataFormat(f)){
|
||||||
case kprint_fmtInt:
|
case kprint_fmtInt:
|
||||||
case kprint_fmtHex:
|
case kprint_fmtHex:
|
||||||
case kprint_fmtBin:
|
case kprint_fmtBin:
|
||||||
format.typeId=ktId_Int64; break;
|
return ktId_Int64;
|
||||||
case kprint_fmtUInt:
|
case kprint_fmtUInt:
|
||||||
format.typeId=ktId_UInt64; break;
|
return ktId_UInt64;
|
||||||
case kprint_fmtFloat:
|
case kprint_fmtFloat:
|
||||||
format.typeId=ktId_Float64; break;
|
return ktId_Float64;
|
||||||
case kprint_fmtChar:
|
case kprint_fmtChar:
|
||||||
format.typeId=ktId_Char; break;
|
return ktId_Char;
|
||||||
case kprint_fmtString:
|
case kprint_fmtString:
|
||||||
format.typeId=ktId_CharPtr; break;
|
return ktId_CharPtr;
|
||||||
default:
|
default:
|
||||||
safethrow("typeId is not set, can't autodetect type",;);
|
return -1;
|
||||||
}
|
}
|
||||||
ktDescriptor typeDesc=ktDescriptor_get(format.typeId);
|
}
|
||||||
|
|
||||||
|
Maybe __next_toString(kprint_format f, void* object){
|
||||||
|
// detecting type
|
||||||
|
ktId typeId=__typeFromFormat(f);
|
||||||
|
if(typeId==-1)
|
||||||
|
safethrow("typeId is not set, can't autodetect type",;);
|
||||||
|
ktDescriptor typeDesc=ktDescriptor_get(typeId);
|
||||||
if(!typeDesc.toString)
|
if(!typeDesc.toString)
|
||||||
safethrow("type descriptor doesnt have toString() func",;);
|
safethrow("type descriptor doesnt have toString() func",;);
|
||||||
try(typeDesc.toString(object, &format), mStr,;);
|
return SUCCESS(UniHeap(ktId_CharPtr, typeDesc.toString(object, f)));
|
||||||
return SUCCESS(mStr.value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Maybe __ksprint(uint8 n, kprint_format* formats, void** objects){
|
Maybe __ksprint(uint8 n, kprint_format* formats, void** objects){
|
||||||
@ -43,6 +51,7 @@ Maybe __kfprint(FILE* file, uint8 n, kprint_format* formats, void** objects){
|
|||||||
safethrow("can't write string to file", Unitype_free(maybeStr.value));
|
safethrow("can't write string to file", Unitype_free(maybeStr.value));
|
||||||
Unitype_free(maybeStr.value);
|
Unitype_free(maybeStr.value);
|
||||||
}
|
}
|
||||||
|
fflush(file);
|
||||||
return MaybeNull;
|
return MaybeNull;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,24 +59,94 @@ void __kprint(uint8 n, kprint_format* formats, void** objects){
|
|||||||
for(uint8 i=0; i<n; i++){
|
for(uint8 i=0; i<n; i++){
|
||||||
kprint_format fmt=formats[i];
|
kprint_format fmt=formats[i];
|
||||||
kprint_setColor(fmt);
|
kprint_setColor(fmt);
|
||||||
tryLast(__next_toString(fmt, objects[i]),maybeStr);
|
tryLast(__next_toString(fmt, objects[i]),maybeStr);
|
||||||
if(fputs(maybeStr.value.VoidPtr, stdout)==EOF)\
|
if(fputs(maybeStr.value.VoidPtr, stdout)==EOF)\
|
||||||
throw("can't write string to stdout");
|
throw("can't write string to stdout");
|
||||||
//, Unitype_free(maybeStr.value)
|
//, Unitype_free(maybeStr.value)
|
||||||
Unitype_free(maybeStr.value);
|
Unitype_free(maybeStr.value);
|
||||||
}
|
}
|
||||||
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(_WIN32)|| defined(_WIN64)
|
||||||
|
#include <windows.h>
|
||||||
|
#define FOREGROUND_YELLOW FOREGROUND_GREEN | FOREGROUND_RED
|
||||||
|
|
||||||
|
DWORD kprint_fgColor_toWin(kprint_fgColor f){
|
||||||
|
//printf("fg: %x\n", f);
|
||||||
|
switch(f){
|
||||||
|
case kprint_fgBlack: return 0;
|
||||||
|
case kprint_fgDarkRed: return FOREGROUND_RED;
|
||||||
|
case kprint_fgDarkGreen: return FOREGROUND_GREEN;
|
||||||
|
case kprint_fgDarkYellow: return FOREGROUND_GREEN | FOREGROUND_RED;
|
||||||
|
case kprint_fgDarkBlue: return FOREGROUND_BLUE;
|
||||||
|
case kprint_fgDarkMagneta: return FOREGROUND_RED | FOREGROUND_BLUE;
|
||||||
|
case kprint_fgDarkCyan: return FOREGROUND_BLUE | FOREGROUND_GREEN;
|
||||||
|
case kprint_fgGray: return FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
|
||||||
|
case kprint_fgDarkGray: return FOREGROUND_INTENSITY;
|
||||||
|
case kprint_fgRed: return FOREGROUND_RED | FOREGROUND_INTENSITY;
|
||||||
|
case kprint_fgGreen: return FOREGROUND_GREEN | FOREGROUND_INTENSITY;
|
||||||
|
case kprint_fgYellow: return FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
|
||||||
|
case kprint_fgBlue: return FOREGROUND_BLUE | FOREGROUND_INTENSITY;
|
||||||
|
case kprint_fgMagneta: return FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY;
|
||||||
|
case kprint_fgCyan: return FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
|
||||||
|
case kprint_fgWhite: return FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
|
||||||
|
default: throw(ERR_FORMAT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DWORD kprint_bgColor_toWin(kprint_bgColor f){
|
||||||
|
//printf("bg: %x\n", f);
|
||||||
|
switch(f){
|
||||||
|
case kprint_bgBlack: return 0;
|
||||||
|
case kprint_bgDarkRed: return BACKGROUND_RED;
|
||||||
|
case kprint_bgDarkGreen: return BACKGROUND_GREEN;
|
||||||
|
case kprint_bgDarkYellow: return BACKGROUND_GREEN | BACKGROUND_RED;
|
||||||
|
case kprint_bgDarkBlue: return BACKGROUND_BLUE;
|
||||||
|
case kprint_bgDarkMagneta: return BACKGROUND_RED | BACKGROUND_BLUE;
|
||||||
|
case kprint_bgDarkCyan: return BACKGROUND_BLUE | BACKGROUND_GREEN;
|
||||||
|
case kprint_bgGray: return BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED;
|
||||||
|
case kprint_bgDarkGray: return BACKGROUND_INTENSITY;
|
||||||
|
case kprint_bgRed: return BACKGROUND_RED | BACKGROUND_INTENSITY;
|
||||||
|
case kprint_bgGreen: return BACKGROUND_GREEN | BACKGROUND_INTENSITY;
|
||||||
|
case kprint_bgYellow: return BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY;
|
||||||
|
case kprint_bgBlue: return BACKGROUND_BLUE | BACKGROUND_INTENSITY;
|
||||||
|
case kprint_bgMagneta: return BACKGROUND_BLUE | BACKGROUND_RED | BACKGROUND_INTENSITY;
|
||||||
|
case kprint_bgCyan: return BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY;
|
||||||
|
case kprint_bgWhite: return BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY;
|
||||||
|
default: throw(ERR_FORMAT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void kprint_setColor(kprint_format f){
|
void kprint_setColor(kprint_format f){
|
||||||
if(!f.bgColorChanged | !f.fgColorChanged)
|
DWORD color=0;
|
||||||
|
if(!kprint_format_fgColorChanged(f) & !kprint_format_bgColorChanged(f))
|
||||||
return;
|
return;
|
||||||
|
if(kprint_format_fgColorChanged(f))
|
||||||
|
color+=kprint_fgColor_toWin(kprint_format_fgColor(f));
|
||||||
|
if(kprint_format_bgColorChanged(f))
|
||||||
|
color+=kprint_bgColor_toWin(kprint_format_bgColor(f));
|
||||||
|
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
|
SetConsoleTextAttribute(hConsole, color);
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
void kprint_setColor(kprint_format f){
|
||||||
|
if(kprint_format_fgColorChanged(f)){
|
||||||
|
uint8 fg=(f&0x0f000000)>>24;
|
||||||
|
if(fg<8) fg+=30;
|
||||||
|
else fg+=90-8;
|
||||||
|
printf("\e[%um", fg);
|
||||||
|
}
|
||||||
|
if(kprint_format_bgColorChanged(f)){
|
||||||
|
uint8 bg=(f&0x00f00000)>>20;
|
||||||
|
if(bg<8) bg+=40;
|
||||||
|
else bg+=100-8;
|
||||||
|
printf("\e[%um", bg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Maybe ksprint_ar(uint32 count, kprint_format format, ktId typeId, void* array){
|
||||||
|
|
||||||
|
|
||||||
Maybe ksprint_ar(uint32 count, kprint_format format, ktId typeId, void* array){
|
|
||||||
ktDescriptor typeDesc=ktDescriptor_get(format.typeId);
|
ktDescriptor typeDesc=ktDescriptor_get(format.typeId);
|
||||||
if(!typeDesc.toString)
|
if(!typeDesc.toString)
|
||||||
safethrow("type descriptor doesnt have toString() func",;);
|
safethrow("type descriptor doesnt have toString() func",;);
|
||||||
@ -75,11 +154,11 @@ Maybe ksprint_ar(uint32 count, kprint_format format, ktId typeId, void* array){
|
|||||||
StringBuilder_append_char(strb, '[');
|
StringBuilder_append_char(strb, '[');
|
||||||
for (uint16 e=1; e<count; e++){
|
for (uint16 e=1; e<count; e++){
|
||||||
StringBuilder_append_char(strb, ' ');
|
StringBuilder_append_char(strb, ' ');
|
||||||
try(typeDesc.toString(array+typeDesc.size*e, &format), estr, StringBuilder_free(strb));
|
char* elStr=typeDesc.toString(array+typeDesc.size*e, &format);
|
||||||
StringBuilder_append_cptr(strb, estr.value.VoidPtr);
|
StringBuilder_append_cptr(strb, elStr);
|
||||||
StringBuilder_append_char(strb, ',');
|
StringBuilder_append_char(strb, ',');
|
||||||
}
|
}
|
||||||
StringBuilder_rmchar(strb);
|
StringBuilder_rmchar(strb);
|
||||||
StringBuilder_append_char(strb, ' ');
|
StringBuilder_append_char(strb, ' ');
|
||||||
StringBuilder_append_char(strb, ']');
|
StringBuilder_append_char(strb, ']');
|
||||||
} */
|
} */
|
||||||
|
|||||||
@ -48,13 +48,13 @@ extern "C" {
|
|||||||
__kprint_argsToObjects32(ARGS))
|
__kprint_argsToObjects32(ARGS))
|
||||||
|
|
||||||
|
|
||||||
Maybe __ksprint(uint8 n, int32* formats, void** objects);
|
Maybe __ksprint(uint8 n, kprint_format* formats, void** objects);
|
||||||
#define ksprint(ARGS...) __ksprint(count_args(ARGS), __kprint_argsToArrs(count_args(ARGS),ARGS, __32zeroes))
|
#define ksprint(ARGS...) __ksprint(count_args(ARGS), __kprint_argsToArrs(count_args(ARGS),ARGS, __32zeroes))
|
||||||
|
|
||||||
Maybe __kfprint(FILE* fd, uint8 n, int32* formats, void** objects);
|
Maybe __kfprint(FILE* fd, uint8 n, kprint_format* formats, void** objects);
|
||||||
#define kfprint(FD, ARGS...) __kfprint(FD, count_args(ARGS), __kprint_argsToArrs(count_args(ARGS),ARGS, __32zeroes))
|
#define kfprint(FD, ARGS...) __kfprint(FD, count_args(ARGS), __kprint_argsToArrs(count_args(ARGS),ARGS, __32zeroes))
|
||||||
|
|
||||||
void __kprint(uint8 n, int32* formats, void** objects);
|
void __kprint(uint8 n, kprint_format* formats, void** 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))
|
||||||
|
|
||||||
// can take (bgColor | fgColor)
|
// can take (bgColor | fgColor)
|
||||||
|
|||||||
@ -31,19 +31,17 @@ typedef enum kprint_dataFormat{
|
|||||||
kprint_fmtUppercase=0x10000000
|
kprint_fmtUppercase=0x10000000
|
||||||
} kprint_dataFormat;
|
} kprint_dataFormat;
|
||||||
|
|
||||||
typedef union {
|
typedef uint32 kprint_format;
|
||||||
int32 i32;
|
|
||||||
struct {
|
#define kprint_format_fgColorChanged(FMT) (FMT&0x80000000)
|
||||||
unsigned char fgColorChanged : 1;
|
#define kprint_format_bgColorChanged(FMT) (FMT&0x40000000)
|
||||||
unsigned char bgColorChanged : 1;
|
#define kprint_format_withPrefix(FMT) (FMT&kprint_fmtWithPrefix)
|
||||||
unsigned char withPrefix : 1;
|
#define kprint_format_withPostfix(FMT) (FMT&kprint_fmtWithPostfix)
|
||||||
unsigned char uppercase : 1;
|
#define kprint_format_uppercase(FMT) (FMT&kprint_fmtUppercase)
|
||||||
unsigned char fgColor : 4;
|
#define kprint_format_fgColor(FMT) (kprint_fgColor)(FMT&0x8f000000)
|
||||||
unsigned char bgColor : 4;
|
#define kprint_format_bgColor(FMT) (kprint_bgColor)(FMT&0x40f00000)
|
||||||
unsigned char dataFmt : 4;
|
#define kprint_format_dataFormat(FMT) (kprint_dataFormat)(FMT&0x000f0000)
|
||||||
ktId typeId;
|
#define kprint_format_ktId(FMT) (kprint_dataFormat)(FMT&0x0000ffff)
|
||||||
};
|
|
||||||
} kprint_format;
|
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@ -52,7 +52,7 @@ char* toString_float(float64 n, bool withPostfix, bool uppercase){
|
|||||||
}
|
}
|
||||||
|
|
||||||
char* toString_bin(char* bytes, uint32 size, bool withPrefix){
|
char* toString_bin(char* bytes, uint32 size, bool withPrefix){
|
||||||
char* str=malloc(size*8+1);
|
char* str=malloc(size*8 + (withPrefix?2:0) +1);
|
||||||
uint32 cn=0;
|
uint32 cn=0;
|
||||||
if(withPrefix){
|
if(withPrefix){
|
||||||
str[cn++]='0';
|
str[cn++]='0';
|
||||||
@ -80,7 +80,7 @@ char _4bitsHex(uint8 u, bool uppercase){
|
|||||||
}
|
}
|
||||||
|
|
||||||
char* toString_hex(char* bytes, uint32 size, bool withPrefix, bool uppercase){
|
char* toString_hex(char* bytes, uint32 size, bool withPrefix, bool uppercase){
|
||||||
char* str=malloc(size*2);
|
char* str=malloc(size*2 + (withPrefix?2:0) + 1);
|
||||||
uint32 cn=0;
|
uint32 cn=0;
|
||||||
if(withPrefix){
|
if(withPrefix){
|
||||||
str[cn++]='0';
|
str[cn++]='0';
|
||||||
@ -96,18 +96,17 @@ char* toString_hex(char* bytes, uint32 size, bool withPrefix, bool uppercase){
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#define __toString_int_def(BITS) char* __toString_int##BITS(void* _n, int32 _f){\
|
#define __toString_int_def(BITS) char* __toString_int##BITS(void* _n, uint32 f){\
|
||||||
kprint_format f=*(kprint_format*)&_f;\
|
switch(kprint_format_dataFormat(f)){\
|
||||||
switch(f.dataFmt | (uint32)0){\
|
case kprint_fmtInt: ;\
|
||||||
case kprint_fmtInt:\
|
|
||||||
int##BITS n=*(int##BITS*)_n;\
|
int##BITS n=*(int##BITS*)_n;\
|
||||||
return toString_int(n);\
|
return toString_int(n);\
|
||||||
case kprint_fmtBin:\
|
case kprint_fmtBin:\
|
||||||
return toString_bin(_n, BITS/8, f.withPrefix);\
|
return toString_bin(_n, BITS/8, kprint_format_withPrefix(f));\
|
||||||
case kprint_fmtHex:\
|
case kprint_fmtHex:\
|
||||||
return toString_hex(_n, BITS/8, f.withPrefix, f.uppercase);\
|
return toString_hex(_n, BITS/8, kprint_format_withPrefix(f), kprint_format_uppercase(f));\
|
||||||
default:\
|
default:\
|
||||||
printf("\n%u\n", f.dataFmt);\
|
printf("\n%u\n", kprint_format_dataFormat(f));\
|
||||||
throw(ERR_FORMAT);\
|
throw(ERR_FORMAT);\
|
||||||
return NULL;\
|
return NULL;\
|
||||||
}\
|
}\
|
||||||
@ -117,18 +116,17 @@ __toString_int_def(16)
|
|||||||
__toString_int_def(32)
|
__toString_int_def(32)
|
||||||
__toString_int_def(64)
|
__toString_int_def(64)
|
||||||
|
|
||||||
#define __toString_uint_def(BITS) char* __toString_uint##BITS(void* _n, int32 _f){\
|
#define __toString_uint_def(BITS) char* __toString_uint##BITS(void* _n, uint32 f){\
|
||||||
kprint_format f=*(kprint_format*)&_f;\
|
switch(kprint_format_dataFormat(f)){\
|
||||||
switch(f.dataFmt | (uint32)0){\
|
case kprint_fmtUInt: ;\
|
||||||
case kprint_fmtUInt:\
|
|
||||||
uint##BITS n=*(uint##BITS*)_n;\
|
uint##BITS n=*(uint##BITS*)_n;\
|
||||||
return toString_uint(n, f.withPrefix, f.uppercase);\
|
return toString_uint(n, kprint_format_withPostfix(f), kprint_format_uppercase(f));\
|
||||||
case kprint_fmtBin:\
|
case kprint_fmtBin:\
|
||||||
return toString_bin(_n, BITS/8, f.withPrefix);\
|
return toString_bin(_n, BITS/8, kprint_format_withPrefix(f));\
|
||||||
case kprint_fmtHex:\
|
case kprint_fmtHex:\
|
||||||
return toString_hex(_n, BITS/8, f.withPrefix, f.uppercase);\
|
return toString_hex(_n, BITS/8, kprint_format_withPrefix(f), kprint_format_uppercase(f));\
|
||||||
default:\
|
default:\
|
||||||
printf("\n%u\n", f.dataFmt);\
|
printf("\n%u\n", kprint_format_dataFormat(f));\
|
||||||
throw(ERR_FORMAT);\
|
throw(ERR_FORMAT);\
|
||||||
return NULL;\
|
return NULL;\
|
||||||
}\
|
}\
|
||||||
@ -138,18 +136,17 @@ __toString_uint_def(16)
|
|||||||
__toString_uint_def(32)
|
__toString_uint_def(32)
|
||||||
__toString_uint_def(64)
|
__toString_uint_def(64)
|
||||||
|
|
||||||
#define __toString_float_def(BITS) char* __toString_float##BITS(void* _n, int32 _f){\
|
#define __toString_float_def(BITS) char* __toString_float##BITS(void* _n, uint32 f){\
|
||||||
kprint_format f=*(kprint_format*)&_f;\
|
switch(kprint_format_dataFormat(f)){\
|
||||||
switch(f.dataFmt | (uint32)0){\
|
case kprint_fmtFloat: ;\
|
||||||
case kprint_fmtFloat:\
|
|
||||||
float##BITS n=*(float##BITS*)_n;\
|
float##BITS n=*(float##BITS*)_n;\
|
||||||
return toString_float(n, f.withPrefix, f.uppercase);\
|
return toString_float(n, kprint_format_withPostfix(f), kprint_format_uppercase(f));\
|
||||||
case kprint_fmtBin:\
|
case kprint_fmtBin:\
|
||||||
return toString_bin(_n, BITS/8, f.withPrefix);\
|
return toString_bin(_n, BITS/8, kprint_format_withPrefix(f));\
|
||||||
case kprint_fmtHex:\
|
case kprint_fmtHex:\
|
||||||
return toString_hex(_n, BITS/8, f.withPrefix, f.uppercase);\
|
return toString_hex(_n, BITS/8, kprint_format_withPrefix(f), kprint_format_uppercase(f));\
|
||||||
default:\
|
default:\
|
||||||
printf("\n%u\n", f.dataFmt);\
|
printf("\n%u\n", kprint_format_dataFormat(f));\
|
||||||
throw(ERR_FORMAT);\
|
throw(ERR_FORMAT);\
|
||||||
return NULL;\
|
return NULL;\
|
||||||
}\
|
}\
|
||||||
|
|||||||
@ -8,20 +8,20 @@ extern "C" {
|
|||||||
|
|
||||||
// functions for base types
|
// functions for base types
|
||||||
char* toString_int(int64 n);
|
char* toString_int(int64 n);
|
||||||
char* __toString_int8(void* n, int32 fmt);
|
char* __toString_int8(void* n, uint32 fmt);
|
||||||
char* __toString_int16(void* n, int32 fmt);
|
char* __toString_int16(void* n, uint32 fmt);
|
||||||
char* __toString_int32(void* n, int32 fmt);
|
char* __toString_uint32(void* n, uint32 fmt);
|
||||||
char* __toString_int64(void* n, int32 fmt);
|
char* __toString_int64(void* n, uint32 fmt);
|
||||||
|
|
||||||
char* toString_uint(uint64 n, bool withPostfix, bool uppercase);
|
char* toString_uint(uint64 n, bool withPostfix, bool uppercase);
|
||||||
char* __toString_uint8(void* n, int32 fmt);
|
char* __toString_uint8(void* n, uint32 fmt);
|
||||||
char* __toString_uint16(void* n, int32 fmt);
|
char* __toString_uint16(void* n, uint32 fmt);
|
||||||
char* __toString_uint32(void* n, int32 fmt);
|
char* __toString_uuint32(void* n, uint32 fmt);
|
||||||
char* __toString_uint64(void* n, int32 fmt);
|
char* __toString_uint64(void* n, uint32 fmt);
|
||||||
|
|
||||||
char* toString_float(float64 n, bool withPostfix, bool uppercase); // very bad implimentation
|
char* toString_float(float64 n, bool withPostfix, bool uppercase); // very bad implimentation
|
||||||
char* __toString_float32(void* n, int32 fmt);
|
char* __toString_float32(void* n, uint32 fmt);
|
||||||
char* __toString_float64(void* n, int32 fmt);
|
char* __toString_float64(void* n, uint32 fmt);
|
||||||
|
|
||||||
// universal functions
|
// universal functions
|
||||||
char* toString_bin(char* bytes, uint32 size, bool withPrefix);
|
char* toString_bin(char* bytes, uint32 size, bool withPrefix);
|
||||||
|
|||||||
@ -12,7 +12,7 @@ typedef struct ktDescriptor{
|
|||||||
ktId id;
|
ktId id;
|
||||||
uint16 size;
|
uint16 size;
|
||||||
void (*freeMembers)(void*); // NULL or function which frees all struct members
|
void (*freeMembers)(void*); // NULL or function which frees all struct members
|
||||||
char* (*toString)(void* obj, int32 fmt); // NULL or function which generates string representaion of object
|
char* (*toString)(void* obj, uint32 fmt); // NULL or function which generates string representaion of object
|
||||||
} ktDescriptor;
|
} ktDescriptor;
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
|
|||||||
@ -58,7 +58,7 @@ void ktDescriptors_endInit(){
|
|||||||
printf("\e[92minitialized %u type descriptors\n", ktId_last);
|
printf("\e[92minitialized %u type descriptors\n", ktId_last);
|
||||||
}
|
}
|
||||||
|
|
||||||
void __kt_register(char* name, int16 size, void (*freeMembers)(void*), char* (*toString)(void*, int32)){
|
void __kt_register(char* name, int16 size, void (*freeMembers)(void*), char* (*toString)(void*, uint32)){
|
||||||
ktDescriptor typeDesc={
|
ktDescriptor typeDesc={
|
||||||
.name=name,
|
.name=name,
|
||||||
.size=size,
|
.size=size,
|
||||||
|
|||||||
@ -8,9 +8,8 @@ extern "C" {
|
|||||||
#include "ktId.h"
|
#include "ktId.h"
|
||||||
#include "ktDescriptor.h"
|
#include "ktDescriptor.h"
|
||||||
|
|
||||||
|
|
||||||
extern ktId ktId_last;
|
extern ktId ktId_last;
|
||||||
void __kt_register(char* name, int16 size, void (*freeMembers)(void*), char* (*toString)(void*, int32));
|
void __kt_register(char* name, int16 size, void (*freeMembers)(void*), char* (*toString)(void*, uint32));
|
||||||
|
|
||||||
#define kt_register(TYPE, ID_VAR_NAME, FREE_MEMBERS_FUNC, TO_STRING_FUNC)\
|
#define kt_register(TYPE, ID_VAR_NAME, FREE_MEMBERS_FUNC, TO_STRING_FUNC)\
|
||||||
__kt_register(#ID_VAR_NAME, sizeof(TYPE), FREE_MEMBERS_FUNC, TO_STRING_FUNC);\
|
__kt_register(#ID_VAR_NAME, sizeof(TYPE), FREE_MEMBERS_FUNC, TO_STRING_FUNC);\
|
||||||
|
|||||||
@ -12,7 +12,7 @@ void Unitype_free(Unitype u){
|
|||||||
}
|
}
|
||||||
void __UnitypePtr_free(void* u) { Unitype_free(*(Unitype*)u); }
|
void __UnitypePtr_free(void* u) { Unitype_free(*(Unitype*)u); }
|
||||||
|
|
||||||
char* toString_Unitype(void* _u, int32 fmt){
|
char* toString_Unitype(void* _u, uint32 fmt){
|
||||||
Unitype* u=_u;
|
Unitype* u=_u;
|
||||||
ktDescriptor type=ktDescriptor_get(u->typeId);
|
ktDescriptor type=ktDescriptor_get(u->typeId);
|
||||||
char* valuestr=type.toString(_u, fmt);
|
char* valuestr=type.toString(_u, fmt);
|
||||||
|
|||||||
@ -9,6 +9,7 @@ void test_all(){
|
|||||||
test_hash_functions();
|
test_hash_functions();
|
||||||
test_hashtable();
|
test_hashtable();
|
||||||
test_dtsod();
|
test_dtsod();
|
||||||
|
test_kprint_colors();
|
||||||
printf("\e[96m--------------------------------------\e[0m\n");
|
printf("\e[96m--------------------------------------\e[0m\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -18,7 +19,8 @@ int main(){
|
|||||||
ktDescriptors_initKerepTypes();
|
ktDescriptors_initKerepTypes();
|
||||||
ktDescriptors_endInit();
|
ktDescriptors_endInit();
|
||||||
printf("\e[97mkerep tests are starting!\n");
|
printf("\e[97mkerep tests are starting!\n");
|
||||||
optime("test_all",1,test_all());
|
// optime("test_all",1,test_all());
|
||||||
|
test_kprint_colors();
|
||||||
printf("\e[0m\n");
|
printf("\e[0m\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
int64 _autoarrVsVector(uint16 blockCount, uint16 blockLength){
|
int64 _autoarrVsVector(uint16 blockCount, uint16 blockLength){
|
||||||
uint32 count=blockLength*blockCount;
|
uint32 count=blockLength*blockCount;
|
||||||
printf("\e[94mblock count: %u block length: %u count: %llu\n", blockCount, blockLength, (uint64)count);
|
printf("\e[94mblock count: %u block length: %u count: " IFWIN("%llu", "%lu") "\n", blockCount, blockLength, (uint64)count);
|
||||||
Autoarr_int64* ar=Autoarr_create(int64, blockCount, blockLength);
|
Autoarr_int64* ar=Autoarr_create(int64, blockCount, blockLength);
|
||||||
std::vector<int64> vec=std::vector<int64>();
|
std::vector<int64> vec=std::vector<int64>();
|
||||||
optime("Autoarr_add", 1, ({
|
optime("Autoarr_add", 1, ({
|
||||||
@ -15,7 +15,7 @@ int64 _autoarrVsVector(uint16 blockCount, uint16 blockLength){
|
|||||||
for(uint32 i=0; i< count; i++)
|
for(uint32 i=0; i< count; i++)
|
||||||
vec.push_back(i);
|
vec.push_back(i);
|
||||||
}));
|
}));
|
||||||
int64 t;
|
int64 t=0;
|
||||||
optime("Autoarr_get", 1, ({
|
optime("Autoarr_get", 1, ({
|
||||||
for(uint32 i=0; i< count; i++)
|
for(uint32 i=0; i< count; i++)
|
||||||
t=Autoarr_get(ar, i);
|
t=Autoarr_get(ar, i);
|
||||||
|
|||||||
47
tests/test_kprint_colors.c
Normal file
47
tests/test_kprint_colors.c
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
#include "tests.h"
|
||||||
|
#if defined(_WIN32)|| defined(_WIN64)
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define testColor(COLOR) \
|
||||||
|
kprint_setColor(kprint_bgBlack | kprint_fg##COLOR);\
|
||||||
|
printf(#COLOR " ");\
|
||||||
|
kprint_setColor(kprint_bg##COLOR | kprint_fgGray);\
|
||||||
|
printf(#COLOR);\
|
||||||
|
kprint_setColor(kprint_bgBlack | kprint_fgBlack);\
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
void test_kprint_colors(){
|
||||||
|
IFWIN(
|
||||||
|
({
|
||||||
|
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
|
for(uint8 col=0; col<255; col++){
|
||||||
|
SetConsoleTextAttribute(hConsole, col);
|
||||||
|
printf("%u ",col);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
({
|
||||||
|
for(uint8 col=0; col<255; col++)
|
||||||
|
printf("\e[%um%u ", col, col);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
testColor(Black);
|
||||||
|
testColor(DarkRed);
|
||||||
|
testColor(DarkGreen);
|
||||||
|
testColor(DarkYellow);
|
||||||
|
testColor(DarkBlue);
|
||||||
|
testColor(DarkMagneta);
|
||||||
|
testColor(DarkCyan);
|
||||||
|
testColor(Gray);
|
||||||
|
testColor(DarkGray);
|
||||||
|
testColor(Red);
|
||||||
|
testColor(Green);
|
||||||
|
testColor(Yellow);
|
||||||
|
testColor(Blue);
|
||||||
|
testColor(Magneta);
|
||||||
|
testColor(Cyan);
|
||||||
|
testColor(White);
|
||||||
|
kprint_setColor(kprint_bgBlack | kprint_fgGray);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user