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

  1. /*
  2.     fnmoney.c 10/30/86
  3.  
  4.     % money_funcs
  5.  
  6.     Money editting functions.
  7.     These functions use a long and put a decimal point at position 2.
  8.     The value returned is in cents. Divide by 100 to get a dollar amount.
  9.  
  10.     Normally this functions saves enough space at the left of the field
  11.     to display a minus sign.  (toggled with the '-' key).
  12.     If you wish to disable this feature undefine the symbol MINUS and
  13.     recompile this file.
  14.  
  15.     C-scape 3.1
  16.     Copyright (c) 1986, 1987, 1988 by Oakland Group, Inc.
  17.     ALL RIGHTS RESERVED.
  18.  
  19.     Revision History:
  20.     -----------------
  21.     10/01/87 jmd     added casting
  22.      4/06/88 jmd     added call to sed_DoSpecial
  23.      5/12/88 jmd    added calls to sed_GetScratchPad()
  24.      5/14/88 jmd    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. #define    DECP    2            /* number of digits past the decimal point */
  45.  
  46. OGLOBAL field_funcs_struct money_funcs = {
  47.     num_fenter,
  48.     money_fexit,
  49.     money_fkey,
  50.     money_senter,
  51.     money_sexit,
  52.     sizeof(long)
  53. };
  54.  
  55. boolean money_fexit(sed)
  56.     sed_type sed;
  57. /*
  58.     Validates a long using the string in field data 1.
  59. */
  60. {
  61.     long val;
  62.  
  63.     if (sed_GetBaton(sed) != SED_ABORT) {
  64.         strcpy(sed_GetScratchPad(sed), sed_GetCurrRecord(sed));
  65.         sscanf(strnodecp(sed_GetScratchPad(sed)), "%ld", &val);
  66.  
  67.         /* call standard numeric validation routine (fnstdval.c) */
  68.         if (!std_NumValid(sed, (double) val)) {
  69.             return(FALSE);
  70.         }
  71.     }
  72.  
  73.     return(std_fexit(sed));
  74. }
  75.  
  76. void money_fkey(sed)
  77.     sed_type sed;
  78. {
  79.     int scancode, key;
  80.  
  81.     scancode = kb_Read();
  82.  
  83.     if (sed_DoSpecial(sed, scancode))
  84.         return;
  85.     if (special_key(sed, scancode))
  86.         return;
  87.     if (inter_field(sed, scancode))
  88.         return;
  89.     if (inter_page(sed, scancode))
  90.         return;
  91.  
  92.     switch(scancode) {
  93.     case BACKSPACE:
  94.         sed_PullLeft(sed);
  95.         strcpy(sed_GetScratchPad(sed), sed_GetCurrRecord(sed));
  96.         sed_SetCurrRecord(sed, strdecp(sed_GetScratchPad(sed), DECP));
  97.         sed_UpdateCurrField(sed);
  98.         break;
  99.     default:
  100.         /* don't allow characters in the first position */
  101.  
  102.         key = ascii(scancode);
  103.         if (isdigit(key)) {
  104.             if (sed_GetBaton(sed) == SED_FIRST) {
  105.                 /* Clear field if first key pressed is a digit */
  106.                 strfill(sed_GetScratchPad(sed), ' ', strlen(sed_GetCurrRecord(sed)));
  107.                 sed_SetCurrRecord(sed, strdecp(sed_GetScratchPad(sed), DECP));
  108.                 sed_UpdateCurrField(sed);
  109.             }
  110.  
  111. #ifdef MINUS
  112.             if (sed_GetChar(sed, 1) != ' ' && sed_GetChar(sed, 1) != '-') {
  113.                 /* don't allow characters in the first position
  114.                    (save for minus sign) */
  115.                 break;
  116.             }
  117. #else
  118.             if (sed_GetChar(sed, 0) != ' ' && sed_GetChar(sed, 0) != '-') {
  119.                 /* if minus is disabled don't allow more numbers
  120.                    after field is filled */
  121.                 break;
  122.             }
  123. #endif
  124.  
  125.             sed_PushLeft(sed, key);
  126.             strcpy(sed_GetScratchPad(sed), sed_GetCurrRecord(sed));
  127.             sed_SetCurrRecord(sed, strdecp(sed_GetScratchPad(sed), DECP));
  128.             sed_UpdateCurrField(sed);
  129.         }
  130.  
  131. #ifdef MINUS
  132.         /* toggle minus sign if appropriate */
  133.         else if (key == '-') {
  134.             strcpy(sed_GetScratchPad(sed), sed_GetCurrRecord(sed));
  135.             sed_SetCurrRecord(sed, strminus(sed_GetScratchPad(sed)));
  136.             sed_UpdateCurrField(sed);
  137.         }
  138. #endif
  139.         /* Clear the field if ' ' is pressed */
  140.  
  141.         else if (key == ' ') {
  142.             strfill(sed_GetScratchPad(sed), ' ', strlen(sed_GetCurrRecord(sed)));
  143.             sed_SetCurrRecord(sed, strdecp(sed_GetScratchPad(sed), DECP));
  144.             sed_UpdateCurrField(sed);
  145.         }
  146.  
  147.         break;
  148.     }
  149.  
  150.     /* reset baton */
  151.     sed_SetBaton(sed, -1);
  152. }
  153.  
  154. void money_senter(sed, fieldno)
  155.     sed_type sed;
  156.     int fieldno;
  157. /*
  158.     Convert native type to string for record.
  159. */
  160. {
  161.     sprintf(sed_GetScratchPad(sed), "%ld", *((long *) sed_GetVar(sed, fieldno)));
  162.     strright(sed_GetScratchPad(sed), sed_GetRecordLen(sed, fieldno));
  163.     sed_SetRecord(sed, strdecp(sed_GetScratchPad(sed), DECP), fieldno);
  164.  
  165.     std_senter(sed, fieldno);
  166. }
  167.  
  168. void money_sexit(sed, fieldno)
  169.     sed_type sed;
  170.     int fieldno;
  171. /*
  172.     Converts record back to native type.
  173. */
  174. {
  175.     if (sed_GetBaton(sed) != SED_ABORT) {
  176.         strcpy(sed_GetScratchPad(sed), sed_GetRecord(sed, fieldno));
  177.         sscanf(strnodecp(sed_GetScratchPad(sed)), "%ld", (long *) sed_GetVar(sed, fieldno));
  178.     }
  179. }
  180.  
  181.