home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
QBasic & Borland Pascal & C
/
Delphi5.iso
/
C
/
Samples
/
CSAPE32.ARJ
/
SOURCE
/
CSSRC
/
FNHEX.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-09-07
|
4KB
|
177 lines
/*
fnhex.c 11/20/86
% hex_funcs
Hexadecimal editing functions.
Just like int_funcs except no minus sign.
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()
9/17/88 jmd added std_ funcs
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)
7/20/89 jdc changed hex_senter, sexit calls in hex_fexit to
sed_DoFieldSenter, sed_DoFieldSexit
2/21/90 pmcm made space key and 1st xdigit keypress clear field
added DEL case
3/14/90 jmd moved formatting before validation
3/28/90 jmd ansi-fied
6/13/90 jdc replaced backwards pointer math
8/08/90 mla added standard numeric validation
9/07/90 jmd ifdef'd out code that call sexit/senter
*/
#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"
OGLOBAL field_funcs_struct hex_funcs = {
num_fenter,
hex_fexit,
hex_fkey,
hex_senter,
hex_sexit,
sizeof(int)
};
boolean hex_fexit(sed_type sed)
{
int val;
if (sed_GetBaton(sed) != SED_ABORT) {
/* format the field's record */
std_format(sed);
sscanf(sed_GetCurrRecord(sed), "%x", &val);
/* call standard numeric validation routine (fnstdval.c) */
if (!std_NumValid(sed, (double) val)) {
return(FALSE);
}
#ifdef DOSEXIT
sed_DoFieldSexit(sed, sed_GetFieldNo(sed));
sed_DoFieldSenter(sed, sed_GetFieldNo(sed));
sed_UpdateCurrField(sed);
#endif
}
return(std_fexit(sed));
}
void hex_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 DEL:
case BACKSPACE:
sed_PullLeft(sed);
strcpy(sed_GetScratchPad(sed), sed_GetCurrRecord(sed));
sed_SetCurrRecord(sed, strhex(sed_GetScratchPad(sed)));
sed_UpdateCurrField(sed);
break;
default:
key = ascii(scancode);
if (isxdigit(key) || key == ' ') {
if (key == ' ') {
/* Clear the record if space key pressed */
sprintf(sed_GetScratchPad(sed), "0");
}
else if (sed_GetBaton(sed) == SED_FIRST) {
/* Start a new hex num if xdigit is first key pressed */
sprintf(sed_GetScratchPad(sed), "%c", key);
}
else {
/* Add xdigits until full (no more leading zeros) */
if (sed_GetChar(sed, 0) == '0') {
sed_PushLeft(sed, key);
}
break;
}
strright(sed_GetScratchPad(sed), sed_GetCurrRecordLen(sed));
sed_SetCurrRecord(sed, strhex(sed_GetScratchPad(sed)));
sed_UpdateCurrField(sed);
}
break;
}
/* reset baton */
sed_SetBaton(sed, -1);
}
void hex_senter(sed_type sed, int fieldno)
/*
Convert native type to string for record.
*/
{
sprintf(sed_GetScratchPad(sed), "%x", *((int *) sed_GetVar(sed, fieldno)));
strright(sed_GetScratchPad(sed), sed_GetRecordLen(sed, fieldno));
sed_SetRecord(sed, strhex(sed_GetScratchPad(sed)), fieldno);
std_senter(sed, fieldno);
}
void hex_sexit(sed_type sed, int fieldno)
/*
Converts record back to native type.
*/
{
if (sed_GetBaton(sed) != SED_ABORT) {
sscanf(sed_GetRecord(sed, fieldno), "%x", (int *) sed_GetVar(sed, fieldno));
}
}
char *strhex(char *s)
/*
Formats a hexadecimal string
The string returned is a right justified hexadecimal
number padded out with leading zeroes.
*/
{
int i;
for (i = strlen(s) - 1; i >= 0; i--) {
if (s[i] == ' ') {
s[i] = '0';
}
}
return(s);
}