1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
#ifndef __Print_H__ #define __Print_H__
#include <stdio.h> #include <stdlib.h> #include <string.h>
#define PRINTMAXSTRLEN 1000
#define TOSTR(obj) (_Generic((obj),\ char: char2str,\ short int: short2str,\ int: int2str,\ float: float2str,\ double: double2str,\ const char*: str2str,\ char *: str2str,\ unsigned long long: ulonglong2str,\ default: print_void_or_error\ )(obj))
#define RED(obj) (str_comb(str_comb("\033[0;32;31m",TOSTR(obj)),"\033[m")) #define LIGHT_RED(obj) (str_comb(str_comb("\033[1;31m" ,TOSTR(obj)),"\033[m")) #define GREEN(obj) (str_comb(str_comb("\033[0;32;32m",TOSTR(obj)),"\033[m")) #define LIGHT_GREEN(obj) (str_comb(str_comb("\033[1;32m" ,TOSTR(obj)),"\033[m")) #define BLUE(obj) (str_comb(str_comb("\033[0;32;34m",TOSTR(obj)),"\033[m")) #define LIGHT_BLUE(obj) (str_comb(str_comb("\033[1;34m" ,TOSTR(obj)),"\033[m")) #define DARY_GRAY(obj) (str_comb(str_comb("\033[1;30m" ,TOSTR(obj)),"\033[m")) #define CYAN(obj) (str_comb(str_comb("\033[0;36m" ,TOSTR(obj)),"\033[m")) #define LIGHT_CYAN(obj) (str_comb(str_comb("\033[1;36m" ,TOSTR(obj)),"\033[m")) #define PURPLE(obj) (str_comb(str_comb("\033[0;35m" ,TOSTR(obj)),"\033[m")) #define LIGHT_PURPLE(obj) (str_comb(str_comb("\033[1;35m" ,TOSTR(obj)),"\033[m")) #define YELLOW(obj) (str_comb(str_comb("\033[0;33m" ,TOSTR(obj)),"\033[m")) #define LIGHT_YELLOW(obj) (str_comb(str_comb("\033[1;33m" ,TOSTR(obj)),"\033[m")) #define LIGHT_GRAY(obj) (str_comb(str_comb("\033[0;37m" ,TOSTR(obj)),"\033[m")) #define WHITE(obj) (str_comb(str_comb("\033[1;37m" ,TOSTR(obj)),"\033[m")) #define HIGHTLIGHT(str) (str_comb(str_comb("\033[1m" ,TOSTR(str)),"\033[m"))
#define ITALIC(str) (str_comb(str_comb("\033[3m",TOSTR(str)),"\033[m"))
#define _(str) (str_comb(str_comb("\033[4m",TOSTR(str)),"\033[m"))
#define FLASGING(str) (str_comb(str_comb("\033[5m",TOSTR(str)),"\033[m"))
#define Reversedisplay(str) (str_comb(str_comb("\033[7m",TOSTR(str)),"\033[m"))
static inline const char* str_comb(const char *str1, const char *str2) { static char str[PRINTMAXSTRLEN]; strcpy(str, str1); strcat(str, str2); return str; }
static inline const char* char2str(char ch) { static char str[2]; str[0] = ch; str[1] = '\0'; return str; }
static inline const char* short2str(short num) { static char str[7]; sprintf(str, "%hd", num); return str; }
static inline const char* int2str(int num) { static char str[12]; sprintf(str, "%d", num); return str; }
static inline const char* float2str(float num) { static char str[12]; sprintf(str, "%f", num); return str; }
static inline const char* double2str(double num) { static char str[21]; sprintf(str, "%lf", num); return str; }
static inline const char* ulonglong2str(unsigned long long num) { static char str[21]; sprintf(str, "%llu", num); return str; }
static inline const char* str2str(const char* str) { return str; }
#define TestColor(obj)\ {\ println(RED(obj));\ println(LIGHT_RED(obj));\ println(GREEN(obj));\ println(LIGHT_GREEN(obj));\ println(BLUE(obj));\ println(LIGHT_BLUE(obj));\ println(CYAN(obj));\ println(LIGHT_CYAN(obj));\ println(PURPLE(obj));\ println(LIGHT_PURPLE(obj));\ println(YELLOW(obj));\ println(LIGHT_YELLOW(obj));\ println(DARY_GRAY(obj));\ println(LIGHT_GRAY(obj));\ println(WHITE(obj));\ }
#define TestColur TestColor
#define _print(obj) (_Generic((obj), \ char: print_char, \ unsigned char: print_uchar, \ \ short int: print_short, \ int: print_int, \ unsigned int: print_uint, \ \ long long: print_longlong, \ unsigned long long: print_ulonglong,\ \ float: print_float, \ double: print_double, \ \ const char*: print_cstr, \ char*: print_str, \ \ int*: print_p, \ long long*: print_p, \ unsigned long long*:print_p, \ float*: print_p, \ double*: print_p, \ \ default: print_void_or_error \ )(obj))
#define _println(obj) _print(obj), _print("\n")
#define _PrintMacroArgCount(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, COUNT,...) COUNT #define PrintMacroArgCount(...) _PrintMacroArgCount("ignored", ##__VA_ARGS__,9,8,7,6,5,4,3,2,1,0)
#define PrintConcat(A, B) _PrintConcat(A, B) #define _PrintConcat(A, B) A##B
#define print(...) _print_(__VA_ARGS__) #define _print_(...) PrintConcat(_print_, PrintMacroArgCount(__VA_ARGS__))(__VA_ARGS__) #define _print_0() printf("") #define _print_1(_0) _print(_0) #define _print_2(_0, _1) _print_1(_0), _print(_1) #define _print_3(_0, _1, _2) _print_2(_0, _1), _print(_2) #define _print_4(_0, _1, _2, _3) _print_3(_0, _1, _2), _print(_3) #define _print_5(_0, _1, _2, _3, _4) _print_4(_0, _1, _2, _3), _print(_4) #define _print_6(_0, _1, _2, _3, _4, _5) _print_5(_0, _1, _2, _3, _4), _print(_5) #define _print_7(_0, _1, _2, _3, _4, _5, _6) _print_6(_0, _1, _2, _3, _4, _5), _print(_6) #define _print_8(_0, _1, _2, _3, _4, _5, _6, _7) _print_7(_0, _1, _2, _3, _4, _5, _6), _print(_7) #define _print_9(_0, _1, _2, _3, _4, _5, _6, _7, _8) _print_8(_0, _1, _2, _3, _4, _5, _6, _7), _print(_8) #define _print_10(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9) _print_9(_0, _1, _2, _3, _4, _5, _6, _7, _8), _print(_9)
#define endl ((char)'\n')
#define println(...) _println_(__VA_ARGS__) #define _println_(...) PrintConcat(_println_, PrintMacroArgCount(__VA_ARGS__))(__VA_ARGS__) #define _println_0() print("\n") #define _println_1(_0) _println(_0) #define _println_2(_0, _1) _println_1(_0), _println(_1) #define _println_3(_0, _1, _2) _println_2(_0, _1), _println(_2) #define _println_4(_0, _1, _2, _3) _println_3(_0, _1, _2), _println(_3) #define _println_5(_0, _1, _2, _3, _4) _println_4(_0, _1, _2, _3), _println(_4) #define _println_6(_0, _1, _2, _3, _4, _5) _println_5(_0, _1, _2, _3, _4), _println(_5) #define _println_7(_0, _1, _2, _3, _4, _5, _6) _println_6(_0, _1, _2, _3, _4, _5), _println(_6) #define _println_8(_0, _1, _2, _3, _4, _5, _6, _7) _println_7(_0, _1, _2, _3, _4, _5, _6), _println(_7) #define _println_9(_0, _1, _2, _3, _4, _5, _6, _7, _8) _println_8(_0, _1, _2, _3, _4, _5, _6, _7), _println(_8) #define _println_10(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9) _println_9(_0, _1, _2, _3, _4, _5, _6, _7, _8), _println(_9)
static inline void print_char(char num) { printf("%c", num); }
static inline void print_uchar(unsigned char num) { printf("%c", num); }
static inline void print_short(short int num) { printf("%hd", num); }
static inline void print_int(int num) { printf("%d", num); }
static inline void print_uint(unsigned int num) { printf("%u", num); }
static inline void print_longlong(long long num) { printf("%lld", num); }
static inline void print_ulonglong(unsigned long long num) { printf("%llu", num); }
static inline void print_float(float num) { printf("%f", num); }
static inline void print_double(double num) { printf("%lf", num); }
static inline void print_cstr(const char* str) { printf("%s", str); }
static inline void print_str(char* str) { printf("%s", str); }
static inline void print_p(void* obj) { printf("%p", obj); }
static inline void print_void_or_error(void *data) { _print(RED( "print error! or" )); _println(RED( "don't have this type to print!" )); }
#define debug_valuename_to_str(value) #value #define debug(obj) print(LIGHT_GREEN("debug: "), debug_valuename_to_str(obj)" = ", obj, "\n")
#define WARN_IF(EXP) \ do \ { \ if (EXP) \ print(YELLOW("warning: "), #EXP "\n"); \ }while(0)
#define ERR_IF(EXP) \ do \ { \ if (EXP) \ print(RED("error: "), #EXP "\n"); \ }while(0)
#define ERR_EXIT_IF(EXP)\ do \ { \ if (EXP) \ { \ print(RED("error: "), #EXP "\n"); \ exit(-1);\ }\ }while(0)
#define printArrTypeSpace 0
#define printArrTypeln 1
#define printArrTypeName 2
#define GET_ARR_LEN(arrobj) \ (_Generic((arrobj),\ char *: strlen((const char *)arrobj),\ default: (sizeof(arrobj) / sizeof(arrobj[0]))\ ))
#define printArr(ArrName, type) { \ for (int i = 0; i < GET_ARR_LEN(ArrName); i++) \ { \ if(type == printArrTypeName) \ { \ print(#ArrName); \ print("["); \ print(i); \ print("] = "); \ println(ArrName[i]); \ } \ else \ { \ print(ArrName[i]); \ if(type) print("\n"); \ else print(" "); \ } \ } \ }
#define printlnArr(ArrName, type) {printArr(ArrName, type); print("\n");}
#endif
|