1 /****************************************************************************
2 * Copyright (c) 1998-2005,2006 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /***************************************************************************
31 * Author : Juergen Pfeifer *
33 ***************************************************************************/
35 #include "form.priv.h"
37 MODULE_ID("$Id: fty_num.c,v 1.23 2006/04/22 21:33:05 tom Exp $")
44 #define isDecimalPoint(c) ((c) == ((L && L->decimal_point) ? *(L->decimal_point) : '.'))
46 #define isDecimalPoint(c) ((c) == '.')
50 #define isDigit(c) (iswdigit((wint_t)(c)) || isdigit(UChar(c)))
52 #define isDigit(c) isdigit(UChar(c))
55 #define thisARG numericARG
66 /*---------------------------------------------------------------------------
68 | Function : static void *Make_This_Type(va_list * ap)
70 | Description : Allocate structure for numeric type argument.
72 | Return Values : Pointer to argument structure or NULL on error
73 +--------------------------------------------------------------------------*/
75 Make_This_Type(va_list *ap)
77 thisARG *argn = (thisARG *) malloc(sizeof(thisARG));
81 argn->precision = va_arg(*ap, int);
82 argn->low = va_arg(*ap, double);
83 argn->high = va_arg(*ap, double);
86 argn->L = localeconv();
94 /*---------------------------------------------------------------------------
96 | Function : static void *Copy_This_Type(const void * argp)
98 | Description : Copy structure for numeric type argument.
100 | Return Values : Pointer to argument structure or NULL on error.
101 +--------------------------------------------------------------------------*/
103 Copy_This_Type(const void *argp)
105 const thisARG *ap = (const thisARG *)argp;
106 thisARG *result = (thisARG *) 0;
110 result = (thisARG *) malloc(sizeof(thisARG));
114 return (void *)result;
117 /*---------------------------------------------------------------------------
118 | Facility : libnform
119 | Function : static void Free_This_Type(void * argp)
121 | Description : Free structure for numeric type argument.
124 +--------------------------------------------------------------------------*/
126 Free_This_Type(void *argp)
132 /*---------------------------------------------------------------------------
133 | Facility : libnform
134 | Function : static bool Check_This_Field(FIELD * field,
137 | Description : Validate buffer content to be a valid numeric value
139 | Return Values : TRUE - field is valid
140 | FALSE - field is invalid
141 +--------------------------------------------------------------------------*/
143 Check_This_Field(FIELD *field, const void *argp)
145 const thisARG *argn = (const thisARG *)argp;
146 double low = argn->low;
147 double high = argn->high;
148 int prec = argn->precision;
149 unsigned char *bp = (unsigned char *)field_buffer(field, 0);
150 char *s = (char *)bp;
152 struct lconv *L = argn->L;
156 while (*bp && *bp == ' ')
160 if (*bp == '-' || *bp == '+')
162 #if USE_WIDEC_SUPPORT
169 wchar_t *list = _nc_Widen_String((char *)bp, &len);
174 for (n = 0; n < len; ++n)
184 else if (list[n] == ' ')
188 else if (isDecimalPoint(list[n]))
196 else if (!isDigit(list[n]))
208 if (!isdigit(UChar(*bp)))
212 if (isDecimalPoint(*bp))
217 if (!isdigit(UChar(*bp)))
222 while (*bp && *bp == ' ')
224 result = (*bp == '\0');
231 if (val < low || val > high)
236 sprintf(buf, "%.*f", (prec > 0 ? prec : 0), val);
237 set_field_buffer(field, 0, buf);
244 /*---------------------------------------------------------------------------
245 | Facility : libnform
246 | Function : static bool Check_This_Character(
250 | Description : Check a character for the numeric type.
252 | Return Values : TRUE - character is valid
253 | FALSE - character is invalid
254 +--------------------------------------------------------------------------*/
256 Check_This_Character(int c, const void *argp)
258 const thisARG *argn = (const thisARG *)argp;
259 struct lconv *L = argn->L;
261 return ((isDigit(c) ||
269 static FIELDTYPE typeTHIS =
271 _HAS_ARGS | _RESIDENT,
272 1, /* this is mutable, so we can't be const */
279 Check_This_Character,
284 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_NUMERIC = &typeTHIS;
286 /* fty_num.c ends here */