1 /****************************************************************************
2 * Copyright (c) 1998-2006,2007 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.22 2007/10/13 19:32:40 tom Exp $")
40 #define isDigit(c) (iswdigit((wint_t)(c)) || isdigit(UChar(c)))
42 #define isDigit(c) isdigit(UChar(c))
45 #define thisARG integerARG
55 /*---------------------------------------------------------------------------
57 | Function : static void *Make_This_Type( va_list * ap )
59 | Description : Allocate structure for integer type argument.
61 | Return Values : Pointer to argument structure or NULL on error
62 +--------------------------------------------------------------------------*/
64 Make_This_Type(va_list *ap)
66 thisARG *argp = typeMalloc(thisARG, 1);
70 T((T_CREATE("thisARG %p"), argp));
71 argp->precision = va_arg(*ap, int);
72 argp->low = va_arg(*ap, long);
73 argp->high = va_arg(*ap, long);
78 /*---------------------------------------------------------------------------
80 | Function : static void *Copy_This_Type(const void * argp)
82 | Description : Copy structure for integer type argument.
84 | Return Values : Pointer to argument structure or NULL on error.
85 +--------------------------------------------------------------------------*/
87 Copy_This_Type(const void *argp)
89 const thisARG *ap = (const thisARG *)argp;
90 thisARG *result = (thisARG *) 0;
94 result = typeMalloc(thisARG, 1);
97 T((T_CREATE("thisARG %p"), result));
101 return (void *)result;
104 /*---------------------------------------------------------------------------
105 | Facility : libnform
106 | Function : static void Free_This_Type(void * argp)
108 | Description : Free structure for integer type argument.
111 +--------------------------------------------------------------------------*/
113 Free_This_Type(void *argp)
119 /*---------------------------------------------------------------------------
120 | Facility : libnform
121 | Function : static bool Check_This_Field(
125 | Description : Validate buffer content to be a valid integer value
127 | Return Values : TRUE - field is valid
128 | FALSE - field is invalid
129 +--------------------------------------------------------------------------*/
131 Check_This_Field(FIELD *field, const void *argp)
133 const thisARG *argi = (const thisARG *)argp;
134 long low = argi->low;
135 long high = argi->high;
136 int prec = argi->precision;
137 unsigned char *bp = (unsigned char *)field_buffer(field, 0);
138 char *s = (char *)bp;
143 while (*bp && *bp == ' ')
149 #if USE_WIDEC_SUPPORT
155 wchar_t *list = _nc_Widen_String((char *)bp, &len);
160 for (n = 0; n < len; ++n)
170 else if (list[n] == ' ')
174 else if (!isDigit(list[n]))
186 if (!isdigit(UChar(*bp)))
190 while (*bp && *bp == ' ')
192 result = (*bp == '\0');
199 if (val < low || val > high)
204 sprintf(buf, "%.*ld", (prec > 0 ? prec : 0), val);
205 set_field_buffer(field, 0, buf);
212 /*---------------------------------------------------------------------------
213 | Facility : libnform
214 | Function : static bool Check_This_Character(
218 | Description : Check a character for the integer type.
220 | Return Values : TRUE - character is valid
221 | FALSE - character is invalid
222 +--------------------------------------------------------------------------*/
224 Check_This_Character(int c, const void *argp GCC_UNUSED)
226 return ((isDigit(UChar(c)) || (c == '-')) ? TRUE : FALSE);
229 static FIELDTYPE typeTHIS =
231 _HAS_ARGS | _RESIDENT,
232 1, /* this is mutable, so we can't be const */
239 Check_This_Character,
244 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_INTEGER = &typeTHIS;
246 /* fty_int.c ends here */