home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c185 / 1.ddi / FUNCS.EXE / CSCAPE / SOURCE / FNDOUBLE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-08  |  5.0 KB  |  224 lines

  1. /*
  2.     fndouble.c         10/14/86
  3.  
  4.     % double_funcs
  5.  
  6.     Regular Double funcs.
  7.     The field variable should be a double *.
  8.  
  9.     C-scape 3.1
  10.     Copyright (c) 1986, 1987, 1988 by Oakland Group, Inc.
  11.     ALL RIGHTS RESERVED.
  12.  
  13.     Revision History:
  14.     -----------------
  15.     10/01/87 jmd     added casting
  16.      4/06/88 jmd     added call to sed_DoSpecial
  17.      5/12/88 jmd    added calls to sed_GetScratchPad()
  18.      9/17/88 jmd     added std_ funcs
  19.     10/09/88 jmd     added SED_ABORT support
  20.     10/14/88 jdc    added var_size element to field_funcs_struct
  21.     12/16/88 jmd    added validation
  22.  
  23.      6/01/89 gam    added ocountry stuff
  24.      6/07/89 jmd    added test for mouse code (later removed)
  25. */
  26.  
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30.  
  31. #include "cscape.h"
  32. #include "fnfunc.h"            /* for field functions */
  33. #include "strdecl.h"        /* for C-scape string functions */
  34. #include "scancode.h"
  35.  
  36. OSTATIC int mantissa_pos(_arg1(char *));
  37.  
  38. /* operating modes (stored in sed baton) */
  39.  
  40. #define        MANTISSA        1       /* SED_FIRST is also treated as MANTISSA */
  41. #define        EXPONENT        2
  42.  
  43. OGLOBAL field_funcs_struct double_funcs = {
  44.     double_fenter,
  45.     double_fexit,
  46.     double_fkey,
  47.     double_senter,
  48.     double_sexit,
  49.     sizeof(double)
  50. };
  51.  
  52. void double_fenter(sed)
  53.     sed_type sed;
  54. {
  55.     /* handle border prompt */
  56.     std_fenter(sed);
  57.  
  58.     sed_GotoChar(sed, mantissa_pos(sed_GetCurrRecord(sed)));
  59. }
  60.  
  61. boolean double_fexit(sed)
  62.     sed_type sed;
  63. {
  64.     double val;
  65.  
  66.     if (sed_GetBaton(sed) != SED_ABORT) {
  67.  
  68.         sscanf(sed_GetCurrRecord(sed), "%le", &val);
  69.  
  70.         /* call standard numeric validation routine (fnstdval.c) */
  71.         if (!std_NumValid(sed, (double) val)) {
  72.             return(FALSE);
  73.         }
  74.     }
  75.  
  76.     return(std_fexit(sed));
  77. }
  78.  
  79. void double_fkey(sed)
  80.     sed_type sed;
  81. {
  82.     int   scancode, key, mode;
  83.     char  c;
  84.  
  85.     scancode = kb_Read();
  86.  
  87.     if (sed_DoSpecial(sed, scancode))
  88.         return;
  89.     if (special_key(sed, scancode))
  90.         return;
  91.     if (inter_field(sed, scancode))
  92.         return;
  93.     if (inter_page(sed, scancode))
  94.         return;
  95.  
  96.     mode = (sed_GetBaton(sed) == SED_FIRST) ? MANTISSA : sed_GetBaton(sed);
  97.     if (mode == EXPONENT) {
  98.         sed_GoEnd(sed);
  99.     }
  100.     else {
  101.         sed_GotoChar(sed, mantissa_pos(sed_GetCurrRecord(sed)));
  102.     }
  103.  
  104.     switch(scancode) {
  105.     case BACKSPACE:
  106.         sed_PullLeft(sed);
  107.         if (digit_count(sed_GetCurrRecord(sed)) == 0) {
  108.             sed_Overwrite(sed, '0');
  109.         }
  110.         break;
  111.     default:
  112.         key = ascii(scancode);
  113.         if (isdigit(key)) {
  114.             if (sed_GetBaton(sed) == SED_FIRST) {
  115.                 /* Clear field if first key pressed is a digit */
  116.                 sprintf(sed_GetScratchPad(sed), "%.le", 0.0);
  117.                 strright(sed_GetScratchPad(sed), sed_GetCurrRecordLen(sed));
  118.                 sed_SetCurrRecord(sed, sed_GetScratchPad(sed));
  119.                 sed_UpdateCurrField(sed);
  120.                 sed_GotoChar(sed, mantissa_pos(sed_GetCurrRecord(sed)));
  121.             }
  122.  
  123.             if (mode == EXPONENT) {
  124.                 c = sed_Overwrite(sed, key);
  125.                 sed_DecChar(sed);
  126.                 sed_Overwrite(sed, c);
  127.                 sed_GoEnd(sed);
  128.             }
  129.             else {
  130.                 if (sed_GetChar(sed, 1) == ' ' || sed_GetChar(sed, 1) == '-'){
  131.                     if (sed_GetCurrChar(sed) == '0' &&
  132.                       digit_count(sed_GetCurrRecord(sed)) == 1) {
  133.                         sed_Overwrite(sed, key);
  134.                     }
  135.                     else {
  136.                         sed_PushLeft(sed, key);
  137.                     }
  138.                 }
  139.             }
  140.         }
  141.  
  142.         /* toggle minus sign if appropriate */
  143.         else if (key == '-') {
  144.             strcpy(sed_GetScratchPad(sed), sed_GetCurrRecord(sed));
  145.             sed_SetCurrRecord(sed, strminus(sed_GetScratchPad(sed)));
  146.             sed_UpdateCurrField(sed);
  147.         }
  148.  
  149.         /* if no decimal point yet, add one */
  150.  
  151.         else if (key == ocountry.dec_char &&
  152.           (sed_GetChar(sed, 1) == ' '|| sed_GetChar(sed, 1) == '-')) {
  153.             if (strchr(sed_GetCurrRecord(sed), ocountry.dec_char) == NULL)  {
  154.                 /* no decimal point */
  155.                   sed_PushLeft(sed, ocountry.dec_char);
  156.             }
  157.         }
  158.         /* toggle EXP mode */
  159.         else if (toupper(key) == 'E') {
  160.                if (mode == EXPONENT) {
  161.                 mode = MANTISSA;
  162.                 sed_GotoChar(sed, mantissa_pos(sed_GetCurrRecord(sed)));
  163.             }
  164.             else {
  165.                    mode = EXPONENT;
  166.                 sed_GoEnd(sed);
  167.             }
  168.         }
  169.         break;
  170.     }
  171.  
  172.     /* reset baton */
  173.     sed_SetBaton(sed, mode);
  174. }
  175.  
  176.  
  177. static int mantissa_pos(record)
  178.     char *record;
  179. /*
  180.     returns the record position of the mantissa
  181.     i.e., the position before the 'e'.
  182. */
  183. {
  184.     char *p;
  185.     int pos;
  186.  
  187.     for (p = record, pos= 0; p[pos] != '\0' && p[pos] != 'e' && p[pos] != 'E'; pos++) {
  188.         ;
  189.     }
  190.  
  191.     return((pos > 0) ? (pos - 1) : 0);
  192. }
  193.  
  194. void double_senter(sed, fieldno)
  195.     sed_type sed;
  196.     int fieldno;
  197. /*
  198.     Convert native type to string for record.
  199. */
  200. {
  201.     sprintf(sed_GetScratchPad(sed), "%.le", *((double *) sed_GetVar(sed, fieldno)));
  202.  
  203.     /* My change to change the decimal default from printf to 
  204.         the ocountry decimal character. */
  205.     strtrans(sed_GetScratchPad(sed), '.', ocountry.dec_char);
  206.     strright(sed_GetScratchPad(sed), sed_GetRecordLen(sed, fieldno));
  207.     sed_SetRecord(sed, sed_GetScratchPad(sed), fieldno);
  208.  
  209.     std_senter(sed, fieldno);
  210. }
  211.  
  212. void double_sexit(sed, fieldno)
  213.     sed_type sed;
  214.     int fieldno;
  215. /*
  216.     Converts record back to native type.
  217. */
  218. {
  219.     if (sed_GetBaton(sed) != SED_ABORT) {
  220.         sscanf(strtrans(sed_GetRecord(sed, fieldno), ocountry.dec_char, '.'), "%le", (double *) sed_GetVar(sed, fieldno));
  221.     }
  222. }
  223.  
  224.