1 /****************************************************************************
2 * Copyright 2019,2020 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.33 2020/05/24 01:40:20 anonymous.maarten 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);
149 T((T_CREATE("thisARG %p"), (void *)result));
153 return (void *)result;
156 /*---------------------------------------------------------------------------
157 | Facility : libnform
158 | Function : static void Free_This_Type(void * argp)
160 | Description : Free structure for numeric type argument.
163 +--------------------------------------------------------------------------*/
165 Free_This_Type(void *argp)
171 /*---------------------------------------------------------------------------
172 | Facility : libnform
173 | Function : static bool Check_This_Field(FIELD * field,
176 | Description : Validate buffer content to be a valid numeric value
178 | Return Values : TRUE - field is valid
179 | FALSE - field is invalid
180 +--------------------------------------------------------------------------*/
182 Check_This_Field(FIELD *field, const void *argp)
184 const thisARG *argn = (const thisARG *)argp;
185 double low = argn->low;
186 double high = argn->high;
187 int prec = argn->precision;
188 unsigned char *bp = (unsigned char *)field_buffer(field, 0);
189 char *s = (char *)bp;
191 struct lconv *L = argn->L;
195 while (*bp && *bp == ' ')
199 if (*bp == '-' || *bp == '+')
201 #if USE_WIDEC_SUPPORT
208 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');
270 if (val < low || val > high)
275 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf))
276 "%.*f", (prec > 0 ? prec : 0), val);
277 set_field_buffer(field, 0, buf);
284 /*---------------------------------------------------------------------------
285 | Facility : libnform
286 | Function : static bool Check_This_Character(
290 | Description : Check a character for the numeric type.
292 | Return Values : TRUE - character is valid
293 | FALSE - character is invalid
294 +--------------------------------------------------------------------------*/
296 Check_This_Character(int c, const void *argp)
298 const thisARG *argn = (const thisARG *)argp;
299 struct lconv *L = argn->L;
301 return ((isDigit(c) ||
309 static FIELDTYPE typeTHIS =
311 _HAS_ARGS | _RESIDENT,
312 1, /* this is mutable, so we can't be const */
318 INIT_FT_FUNC(Check_This_Field),
319 INIT_FT_FUNC(Check_This_Character),
322 #if NCURSES_INTEROP_FUNCS
327 FORM_EXPORT_VAR(FIELDTYPE*) TYPE_NUMERIC = &typeTHIS;
329 #if NCURSES_INTEROP_FUNCS
330 /* The next routines are to simplify the use of ncurses from
331 programming languages with restictions on interop with C level
332 constructs (e.g. variable access or va_list + ellipsis constructs)
334 FORM_EXPORT(FIELDTYPE *)
335 _nc_TYPE_NUMERIC(void)
341 /* fty_num.c ends here */