1 /****************************************************************************
2 * Copyright 2019-2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2010,2012 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /***************************************************************************
32 * Author : Juergen Pfeifer *
34 ***************************************************************************/
36 #include "form.priv.h"
38 MODULE_ID("$Id: fty_num.c,v 1.37 2021/03/27 23:49:58 tom Exp $")
44 #if HAVE_LOCALE_H && HAVE_LOCALECONV
45 #define isDecimalPoint(c) ((c) == ((L && L->decimal_point) ? *(L->decimal_point) : '.'))
47 #define isDecimalPoint(c) ((c) == '.')
51 #define isDigit(c) (iswdigit((wint_t)(c)) || isdigit(UChar(c)))
53 #define isDigit(c) isdigit(UChar(c))
56 #define thisARG numericARG
75 /*---------------------------------------------------------------------------
77 | Function : static void *Generic_This_Type(void * arg)
79 | Description : Allocate structure for numeric type argument.
81 | Return Values : Pointer to argument structure or NULL on error
82 +--------------------------------------------------------------------------*/
84 Generic_This_Type(void *arg)
86 thisARG *argn = (thisARG *)0;
87 thisPARM *args = (thisPARM *)arg;
91 argn = typeMalloc(thisARG, 1);
95 T((T_CREATE("thisARG %p"), (void *)argn));
96 argn->precision = args->precision;
97 argn->low = args->low;
98 argn->high = args->high;
100 #if HAVE_LOCALE_H && HAVE_LOCALECONV
101 argn->L = localeconv();
110 /*---------------------------------------------------------------------------
111 | Facility : libnform
112 | Function : static void *Make_This_Type(va_list * ap)
114 | Description : Allocate structure for numeric type argument.
116 | Return Values : Pointer to argument structure or NULL on error
117 +--------------------------------------------------------------------------*/
119 Make_This_Type(va_list *ap)
123 arg.precision = va_arg(*ap, int);
124 arg.low = va_arg(*ap, double);
125 arg.high = va_arg(*ap, double);
127 return Generic_This_Type((void *)&arg);
130 /*---------------------------------------------------------------------------
131 | Facility : libnform
132 | Function : static void *Copy_This_Type(const void * argp)
134 | Description : Copy structure for numeric type argument.
136 | Return Values : Pointer to argument structure or NULL on error.
137 +--------------------------------------------------------------------------*/
139 Copy_This_Type(const void *argp)
141 const thisARG *ap = (const thisARG *)argp;
142 thisARG *result = (thisARG *)0;
146 result = typeMalloc(thisARG, 1);
150 T((T_CREATE("thisARG %p"), (void *)result));
154 return (void *)result;
157 /*---------------------------------------------------------------------------
158 | Facility : libnform
159 | Function : static void Free_This_Type(void * argp)
161 | Description : Free structure for numeric type argument.
164 +--------------------------------------------------------------------------*/
166 Free_This_Type(void *argp)
172 /*---------------------------------------------------------------------------
173 | Facility : libnform
174 | Function : static bool Check_This_Field(FIELD * field,
177 | Description : Validate buffer content to be a valid numeric value
179 | Return Values : TRUE - field is valid
180 | FALSE - field is invalid
181 +--------------------------------------------------------------------------*/
183 Check_This_Field(FIELD *field, const void *argp)
185 const thisARG *argn = (const thisARG *)argp;
186 double low = argn->low;
187 double high = argn->high;
188 int prec = argn->precision;
189 unsigned char *bp = (unsigned char *)field_buffer(field, 0);
190 char *s = (char *)bp;
191 struct lconv *L = argn->L;
198 if (*bp == '-' || *bp == '+')
200 #if USE_WIDEC_SUPPORT
204 wchar_t *list = _nc_Widen_String((char *)bp, &len);
213 for (n = 0; n < len; ++n)
223 else if (list[n] == ' ')
227 else if (isDecimalPoint(list[n]))
235 else if (!isDigit(list[n]))
247 if (!isdigit(UChar(*bp)))
251 if (isDecimalPoint(*bp))
256 if (!isdigit(UChar(*bp)))
261 while (*bp && *bp == ' ')
263 result = (*bp == '\0');
267 double val = atof(s);
271 if (val < low || val > high)
278 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf))
279 "%.*f", (prec > 0 ? prec : 0), val);
280 set_field_buffer(field, 0, buf);
287 /*---------------------------------------------------------------------------
288 | Facility : libnform
289 | Function : static bool Check_This_Character(
293 | Description : Check a character for the numeric type.
295 | Return Values : TRUE - character is valid
296 | FALSE - character is invalid
297 +--------------------------------------------------------------------------*/
299 Check_This_Character(int c, const void *argp)
301 const thisARG *argn = (const thisARG *)argp;
302 struct lconv *L = argn->L;
304 return ((isDigit(c) ||
312 static FIELDTYPE typeTHIS =
314 _HAS_ARGS | _RESIDENT,
315 1, /* this is mutable, so we can't be const */
321 INIT_FT_FUNC(Check_This_Field),
322 INIT_FT_FUNC(Check_This_Character),
325 #if NCURSES_INTEROP_FUNCS
330 FORM_EXPORT_VAR(FIELDTYPE *) TYPE_NUMERIC = &typeTHIS;
332 #if NCURSES_INTEROP_FUNCS
333 /* The next routines are to simplify the use of ncurses from
334 programming languages with restrictions on interop with C level
335 constructs (e.g. variable access or va_list + ellipsis constructs)
337 FORM_EXPORT(FIELDTYPE *)
338 _nc_TYPE_NUMERIC(void)
344 /* fty_num.c ends here */