1 /****************************************************************************
2 * Copyright 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_int.c,v 1.33 2021/06/17 21:11:08 tom Exp $")
41 #define isDigit(c) (iswdigit((wint_t)(c)) || isdigit(UChar(c)))
43 #define isDigit(c) isdigit(UChar(c))
46 #define thisARG integerARG
64 /*---------------------------------------------------------------------------
66 | Function : static void *Generic_This_Type( void * arg )
68 | Description : Allocate structure for integer type argument.
70 | Return Values : Pointer to argument structure or NULL on error
71 +--------------------------------------------------------------------------*/
73 Generic_This_Type(void *arg)
75 thisARG *argp = (thisARG *)0;
76 thisARG *param = (thisARG *)arg;
80 argp = typeMalloc(thisARG, 1);
84 T((T_CREATE("thisARG %p"), (void *)argp));
91 /*---------------------------------------------------------------------------
93 | Function : static void *Make_This_Type( va_list * ap )
95 | Description : Allocate structure for integer type argument.
97 | Return Values : Pointer to argument structure or NULL on error
98 +--------------------------------------------------------------------------*/
100 Make_This_Type(va_list *ap)
104 arg.precision = va_arg(*ap, int);
105 arg.low = va_arg(*ap, long);
106 arg.high = va_arg(*ap, long);
108 return Generic_This_Type((void *)&arg);
111 /*---------------------------------------------------------------------------
112 | Facility : libnform
113 | Function : static void *Copy_This_Type(const void * argp)
115 | Description : Copy structure for integer type argument.
117 | Return Values : Pointer to argument structure or NULL on error.
118 +--------------------------------------------------------------------------*/
120 Copy_This_Type(const void *argp)
122 const thisARG *ap = (const thisARG *)argp;
123 thisARG *result = (thisARG *)0;
127 result = typeMalloc(thisARG, 1);
131 T((T_CREATE("thisARG %p"), (void *)result));
135 return (void *)result;
138 /*---------------------------------------------------------------------------
139 | Facility : libnform
140 | Function : static void Free_This_Type(void * argp)
142 | Description : Free structure for integer type argument.
145 +--------------------------------------------------------------------------*/
147 Free_This_Type(void *argp)
153 /*---------------------------------------------------------------------------
154 | Facility : libnform
155 | Function : static bool Check_This_Field(
159 | Description : Validate buffer content to be a valid integer value
161 | Return Values : TRUE - field is valid
162 | FALSE - field is invalid
163 +--------------------------------------------------------------------------*/
165 Check_This_Field(FIELD *field, const void *argp)
167 const thisARG *argi = (const thisARG *)argp;
168 long low = argi->low;
169 long high = argi->high;
170 int prec = argi->precision;
171 unsigned char *bp = (unsigned char *)field_buffer(field, 0);
172 char *s = (char *)bp;
181 #if USE_WIDEC_SUPPORT
185 wchar_t *list = _nc_Widen_String((char *)bp, &len);
193 for (n = 0; n < len; ++n)
203 else if (list[n] == ' ')
207 else if (!isDigit(list[n]))
219 if (!isdigit(UChar(*bp)))
223 while (*bp && *bp == ' ')
225 result = (*bp == '\0');
233 if (val < low || val > high)
240 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf))
241 "%.*ld", (prec > 0 ? prec : 0), val);
242 set_field_buffer(field, 0, buf);
249 /*---------------------------------------------------------------------------
250 | Facility : libnform
251 | Function : static bool Check_This_Character(
255 | Description : Check a character for the integer type.
257 | Return Values : TRUE - character is valid
258 | FALSE - character is invalid
259 +--------------------------------------------------------------------------*/
261 Check_This_Character(int c, const void *argp GCC_UNUSED)
263 return ((isDigit(UChar(c)) || (c == '-')) ? TRUE : FALSE);
266 static FIELDTYPE typeTHIS =
268 _HAS_ARGS | _RESIDENT,
269 1, /* this is mutable, so we can't be const */
275 INIT_FT_FUNC(Check_This_Field),
276 INIT_FT_FUNC(Check_This_Character),
279 #if NCURSES_INTEROP_FUNCS
284 FORM_EXPORT_VAR(FIELDTYPE *) TYPE_INTEGER = &typeTHIS;
286 #if NCURSES_INTEROP_FUNCS
287 /* The next routines are to simplify the use of ncurses from
288 programming languages with restrictions on interop with C level
289 constructs (e.g. variable access or va_list + ellipsis constructs)
291 FORM_EXPORT(FIELDTYPE *)
292 _nc_TYPE_INTEGER(void)
298 /* fty_int.c ends here */