kerep-headers

This commit is contained in:
2023-07-12 11:34:41 +03:00
parent d3525f66d4
commit 683395983c
49 changed files with 2003 additions and 20 deletions

View File

@@ -0,0 +1,52 @@
# kprintf
It is just my cross-plaform variant of printf.
Unlike in standard printf, `%l...` and `%ll...` placeholders dont depend on size of `long int` and `long long int`. And you can change terminal colors by unix codes (`\e[92m`) even on Windows.
| type | placeholder |
|----------------|-------------------------|
| i8 / i16 / i32 | %i / %d |
| i64 | %li / %ld / %lld / %lli |
| u8 / u16 / u32 | %u |
| u64 | %lu / %llu |
| f32 / f64 | %f |
| char | %c |
| char[] | %s |
| void\* | %p |
| 32bit or less | %x |
| 64bit | %lx |
<br>
# kprint
I don't really like printf function (and its variants), so i made safer and more convinient replacement.
| function | returns | arguments |
|----------|---------|-----------|
| kprint | void/throw | kp_fmt, void\*, kp_fmt, void\*... |
| ksprint | Maybe<char\*>| kp_fmt, void\*, kp_fmt, void\*... |
| kfprint | Maybe<void> | FILE\*, kp_fmt, void\*, kp_fmt, void\*... |
## how to use it:
+ **format construction:**
```
kp_fmt fmt= kp_fgColor | kp_bgColor | kp_dataFmt | flags | ktid;
```
[more about `kp_fmt`](kp_fmt.md)
+ fgColor and bgColor can be set to change console output color
+ you should set dataFormat for `int`/`uint`/`float`/`char\*` arguments and ktid for other types
+ flags can be set to modify TypeDescriptor.toString() behavior
+ don't forget to set TypeDescriptor.toString when registering type, or kprint will crash
+ **using base type arguments:**
you can just put them into a function
```
kprint(kp_h|kp_upper|kp_prefix, 255);
```
output: 0xFF
+ **using other registered types:**
should be sent as pointers
```
Maybe m=MaybeNull;
kprint(kp_fgBlue|kp_s, "Maybe: ", kp_fgGreen|ktid_MaybePtr, &m);
```
output: <span style="color:blue">Maybe:</span> <span style="color:lightgreen">{value: { Pointer, 0x0 }}</span>

View File

@@ -0,0 +1,102 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "../base/errors.h"
#include "kprint_format.h"
/*
This file looks like a mess, but all cotnent here just solves the problem of putting variadic arguments to array of formats and array of objects.
*/
typedef union {
i64 i64;
u64 u64;
f64 f64;
void* ptr;
} __kp_value_union;
static inline __kp_value_union __kpVU_f(f64 f) { return (__kp_value_union){ .f64=f }; }
static inline __kp_value_union __kpVU_i(i64 f) { return (__kp_value_union){ .i64=f }; }
#define __kpVU_selectType(V) _Generic(V, float: __kpVU_f, f64: __kpVU_f, default: __kpVU_i)(V)
#define __kpVU(V) __kpVU_selectType(V)
#define __kp_argsToFmts8( \
a0, a1, a2, a3, a4, a5, a6, a7,...) \
((i32[]){ a0,a2,a4,a6 })
#define __kp_argsToObjs8( \
a0, a1, a2, a3, a4, a5, a6, a7,...) \
((__kp_value_union[]){ __kpVU(a1),__kpVU(a3),__kpVU(a5),__kpVU(a7) })
#define __kp_argsToFmts16( \
a0, a1, a2, a3, a4, a5, a6, a7, \
a8, a9, a10,a11,a12,a13,a14,a15,...) \
((i32[]){ a0,a2,a4,a6,a8,a10,a12,a14 })
#define __kp_argsToObjs16( \
a0, a1, a2, a3, a4, a5, a6, a7, \
a8, a9, a10,a11,a12,a13,a14,a15,...) \
((__kp_value_union[]){ __kpVU(a1),__kpVU(a3),__kpVU(a5),__kpVU(a7),__kpVU(a9),__kpVU(a11),__kpVU(a13),__kpVU(a15) })
#define __kp_argsToFmts32( \
a0, a1, a2, a3, a4, a5, a6, a7, \
a8, a9, a10,a11,a12,a13,a14,a15, \
a16,a17,a18,a19,a20,a21,a22,a23, \
a24,a25,a26,a27,a28,a29,a30,a31,...) \
((i32[]){ a0,a2,a4,a6,a8,a10,a12,a14,a16,a18,a20,a22,a24,a26,a28,a30 })
#define __kp_argsToObjs32( \
a0, a1, a2, a3, a4, a5, a6, a7, \
a8, a9, a10,a11,a12,a13,a14,a15, \
a16,a17,a18,a19,a20,a21,a22,a23, \
a24,a25,a26,a27,a28,a29,a30,a31,...) \
((__kp_value_union[]){ __kpVU(a1),__kpVU(a3),__kpVU(a5),__kpVU(a7),__kpVU(a9),__kpVU(a11),__kpVU(a13),__kpVU(a15),__kpVU(a17),__kpVU(a19),__kpVU(a21),__kpVU(a23),__kpVU(a25),__kpVU(a27),__kpVU(a29),__kpVU(a31) })
#define __32zeroes 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
#define __kp_argsToArrs(COUNT,ARGS...) \
(kp_fmt*)( \
COUNT<=8 ? __kp_argsToFmts8(ARGS) : \
COUNT<=16 ? __kp_argsToFmts16(ARGS) : \
__kp_argsToFmts32(ARGS)), \
(__kp_value_union*)( \
COUNT<=8 ? __kp_argsToObjs8(ARGS) : \
COUNT<=16 ? __kp_argsToObjs16(ARGS) : \
__kp_argsToObjs32(ARGS))
Maybe __ksprint(u8 n, kp_fmt* formats, __kp_value_union* objects);
/// @param ARGS kp_fmt, value, kp_fmt, value...
///@returns Maybe<char*>
#define ksprint(ARGS...) WARNING_DISABLE( W_INT_CONVERSION, \
__ksprint(count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes)) \
)
/*-Wint-conversion warning was produced during value to __kp_value_union conversion*/
Maybe __kfprint(FILE* fd, u8 n, kp_fmt* formats, __kp_value_union* objects);
/// @param FD FILE*
/// @param ARGS kp_fmt, value, kp_fmt, value...
///@returns Maybe<void>
#define kfprint(FD, ARGS...) WARNING_DISABLE( W_INT_CONVERSION, \
__kfprint(FD, count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes)) \
)
void __kprint(u8 n, kp_fmt* formats, __kp_value_union* objects);
///can use non-catchable throw !!!
///@param ARGS kp_fmt, value, kp_fmt, value...
///@returns void
#define kprint(ARGS...) WARNING_DISABLE( W_INT_CONVERSION, \
__kprint(count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes)) \
)
#if __cplusplus
}
#endif

