home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
QBasic & Borland Pascal & C
/
Delphi5.iso
/
C
/
Samples
/
CSAPE32.ARJ
/
SOURCE
/
FUNCS
/
FNNUM.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-07-26
|
4KB
|
171 lines
/*
fnnum.c 11/3/86
% num_fenter, num_fkey, strminus, digit_count, clear_field
Misc. routines used by numeric field functions.
C-scape 3.2
Copyright (c) 1986, 1987, 1988 by Oakland Group, Inc.
ALL RIGHTS RESERVED.
Revision History:
-----------------
4/06/88 jmd added call to sed_DoSpecial
5/12/88 jmd added calls to sed_GetScratchPad()
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/06/88 jmd added snum_fenter
10/09/88 jmd added SED_ABORT support
6/07/89 jmd added test for mouse code (later removed)
1/31/90 jmd don't reset baton on MOU_CLICK
3/28/90 jmd ansi-fied
7/26/90 jdc fixed for use with only two writeable positions
*/
#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"
void num_fenter(sed_type sed)
/*
fenter function for numeric fields.
*/
{
std_fenter(sed);
sed_GoEnd(sed);
}
void num_fkey(sed_type sed)
/*
Generic fkey function for numeric fields.
Supports right to left calculator style entry.
The '-' key toggles the minus sign.
The Space Bar key clears the field to 0.
*/
{
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 DEL:
case BACKSPACE:
sed_PullLeft(sed);
if (digit_count(sed_GetCurrRecord(sed)) == 0) {
sed_Overwrite(sed, '0');
}
break;
default:
/* don't allow characters in the first position */
/* overwrite leading zeroes */
key = ascii(scancode);
if (isdigit(key)) {
if (sed_GetBaton(sed) == SED_FIRST) {
/* Clear field if first key pressed is a digit */
clear_field(sed);
}
if (sed_GetCurrChar(sed) == '0'
&& digit_count(sed_GetCurrRecord(sed)) == 1) {
sed_Overwrite(sed, key);
}
else if (sed_GetCurrRecordLen(sed) > 1
&& sed_GetChar(sed, 1) == ' ') {
sed_PushLeft(sed, key);
}
}
/* 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);
}
/* Clear the field if ' ' is pressed */
else if (key == ' ') {
clear_field(sed);
}
break;
}
/* reset baton */
if (scancode != MOU_CLICK) {
sed_SetBaton(sed, -1);
}
}
char *strminus(char *s)
/*
Toggles the minus sign in a numeric string.
*/
{
char *p;
for (p = s;; p++) {
if (*p == '-') {
*p = ' ';
break;
}
else if (*p != ' ' && p > s) {
*(p-1) = '-';
break;
}
else if (*p == '\0' && p > s+1) {
*(p-2) = '-';
*(p-1) = '0';
break;
}
}
return(s);
}
int digit_count(char *str)
/*
Counts number of (hex) digits in a string.
*/
{
register int count = 0;
for(;*str;str++) {
if (isxdigit(*str))
count++;
}
return(count);
}
void clear_field(sed_type sed)
/*
Clear a field to 0.
*/
{
strcpy(sed_GetScratchPad(sed), "");
strpad(sed_GetScratchPad(sed), sed_GetCurrRecordLen(sed));
sed_SetCurrRecord(sed, sed_GetScratchPad(sed));
sed_UpdateCurrField(sed);
sed_Overwrite(sed, '0');
}