1 /****************************************************************************
2 * Copyright (c) 1998-2006,2007 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.25 2007/10/13 19:32:54 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 = typeMalloc(thisARG, 1);
81 T((T_CREATE("thisARG %p"), argn));
82 argn->precision = va_arg(*ap, int);
83 argn->low = va_arg(*ap, double);
84 argn->high = va_arg(*ap, double);
87 argn->L = localeconv();
95 /*---------------------------------------------------------------------------
97 | Function : static void *Copy_This_Type(const void * argp)
99 | Description : Copy structure for numeric type argument.
101 | Return Values : Pointer to argument structure or NULL on error.
102 +--------------------------------------------------------------------------*/
104 Copy_This_Type(const void *argp)
106 const thisARG *ap = (const thisARG *)argp;
107 thisARG *result = (thisARG *) 0;
111 result = typeMalloc(thisARG, 1);
114 T((T_CREATE("thisARG %p"), result));
118 return (void *)result;
121 /*---------------------------------------------------------------------------
122 | Facility : libnform
123 | Function : static void Free_This_Type(void * argp)
125 | Description : Free structure for numeric type argument.
128 +--------------------------------------------------------------------------*/
130 Free_This_Type(void *argp)
136 /*---------------------------------------------------------------------------
137 | Facility : libnform
138 | Function : static bool Check_This_Field(FIELD * field,
141 | Description : Validate buffer content to be a valid numeric value
143 | Return Values : TRUE - field is valid
144 | FALSE - field is invalid
145 +--------------------------------------------------------------------------*/
147 Check_This_Field(FIELD *field, const void *argp)
149 const thisARG *argn = (const thisARG *)argp;
150 double low = argn->low;
151 double high = argn->high;
152 int prec = argn->precision;
153 unsigned char *bp = (unsigned char *)field_buffer(field, 0);
154 char *s = (char *)bp;
156 struct lconv *L = argn->L;
160 while (*bp && *bp == ' ')
164 if (*bp == '-' || *bp == '+')
166 #if USE_WIDEC_SUPPORT
173 wchar_t *list = _nc_Widen_String((char *)bp, &len);
178 for (n = 0; n < len; ++n)
188 else if (list[n] == ' ')
192 else if (isDecimalPoint(list[n]))
200 else if (!isDigit(list[n]))
212 if (!isdigit(UChar(*bp)))
216 if (isDecimalPoint(*bp))
221 if (!isdigit(UChar(*bp)))
226 while (*bp && *bp == ' ')
228 result = (*bp == '\0');
235 if (val < low || val > high)
240 sprintf(buf, "%.*f", (prec > 0 ? prec : 0), val);
241 set_field_buffer(field, 0, buf);
248 /*---------------------------------------------------------------------------
249 | Facility : libnform
250 | Function : static bool Check_This_Character(
254 | Description : Check a character for the numeric type.
256 | Return Values : TRUE - character is valid
257 | FALSE - character is invalid
258 +--------------------------------------------------------------------------*/
260 Check_This_Character(int c, const void *argp)
262 const thisARG *argn = (const thisARG *)argp;
263 struct lconv *L = argn->L;
265 return ((isDigit(c) ||
273 static FIELDTYPE typeTHIS =
275 _HAS_ARGS | _RESIDENT,
276 1, /* this is mutable, so we can't be const */
283 Check_This_Character,
288 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_NUMERIC = &typeTHIS;
290 /* fty_num.c ends here */