View File

@@ -0,0 +1,89 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
// 10000000 00000000 00000000 00000000
// ^ ^^^^
// | color num
// fgColorSet flag
PACKED_ENUM(kp_fgColor,
/// black foreground
kp_fgBlack = 0x80000000,
/// dark red foreground
kp_fgRedD = 0x81000000,
/// dark green foreground
kp_fgGreenD = 0x82000000,
/// dark yellow foreground
kp_fgYellowD = 0x83000000,
/// dark blue foreground
kp_fgBlueD = 0x84000000,
/// dark magenta foreground
kp_fgMagentaD= 0x85000000,
/// dark cyan foreground
kp_fgCyanD = 0x86000000,
/// gray foreground
kp_fgGray = 0x87000000,
/// dark gray foreground
kp_fgGrayD = 0x88000000,
/// red foreground
kp_fgRed = 0x89000000,
/// green foreground
kp_fgGreen = 0x8a000000,
/// yellow foreground
kp_fgYellow = 0x8b000000,
/// blue foreground
kp_fgBlue = 0x8c000000,
/// magenta foreground
kp_fgMagenta = 0x8d000000,
/// cyan foreground
kp_fgCyan = 0x8e000000,
/// white foreground
kp_fgWhite = 0x8f000000
)
// 01000000 00000000 00000000 00000000
// ^ ^^^^
// bgColorSet flag color num
PACKED_ENUM(kp_bgColor,
/// black background
kp_bgBlack = 0x40000000,
/// dark red background
kp_bgRedD = 0x40100000,
/// dark green background
kp_bgGreenD = 0x40200000,
/// dark yellow background
kp_bgYellowD = 0x40300000,
/// dark blue background
kp_bgBlueD = 0x40400000,
/// dark magenta background
kp_bgMagentaD= 0x40500000,
/// dark cyan background
kp_bgCyanD = 0x40600000,
/// gray background
kp_bgGray = 0x40700000,
/// dark gray background
kp_bgGrayD = 0x40800000,
/// red background
kp_bgRed = 0x40900000,
/// green background
kp_bgGreen = 0x40a00000,
/// yellow background
kp_bgYellow = 0x40b00000,
/// blue background
kp_bgBlue = 0x40c00000,
/// magenta background
kp_bgMagenta = 0x40d00000,
/// cyan background
kp_bgCyan = 0x40e00000,
/// white background
kp_bgWhite = 0x40f00000
)
char* kp_bgColor_toString(kp_bgColor c);
char* kp_fgColor_toString(kp_fgColor c);
#if __cplusplus
}
#endif

View File

