home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
QBasic & Borland Pascal & C
/
Delphi5.iso
/
C
/
Samples
/
CSAPE32.ARJ
/
SOURCE
/
FUNCS
/
FNMONEY.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-09-11
|
5KB
|
209 lines
/*
fnmoney.c 10/30/86
% money_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.
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 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)
1/31/90 jmd don't reset baton on MOU_CLICK
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
8/15/90 pmcm now works w/minimum of 4 writeables, 3 if MINUS not def'd
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 money_funcs = {
num_fenter,
money_fexit,
money_fkey,
money_senter,
money_sexit,
sizeof(long)
};
boolean money_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(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 money_fkey(sed_type sed)
{
int scancode;
int 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, strdecp(sed_GetScratchPad(sed), DECP));
sed_UpdateCurrField(sed);
break;
default:
/* don't allow characters in the first position */
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, strdecp(sed_GetScratchPad(sed), DECP));
sed_UpdateCurrField(sed);
}
/* allow entry of digits into field until full.
i.e., until there are no more leading blanks and
the leading digit is non-zero.
consider the minus sign, if enabled.
*/
#ifdef MINUS
if (sed_GetChar(sed, 1) != ' ' &&
sed_GetChar(sed, 1) != '-' &&
sed_GetChar(sed, 1) != '0' ) {
break;
}
#else
if (sed_GetChar(sed, 0) != ' ' &&
sed_GetChar(sed, 0) != '-' &&
sed_GetChar(sed, 0) != '0' ) {
break;
}
#endif
if (sed_PushLeft(sed, key) == '-') {
*sed_GetCurrRecord(sed) = '-';
}
strcpy(sed_GetScratchPad(sed), sed_GetCurrRecord(sed));
sed_SetCurrRecord(sed, 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, strdecp(sed_GetScratchPad(sed), DECP));
sed_UpdateCurrField(sed);
}
break;
}
/* reset baton */
if (scancode != MOU_CLICK) {
sed_SetBaton(sed, -1);
}
}
void money_senter(sed_type sed, int fieldno)
/*
Convert native 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, strdecp(sed_GetScratchPad(sed), DECP), fieldno);
std_senter(sed, fieldno);
}
void money_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(strnodecp(sed_GetScratchPad(sed)), "%ld", (long *) sed_GetVar(sed, fieldno));
}
}