home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <ctype.h>
- #include <curses.h>
- #include "c_term.h"
-
- float get_float(Default, field_width, places)
- float Default;
- int field_width, places;
-
- /*
- ---------------------------------------------------------------------------
-
- Last revision -
- 6 January 1985 - GWS
- Make it use curses
-
- 16 November 1984 - GWS
- Ignore XON, XOFF
-
- 11 April 1984 - GWS
-
-
- NAME
- get_float - "crash-proof" float from keyboard routine
-
- SYNOPSIS
- float get_float(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 pad;
- int point, frac_count;
- int count;
- int int_part;
- int where_x,
- where_y,
- x,
- y;
-
- void f_clean_up();
-
- pad = ' ';
- point = 0;
- frac_count = 0 ;
-
- getyx(stdscr, where_y, where_x);
- standout();
-
- for (loop = 0; loop < field_width; loop++)
- printw(" ");
-
- move(where_y, where_x);
- refresh();
-
- sprintf(Format, "%%%d.%df", field_width, places);
- printw(Format, Default);
- move(where_y, where_x);
- refresh();
- for (loop = 0; loop <= field_width; loop++)
- line_buff[loop] = 0;
-
- count = 0;
- int_part = field_width - places - 1;
- while (1)
- {
- if (!point && count == int_part)
- {
- printw(".");
- line_buff[count++] = '.';
- point++;
- continue;
- }
- refresh();
-
- c = (getch() & 0x7f);
- switch(c)
- {
- #ifdef ABORT_CHAR
- case ABORT_CHAR:
- clear();
- standend();
- mvprintw(0, 0, "Program aborted at your request...");
- move(LINES -1 , 0);
- refresh();
- endwin();
- exit(1);
- break;
- #endif ABORT_CHAR
-
- case '\r':
- if (count && line_buff[count - 1] != '-')
- {
- sscanf(line_buff, "%f", &val);
- f_clean_up(where_y, where_x, Format, val);
- return(val);
- }
- else
- {
- f_clean_up(where_y, where_x, Format, Default);
- return(Default);
- }
- break;
- case '\030':
- move(where_y, where_x);
- for (loop = 0; loop < field_width; loop++)
- line_buff[loop] = 0;
- count = frac_count = point = 0;
- printw(Format, Default);
- move(where_y, where_x);
- break;
- case '.':
- if (!point)
- {
- 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(".");
- line_buff[count++] = '.';
- point++;
- break;
- }
- case '\021':
- case '\023':
- break;
- default:
- if (c == erase_char && count)
- {
- getyx(stdscr, y, x);
- move(y, x - 1);
- printw("%c", pad);
- move(y, x - 1);
- if (point)
- {
- if (frac_count)
- frac_count--;
- else
- point--;
- }
- 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 && frac_count < places)
- {
- 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;
- if (point)
- frac_count++;
- }
- else
- fprintf(stderr, "%c", '\007');
- }
- }
- }
-
- void f_clean_up(where_y, where_x, Format, val)
- int where_y, where_x;
- char *Format;
- float val;
-
- {
- int loop;
-
- standend();
- move(where_y, where_x);
- printw(Format, val);
-
- refresh();
- }
-