-
Notifications
You must be signed in to change notification settings - Fork 1
/
conio.c
220 lines (174 loc) · 4.41 KB
/
conio.c
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
/* A conio implementation for Mingw/Dev-C++.
*
* Written by:
* Hongli Lai <hongli@telekabel.nl>
* tkorrovi <tkorrovi@altavista.net> on 2002/02/26.
* Andrew Westcott <ajwestco@users.sourceforge.net>
*
* Offered for use in the public domain without any warranty.
*/
#ifndef _CONIO_C_
#define _CONIO_C_
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <windows.h>
#include <string.h>
#include "conio.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum
{
BLACK,
BLUE,
GREEN,
CYAN,
RED,
MAGENTA,
BROWN,
LIGHTGRAY,
DARKGRAY,
LIGHTBLUE,
LIGHTGREEN,
LIGHTCYAN,
LIGHTRED,
LIGHTMAGENTA,
YELLOW,
WHITE
} COLORS;
static int __BACKGROUND = BLACK;
static int __FOREGROUND = LIGHTGRAY;
void
clrscr ()
{
DWORD written;
FillConsoleOutputAttribute (GetStdHandle (STD_OUTPUT_HANDLE),
__FOREGROUND + (__BACKGROUND << 4), 2000, (COORD) {0, 0},
&written);
FillConsoleOutputCharacter (GetStdHandle
(STD_OUTPUT_HANDLE), ' ',
2000, (COORD) {0, 0}, &written);
gotoxy (1, 1);
}
void
clreol ()
{
COORD coord;
DWORD written;
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE),
&info);
coord.X = info.dwCursorPosition.X;
coord.Y = info.dwCursorPosition.Y;
FillConsoleOutputCharacter (GetStdHandle (STD_OUTPUT_HANDLE),
' ', info.dwSize.X - info.dwCursorPosition.X, coord, &written);
gotoxy (coord.X, coord.Y);
}
void
delline()
{
COORD coord;
DWORD written;
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE),
&info);
coord.X = info.dwCursorPosition.X;
coord.Y = info.dwCursorPosition.Y;
FillConsoleOutputCharacter (GetStdHandle (STD_OUTPUT_HANDLE),
' ', info.dwSize.X * info.dwCursorPosition.Y, coord, &written);
gotoxy (info.dwCursorPosition.X + 1,
info.dwCursorPosition.Y + 1);
}
int
_conio_gettext (int left, int top, int right, int bottom,
char *str)
{
int i, j, n;
SMALL_RECT r;
CHAR_INFO buffer[25][80];
r = (SMALL_RECT) {left - 1, top - 1, right - 1, bottom - 1};
ReadConsoleOutput (GetStdHandle (STD_OUTPUT_HANDLE),
(PCHAR_INFO) buffer, (COORD) {80, 25}, (COORD) {0, 0}, &r);
lstrcpy (str, "");
for (i = n = 0; i <= bottom - top; i++)
for (j = 0; j <= right - left; j++)
{
str[n] = buffer[i][j].Char.AsciiChar;
n++;
}
str[n] = 0;
return 1;
}
int gotoxy(int x, int y)
{
COORD c;
c.X = x - 1;
c.Y = y - 1;
SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
return 1;
}
void
puttext (int left, int top, int right, int bottom, char *str)
{
int i, j, n;
SMALL_RECT r;
CHAR_INFO buffer[25][80];
memset (buffer, 0, sizeof (buffer));
r = (SMALL_RECT) {left - 1, top - 1, right - 1, bottom - 1};
for (i = n = 0; i <= bottom - top; i++)
for (j = 0; j <= right - left && str[n] != 0; j++)
{
buffer[i][j].Char.AsciiChar = str[n];
buffer[i][j].Attributes = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
n++;
}
WriteConsoleOutput (GetStdHandle (STD_OUTPUT_HANDLE),
(CHAR_INFO *) buffer, (COORD) {80, 25},
(COORD) {0, 0}, &r);
}
void
_setcursortype (int type)
{
CONSOLE_CURSOR_INFO Info;
Info.dwSize = type;
SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE),
&Info);
}
void
textattr (int _attr)
{
SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), _attr);
}
void
textbackground (int color)
{
__BACKGROUND = color;
SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),
__FOREGROUND + (color << 4));
}
void
textcolor (int color)
{
__FOREGROUND = color;
SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),
color + (__BACKGROUND << 4));
}
int
wherex ()
{
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
return info.dwCursorPosition.X + 1;
}
int
wherey ()
{
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
return info.dwCursorPosition.Y + 1;
}
#ifdef __cplusplus
}
#endif
#endif /* _CONIO_C_ */