@@ -0,0 +1,53 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "../base/std.h"
#include "../base/type_system/ktid.h"
#include "kprint_colors.h"
/// kprint_format
typedef u32 kp_fmt;
PACKED_ENUM(kp_dataFmt,
// 00000000 00000000 00000000 00000000
// ^^^^
// type
kp_i = 0x00000000,
kp_u = 0x00010000,
kp_h = 0x00020000,
kp_b = 0x00030000,
kp_f = 0x00040000,
kp_c = 0x00050000,
kp_s = 0x00060000,
// 00100000 00000000 00000000 00000000
// ^
// prefix/postfix flag
kp_pre=0x20000000,
kp_post=kp_pre,
// 00010000 00000000 00000000 00000000
// ^
// uppercase flag
kp_upper=0x10000000
)
#define kp_fmt_fgColorSet(FMT) (bool)((FMT&0x80000000)!=0)
#define kp_fmt_bgColorSet(FMT) (bool)((FMT&0x40000000)!=0)
#define kp_fmt_withPrefix(FMT) (bool)((FMT&kp_pre)!=0)
#define kp_fmt_withPostfix(FMT) (bool)((FMT&kp_post)!=0)
#define kp_fmt_isUpper(FMT) (bool)((FMT&kp_upper)!=0)
#define kp_fmt_fgColor(FMT) (kp_fgColor)(FMT&0x8f000000)
#define kp_fmt_bgColor(FMT) (kp_bgColor)(FMT&0x40f00000)
#define kp_fmt_dataFormat(FMT) (kp_dataFmt)(FMT&0x000f0000)
#define kp_fmt_ktid(FMT) (ktid)(FMT&0x0000ffff)
///@param f bgColor | fgColor
void kprint_setColor(kp_fmt f);
#if __cplusplus
}
#endif

View File

@@ -0,0 +1,74 @@
# kerep_format
```
00000000 00000000 00000000 00000000
fgColorSet┘│││└┼┴┘ └┼┴┘└┴┴┤ ktid
bgColorSet─┘││ │ bgColor └data format
prefix┬────┘│ └fgColor
postfix └uppercase
```
## Console colors
### *Foreground*
| kp_fg | hex | bin |
|-------|-----|-----|
| Black | 0x80000000 | 10000000 00000000... |
| RedD | 0x81000000 | 10000001 00000000... |
| GreenD | 0x82000000 | 10000010 00000000... |
| YellowD | 0x83000000 | 10000011 00000000... |
| BlueD | 0x84000000 | 10000100 00000000... |
| MagentaD | 0x85000000 | 10000101 00000000... |
| CyanD | 0x86000000 | 10000110 00000000... |
| Gray | 0x87000000 | 10000111 00000000... |
| GrayD | 0x88000000 | 10001000 00000000... |
| Red | 0x89000000 | 10001001 00000000... |
| Green | 0x8a000000 | 10001010 00000000... |
| Yellow | 0x8b000000 | 10001011 00000000... |
| Blue | 0x8c000000 | 10001100 00000000... |
| Magenta | 0x8d000000 | 10001101 00000000... |
| Cyan | 0x8e000000 | 10001110 00000000... |
| White | 0x8f000000 | 10001111 00000000... |
### *Background*
| kp_bg | hex | bin |
|-------|-----|-----|
| Black | 0x40000000 | 01000000 00000000... |
| RedD | 0x40100000 | 01000000 00010000... |
| GreenD | 0x40200000 | 01000000 00100000... |
| YellowD | 0x40300000 | 01000000 00110000... |
| BlueD | 0x40400000 | 01000000 01000000... |
| MagentaD | 0x40500000 | 01000000 01010000... |
| CyanD | 0x40600000 | 01000000 01100000... |
| Gray | 0x40700000 | 01000000 01110000... |
| GrayD | 0x40800000 | 01000000 10000000... |
| Red | 0x40900000 | 01000000 10010000... |
| Green | 0x40a00000 | 01000000 10100000... |
| Yellow | 0x40b00000 | 01000000 10110000... |
| Blue | 0x40c00000 | 01000000 11000000... |
| Magenta | 0x40d00000 | 01000000 11010000... |
| Cyan | 0x40e00000 | 01000000 11100000... |
| White | 0x40f00000 | 01000000 11110000... |
## Data format
| format | possible flags | data types | hex value | bin value |
|-----------|----------------|------------|------------|-----------|
| kp_i | | i8... i64 | 0x00000000 | 00000000 00000000... |
| kp_u | Postfix, Upper | u8... u64 | 0x00010000 | 00000000 00000001... |
| kp_h | Prefix, Upper | any | 0x00020000 | 00000000 00000010... |
| kp_b | Prefix | any | 0x00030000 | 00000000 00000011... |
| kp_f | Postfix, Upper | f32, f64 | 0x00040000 | 00000000 00000100... |
| kp_c | | char | 0x00050000 | 00000000 00000101... |
| kp_string | | char* | 0x00060000 | 00000000 00000110... |
P.S. `any` means you must add `kpid` to `kp_fmt` if data type is not base type
### *Flags*
| flag | hex value | bin value |
|------|------------|-----------|
| kp_pre | 0x20000000 | 00100000 00000000... |
| kp_post | 0x20000000 | 00100000 00000000... |
| kp_upper | 0x10000000 | 00010000 00000000... |

View File

@@ -0,0 +1,14 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "../base/type_system/base_toString.h"
// cross-platform printf analog
void kprintf(const char* format, ...);
#if __cplusplus
}
#endif