home *** CD-ROM | disk | FTP | other *** search
- /*
- fndouble.c 10/14/86
-
- % double_funcs
-
- Regular Double funcs.
- The field variable should be a double *.
-
- C-scape 3.1
- Copyright (c) 1986, 1987, 1988 by Oakland Group, Inc.
- ALL RIGHTS RESERVED.
-
- Revision History:
- -----------------
- 10/01/87 jmd added casting
- 4/06/88 jmd added call to sed_DoSpecial
- 5/12/88 jmd added calls to sed_GetScratchPad()
- 9/17/88 jmd added std_ funcs
- 10/09/88 jmd added SED_ABORT support
- 10/14/88 jdc added var_size element to field_funcs_struct
- 12/16/88 jmd added validation
-
- 6/01/89 gam added ocountry stuff
- 6/07/89 jmd added test for mouse code (later removed)
- */
-
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
-
- #include "cscape.h"
- #include "fnfunc.h" /* for field functions */
- #include "strdecl.h" /* for C-scape string functions */
- #include "scancode.h"
-
- OSTATIC int mantissa_pos(_arg1(char *));
-
- /* operating modes (stored in sed baton) */
-
- #define MANTISSA 1 /* SED_FIRST is also treated as MANTISSA */
- #define EXPONENT 2
-
- OGLOBAL field_funcs_struct double_funcs = {
- double_fenter,
- double_fexit,
- double_fkey,
- double_senter,
- double_sexit,
- sizeof(double)
- };
-
- void double_fenter(sed)
- sed_type sed;
- {
- /* handle border prompt */
- std_fenter(sed);
-
- sed_GotoChar(sed, mantissa_pos(sed_GetCurrRecord(sed)));
- }
-
- boolean double_fexit(sed)
- sed_type sed;
- {
- double val;
-
- if (sed_GetBaton(sed) != SED_ABORT) {
-
- sscanf(sed_GetCurrRecord(sed), "%le", &val);
-
- /* call standard numeric validation routine (fnstdval.c) */
- if (!std_NumValid(sed, (double) val)) {
- return(FALSE);
- }
- }
-
- return(std_fexit(sed));
- }
-
- void double_fkey(sed)
- sed_type sed;
- {
- int scancode, key, mode;
- char c;
-
- scancode = kb_Read();
-
- if (sed_DoSpecial(sed, scancode))
- return;
- if (special_key(sed, scancode))
- return;
- if (inter_field(sed, scancode))
- return;
- if (inter_page(sed, scancode))
- return;
-
- mode = (sed_GetBaton(sed) == SED_FIRST) ? MANTISSA : sed_GetBaton(sed);
- if (mode == EXPONENT) {
- sed_GoEnd(sed);
- }
- else {
- sed_GotoChar(sed, mantissa_pos(sed_GetCurrRecord(sed)));
- }
-
- switch(scancode) {
- case BACKSPACE:
- sed_PullLeft(sed);
- if (digit_count(sed_GetCurrRecord(sed)) == 0) {
- sed_Overwrite(sed, '0');
- }
- break;
- default:
- key = ascii(scancode);
- if (isdigit(key)) {
- if (sed_GetBaton(sed) == SED_FIRST) {
- /* Clear field if first key pressed is a digit */
- sprintf(sed_GetScratchPad(sed), "%.le", 0.0);
- strright(sed_GetScratchPad(sed), sed_GetCurrRecordLen(sed));
- sed_SetCurrRecord(sed, sed_GetScratchPad(sed));
- sed_UpdateCurrField(sed);
- sed_GotoChar(sed, mantissa_pos(sed_GetCurrRecord(sed)));
- }
-
- if (mode == EXPONENT) {
- c = sed_Overwrite(sed, key);
- sed_DecChar(sed);
- sed_Overwrite(sed, c);
- sed_GoEnd(sed);
- }
- else {
- if (sed_GetChar(sed, 1) == ' ' || sed_GetChar(sed, 1) == '-'){
- if (sed_GetCurrChar(sed) == '0' &&
- digit_count(sed_GetCurrRecord(sed)) == 1) {
- sed_Overwrite(sed, key);
- }
- else {
- sed_PushLeft(sed, key);
- }
- }
- }
- }
-
- /* toggle minus sign if appropriate */
- else if (key == '-') {
- strcpy(sed_GetScratchPad(sed), sed_GetCurrRecord(sed));
- sed_SetCurrRecord(sed, strminus(sed_GetScratchPad(sed)));
- sed_UpdateCurrField(sed);
- }
-
- /* if no decimal point yet, add one */
-
- else if (key == ocountry.dec_char &&
- (sed_GetChar(sed, 1) == ' '|| sed_GetChar(sed, 1) == '-')) {
- if (strchr(sed_GetCurrRecord(sed), ocountry.dec_char) == NULL) {
- /* no decimal point */
- sed_PushLeft(sed, ocountry.dec_char);
- }
- }
- /* toggle EXP mode */
- else if (toupper(key) == 'E') {
- if (mode == EXPONENT) {
- mode = MANTISSA;
- sed_GotoChar(sed, mantissa_pos(sed_GetCurrRecord(sed)));
- }
- else {
- mode = EXPONENT;
- sed_GoEnd(sed);
- }
- }
- break;
- }
-
- /* reset baton */
- sed_SetBaton(sed, mode);
- }
-
-
- static int mantissa_pos(record)
- char *record;
- /*
- returns the record position of the mantissa
- i.e., the position before the 'e'.
- */
- {
- char *p;
- int pos;
-
- for (p = record, pos= 0; p[pos] != '\0' && p[pos] != 'e' && p[pos] != 'E'; pos++) {
- ;
- }
-
- return((pos > 0) ? (pos - 1) : 0);
- }
-
- void double_senter(sed, fieldno)
- sed_type sed;
- int fieldno;
- /*
- Convert native type to string for record.
- */
- {
- sprintf(sed_GetScratchPad(sed), "%.le", *((double *) sed_GetVar(sed, fieldno)));
-
- /* My change to change the decimal default from printf to
- the ocountry decimal character. */
- strtrans(sed_GetScratchPad(sed), '.', ocountry.dec_char);
- strright(sed_GetScratchPad(sed), sed_GetRecordLen(sed, fieldno));
- sed_SetRecord(sed, sed_GetScratchPad(sed), fieldno);
-
- std_senter(sed, fieldno);
- }
-
- void double_sexit(sed, fieldno)
- sed_type sed;
- int fieldno;
- /*
- Converts record back to native type.
- */
- {
- if (sed_GetBaton(sed) != SED_ABORT) {
- sscanf(strtrans(sed_GetRecord(sed, fieldno), ocountry.dec_char, '.'), "%le", (double *) sed_GetVar(sed, fieldno));
- }
- }
-
-