-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwconsole.h
More file actions
316 lines (263 loc) · 6.85 KB
/
Copy pathwconsole.h
File metadata and controls
316 lines (263 loc) · 6.85 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
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
#ifndef _INC_WCONSOLE_
#define _INC_WCONSOLE_
#include <windows.h>
#include <wsync.h>
#if _WIN32_WINNT < 0x0500
HWND WINAPI GetConsoleWindow();
#endif
#ifdef __cplusplus
#ifndef EXTERN_C
# define EXTERN_C extern "C"
#endif
#ifdef _MSC_VER
# pragma comment(lib, "winstrcp")
#endif
class WConsoleInterface
{
public:
virtual HANDLE GetHandle() const = 0;
virtual HANDLE SetHandle(HANDLE hNewCon) = 0;
bool SetMode(DWORD dwConMode) const
{
DWORD dwOldConMode = GetMode();
return SetConsoleMode(GetHandle(), dwOldConMode | dwConMode) == TRUE;
}
bool UnsetMode(DWORD dwConMode) const
{
DWORD dwOldConMode = GetMode();
return SetConsoleMode(GetHandle(), dwOldConMode & ~dwConMode) == TRUE;
}
DWORD GetMode() const
{
DWORD dwConMode;
if (GetConsoleMode(GetHandle(), &dwConMode))
return dwConMode;
else
return 0;
}
void ClrEOL()
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE hConOut = GetHandle();
LPSTR cBlankStr;
DWORD dwWriteLength;
if (!GetConsoleScreenBufferInfo(hConOut, &csbi))
return;
dwWriteLength = csbi.dwSize.X - csbi.dwCursorPosition.X;
cBlankStr = (LPSTR)HeapAlloc(GetProcessHeap(), 0, dwWriteLength);
if (cBlankStr == (LPSTR)NULL)
return;
FillMemory(cBlankStr, dwWriteLength, ' ');
WriteConsoleOutputCharacterA(hConOut, cBlankStr, dwWriteLength,
csbi.dwCursorPosition, &dwWriteLength);
HeapFree(GetProcessHeap(), 0, (LPVOID)cBlankStr);
}
void GotoXY(SHORT x, SHORT y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetHandle(), coord);
}
};
class WConsole : public WSynchronizer, public WConsoleInterface
{
public:
HANDLE GetHandle() const
{
return hObject;
}
HANDLE SetHandle(HANDLE hNewCon)
{
Close();
hObject = hNewCon;
return hObject;
}
BOOL Open(LPCTSTR pstrNewCon)
{
Close();
hObject = CreateFile(pstrNewCon, GENERIC_READ|GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, 0, NULL);
return hObject != INVALID_HANDLE_VALUE;
}
WConsole & operator=(LPCTSTR pstrNewCon)
{
Open(pstrNewCon);
return *this;
}
WConsole & operator=(const WConsoleInterface & wcb)
{
Close();
WKernelObject::operator=(wcb.GetHandle());
return *this;
}
explicit WConsole(LPCTSTR pstrNewCon)
: WSynchronizer(INVALID_HANDLE_VALUE)
{
Open(pstrNewCon);
}
explicit WConsole(const WConsoleInterface & wc)
: WSynchronizer(INVALID_HANDLE_VALUE)
{
HANDLE hNew;
if (DuplicateHandle(GetCurrentProcess(), wc.GetHandle(),
GetCurrentProcess(), &hNew, 0, true, DUPLICATE_SAME_ACCESS))
SetHandle(hNew);
}
};
extern const class WConsoleStdIn : public WConsoleInterface
{
HANDLE GetHandle() const
{
return GetStdHandle(STD_INPUT_HANDLE);
}
HANDLE SetHandle(HANDLE hCon)
{
SetStdHandle(STD_INPUT_HANDLE, hCon);
return GetStdHandle(STD_INPUT_HANDLE);
}
} wcStdIn;
extern const class WConsoleStdOut : public WConsoleInterface
{
HANDLE GetHandle() const
{
return GetStdHandle(STD_OUTPUT_HANDLE);
}
HANDLE SetHandle(HANDLE hCon)
{
SetStdHandle(STD_OUTPUT_HANDLE, hCon);
return GetStdHandle(STD_OUTPUT_HANDLE);
}
} wcStdOut;
extern const class WConsoleStdErr : public WConsoleInterface
{
HANDLE GetHandle() const
{
return GetStdHandle(STD_ERROR_HANDLE);
}
HANDLE SetHandle(HANDLE hCon)
{
SetStdHandle(STD_ERROR_HANDLE, hCon);
return GetStdHandle(STD_ERROR_HANDLE);
}
} wcStdErr;
struct WConsoleCursorInfo : public CONSOLE_CURSOR_INFO
{
BOOL Get(HANDLE hConsoleOutput)
{
return GetConsoleCursorInfo(hConsoleOutput, this);
}
BOOL Get(const WConsoleInterface *wcbConsoleOutput)
{
return GetConsoleCursorInfo(wcbConsoleOutput->GetHandle(), this);
}
BOOL Get()
{
HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
if (hConsoleOutput == INVALID_HANDLE_VALUE)
return false;
else
return GetConsoleCursorInfo(hConsoleOutput, this);
}
BOOL Set(HANDLE hConsoleOutput) const
{
return SetConsoleCursorInfo(hConsoleOutput, this);
}
BOOL Set(const WConsoleInterface *wcbConsoleOutput)
{
return SetConsoleCursorInfo(wcbConsoleOutput->GetHandle(), this);
}
BOOL Set() const
{
HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
if (hConsoleOutput == INVALID_HANDLE_VALUE)
return false;
else
return SetConsoleCursorInfo(hConsoleOutput, this);
}
WConsoleCursorInfo(CONST CONSOLE_CURSOR_INFO &cci)
{
*(CONSOLE_CURSOR_INFO*)this = cci;
}
WConsoleCursorInfo()
{
if (!Get())
ZeroMemory(this, sizeof *this);
}
WConsoleCursorInfo(HANDLE hConsoleOutput)
{
if (!Get(hConsoleOutput))
ZeroMemory(this, sizeof *this);
}
WConsoleCursorInfo(const WConsoleInterface *wcbConsoleOutput)
{
if (!Get(wcbConsoleOutput))
ZeroMemory(this, sizeof *this);
}
};
struct WConsoleScreenBufferInfo : public CONSOLE_SCREEN_BUFFER_INFO
{
BOOL Get(HANDLE hConsoleOutput)
{
return GetConsoleScreenBufferInfo(hConsoleOutput, this);
}
BOOL Get(const WConsoleInterface *wcbConsoleOutput)
{
return GetConsoleScreenBufferInfo(wcbConsoleOutput->GetHandle(), this);
}
BOOL Get()
{
HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
if (hConsoleOutput == INVALID_HANDLE_VALUE)
return false;
else
return GetConsoleScreenBufferInfo(hConsoleOutput, this);
}
/*
BOOL Set(HANDLE hConsoleOutput) const
{
return SetConsoleScreenBufferInfo(hConsoleOutput, this);
}
BOOL Set(const WConsoleInterface *wcbConsoleOutput)
{
return SetConsoleScreenBufferInfo(wcbConsoleOutput->GetHandle(), this);
}
BOOL Set() const
{
HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
if (hConsoleOutput == INVALID_HANDLE_VALUE)
return false;
else
return SetConsoleScreenBufferInfo(hConsoleOutput, this);
}
*/
WConsoleScreenBufferInfo(CONST CONSOLE_SCREEN_BUFFER_INFO &cci)
{
*(CONSOLE_SCREEN_BUFFER_INFO*)this = cci;
}
WConsoleScreenBufferInfo()
{
if (!Get())
ZeroMemory(this, sizeof *this);
}
WConsoleScreenBufferInfo(HANDLE hConsoleOutput)
{
if (!Get(hConsoleOutput))
ZeroMemory(this, sizeof *this);
}
WConsoleScreenBufferInfo(const WConsoleInterface *wcbConsoleOutput)
{
if (!Get(wcbConsoleOutput))
ZeroMemory(this, sizeof *this);
}
};
#else // __cplusplus
#pragma comment(lib, "winstrct")
#ifndef EXTERN_C
# define EXTERN_C
#endif
#endif // __cplusplus
EXTERN_C void clreol();
EXTERN_C void clrscr();
EXTERN_C void gotoxy(SHORT x, SHORT y);
#endif // _INC_WCONSOLE_