-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.cpp
91 lines (80 loc) · 1.92 KB
/
misc.cpp
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
#include "misc.h"
#ifdef _WIN
# include <Windows.h>
#else
# include <time.h>
# include <sys/ioctl.h>
#endif
namespace MLib
{
/* Wait for X milliseconds */
void wait(ulong_t milliseconds)
{
fflush(stdout);
#ifndef DISABLE_TIMING
# ifdef _WIN
Sleep(milliseconds);
# else
struct timespec ts;
ts.tv_sec = milliseconds / 1000;
ts.tv_nsec = (milliseconds % 1000) * 1000000;
nanosleep(&ts, NULL);
# endif
#endif
}
/* Get size of the console (real, not buffer) */
void getConsoleSize(vector2_t<long>& result)
{
#ifdef _WIN
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
result.x = csbi.srWindow.Right - csbi.srWindow.Left + 1;
result.y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
#else
struct winsize win;
ioctl(fileno(stdout), TIOCGWINSZ, &win);
result.x = win.ws_col;
result.y = win.ws_row;
#endif
}
/* set cursor position in console (top-left <=> 0, 0) */
void setConsolePos(long x, long y)
{
#ifdef _WIN
COORD coord; // coordinates
coord.X = x;
coord.Y = y; // X and Y coordinates
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
#else
fprintf(stdout, "%c[%ld;%ldf", 0x1B, y + 1, x + 1);
#endif
}
void setConsoleFgColor(long color)
{
fprintf(stdout, "%c[38;5;%ldm", 0x1B, color);
}
void setConsoleBgColor(long color)
{
fprintf(stdout, "%c[48;5;%ldm", 0x1B, color);
}
/* Count target in string str */
ulong_t strCountChar(std::string& str, char target)
{
ulong_t result = 0;
for (char c : str) {
if (c == target)
result++;
}
return result;
}
template <typename... Args>
std::string string_format(const std::string& format, Args... args)
{
size_t size = snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
if (size <= 0)
throw std::runtime_error("Error during formatting.");
std::unique_ptr<char[]> buf(new char[size]);
snprintf(buf.get(), size, format.c_str(), args...);
return std::string(buf.get());
}
} // namespace MLib