home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <ctype.h>
-
- float GetFloat(Default, field_width, places)
- float Default;
- int field_width, places;
-
- /*
- ---------------------------------------------------------------------------
-
- Last revision -
- 16 November 1984 - GWS
- Ignore XON, XOFF
-
- 11 April 1984 - GWS
-
-
- NAME
- GetFloat - "crash-proof" float from keyboard routine
-
- SYNOPSIS
- float GetFloat(Default, field_width, places)
- float Default;
- int field_width, places;
-
- DESCRIPTION
- On a good day this routine will get a real 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. Watch out for that minus sign!!
-
- Exponential notation is not supported.
-
- This version does not let you erase over the decimal point if
- the integer part of the field is already full. This is not a
- terrible hardship because you are free to ^X and start over,
- but it really should be fixed.
-
- AUTHOR
- George W. Sherouse
- 9 April 1984
-
- ---------------------------------------------------------------------------
- */
-
- {
- int c;
- float val;
- int loop;
- char line_buff[20];
- char Format[80];
- char erase, pad;
- int point, frac_count;
- int count;
- int cookie;
- int int_part;
-
- void underline();
- void f_clean_up();
- char TermSetUp();
- int tgetnum();
- void TermRewind();
-
- pad = ' ';
- point = 0;
- frac_count = 0 ;
- if ((cookie = tgetnum("ug")) < 0)
- cookie = 0;
-
- underline(1);
-
- for (loop = 0; loop < field_width; loop++)
- printf(" ");
- if (cookie)
- {
- underline(0);
- TermRewind(cookie);
- }
- TermRewind(field_width);
-
- sprintf(Format, "%%%d.%df", field_width, places);
- printf(Format, Default);
- TermRewind(field_width);
- for (loop = 0; loop <= field_width; loop++)
- line_buff[loop] = 0;
-
- erase = TermSetUp(); /* set no echo, single char input */
- /* get erase character */
- count = 0;
- int_part = field_width - places - 1;
- while (1)
- {
- if (!point && count == int_part)
- {
- printf(".");
- line_buff[count++] = '.';
- point++;
- continue;
- }
-
- switch (c = (getchar() & 0177))
- {
- case '\015':
- if (count && line_buff[count - 1] != '-')
- {
- sscanf(line_buff, "%f", &val);
- f_clean_up(count, field_width, Format, val);
- return(val);
- }
- else
- {
- f_clean_up(count, field_width, Format, Default);
- return(Default);
- }
- break;
- case 030:
- TermRewind(count);
- for (loop = 0; loop < field_width; loop++)
- line_buff[loop] = 0;
- count = frac_count = point = 0;
- printf(Format, Default);
- TermRewind(field_width);
- break;
- case '.':
- if (!point)
- {
- if (!count)
- {
- for (loop = 0; loop < field_width; loop++)
- printf("%c", pad);
- TermRewind(field_width);
- for (loop = 0; loop < field_width; loop++)
- line_buff[loop] = 0;
- }
- printf(".");
- line_buff[count++] = '.';
- point++;
- break;
- }
- case '\021':
- case '\023':
- break;
- default:
- if (c == erase && count)
- {
- printf("\b%c\b", pad);
- if (point)
- {
- if (frac_count)
- frac_count--;
- else
- point--;
- }
- line_buff[--count] = 0;
- break;
- }
-
- if (!count && c == '-')
- {
- for (loop = 0; loop < field_width; loop++)
- printf("%c", pad);
- TermRewind(field_width);
- for (loop = 0; loop < field_width; loop++)
- line_buff[loop] = 0;
-
- line_buff[count++] = (char) c;
- printf("-");
- break;
- }
-
- if (isdigit(c) && count < field_width && frac_count < places)
- {
- if (!count)
- {
- for (loop = 0; loop < field_width; loop++)
- printf("%c", pad);
- TermRewind(field_width);
- for (loop = 0; loop < field_width; loop++)
- line_buff[loop] = 0;
- }
- printf("%c", c);
- line_buff[count++] = (char) c;
- if (point)
- frac_count++;
- }
- else
- printf("%c", '\007');
- }
- }
- }
-
- void f_clean_up(count, field_width, Format, val)
- int count, field_width;
- char *Format;
- float val;
-
- {
- int loop;
- void underline();
- char TermSetUp();
- void TermRewind();
-
- TermRewind(count);
- printf(Format, val);
-
- underline(0);
- (void) TermSetUp();
- }
-