This repository was archived by the owner on Jun 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconsole_handler.h
More file actions
131 lines (94 loc) · 3.55 KB
/
console_handler.h
File metadata and controls
131 lines (94 loc) · 3.55 KB
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
#ifndef CONSOLE_HANDLER_H
#define CONSOLE_HANDLER_H CONSOLE_HANDLER_H
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <Windows.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#define CONSOLE_MAX_CHARACTERS_LINE 2048
/* Non-blocking printf functions */
/*
* printf defined in stdio.h will block if the user highlights (marks) any text in the terminal
* This would block the main thread and would effectively block any user input, so these functions prevent that by
* manually setting characters in the console
*/
/* All functions are thread-safe (unless on conflicting positions...) */
void console_set_nextline_number(const uint16_t n);
void console_modify_nextline_number(const uint16_t n);
int print_to_console(const char *message, const int clear);
static int vprintf_to_console(const char *format, va_list args) {
char message[CONSOLE_MAX_CHARACTERS_LINE + 1];
const int len = vsnprintf(message, sizeof(message), format, args);
if (len >= CONSOLE_MAX_CHARACTERS_LINE) {
SetLastError(ERROR_OUTOFMEMORY);
return -1;
} else if (len < 0) {
SetLastError(ERROR_INVALID_PARAMETER);
return -1;
}
return print_to_console(message, 0);
}
static int vprintf_to_console_clear(const char *format, va_list args) {
char message[CONSOLE_MAX_CHARACTERS_LINE + 1];
const int len = vsnprintf(message, sizeof(message), format, args);
if (len >= CONSOLE_MAX_CHARACTERS_LINE) {
SetLastError(ERROR_OUTOFMEMORY);
return -1;
} else if (len < 0) {
SetLastError(ERROR_INVALID_PARAMETER);
return -1;
}
return print_to_console(message, 1);
}
static inline int printf_to_console(const char *format, ...) {
va_list args;
va_start(args, format);
return vprintf_to_console(format, args);
}
static inline int printf_to_console_clear(const char *format, ...) {
va_list args;
va_start(args, format);
return vprintf_to_console_clear(format, args);
}
/* */
int print_to_console_pos(COORD pos, const char *message, const int clear);
static int vprintf_to_console_pos(const COORD pos, const char *format, va_list args) {
char message[CONSOLE_MAX_CHARACTERS_LINE + 1];
const int len = vsnprintf(message, sizeof(message), format, args);
if (len >= CONSOLE_MAX_CHARACTERS_LINE) {
SetLastError(ERROR_OUTOFMEMORY);
return -1;
} else if (len < 0) {
SetLastError(ERROR_INVALID_PARAMETER);
return -1;
}
return print_to_console_pos(pos, message, 0);
}
static int vprintf_to_console_pos_clear(const COORD pos, const char *format, va_list args) {
char message[CONSOLE_MAX_CHARACTERS_LINE + 1];
const int len = vsnprintf(message, sizeof(message), format, args);
if (len >= CONSOLE_MAX_CHARACTERS_LINE) {
SetLastError(ERROR_OUTOFMEMORY);
return -1;
} else if (len < 0) {
SetLastError(ERROR_INVALID_PARAMETER);
return -1;
}
return print_to_console_pos(pos, message, 1);
}
static inline int printf_to_console_pos(const COORD pos, const char *format, ...) {
va_list args;
va_start(args, format);
return vprintf_to_console_pos(pos, format, args);
}
static inline int printf_to_console_pos_clear(const COORD pos, const char *format, ...) {
va_list args;
va_start(args, format);
return vprintf_to_console_pos_clear(pos, format, args);
}
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* CONSOLE_HANDLER_H */