home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume3 / libc_term / get_int.c < prev    next >
C/C++ Source or Header  |  1986-11-30  |  3KB  |  174 lines

  1. #include <curses.h>
  2. #include <ctype.h>
  3. #include "c_term.h"
  4.  
  5. int get_int(Default, field_width)
  6. int Default, field_width;
  7.  
  8. /*
  9.  ---------------------------------------------------------------------------
  10.  
  11.    Last revision - 
  12.     6 January 1985 - GWS
  13.     Change to use curses
  14.  
  15.     16 November 1984 - GWS
  16.     Ignore XON, XOFF
  17.  
  18.      11 April 1984 - GWS
  19.  
  20.  
  21.    NAME
  22.      get_int - "crash-proof" integer from keyboard routine
  23.  
  24.    SYNOPSIS
  25.     int get_int(Default, field_width)
  26.     int Default, field_width;
  27.  
  28.    DESCRIPTION
  29.     On a good day this routine will get an integer value from the
  30.     keyboard and return it safely.  The terminal is placed in raw
  31.     mode and most non-digit values are beeped at and discarded.  Entry
  32.     is terminated by filling the field or by CR.  CR as first character
  33.     assumes Default.  ^X restarts.
  34.  
  35.    SEE ALSO
  36.  
  37.    DIAGNOSTICS
  38.     none - cannot fail :-) 
  39.  
  40.    BUGS
  41.     Doesn't check for silly things like Default too big to fit in
  42.     field_width, etc.  Let's be particularly careful out there.
  43.  
  44.    AUTHOR
  45.      George W. Sherouse
  46.      6 April 1984
  47.  
  48.  ---------------------------------------------------------------------------
  49. */
  50.  
  51. {
  52.     int c, val;
  53.     int loop;
  54.     char line_buff[20];
  55.     char Format[80];
  56.     char pad;
  57.     int count;
  58.     int    where_x,
  59.         where_y,
  60.         x,
  61.         y;
  62.  
  63.     void clean_up();
  64.  
  65.     pad = ' ';
  66.     getyx(stdscr, where_y, where_x);
  67.     standout();
  68.  
  69.     for (loop = 0; loop < field_width; loop++)
  70.         printw(" ");
  71.  
  72.     sprintf(Format, "%%%dd", field_width);
  73.     mvprintw(where_y, where_x, Format, Default);
  74.     move(where_y, where_x);
  75.     refresh();
  76.     for (loop = 0; loop <= field_width; loop++)
  77.         line_buff[loop] = 0;
  78.  
  79.     count = 0;
  80.     while (1)
  81.     {
  82.         switch (c = (getch() & 0177))
  83.         {
  84. #ifdef ABORT_CHAR
  85.         case ABORT_CHAR:
  86.         clear();
  87.         standend();
  88.         mvprintw(0, 0, "Program aborted at your request...");
  89.         move(LINES - 1, 0);
  90.         refresh();
  91.         endwin();
  92.         exit(0);
  93.         break;
  94. #endif ABORT_CHAR
  95.  
  96.         case '\015':
  97.         if (count && line_buff[count - 1] != '-')
  98.         {
  99.             sscanf(line_buff, "%d", &val);
  100.             clean_up(where_y, where_x, Format, val);
  101.             return(val);
  102.         }
  103.         else
  104.         {
  105.             clean_up(where_y, where_x, Format, Default);
  106.             return(Default);
  107.         }
  108.         break;
  109.         case 030:
  110.         for (loop = 0; loop < field_width; loop++)
  111.             line_buff[loop] = 0;
  112.         count = 0;
  113.         mvprintw(where_y, where_x, Format, Default);
  114.         move(where_y, where_x);
  115.         break;
  116.         case '\021':
  117.         case '\023':
  118.         break;
  119.         default:
  120.         if (c == erase_char && count)
  121.         {
  122.             getyx(stdscr, y, x);
  123.             mvprintw(y, x - 1, "%c", pad);
  124.             move(y, x - 1);
  125.             line_buff[--count] = 0;
  126.             break;
  127.         }
  128.  
  129.         if (!count && c == '-')
  130.         {
  131.             for (loop = 0; loop < field_width; loop++)
  132.             printw("%c", pad);
  133.             move(where_y, where_x);
  134.             for (loop = 0; loop < field_width; loop++)
  135.                 line_buff[loop] = 0;
  136.  
  137.             line_buff[count++] = (char) c;
  138.             printw("-");
  139.             break;
  140.         }
  141.  
  142.         if (isdigit(c) && count < field_width)
  143.         {
  144.             if (!count)
  145.             {
  146.             for (loop = 0; loop < field_width; loop++)
  147.                 printw("%c", pad);
  148.             move(where_y, where_x);
  149.             for (loop = 0; loop < field_width; loop++)
  150.                 line_buff[loop] = 0;
  151.             }
  152.             printw("%c", c);
  153.             line_buff[count++] = (char) c;
  154.         }
  155.         else
  156.             fprintf(stderr, "%c", '\007');
  157.         }
  158.     refresh();
  159.     }
  160. }
  161.  
  162. void clean_up(where_y, where_x, Format, val)
  163. int where_y, where_x;
  164. char *Format;
  165. int val;
  166.  
  167. {
  168.     int loop;
  169.  
  170.     standend();
  171.     mvprintw(where_y, where_x, Format, val);
  172.     refresh();
  173. }
  174.