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

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