Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
qishipai authored Aug 25, 2021
1 parent 57338f0 commit 3b33280
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// utils.cpp 2021-08-03 by 云中龙++
#include <stdarg.h>
#include <cstdio>
#include "NVutils.h"

#if defined(_WIN32) || defined(_WIN64)

#include <Windows.h> /* 使用Windows API设置字体颜色 */

void nv_error(const char *prefix, const char *str, ...)
{
va_list args; va_start(args, str);
static HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
SetConsoleTextAttribute(h, 0x0F); fprintf(stderr, "[%s] ", prefix);
SetConsoleTextAttribute(h, 0x0C); fprintf(stderr, "Error: ");
SetConsoleTextAttribute(h, 0x07); vfprintf(stderr, str, args);
va_end(args);
}

void nv_warn(const char *prefix, const char *str, ...)
{
va_list args; va_start(args, str);
static HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
SetConsoleTextAttribute(h, 0x0F); fprintf(stderr, "[%s] ", prefix);
SetConsoleTextAttribute(h, 0x0E); fprintf(stderr, "Warning: ");
SetConsoleTextAttribute(h, 0x07); vfprintf(stderr, str, args);
va_end(args);
}

void nv_print(const char *prefix, const char *str, ...)
{
va_list args; va_start(args, str);
static HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(h, 0x0A); printf("[%s] ", prefix);
SetConsoleTextAttribute(h, 0x07); vprintf(str, args); va_end(args);
}

#else /* 使用\033控制字符设置字体颜色 */

void nv_error(const char *prefix, const char *str, ...)
{
va_list args; va_start(args, str);
fprintf(stderr, "\033[1m[%s] \033[31mError: \033[m", prefix);
vfprintf(stderr, str, args); va_end(args);
}

void nv_warn(const char *prefix, const char *str, ...)
{
va_list args; va_start(args, str);
fprintf(stderr, "\033[1m[%s] \033[33mWarning: \033[m", prefix);
vfprintf(stderr, str, args); va_end(args);
}

void nv_print(const char *prefix, const char *str, ...)
{
va_list args; va_start(args, str);
printf("\033[32;1m[%s] \033[m", prefix); vprintf(str, args);
va_end(args);
}

#endif /* defined(_WIN32) || defined(_WIN64) */

void nv_revu16(u16_t &x)
{
x = x >> 8 | x << 8;
}

void nv_revu32(u32_t &x)
{
x = x >> 24 | (x & 0xFF0000) >> 8 | (x & 0xFF00) << 8 | x << 24;
}

0 comments on commit 3b33280

Please sign in to comment.