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 send
5 * it back to the ncurses maintainers for inclusion in the next version.
7 /***************************************************************************
9 * Author : Juergen Pfeifer *
11 ***************************************************************************/
13 #include "form.priv.h"
15 MODULE_ID("$Id: fty_int.c,v 1.19 2005/08/20 18:26:16 tom Exp $")
18 #define isDigit(c) (iswdigit((wint_t)(c)) || isdigit(UChar(c)))
20 #define isDigit(c) isdigit(UChar(c))
23 #define thisARG integerARG
33 /*---------------------------------------------------------------------------
35 | Function : static void *Make_This_Type( va_list * ap )
37 | Description : Allocate structure for integer type argument.
39 | Return Values : Pointer to argument structure or NULL on error
40 +--------------------------------------------------------------------------*/
42 Make_This_Type(va_list *ap)
44 thisARG *argp = (thisARG *) malloc(sizeof(thisARG));
48 argp->precision = va_arg(*ap, int);
49 argp->low = va_arg(*ap, long);
50 argp->high = va_arg(*ap, long);
55 /*---------------------------------------------------------------------------
57 | Function : static void *Copy_This_Type(const void * argp)
59 | Description : Copy structure for integer type argument.
61 | Return Values : Pointer to argument structure or NULL on error.
62 +--------------------------------------------------------------------------*/
64 Copy_This_Type(const void *argp)
66 const thisARG *ap = (const thisARG *)argp;
67 thisARG *result = (thisARG *) 0;
71 result = (thisARG *) malloc(sizeof(thisARG));
75 return (void *)result;
78 /*---------------------------------------------------------------------------
80 | Function : static void Free_This_Type(void * argp)
82 | Description : Free structure for integer type argument.
85 +--------------------------------------------------------------------------*/
87 Free_This_Type(void *argp)
93 /*---------------------------------------------------------------------------
95 | Function : static bool Check_This_Field(
99 | Description : Validate buffer content to be a valid integer value
101 | Return Values : TRUE - field is valid
102 | FALSE - field is invalid
103 +--------------------------------------------------------------------------*/
105 Check_This_Field(FIELD *field, const void *argp)
107 const thisARG *argi = (const thisARG *)argp;
108 long low = argi->low;
109 long high = argi->high;
110 int prec = argi->precision;
111 unsigned char *bp = (unsigned char *)field_buffer(field, 0);
112 char *s = (char *)bp;
117 while (*bp && *bp == ' ')
123 #if USE_WIDEC_SUPPORT
129 wchar_t *list = _nc_Widen_String((char *)bp, &len);
134 for (n = 0; n < len; ++n)
144 else if (list[n] == ' ')
148 else if (!isDigit(list[n]))
160 if (!isdigit(UChar(*bp)))
164 while (*bp && *bp == ' ')
166 result = (*bp == '\0');
173 if (val < low || val > high)
178 sprintf(buf, "%.*ld", (prec > 0 ? prec : 0), val);
179 set_field_buffer(field, 0, buf);
186 /*---------------------------------------------------------------------------
187 | Facility : libnform
188 | Function : static bool Check_This_Character(
192 | Description : Check a character for the integer type.
194 | Return Values : TRUE - character is valid
195 | FALSE - character is invalid
196 +--------------------------------------------------------------------------*/
198 Check_This_Character(int c, const void *argp GCC_UNUSED)
200 return ((isDigit(UChar(c)) || (c == '-')) ? TRUE : FALSE);
203 static FIELDTYPE typeTHIS =
205 _HAS_ARGS | _RESIDENT,
206 1, /* this is mutable, so we can't be const */
213 Check_This_Character,
218 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_INTEGER = &typeTHIS;
220 /* fty_int.c ends here */