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_num.c,v 1.15 2002/07/06 15:33:27 juergen Exp $")
30 /*---------------------------------------------------------------------------
32 | Function : static void *Make_Numeric_Type(va_list * ap)
34 | Description : Allocate structure for numeric type argument.
36 | Return Values : Pointer to argument structure or NULL on error
37 +--------------------------------------------------------------------------*/
38 static void *Make_Numeric_Type(va_list * ap)
40 numericARG *argn = (numericARG *)malloc(sizeof(numericARG));
44 argn->precision = va_arg(*ap,int);
45 argn->low = va_arg(*ap,double);
46 argn->high = va_arg(*ap,double);
48 argn->L = localeconv();
56 /*---------------------------------------------------------------------------
58 | Function : static void *Copy_Numeric_Type(const void * argp)
60 | Description : Copy structure for numeric type argument.
62 | Return Values : Pointer to argument structure or NULL on error.
63 +--------------------------------------------------------------------------*/
64 static void *Copy_Numeric_Type(const void * argp)
66 const numericARG *ap = (const numericARG *)argp;
67 numericARG *result = (numericARG *)0;
71 result = (numericARG *)malloc(sizeof(numericARG));
75 return (void *)result;
78 /*---------------------------------------------------------------------------
80 | Function : static void Free_Numeric_Type(void * argp)
82 | Description : Free structure for numeric type argument.
85 +--------------------------------------------------------------------------*/
86 static void Free_Numeric_Type(void * argp)
92 /*---------------------------------------------------------------------------
94 | Function : static bool Check_Numeric_Field(FIELD * field,
97 | Description : Validate buffer content to be a valid numeric value
99 | Return Values : TRUE - field is valid
100 | FALSE - field is invalid
101 +--------------------------------------------------------------------------*/
102 static bool Check_Numeric_Field(FIELD * field, const void * argp)
104 const numericARG *argn = (const numericARG *)argp;
105 double low = argn->low;
106 double high = argn->high;
107 int prec = argn->precision;
108 unsigned char *bp = (unsigned char *)field_buffer(field,0);
109 char *s = (char *)bp;
111 struct lconv* L = argn->L;
114 while(*bp && *bp==' ') bp++;
117 if (*bp=='-' || *bp=='+')
121 if (!isdigit(*bp)) break;
126 (L && L->decimal_point) ? *(L->decimal_point) :
133 if (!isdigit(*bp)) break;
137 while(*bp && *bp==' ') bp++;
143 if (val<low || val>high) return FALSE;
145 sprintf(buf,"%.*f",(prec>0?prec:0),val);
146 set_field_buffer(field,0,buf);
153 /*---------------------------------------------------------------------------
154 | Facility : libnform
155 | Function : static bool Check_Numeric_Character(
159 | Description : Check a character for the numeric type.
161 | Return Values : TRUE - character is valid
162 | FALSE - character is invalid
163 +--------------------------------------------------------------------------*/
164 static bool Check_Numeric_Character(int c, const void * argp)
166 const numericARG *argn = (const numericARG *)argp;
167 struct lconv* L = argn->L;
169 return (isdigit(c) ||
174 (L && L->decimal_point) ? *(L->decimal_point) :
180 static FIELDTYPE typeNUMERIC = {
181 _HAS_ARGS | _RESIDENT,
182 1, /* this is mutable, so we can't be const */
189 Check_Numeric_Character,
194 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_NUMERIC = &typeNUMERIC;
196 /* fty_num.c ends here */