home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
unix
/
volume3
/
libc_term
/
get_int.c
< prev
next >
Wrap
C/C++ Source or Header
|
1986-11-30
|
3KB
|
174 lines
#include <curses.h>
#include <ctype.h>
#include "c_term.h"
int get_int(Default, field_width)
int Default, field_width;
/*
---------------------------------------------------------------------------
Last revision -
6 January 1985 - GWS
Change to use curses
16 November 1984 - GWS
Ignore XON, XOFF
11 April 1984 - GWS
NAME
get_int - "crash-proof" integer from keyboard routine
SYNOPSIS
int get_int(Default, field_width)
int Default, field_width;
DESCRIPTION
On a good day this routine will get an integer value from the
keyboard and return it safely. The terminal is placed in raw
mode and most non-digit values are beeped at and discarded. Entry
is terminated by filling the field or by CR. CR as first character
assumes Default. ^X restarts.
SEE ALSO
DIAGNOSTICS
none - cannot fail :-)
BUGS
Doesn't check for silly things like Default too big to fit in
field_width, etc. Let's be particularly careful out there.
AUTHOR
George W. Sherouse
6 April 1984
---------------------------------------------------------------------------
*/
{
int c, val;
int loop;
char line_buff[20];
char Format[80];
char pad;
int count;
int where_x,
where_y,
x,
y;
void clean_up();
pad = ' ';
getyx(stdscr, where_y, where_x);
standout();
for (loop = 0; loop < field_width; loop++)
printw(" ");
sprintf(Format, "%%%dd", field_width);
mvprintw(where_y, where_x, Format, Default);
move(where_y, where_x);
refresh();
for (loop = 0; loop <= field_width; loop++)
line_buff[loop] = 0;
count = 0;
while (1)
{
switch (c = (getch() & 0177))
{
#ifdef ABORT_CHAR
case ABORT_CHAR:
clear();
standend();
mvprintw(0, 0, "Program aborted at your request...");
move(LINES - 1, 0);
refresh();
endwin();
exit(0);
break;
#endif ABORT_CHAR
case '\015':
if (count && line_buff[count - 1] != '-')
{
sscanf(line_buff, "%d", &val);
clean_up(where_y, where_x, Format, val);
return(val);
}
else
{
clean_up(where_y, where_x, Format, Default);
return(Default);
}
break;
case 030:
for (loop = 0; loop < field_width; loop++)
line_buff[loop] = 0;
count = 0;
mvprintw(where_y, where_x, Format, Default);
move(where_y, where_x);
break;
case '\021':
case '\023':
break;
default:
if (c == erase_char && count)
{
getyx(stdscr, y, x);
mvprintw(y, x - 1, "%c", pad);
move(y, x - 1);
line_buff[--count] = 0;
break;
}
if (!count && c == '-')
{
for (loop = 0; loop < field_width; loop++)
printw("%c", pad);
move(where_y, where_x);
for (loop = 0; loop < field_width; loop++)
line_buff[loop] = 0;
line_buff[count++] = (char) c;
printw("-");
break;
}
if (isdigit(c) && count < field_width)
{
if (!count)
{
for (loop = 0; loop < field_width; loop++)
printw("%c", pad);
move(where_y, where_x);
for (loop = 0; loop < field_width; loop++)
line_buff[loop] = 0;
}
printw("%c", c);
line_buff[count++] = (char) c;
}
else
fprintf(stderr, "%c", '\007');
}
refresh();
}
}
void clean_up(where_y, where_x, Format, val)
int where_y, where_x;
char *Format;
int val;
{
int loop;
standend();
mvprintw(where_y, where_x, Format, val);
refresh();
}