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
|
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
struct employeeInfo
{
char* depCode;
char* empNr;
char* firstName;
char* lastName;
int age;
};
/* ---------------------------------------------------------------------------
* This function is basically the Windows way to do isatty().
*/
int isconsole( HANDLE handle )
{
DWORD foo;
return GetConsoleMode( handle, &foo ) != 0;
}
/* ---------------------------------------------------------------------------
* Returns the number of lines and columns currently VISIBLE.
*/
void windowsize(
int* lines,
int* columns
) {
HANDLE hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
if (isconsole( hStdOut ))
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo( hStdOut, &csbi );
*lines = csbi.srWindow.Bottom - csbi.srWindow.Top +1;
*columns = csbi.srWindow.Right - csbi.srWindow.Left +1;
}
else *lines = *columns = 0;
}
/* ---------------------------------------------------------------------------
* Return the number of lines currently VISIBLE.
*/
int linecount()
{
int rows, cols;
windowsize( &rows, &cols );
return rows;
}
/* ---------------------------------------------------------------------------
* Clear the entire window and home the cursor.
*/
void clear()
{
HANDLE hStdOut;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cellCount;
COORD homeCoords = { 0, 0 };
hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
if (hStdOut == INVALID_HANDLE_VALUE) return;
/* Get the number of cells in the current buffer */
if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
cellCount = csbi.dwSize.X *csbi.dwSize.Y;
/* Fill the entire buffer with spaces */
if (!FillConsoleOutputCharacter(
hStdOut,
(TCHAR) ' ',
cellCount,
homeCoords,
&count
)) return;
/* Fill the entire buffer with the current colors and attributes */
if (!FillConsoleOutputAttribute(
hStdOut,
csbi.wAttributes,
cellCount,
homeCoords,
&count
)) return;
/* Move the cursor home */
SetConsoleCursorPosition( hStdOut, homeCoords );
}
/* ---------------------------------------------------------------------------
* Wait for the user to "Press a key to continue...".
* Returns the Virtual Key Code of the key that was pressed.
*/
int pressanykey( const char* prompt )
{
DWORD mode;
HANDLE hStdIn;
INPUT_RECORD inrec;
DWORD count;
char default_prompt[] = "Press a key to continue...";
/* Set the console mode to no-echo, raw input, */
/* and no window or mouse events. */
hStdIn = GetStdHandle( STD_INPUT_HANDLE );
if (!GetConsoleMode( hStdIn, &mode )
|| !SetConsoleMode( hStdIn, 0 ))
return;
if (!prompt) prompt = default_prompt;
/* Instruct the user */
WriteConsole(
GetStdHandle( STD_OUTPUT_HANDLE ),
prompt,
lstrlen( prompt ),
&count,
NULL
);
/* Clear all input and wait for a key press */
FlushConsoleInputBuffer( hStdIn );
WaitForSingleObject( hStdIn, INFINITE );
/* Wait for and get a single key RELEASE */
do ReadConsoleInput( hStdIn, &inrec, 1, &count );
while ((inrec.EventType != KEY_EVENT) && inrec.Event.KeyEvent.bKeyDown);
/* Restore the original console mode */
SetConsoleMode( hStdIn, mode );
return inrec.Event.KeyEvent.wVirtualKeyCode;
}
/* ------------------------------------------------------------------------ */
void list_employees(
struct employeeInfo employee_info[],
int employee_count
) {
int lines = linecount() - 1;
int n;
for (n = 0; n < employee_count; n++)
{
printf(
"%-6s %-12s %s %s %d\n",
employee_info[ n ].depCode,
employee_info[ n ].empNr,
employee_info[ n ].firstName,
employee_info[ n ].lastName,
employee_info[ n ].age
);
if ((n != 0) && ((n % lines) == 0))
{
int key = pressanykey( "Press ENTER to continue, ESC to quit" );
printf( "\n" );
if (key == VK_ESCAPE) return;
}
}
}
/* ------------------------------------------------------------------------ */
int main()
{
#define NUM_EMPLOYEES 300
struct employeeInfo employees[ NUM_EMPLOYEES ];
int n;
for (n = 0; n < NUM_EMPLOYEES; n++)
{
employees[ n ].depCode = "ITCS";
employees[ n ].empNr = "000-00-0000";
employees[ n ].firstName = "John";
employees[ n ].lastName = "Doe";
employees[ n ].age = n;
}
list_employees( employees, NUM_EMPLOYEES );
return 0;
}
|