-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen.c
284 lines (225 loc) · 7.62 KB
/
screen.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
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
/**************************************************************************
DOSBase Version 1.5 Final
Copyright (C)1995-2000 Cliny Kennedy
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
DOSBase Website: http://revolutionaudio.dhs.org/dosbase
**************************************************************************/
/* Miscellaneous Screen-Interface Functions */
/****************************************************************************
Most of these functions were written in order to port the code from
Borland 5 to Visual C++ 6.0 and use win32 console-related API calls to
implement features which were natively supported under Borland 5,
such as db_gotoxy() & color printing. This code is compatible with
standard stdio functions (printf, etc), which ultimately are implemented
by Windows calling WriteConsoleOutput(). I recommend viewing this module
under a DOS text-editor as it includes older ASCII line-drawing
characters that will not show up properly in a windows based IDE.
****************************************************************************/
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include "screen.h"
#include "defs.h"
extern int entries;
static void scrollBox(char *message); /* Scroll Contents Of Action Box */
HANDLE consoleScreen;
char contents[11][73]; /* Contents Of Action Box */
/* Initialize The Console */
void init_console(void)
{
consoleScreen = GetStdHandle(STD_OUTPUT_HANDLE);
}
/* Clears The Console Window */
void db_clrscr(void)
{
unsigned int i; /* Loop Counter */
db_textcolor(BLACK, BG_BLACK);
db_gotoxy(1, 1); /* Start at (1,1) */
for(i=0; i<25; i++)
printf("%80c", ' '); /* Flood The Screen With Spaces */
db_gotoxy(1, 1); /* Reposition Cursor To Home Position */
}
/* Sets the cursor position specified to (x, y) */
void db_gotoxy(short int x, short int y)
{
COORD coordinates;
coordinates.X = x-1; /* 0-based Offset */
coordinates.Y = y-1; /* O-based Offset */
SetConsoleCursorPosition(consoleScreen, coordinates);
}
/* Sets the foreground / background color */
void db_textcolor(unsigned short db_textcolor, unsigned short backcolor)
{
SetConsoleTextAttribute(consoleScreen, (unsigned short)(db_textcolor | backcolor));
}
/* Hides the cursor */
void HideCursor(void)
{
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(consoleScreen, &cursorInfo);
cursorInfo.bVisible = FALSE;
SetConsoleCursorInfo(consoleScreen, &cursorInfo);
}
/* Unhides the cursor */
void DisplayCursor(void)
{
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(consoleScreen, &cursorInfo);
cursorInfo.bVisible = TRUE;
SetConsoleCursorInfo(consoleScreen, &cursorInfo);
}
/****************************************************************************
This function writes to the main action box. It keeps track of current
position and automatically scrolls the box when needed.
****************************************************************************/
void writeAction(char *message)
{
static char row = 7; /* Max Row = 17 */
if (row == 18)
{
scrollBox(message); /* Scroll the contents of the buffer */
printBuffer(); /* Reprint the buffer */
}
else
{
db_gotoxy(5, row); /* Goto The Row To Print On */
strcpy(contents[row-7], message); /* Copy It Into The Contents Buffer */
row++;
db_textcolor(WHITE, BG_BLACK); /* Set Color To White */
printf("%s\n", message); /* Print The Message */
}
}
/****************************************************************************
The scrollBox() function scrolls the contents of the string buffer IF the
screen needs to be scrolled.
****************************************************************************/
static void scrollBox(char *message)
{
char row;
for (row = 0; row < 10; row++) /* Move Each Row Up */
{
strcpy(contents[row], contents[row+1]);
}
strcpy(contents[10], message); /* Attach The New Message */
}
/****************************************************************************
The printBuffer() routines displays the contents of the buffer to the
action box.
****************************************************************************/
void printBuffer(void)
{
char entry;
db_textcolor(WHITE, BG_BLACK); /* Prepare For Color Printing */
for (entry = 0; entry < 11; entry++) /* Print Contents Of Action Box */
{
db_gotoxy(5, (short int)(entry+7));
printf("%-73s\n", contents[entry]);
}
}
/* Increments Entry Counter On Main Interface */
void addToTotalCount(void)
{
entries++;
db_gotoxy(25, 4);
db_textcolor(YELLOW, BG_BLACK);
printf("%d", entries);
}
/****************************************************************************
getns() is used as a custom replacement for cgets(). It functions *exactly*
the same as cgets() or _cgets(), but it also looks for ESC for terminating
a dialog. It operates exactly like cgets(), but returns -1 in [1] of
inputBuffer if user hits escape.
****************************************************************************/
char* getns(char *inputBuffer)
{
int maxChar;
int charCount = 0;
char inChar;
maxChar = *(inputBuffer);
do {
/* Enter Input Loop */
while (charCount < maxChar)
{
inChar = _getch();
/* The following block of code checks for control keys, etc
and if found clears the keyboard buffer and attempts to
read another valid character until one is given. */
while (inChar == 0)
{
getch();
inChar = getch();
}
/* Check For ESC */
if (inChar == 0x1B)
{
inputBuffer[1] = -1;
inputBuffer[2] = '\0';
return NULL;
}
/* Check If Enter Was Pressed */
if (inChar == 0x0D)
{
*(inputBuffer + 2 + charCount) = '\0'; /* NULL out string */
inputBuffer[1] = charCount;
return &inputBuffer[2]; /* Input Completed */
}
/* Check If Backspace Was Pressed */
if (inChar == 0x08)
{
/* First confirm inputBuffer isn't already empty */
if (charCount != 0)
{
/* If Not, Clear Last Character As Requested */
printf("\b \b");
charCount--; /* Remove from inputBuffer */
}
}
/* If Niether Enter nor Backspace, Confirm Valid Input */
if((((isalnum(inChar) || ispunct(inChar)) && (!(iscntrl(inChar))))
|| isspace(inChar)))
{
*(inputBuffer + 2 + charCount) = inChar; /* Put in inuptBuffer */
charCount++; /* Increment Character Count */
printf("%c", inChar);
}
}
/* Wait For Enter Then Return */
inChar = getch();
while ((inChar != 0x0D) && (inChar != 0x08) && (inChar != 0x1B))
inChar = getch();
/* Check For ESC */
if (inChar == 0x1B)
{
inputBuffer[1] = -1;
inputBuffer[2] = '\0';
return NULL;
}
/* If ENTER is pressed */
if (inChar == 0x0D)
{
*(inputBuffer + 2 + charCount) = '\0'; /* NULL out string */
inputBuffer[1] = charCount;
return &inputBuffer[2];
}
else /* Backspace */
{
printf("\b \b");
charCount--; /* Remove from inputBuffer */
}
/* End Of Do-While */
} while (inChar == 0x08); /* If last character typed is backspace,
re-enter inupt loop */
return NULL; /* Unreachable, but avoids warnings */
}