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_num.c,v 1.3 2002/06/18 21:19:38 king Exp $")
29 /*---------------------------------------------------------------------------
31 | Function : static void *Make_Numeric_Type(va_list * ap)
33 | Description : Allocate structure for numeric type argument.
35 | Return Values : Pointer to argument structure or NULL on error
36 +--------------------------------------------------------------------------*/
37 static void *Make_Numeric_Type(va_list * ap
)
39 numericARG
*argn
= (numericARG
*)malloc(sizeof(numericARG
));
43 argn
->precision
= va_arg(*ap
,int);
44 argn
->low
= va_arg(*ap
,double);
45 argn
->high
= va_arg(*ap
,double);
47 argn
->L
= localeconv();
55 /*---------------------------------------------------------------------------
57 | Function : static void *Copy_Numeric_Type(const void * argp)
59 | Description : Copy structure for numeric type argument.
61 | Return Values : Pointer to argument structure or NULL on error.
62 +--------------------------------------------------------------------------*/
63 static void *Copy_Numeric_Type(const void * argp
)
65 const numericARG
*ap
= (const numericARG
*)argp
;
66 numericARG
*result
= (numericARG
*)0;
70 result
= (numericARG
*)malloc(sizeof(numericARG
));
74 return (void *)result
;
77 /*---------------------------------------------------------------------------
79 | Function : static void Free_Numeric_Type(void * argp)
81 | Description : Free structure for numeric type argument.
84 +--------------------------------------------------------------------------*/
85 static void Free_Numeric_Type(void * argp
)
91 /*---------------------------------------------------------------------------
93 | Function : static bool Check_Numeric_Field(FIELD * field,
96 | Description : Validate buffer content to be a valid numeric value
98 | Return Values : TRUE - field is valid
99 | FALSE - field is invalid
100 +--------------------------------------------------------------------------*/
101 static bool Check_Numeric_Field(FIELD
* field
, const void * argp
)
103 const numericARG
*argn
= (const numericARG
*)argp
;
104 double low
= argn
->low
;
105 double high
= argn
->high
;
106 int prec
= argn
->precision
;
107 unsigned char *bp
= (unsigned char *)field_buffer(field
,0);
108 char *s
= (char *)bp
;
112 while(*bp
&& *bp
==' ') bp
++;
115 if (*bp
=='-' || *bp
=='+')
119 if (!isdigit(*bp
)) break;
124 (L
&& L
->decimal_point
) ? *(L
->decimal_point
) :
131 if (!isdigit(*bp
)) break;
135 while(*bp
&& *bp
==' ') bp
++;
141 if (val
<low
|| val
>high
) return FALSE
;
143 sprintf(buf
,"%.*f",(prec
>0?prec
:0),val
);
144 set_field_buffer(field
,0,buf
);
151 /*---------------------------------------------------------------------------
152 | Facility : libnform
153 | Function : static bool Check_Numeric_Character(
157 | Description : Check a character for the numeric type.
159 | Return Values : TRUE - character is valid
160 | FALSE - character is invalid
161 +--------------------------------------------------------------------------*/
162 static bool Check_Numeric_Character(int c
, const void * argp
)
164 argp
=0; /* Silence unused parameter warning. */
165 return (isdigit(c
) ||
170 (L
&& L
->decimal_point
) ? *(L
->decimal_point
) :
176 static FIELDTYPE typeNUMERIC
= {
177 _HAS_ARGS
| _RESIDENT
,
178 1, /* this is mutable, so we can't be const */
185 Check_Numeric_Character
,
190 FIELDTYPE
* TYPE_NUMERIC
= &typeNUMERIC
;
192 /* fty_num.c ends here */