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 *
11 * Contact: http://www.familiepfeifer.de/Contact.aspx?Lang=en *
13 ***************************************************************************/
15 #include "form.priv.h"
17 MODULE_ID("$Id: fty_int.c,v 1.12 2002/07/06 15:33:27 juergen Exp $")
25 /*---------------------------------------------------------------------------
27 | Function : static void *Make_Integer_Type( va_list * ap )
29 | Description : Allocate structure for integer type argument.
31 | Return Values : Pointer to argument structure or NULL on error
32 +--------------------------------------------------------------------------*/
33 static void *Make_Integer_Type(va_list * ap)
35 integerARG *argp = (integerARG *)malloc(sizeof(integerARG));
39 argp->precision = va_arg(*ap,int);
40 argp->low = va_arg(*ap,long);
41 argp->high = va_arg(*ap,long);
46 /*---------------------------------------------------------------------------
48 | Function : static void *Copy_Integer_Type(const void * argp)
50 | Description : Copy structure for integer type argument.
52 | Return Values : Pointer to argument structure or NULL on error.
53 +--------------------------------------------------------------------------*/
54 static void *Copy_Integer_Type(const void * argp)
56 const integerARG *ap = (const integerARG *)argp;
57 integerARG *result = (integerARG *)0;
61 result = (integerARG *)malloc(sizeof(integerARG));
65 return (void *)result;
68 /*---------------------------------------------------------------------------
70 | Function : static void Free_Integer_Type(void * argp)
72 | Description : Free structure for integer type argument.
75 +--------------------------------------------------------------------------*/
76 static void Free_Integer_Type(void * argp)
82 /*---------------------------------------------------------------------------
84 | Function : static bool Check_Integer_Field(
88 | Description : Validate buffer content to be a valid integer value
90 | Return Values : TRUE - field is valid
91 | FALSE - field is invalid
92 +--------------------------------------------------------------------------*/
93 static bool Check_Integer_Field(FIELD * field, const void * argp)
95 const integerARG *argi = (const integerARG *)argp;
97 long high = argi->high;
98 int prec = argi->precision;
99 unsigned char *bp = (unsigned char *)field_buffer(field,0);
100 char *s = (char *)bp;
104 while( *bp && *bp==' ') bp++;
110 if (!isdigit(*bp)) break;
113 while(*bp && *bp==' ') bp++;
119 if (val<low || val>high) return FALSE;
121 sprintf(buf,"%.*ld",(prec>0?prec:0),val);
122 set_field_buffer(field,0,buf);
129 /*---------------------------------------------------------------------------
130 | Facility : libnform
131 | Function : static bool Check_Integer_Character(
135 | Description : Check a character for the integer type.
137 | Return Values : TRUE - character is valid
138 | FALSE - character is invalid
139 +--------------------------------------------------------------------------*/
140 static bool Check_Integer_Character(int c, const void * argp GCC_UNUSED)
142 return ((isdigit(c) || (c=='-')) ? TRUE : FALSE);
145 static FIELDTYPE typeINTEGER = {
146 _HAS_ARGS | _RESIDENT,
147 1, /* this is mutable, so we can't be const */
154 Check_Integer_Character,
159 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_INTEGER = &typeINTEGER;
161 /* fty_int.c ends here */