1 /****************************************************************************
2 * Copyright (c) 1998-2010,2012 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.29 2012/02/23 10:02:15 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
74 /*---------------------------------------------------------------------------
76 | Function : static void *Generic_This_Type(void * arg)
78 | Description : Allocate structure for numeric type argument.
80 | Return Values : Pointer to argument structure or NULL on error
81 +--------------------------------------------------------------------------*/
83 Generic_This_Type(void *arg)
85 thisARG *argn = (thisARG *) 0;
86 thisPARM *args = (thisPARM *) arg;
90 argn = typeMalloc(thisARG, 1);
94 T((T_CREATE("thisARG %p"), (void *)argn));
95 argn->precision = args->precision;
96 argn->low = args->low;
97 argn->high = args->high;
100 argn->L = localeconv();
109 /*---------------------------------------------------------------------------
110 | Facility : libnform
111 | Function : static void *Make_This_Type(va_list * ap)
113 | Description : Allocate structure for numeric type argument.
115 | Return Values : Pointer to argument structure or NULL on error
116 +--------------------------------------------------------------------------*/
118 Make_This_Type(va_list *ap)
122 arg.precision = va_arg(*ap, int);
123 arg.low = va_arg(*ap, double);
124 arg.high = va_arg(*ap, double);
126 return Generic_This_Type((void *)&arg);
129 /*---------------------------------------------------------------------------
130 | Facility : libnform
131 | Function : static void *Copy_This_Type(const void * argp)
133 | Description : Copy structure for numeric type argument.
135 | Return Values : Pointer to argument structure or NULL on error.
136 +--------------------------------------------------------------------------*/
138 Copy_This_Type(const void *argp)
140 const thisARG *ap = (const thisARG *)argp;
141 thisARG *result = (thisARG *) 0;
145 result = typeMalloc(thisARG, 1);
148 T((T_CREATE("thisARG %p"), (void *)result));
152 return (void *)result;
155 /*---------------------------------------------------------------------------
156 | Facility : libnform
157 | Function : static void Free_This_Type(void * argp)
159 | Description : Free structure for numeric type argument.
162 +--------------------------------------------------------------------------*/
164 Free_This_Type(void *argp)
170 /*---------------------------------------------------------------------------
171 | Facility : libnform
172 | Function : static bool Check_This_Field(FIELD * field,
175 | Description : Validate buffer content to be a valid numeric value
177 | Return Values : TRUE - field is valid
178 | FALSE - field is invalid
179 +--------------------------------------------------------------------------*/
181 Check_This_Field(FIELD *field, const void *argp)
183 const thisARG *argn = (const thisARG *)argp;
184 double low = argn->low;
185 double high = argn->high;
186 int prec = argn->precision;
187 unsigned char *bp = (unsigned char *)field_buffer(field, 0);
188 char *s = (char *)bp;
190 struct lconv *L = argn->L;
194 while (*bp && *bp == ' ')
198 if (*bp == '-' || *bp == '+')
200 #if USE_WIDEC_SUPPORT
207 wchar_t *list = _nc_Widen_String((char *)bp, &len);
212 for (n = 0; n < len; ++n)
222 else if (list[n] == ' ')
226 else if (isDecimalPoint(list[n]))
234 else if (!isDigit(list[n]))
246 if (!isdigit(UChar(*bp)))
250 if (isDecimalPoint(*bp))
255 if (!isdigit(UChar(*bp)))
260 while (*bp && *bp == ' ')
262 result = (*bp == '\0');
269 if (val < low || val > high)
274 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf))
275 "%.*f", (prec > 0 ? prec : 0), val);
276 set_field_buffer(field, 0, buf);
283 /*---------------------------------------------------------------------------
284 | Facility : libnform
285 | Function : static bool Check_This_Character(
289 | Description : Check a character for the numeric type.
291 | Return Values : TRUE - character is valid
292 | FALSE - character is invalid
293 +--------------------------------------------------------------------------*/
295 Check_This_Character(int c, const void *argp)
297 const thisARG *argn = (const thisARG *)argp;
298 struct lconv *L = argn->L;
300 return ((isDigit(c) ||
308 static FIELDTYPE typeTHIS =
310 _HAS_ARGS | _RESIDENT,
311 1, /* this is mutable, so we can't be const */
317 INIT_FT_FUNC(Check_This_Field),
318 INIT_FT_FUNC(Check_This_Character),
321 #if NCURSES_INTEROP_FUNCS
326 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_NUMERIC = &typeTHIS;
328 #if NCURSES_INTEROP_FUNCS
329 /* The next routines are to simplify the use of ncurses from
330 programming languages with restictions on interop with C level
331 constructs (e.g. variable access or va_list + ellipsis constructs)
333 NCURSES_EXPORT(FIELDTYPE *)
334 _nc_TYPE_NUMERIC(void)
340 /* fty_num.c ends here */