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

  1. /*
  2.     fnclong.c             11/13/86
  3.  
  4.     % clong_funcs
  5.  
  6.     Long editting functions.
  7.     With commas.
  8.  
  9.     Normally this functions saves enough space at the left of the field
  10.     to display a minus sign.  (toggled with the '-' key).
  11.     If you wish to disable this feature undefine the symbol MINUS and
  12.     recompile this file.
  13.  
  14.     C-scape 3.1
  15.     Copyright (c) 1986, 1987, 1988 by Oakland Group, Inc.
  16.     ALL RIGHTS RESERVED.
  17.  
  18.     Revision History:
  19.     -----------------
  20.     10/01/87 jmd     added casting
  21.      4/06/88 jmd     added call to sed_DoSpecial
  22.      5/12/88 jmd    added calls to sed_GetScratchPad()
  23.      5/14/88 jmd    now prevents comma expansion from putting chars in
  24.                     the first location, added MINUS option
  25.      9/17/88 jmd    added global error msg strings for easy changing
  26.      9/17/88 jmd     added std_ funcs
  27.      9/24/88 jmd     clears after first key pressed
  28.     10/09/88 jmd     added SED_ABORT support
  29.     10/14/88 jdc    added var_size element to field_funcs_struct
  30.  
  31.      6/07/89 jmd    added test for mouse code (later removed)
  32. */
  33.  
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <ctype.h>
  37.  
  38. #include "cscape.h"
  39. #include "fnfunc.h"            /* for field functions */
  40. #include "strdecl.h"        /* for C-scape string functions */
  41. #include "scancode.h"
  42.  
  43. #define MINUS                /* defining this enables the minus sign */
  44.  
  45. OGLOBAL field_funcs_struct clong_funcs = {
  46.     num_fenter,
  47.     clong_fexit,
  48.     clong_fkey,
  49.     clong_senter,
  50.     clong_sexit,
  51.     sizeof(long)
  52. };
  53.  
  54. boolean clong_fexit(sed)
  55.     sed_type sed;
  56. /*
  57.     Validates a long using the string in field data 1.
  58. */
  59. {
  60.     long val;
  61.  
  62.     if (sed_GetBaton(sed) != SED_ABORT) {
  63.         strcpy(sed_GetScratchPad(sed), sed_GetCurrRecord(sed));
  64.         sscanf(strnocomma(sed_GetScratchPad(sed)), "%ld", &val);
  65.  
  66.         /* call standard numeric validation routine (fnstdval.c) */
  67.         if (!std_NumValid(sed, (double) val)) {
  68.             return(FALSE);
  69.         }
  70.     }
  71.  
  72.     return(std_fexit(sed));
  73. }
  74.  
  75. void clong_fkey(sed)
  76.     sed_type sed;
  77. {
  78.     int   scancode, key;
  79.  
  80.     scancode = kb_Read();
  81.  
  82.     if (sed_DoSpecial(sed, scancode))
  83.         return;
  84.     if (special_key(sed, scancode))
  85.         return;
  86.     if (inter_field(sed, scancode))
  87.         return;
  88.     if (inter_page(sed, scancode))
  89.         return;
  90.  
  91.     switch(scancode) {
  92.     case BACKSPACE:
  93.         sed_PullLeft(sed);
  94.         if (digit_count(sed_GetCurrRecord(sed)) == 0)
  95.             sed_Overwrite(sed, '0');
  96.         else {
  97.             strcpy(sed_GetScratchPad(sed), sed_GetCurrRecord(sed));
  98.             sed_SetCurrRecord(sed, strcomma(sed_GetScratchPad(sed)));
  99.             sed_UpdateCurrField(sed);
  100.         }
  101.         break;
  102.     default:
  103.         /* don't allow characters in the first position */
  104.         /* overwrite leading zeroes                        */
  105.         /* insert commas where neccessary                */
  106.  
  107.         key = ascii(scancode);
  108.         if (isdigit(key)) {
  109.             if (sed_GetBaton(sed) == SED_FIRST) {
  110.                 /* Clear field if first key pressed is a digit */
  111.                 clear_field(sed);
  112.             }
  113.  
  114. #ifdef MINUS
  115.             if (sed_GetChar(sed, 1) != ' ' && sed_GetChar(sed, 1) != '-') {
  116.                 /* don't allow characters in the first position
  117.                    (save for minus sign) */
  118.                 break;
  119.             }
  120.  
  121.             if ((sed_GetCurrRecordLen(sed) > 4) &&
  122.                 isdigit(sed_GetChar(sed, 2)) &&
  123.                 isdigit(sed_GetChar(sed, 3)) &&
  124.                 isdigit(sed_GetChar(sed, 4)) ){
  125.  
  126.                 /*     make sure comma expansion doesn't
  127.                     place a character into first position */
  128.                 break;
  129.             }
  130. #else
  131.             if (sed_GetChar(sed, 0) != ' ' && sed_GetChar(sed, 0) != '-') {
  132.                 /* if minus is disable don't allow more numbers
  133.                    after field is filled */
  134.                 break;
  135.             }
  136.  
  137.             if ((sed_GetCurrRecordLen(sed) > 3) &&
  138.                 isdigit(sed_GetChar(sed, 1)) &&
  139.                 isdigit(sed_GetChar(sed, 2)) &&
  140.                 isdigit(sed_GetChar(sed, 3)) ){
  141.  
  142.                 /*     make sure comma expansion doesn't get too big */
  143.                 break;
  144.             }
  145.  
  146. #endif
  147.  
  148.             if (sed_GetCurrChar(sed) == '0' &&
  149.                 digit_count(sed_GetCurrRecord(sed)) == 1) {
  150.  
  151.                 sed_Overwrite(sed, key);
  152.             }
  153.             else {
  154.                 sed_PushLeft(sed, key);
  155.                 strcpy(sed_GetScratchPad(sed), sed_GetCurrRecord(sed));
  156.                 sed_SetCurrRecord(sed, strcomma(sed_GetScratchPad(sed)));
  157.                 sed_UpdateCurrField(sed);
  158.             }
  159.         }
  160.  
  161. #ifdef MINUS
  162.         /* toggle minus sign if appropriate */
  163.         else if (key == '-') {
  164.             strcpy(sed_GetScratchPad(sed), sed_GetCurrRecord(sed));
  165.             sed_SetCurrRecord(sed, strminus(sed_GetScratchPad(sed)));
  166.             sed_UpdateCurrField(sed);
  167.         }
  168. #endif
  169.         /* Clear the field if ' ' is pressed */
  170.         else if (key == ' ') {
  171.             clear_field(sed);
  172.         }
  173.         break;
  174.     }
  175.  
  176.     /* reset baton */
  177.     sed_SetBaton(sed, -1);
  178. }
  179.  
  180. void clong_senter(sed, fieldno)
  181.     sed_type sed;
  182.     int fieldno;
  183. /*
  184.     Convert native type to string for record.
  185. */
  186. {
  187.     sprintf(sed_GetScratchPad(sed), "%ld", *((long *) sed_GetVar(sed, fieldno)));
  188.     strright(sed_GetScratchPad(sed), sed_GetRecordLen(sed, fieldno));
  189.     sed_SetRecord(sed, strcomma(sed_GetScratchPad(sed)), fieldno);
  190.  
  191.     std_senter(sed, fieldno);
  192. }
  193.  
  194. void clong_sexit(sed, fieldno)
  195.     sed_type sed;
  196.     int fieldno;
  197. /*
  198.     Converts record back to native type.
  199. */
  200. {
  201.     if (sed_GetBaton(sed) != SED_ABORT) {
  202.         strcpy(sed_GetScratchPad(sed), sed_GetRecord(sed, fieldno));
  203.         sscanf(strnocomma(sed_GetScratchPad(sed)), "%ld", (long *) sed_GetVar(sed, fieldno));
  204.     }
  205. }
  206.  
  207.