3 * THIS CODE IS SPECIFICALLY EXEMPTED FROM THE NCURSES PACKAGE COPYRIGHT.
4 * You may freely copy it for use as a template for your own field types.
5 * If you develop a field type that might be of general use, please send
6 * it back to the ncurses maintainers for inclusion in the next version.
8 /***************************************************************************
10 * Author : Juergen Pfeifer, juergen.pfeifer@gmx.net *
12 ***************************************************************************/
14 #include "form.priv.h"
16 MODULE_ID("$Id: fty_int.c,v 1.11 2000/12/09 23:46:12 tom Exp $")
24 /*---------------------------------------------------------------------------
26 | Function : static void *Make_Integer_Type( va_list * ap )
28 | Description : Allocate structure for integer type argument.
30 | Return Values : Pointer to argument structure or NULL on error
31 +--------------------------------------------------------------------------*/
32 static void *Make_Integer_Type(va_list * ap)
34 integerARG *argp = (integerARG *)malloc(sizeof(integerARG));
38 argp->precision = va_arg(*ap,int);
39 argp->low = va_arg(*ap,long);
40 argp->high = va_arg(*ap,long);
45 /*---------------------------------------------------------------------------
47 | Function : static void *Copy_Integer_Type(const void * argp)
49 | Description : Copy structure for integer type argument.
51 | Return Values : Pointer to argument structure or NULL on error.
52 +--------------------------------------------------------------------------*/
53 static void *Copy_Integer_Type(const void * argp)
55 const integerARG *ap = (const integerARG *)argp;
56 integerARG *result = (integerARG *)0;
60 result = (integerARG *)malloc(sizeof(integerARG));
64 return (void *)result;
67 /*---------------------------------------------------------------------------
69 | Function : static void Free_Integer_Type(void * argp)
71 | Description : Free structure for integer type argument.
74 +--------------------------------------------------------------------------*/
75 static void Free_Integer_Type(void * argp)
81 /*---------------------------------------------------------------------------
83 | Function : static bool Check_Integer_Field(
87 | Description : Validate buffer content to be a valid integer value
89 | Return Values : TRUE - field is valid
90 | FALSE - field is invalid
91 +--------------------------------------------------------------------------*/
92 static bool Check_Integer_Field(FIELD * field, const void * argp)
94 const integerARG *argi = (const integerARG *)argp;
96 long high = argi->high;
97 int prec = argi->precision;
98 unsigned char *bp = (unsigned char *)field_buffer(field,0);
103 while( *bp && *bp==' ') bp++;
109 if (!isdigit(*bp)) break;
112 while(*bp && *bp==' ') bp++;
118 if (val<low || val>high) return FALSE;
120 sprintf(buf,"%.*ld",(prec>0?prec:0),val);
121 set_field_buffer(field,0,buf);
128 /*---------------------------------------------------------------------------
129 | Facility : libnform
130 | Function : static bool Check_Integer_Character(
134 | Description : Check a character for the integer type.
136 | Return Values : TRUE - character is valid
137 | FALSE - character is invalid
138 +--------------------------------------------------------------------------*/
139 static bool Check_Integer_Character(int c, const void * argp GCC_UNUSED)
141 return ((isdigit(c) || (c=='-')) ? TRUE : FALSE);
144 static FIELDTYPE typeINTEGER = {
145 _HAS_ARGS | _RESIDENT,
146 1, /* this is mutable, so we can't be const */
153 Check_Integer_Character,
158 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_INTEGER = &typeINTEGER;
160 /* fty_int.c ends here */