-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cxx
87 lines (71 loc) · 2.34 KB
/
Utils.cxx
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
#include <cstdarg>
#include <cstdio>
#include "Utils.hxx"
#if defined(_WIN32) || defined(_WIN64) /* 使用Windows API设置字体颜色 */
#include <Windows.h>
void NVi::error(const char *prefix, const char *str, ...)
{
va_list args;
static HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
SetConsoleTextAttribute(h, 0x0F); fprintf(stderr, "[%s] ", prefix);
SetConsoleTextAttribute(h, 0x0C); fprintf(stderr, "错误: ");
SetConsoleTextAttribute(h, 0x07);
va_start(args, str); vfprintf(stderr, str, args); va_end(args);
}
void NVi::warn(const char *prefix, const char *str, ...)
{
va_list args;
static HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
SetConsoleTextAttribute(h, 0x0F); fprintf(stderr, "[%s] ", prefix);
SetConsoleTextAttribute(h, 0x0E); fprintf(stderr, "警告: ");
SetConsoleTextAttribute(h, 0x07);
va_start(args, str); vfprintf(stderr, str, args); va_end(args);
}
void NVi::info(const char *prefix, const char *str, ...)
{
va_list args;
static HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
SetConsoleTextAttribute(h, 0x0F); fprintf(stderr, "[%s] ", prefix);
SetConsoleTextAttribute(h, 0x0A); fprintf(stderr, "注意: ");
SetConsoleTextAttribute(h, 0x07);
va_start(args, str); vfprintf(stderr, str, args); va_end(args);
}
#else /* 使用\033控制字符设置终端字体颜色 */
void NVi::error(const char *prefix, const char *str, ...)
{
va_list args;
fprintf(stderr, "\033[1m[%s] \033[31m错误: \033[m", prefix);
va_start(args, str); vfprintf(stderr, str, args); va_end(args);
}
void NVi::warn(const char *prefix, const char *str, ...)
{
va_list args;
fprintf(stderr, "\033[1m[%s] \033[33m警告: \033[m", prefix);
va_start(args, str); vfprintf(stderr, str, args); va_end(args);
}
void NVi::info(const char *prefix, const char *str, ...)
{
va_list args;
fprintf(stderr, "\033[1m[%s] \033[32m注意: \033[m", prefix);
va_start(args, str); vfprintf(stderr, str, args); va_end(args);
}
#endif
using namespace NVi;
nv_ul64 NVi::operator"" _u64be(const char *str, size_t n)
{
nv_ul64 ans = 0;
u16_t sft = 0;
while (nv_ul64 ch = str[sft])
{
ans |= ch << (sft++ << 3);
}
return ans;
}
void NVi::revU16(u16_t &x)
{
x = x >> 8 | x << 8;
}
void NVi::revU32(u32_t &x)
{
x = x >> 24 | (x & 0xFF0000) >> 8 | (x & 0xFF00) << 8 | x << 24;
}