home *** CD-ROM | disk | FTP | other *** search
- /*
- fncmoney.c 11/18/86
-
- % cmoney_funcs
-
- Money editing functions.
- These functions use a long and put a decimal point at position 2.
- The value returned is in cents. Divide by 100 to get a dollar amount.
- This one has commas.
-
- Normally this functions saves enough space at the left of the field
- to display a minus sign. (toggled with the '-' key).
- If you wish to disable this feature undefine the symbol MINUS and
- recompile this file.
-
- C-scape 3.2
- 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()
- 5/14/88 jmd now prevents comma expansion from putting chars in
- the first location, added MINUS option
- 9/17/88 jmd added global error msg strings for easy changing
- 9/17/88 jmd added std_ funcs
- 9/24/88 jmd clears after first key pressed
- 10/09/88 jmd added SED_ABORT support
- 10/14/88 jdc added var_size element to field_funcs_struct
-
- 6/07/89 jmd added test for mouse code (later removed)
- 3/14/90 jmd moved formatting before validation
- 3/28/90 jmd ansi-fied
- 4/12/90 pmcm added valid_Range check for over/underflow in fexit
- 4/13/90 jmd added olimits.h
- 9/11/90 pmcm changed sed_SetMouseCode to kb_Stuff
- */
-
- #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"
- #include "olimits.h"
-
- #define MINUS /* defining this enables the minus sign */
- #define DECP 2 /* number of digits past the decimal point */
-
- OGLOBAL field_funcs_struct cmoney_funcs = {
- num_fenter,
- cmoney_fexit,
- cmoney_fkey,
- cmoney_senter,
- cmoney_sexit,
- sizeof(long)
- };
-
- boolean cmoney_fexit(sed_type sed)
- /*
- Validates a long using the string in field data 1.
- */
- {
- long val;
-
- if (sed_GetBaton(sed) != SED_ABORT) {
-
- /* test for overflow/underflow */
-
- if (!valid_Range(sed, (long)LONG_MIN, (long)LONG_MAX)) {
- kb_Stuff(KEY_INVALID);
- return(FALSE);
- }
-
- /* format the field's record */
- std_format(sed);
-
- strcpy(sed_GetScratchPad(sed), sed_GetCurrRecord(sed));
- sscanf(strnocomma(strnodecp(sed_GetScratchPad(sed))), "%ld", &val);
-
- /* call standard numeric validation routine (fnstdval.c) */
- if (!std_NumValid(sed, (double) val)) {
- return(FALSE);
- }
- }
-
- return(std_fexit(sed));
- }
-
- void cmoney_fkey(sed_type sed)
- {
- int scancode, key;
-
- 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;
-
- switch(scancode) {
- case BACKSPACE:
- sed_PullLeft(sed);
- strcpy(sed_GetScratchPad(sed), sed_GetCurrRecord(sed));
- sed_SetCurrRecord(sed, strcomma(strdecp(sed_GetScratchPad(sed), DECP)));
- sed_UpdateCurrField(sed);
- break;
- default:
-
- key = ascii(scancode);
- if (isdigit(key)) {
- if (sed_GetBaton(sed) == SED_FIRST) {
- /* Clear field if first key pressed is a digit */
- strfill(sed_GetScratchPad(sed), ' ', strlen(sed_GetCurrRecord(sed)));
- sed_SetCurrRecord(sed, strcomma(strdecp(sed_GetScratchPad(sed), DECP)));
- sed_UpdateCurrField(sed);
- }
-
- #ifdef MINUS
- if (sed_GetChar(sed, 1) != ' ' && sed_GetChar(sed, 1) != '-') {
- /* don't allow characters in the first position
- (save for minus sign) */
- break;
- }
-
- if ((sed_GetCurrRecordLen(sed) > 4) &&
- isdigit(sed_GetChar(sed, 2)) &&
- isdigit(sed_GetChar(sed, 3)) &&
- isdigit(sed_GetChar(sed, 4)) ){
-
- /* make sure comma expansion doesn't
- place a character into first position */
- break;
- }
- #else
- if (sed_GetChar(sed, 0) != ' ' && sed_GetChar(sed, 0) != '-') {
- /* if minus is disable don't allow more numbers
- after field is filled */
- break;
- }
-
- if ((sed_GetCurrRecordLen(sed) > 3) &&
- isdigit(sed_GetChar(sed, 1)) &&
- isdigit(sed_GetChar(sed, 2)) &&
- isdigit(sed_GetChar(sed, 3)) ){
-
- /* make sure comma expansion doesn't get too big */
- break;
- }
-
- #endif
- sed_PushLeft(sed, key);
- strcpy(sed_GetScratchPad(sed), sed_GetCurrRecord(sed));
- sed_SetCurrRecord(sed, strcomma(strdecp(sed_GetScratchPad(sed), DECP)));
- sed_UpdateCurrField(sed);
- }
-
- #ifdef MINUS
- /* 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);
- }
- #endif
- /* Clear the field if ' ' is pressed */
- else if (key == ' ') {
- strfill(sed_GetScratchPad(sed), ' ', strlen(sed_GetCurrRecord(sed)));
- sed_SetCurrRecord(sed, strcomma(strdecp(sed_GetScratchPad(sed), DECP)));
- sed_UpdateCurrField(sed);
- }
-
- break;
- }
-
- /* reset baton */
- sed_SetBaton(sed, -1);
- }
-
- void cmoney_senter(sed_type sed, int fieldno)
- /*
- Convert native long type to string for record.
- */
- {
- sprintf(sed_GetScratchPad(sed), "%ld", *((long *) sed_GetVar(sed, fieldno)));
-
- strright(sed_GetScratchPad(sed), sed_GetRecordLen(sed, fieldno));
- sed_SetRecord(sed, strcomma(strdecp(sed_GetScratchPad(sed), DECP)), fieldno);
-
- std_senter(sed, fieldno);
- }
-
- void cmoney_sexit(sed_type sed, int fieldno)
- /*
- Converts record back to native type.
- */
- {
- if (sed_GetBaton(sed) != SED_ABORT) {
- strcpy(sed_GetScratchPad(sed), sed_GetRecord(sed, fieldno));
- sscanf(strnocomma(strnodecp(sed_GetScratchPad(sed))), "%ld", (long *) sed_GetVar(sed, fieldno));
- }
- }
-
-