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_num.c,v 1.22 2005/08/20 18:26:16 tom Exp $")
22 #define isDecimalPoint(c) ((c) == ((L && L->decimal_point) ? *(L->decimal_point) : '.'))
24 #define isDecimalPoint(c) ((c) == '.')
28 #define isDigit(c) (iswdigit((wint_t)(c)) || isdigit(UChar(c)))
30 #define isDigit(c) isdigit(UChar(c))
33 #define thisARG numericARG
44 /*---------------------------------------------------------------------------
46 | Function : static void *Make_This_Type(va_list * ap)
48 | Description : Allocate structure for numeric type argument.
50 | Return Values : Pointer to argument structure or NULL on error
51 +--------------------------------------------------------------------------*/
53 Make_This_Type(va_list *ap)
55 thisARG *argn = (thisARG *) malloc(sizeof(thisARG));
59 argn->precision = va_arg(*ap, int);
60 argn->low = va_arg(*ap, double);
61 argn->high = va_arg(*ap, double);
64 argn->L = localeconv();
72 /*---------------------------------------------------------------------------
74 | Function : static void *Copy_This_Type(const void * argp)
76 | Description : Copy structure for numeric type argument.
78 | Return Values : Pointer to argument structure or NULL on error.
79 +--------------------------------------------------------------------------*/
81 Copy_This_Type(const void *argp)
83 const thisARG *ap = (const thisARG *)argp;
84 thisARG *result = (thisARG *) 0;
88 result = (thisARG *) malloc(sizeof(thisARG));
92 return (void *)result;
95 /*---------------------------------------------------------------------------
97 | Function : static void Free_This_Type(void * argp)
99 | Description : Free structure for numeric type argument.
102 +--------------------------------------------------------------------------*/
104 Free_This_Type(void *argp)
110 /*---------------------------------------------------------------------------
111 | Facility : libnform
112 | Function : static bool Check_This_Field(FIELD * field,
115 | Description : Validate buffer content to be a valid numeric value
117 | Return Values : TRUE - field is valid
118 | FALSE - field is invalid
119 +--------------------------------------------------------------------------*/
121 Check_This_Field(FIELD *field, const void *argp)
123 const thisARG *argn = (const thisARG *)argp;
124 double low = argn->low;
125 double high = argn->high;
126 int prec = argn->precision;
127 unsigned char *bp = (unsigned char *)field_buffer(field, 0);
128 char *s = (char *)bp;
130 struct lconv *L = argn->L;
134 while (*bp && *bp == ' ')
138 if (*bp == '-' || *bp == '+')
140 #if USE_WIDEC_SUPPORT
147 wchar_t *list = _nc_Widen_String((char *)bp, &len);
152 for (n = 0; n < len; ++n)
162 else if (list[n] == ' ')
166 else if (isDecimalPoint(list[n]))
174 else if (!isDigit(list[n]))
186 if (!isdigit(UChar(*bp)))
190 if (isDecimalPoint(*bp))
195 if (!isdigit(UChar(*bp)))
200 while (*bp && *bp == ' ')
202 result = (*bp == '\0');
209 if (val < low || val > high)
214 sprintf(buf, "%.*f", (prec > 0 ? prec : 0), val);
215 set_field_buffer(field, 0, buf);
222 /*---------------------------------------------------------------------------
223 | Facility : libnform
224 | Function : static bool Check_This_Character(
228 | Description : Check a character for the numeric type.
230 | Return Values : TRUE - character is valid
231 | FALSE - character is invalid
232 +--------------------------------------------------------------------------*/
234 Check_This_Character(int c, const void *argp)
236 const thisARG *argn = (const thisARG *)argp;
237 struct lconv *L = argn->L;
239 return ((isDigit(c) ||
247 static FIELDTYPE typeTHIS =
249 _HAS_ARGS | _RESIDENT,
250 1, /* this is mutable, so we can't be const */
257 Check_This_Character,
262 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_NUMERIC = &typeTHIS;
264 /* fty_num.c ends here */