1/*2* THIS CODE IS SPECIFICALLY EXEMPTED FROM THE NCURSES PACKAGE COPYRIGHT.3* You may freely copy it for use as a template for your own field types.4* If you develop a field type that might be of general use, please send5* it back to the ncurses maintainers for inclusion in the next version.6*/7/***************************************************************************8* *9* Author : Juergen Pfeifer, [email protected] *10* *11***************************************************************************/1213#include "form.priv.h"1415MODULE_ID("$Id$")1617typedef struct {18int precision;19long low;20long high;21} integerARG;2223/*---------------------------------------------------------------------------24| Facility : libnform25| Function : static void *Make_Integer_Type( va_list * ap )26|27| Description : Allocate structure for integer type argument.28|29| Return Values : Pointer to argument structure or NULL on error30+--------------------------------------------------------------------------*/31static void *Make_Integer_Type(va_list * ap)32{33integerARG *argp = (integerARG *)malloc(sizeof(integerARG));3435if (argp)36{37argp->precision = va_arg(*ap,int);38argp->low = va_arg(*ap,long);39argp->high = va_arg(*ap,long);40}41return (void *)argp;42}4344/*---------------------------------------------------------------------------45| Facility : libnform46| Function : static void *Copy_Integer_Type(const void * argp)47|48| Description : Copy structure for integer type argument.49|50| Return Values : Pointer to argument structure or NULL on error.51+--------------------------------------------------------------------------*/52static void *Copy_Integer_Type(const void * argp)53{54const integerARG *ap = (const integerARG *)argp;55integerARG *result = (integerARG *)0;5657if (argp)58{59result = (integerARG *)malloc(sizeof(integerARG));60if (result)61*result = *ap;62}63return (void *)result;64}6566/*---------------------------------------------------------------------------67| Facility : libnform68| Function : static void Free_Integer_Type(void * argp)69|70| Description : Free structure for integer type argument.71|72| Return Values : -73+--------------------------------------------------------------------------*/74static void Free_Integer_Type(void * argp)75{76if (argp)77free(argp);78}7980/*---------------------------------------------------------------------------81| Facility : libnform82| Function : static bool Check_Integer_Field(83| FIELD * field,84| const void * argp)85|86| Description : Validate buffer content to be a valid integer value87|88| Return Values : TRUE - field is valid89| FALSE - field is invalid90+--------------------------------------------------------------------------*/91static bool Check_Integer_Field(FIELD * field, const void * argp)92{93const integerARG *argi = (const integerARG *)argp;94long low = argi->low;95long high = argi->high;96int prec = argi->precision;97unsigned char *bp = (unsigned char *)field_buffer(field,0);98char *s = (char *)bp;99long val;100char buf[100];101102while( *bp && *bp==' ') bp++;103if (*bp)104{105if (*bp=='-') bp++;106while (*bp)107{108if (!isdigit(*bp)) break;109bp++;110}111while(*bp && *bp==' ') bp++;112if (*bp=='\0')113{114val = atol(s);115if (low<high)116{117if (val<low || val>high) return FALSE;118}119snprintf(buf,sizeof(buf),"%.*ld",(prec>0?prec:0),val);120set_field_buffer(field,0,buf);121return TRUE;122}123}124return FALSE;125}126127/*---------------------------------------------------------------------------128| Facility : libnform129| Function : static bool Check_Integer_Character(130| int c,131| const void * argp)132|133| Description : Check a character for the integer type.134|135| Return Values : TRUE - character is valid136| FALSE - character is invalid137+--------------------------------------------------------------------------*/138static bool Check_Integer_Character(int c, const void * argp)139{140argp=0; /* Silence unused parameter warning. */141return ((isdigit(c) || (c=='-')) ? TRUE : FALSE);142}143144static FIELDTYPE typeINTEGER = {145_HAS_ARGS | _RESIDENT,1461, /* this is mutable, so we can't be const */147(FIELDTYPE *)0,148(FIELDTYPE *)0,149Make_Integer_Type,150Copy_Integer_Type,151Free_Integer_Type,152Check_Integer_Field,153Check_Integer_Character,154NULL,155NULL156};157158FIELDTYPE* TYPE_INTEGER = &typeINTEGER;159160/* fty_int.c ends here */161162163