home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume3 / lib_term / GetFloat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  4.3 KB  |  220 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. float GetFloat(Default, field_width, places)
  5. float Default;
  6. int field_width, places;
  7.  
  8. /*
  9.  ---------------------------------------------------------------------------
  10.  
  11.    Last revision - 
  12.     16 November 1984 - GWS
  13.     Ignore XON, XOFF
  14.  
  15.      11 April 1984 - GWS
  16.  
  17.  
  18.    NAME
  19.      GetFloat - "crash-proof" float from keyboard routine
  20.  
  21.    SYNOPSIS
  22.     float GetFloat(Default, field_width, places)
  23.     float Default;
  24.     int field_width, places;
  25.  
  26.    DESCRIPTION
  27.     On a good day this routine will get a real value from the
  28.     keyboard and return it safely.  The terminal is placed in raw
  29.     mode and most non-digit values are beeped at and discarded.  Entry
  30.     is terminated by filling the field or by CR.  CR as first character
  31.     assumes Default.  ^X restarts.
  32.  
  33.    SEE ALSO
  34.  
  35.  
  36.    DIAGNOSTICS
  37.     none - cannot fail :-) 
  38.  
  39.    BUGS
  40.     Doesn't check for silly things like Default too big to fit in
  41.     field_width, etc.  Watch out for that minus sign!!
  42.     
  43.     Exponential notation is not supported.
  44.  
  45.     This version does not let you erase over the decimal point if
  46.     the integer part of the field is already full.  This is not a
  47.     terrible hardship because you are free to ^X and start over,
  48.     but it really should be fixed.
  49.  
  50.    AUTHOR
  51.      George W. Sherouse
  52.      9 April 1984
  53.  
  54.  ---------------------------------------------------------------------------
  55. */
  56.  
  57. {
  58.     int c;
  59.     float val;
  60.     int loop;
  61.     char line_buff[20];
  62.     char Format[80];
  63.     char erase, pad;
  64.     int point, frac_count;
  65.     int count;
  66.     int cookie;
  67.     int int_part;
  68.  
  69.     void underline();
  70.     void f_clean_up();
  71.     char TermSetUp();
  72.     int tgetnum();
  73.     void TermRewind();
  74.  
  75.     pad = ' ';
  76.     point = 0;
  77.     frac_count = 0 ;
  78.     if ((cookie = tgetnum("ug")) < 0)
  79.         cookie = 0;
  80.  
  81.     underline(1);
  82.  
  83.     for (loop = 0; loop < field_width; loop++)
  84.         printf(" ");
  85.     if (cookie)
  86.     {
  87.         underline(0);
  88.         TermRewind(cookie);
  89.     }
  90.     TermRewind(field_width);
  91.  
  92.     sprintf(Format, "%%%d.%df", field_width, places);
  93.     printf(Format, Default);
  94.     TermRewind(field_width);
  95.     for (loop = 0; loop <= field_width; loop++)
  96.         line_buff[loop] = 0;
  97.  
  98.     erase = TermSetUp();    /* set no echo, single char input */
  99.                 /* get erase character */
  100.     count = 0;
  101.     int_part = field_width - places - 1;
  102.     while (1)
  103.     {
  104.         if (!point && count == int_part)
  105.         {
  106.         printf(".");
  107.         line_buff[count++] = '.';
  108.         point++;
  109.         continue;
  110.         }
  111.  
  112.         switch (c = (getchar() & 0177))
  113.         {
  114.         case '\015':
  115.         if (count && line_buff[count - 1] != '-')
  116.         {
  117.             sscanf(line_buff, "%f", &val);
  118.             f_clean_up(count, field_width, Format, val);
  119.             return(val);
  120.         }
  121.         else
  122.         {
  123.             f_clean_up(count, field_width, Format, Default);
  124.             return(Default);
  125.         }
  126.         break;
  127.         case 030:
  128.         TermRewind(count);
  129.         for (loop = 0; loop < field_width; loop++)
  130.             line_buff[loop] = 0;
  131.         count = frac_count = point = 0;
  132.         printf(Format, Default);
  133.         TermRewind(field_width);
  134.         break;
  135.         case '.':
  136.         if (!point)
  137.         {
  138.             if (!count)
  139.             {
  140.             for (loop = 0; loop < field_width; loop++)
  141.                 printf("%c", pad);
  142.             TermRewind(field_width);
  143.             for (loop = 0; loop < field_width; loop++)
  144.                 line_buff[loop] = 0;
  145.             }
  146.             printf(".");
  147.             line_buff[count++] = '.';
  148.             point++;
  149.             break;
  150.         }
  151.         case '\021':
  152.         case '\023':
  153.         break;
  154.         default:
  155.         if (c == erase && count)
  156.         {
  157.             printf("\b%c\b", pad);
  158.             if (point)
  159.             {
  160.             if (frac_count)
  161.                 frac_count--;
  162.             else
  163.                 point--;
  164.             }
  165.             line_buff[--count] = 0;
  166.             break;
  167.         }
  168.  
  169.         if (!count && c == '-')
  170.         {
  171.             for (loop = 0; loop < field_width; loop++)
  172.             printf("%c", pad);
  173.             TermRewind(field_width);
  174.             for (loop = 0; loop < field_width; loop++)
  175.                 line_buff[loop] = 0;
  176.  
  177.             line_buff[count++] = (char) c;
  178.             printf("-");
  179.             break;
  180.         }
  181.  
  182.         if (isdigit(c) && count < field_width && frac_count < places)
  183.         {
  184.             if (!count)
  185.             {
  186.             for (loop = 0; loop < field_width; loop++)
  187.                 printf("%c", pad);
  188.             TermRewind(field_width);
  189.             for (loop = 0; loop < field_width; loop++)
  190.                 line_buff[loop] = 0;
  191.             }
  192.             printf("%c", c);
  193.             line_buff[count++] = (char) c;
  194.             if (point)
  195.             frac_count++;
  196.         }
  197.         else
  198.             printf("%c", '\007');
  199.         }
  200.     }
  201. }
  202.  
  203. void f_clean_up(count, field_width, Format, val)
  204. int count, field_width;
  205. char *Format;
  206. float val;
  207.  
  208. {
  209.     int loop;
  210.     void underline();
  211.     char TermSetUp();
  212.     void TermRewind();
  213.  
  214.     TermRewind(count);
  215.     printf(Format, val);
  216.  
  217.     underline(0);
  218.     (void) TermSetUp();
  219. }
  220.