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_int.c,v 1.26 2012/02/23 10:02:15 tom Exp $")
40 #define isDigit(c) (iswdigit((wint_t)(c)) || isdigit(UChar(c)))
42 #define isDigit(c) isdigit(UChar(c))
45 #define thisARG integerARG
63 /*---------------------------------------------------------------------------
65 | Function : static void *Generic_This_Type( void * arg )
67 | Description : Allocate structure for integer type argument.
69 | Return Values : Pointer to argument structure or NULL on error
70 +--------------------------------------------------------------------------*/
72 Generic_This_Type(void *arg)
74 thisARG *argp = (thisARG *) 0;
75 thisARG *param = (thisARG *) arg;
79 argp = typeMalloc(thisARG, 1);
83 T((T_CREATE("thisARG %p"), (void *)argp));
90 /*---------------------------------------------------------------------------
92 | Function : static void *Make_This_Type( va_list * ap )
94 | Description : Allocate structure for integer type argument.
96 | Return Values : Pointer to argument structure or NULL on error
97 +--------------------------------------------------------------------------*/
99 Make_This_Type(va_list *ap)
103 arg.precision = va_arg(*ap, int);
104 arg.low = va_arg(*ap, long);
105 arg.high = va_arg(*ap, long);
107 return Generic_This_Type((void *)&arg);
110 /*---------------------------------------------------------------------------
111 | Facility : libnform
112 | Function : static void *Copy_This_Type(const void * argp)
114 | Description : Copy structure for integer type argument.
116 | Return Values : Pointer to argument structure or NULL on error.
117 +--------------------------------------------------------------------------*/
119 Copy_This_Type(const void *argp)
121 const thisARG *ap = (const thisARG *)argp;
122 thisARG *result = (thisARG *) 0;
126 result = typeMalloc(thisARG, 1);
129 T((T_CREATE("thisARG %p"), (void *)result));
133 return (void *)result;
136 /*---------------------------------------------------------------------------
137 | Facility : libnform
138 | Function : static void Free_This_Type(void * argp)
140 | Description : Free structure for integer type argument.
143 +--------------------------------------------------------------------------*/
145 Free_This_Type(void *argp)
151 /*---------------------------------------------------------------------------
152 | Facility : libnform
153 | Function : static bool Check_This_Field(
157 | Description : Validate buffer content to be a valid integer value
159 | Return Values : TRUE - field is valid
160 | FALSE - field is invalid
161 +--------------------------------------------------------------------------*/
163 Check_This_Field(FIELD *field, const void *argp)
165 const thisARG *argi = (const thisARG *)argp;
166 long low = argi->low;
167 long high = argi->high;
168 int prec = argi->precision;
169 unsigned char *bp = (unsigned char *)field_buffer(field, 0);
170 char *s = (char *)bp;
175 while (*bp && *bp == ' ')
181 #if USE_WIDEC_SUPPORT
187 wchar_t *list = _nc_Widen_String((char *)bp, &len);
192 for (n = 0; n < len; ++n)
202 else if (list[n] == ' ')
206 else if (!isDigit(list[n]))
218 if (!isdigit(UChar(*bp)))
222 while (*bp && *bp == ' ')
224 result = (*bp == '\0');
231 if (val < low || val > high)
236 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf))
237 "%.*ld", (prec > 0 ? prec : 0), val);
238 set_field_buffer(field, 0, buf);
245 /*---------------------------------------------------------------------------
246 | Facility : libnform
247 | Function : static bool Check_This_Character(
251 | Description : Check a character for the integer type.
253 | Return Values : TRUE - character is valid
254 | FALSE - character is invalid
255 +--------------------------------------------------------------------------*/
257 Check_This_Character(int c, const void *argp GCC_UNUSED)
259 return ((isDigit(UChar(c)) || (c == '-')) ? TRUE : FALSE);
262 static FIELDTYPE typeTHIS =
264 _HAS_ARGS | _RESIDENT,
265 1, /* this is mutable, so we can't be const */
271 INIT_FT_FUNC(Check_This_Field),
272 INIT_FT_FUNC(Check_This_Character),
275 #if NCURSES_INTEROP_FUNCS
280 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_INTEGER = &typeTHIS;
282 #if NCURSES_INTEROP_FUNCS
283 /* The next routines are to simplify the use of ncurses from
284 programming languages with restictions on interop with C level
285 constructs (e.g. variable access or va_list + ellipsis constructs)
287 NCURSES_EXPORT(FIELDTYPE *)
288 _nc_TYPE_INTEGER(void)
294 /* fty_int.c ends here */