1 /****************************************************************************
2 * Copyright (c) 1998-2005,2006 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.20 2006/04/22 21:33:05 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 = (thisARG *) malloc(sizeof(thisARG));
70 argp->precision = va_arg(*ap, int);
71 argp->low = va_arg(*ap, long);
72 argp->high = va_arg(*ap, long);
77 /*---------------------------------------------------------------------------
79 | Function : static void *Copy_This_Type(const void * argp)
81 | Description : Copy structure for integer type argument.
83 | Return Values : Pointer to argument structure or NULL on error.
84 +--------------------------------------------------------------------------*/
86 Copy_This_Type(const void *argp)
88 const thisARG *ap = (const thisARG *)argp;
89 thisARG *result = (thisARG *) 0;
93 result = (thisARG *) malloc(sizeof(thisARG));
97 return (void *)result;
100 /*---------------------------------------------------------------------------
101 | Facility : libnform
102 | Function : static void Free_This_Type(void * argp)
104 | Description : Free structure for integer type argument.
107 +--------------------------------------------------------------------------*/
109 Free_This_Type(void *argp)
115 /*---------------------------------------------------------------------------
116 | Facility : libnform
117 | Function : static bool Check_This_Field(
121 | Description : Validate buffer content to be a valid integer value
123 | Return Values : TRUE - field is valid
124 | FALSE - field is invalid
125 +--------------------------------------------------------------------------*/
127 Check_This_Field(FIELD *field, const void *argp)
129 const thisARG *argi = (const thisARG *)argp;
130 long low = argi->low;
131 long high = argi->high;
132 int prec = argi->precision;
133 unsigned char *bp = (unsigned char *)field_buffer(field, 0);
134 char *s = (char *)bp;
139 while (*bp && *bp == ' ')
145 #if USE_WIDEC_SUPPORT
151 wchar_t *list = _nc_Widen_String((char *)bp, &len);
156 for (n = 0; n < len; ++n)
166 else if (list[n] == ' ')
170 else if (!isDigit(list[n]))
182 if (!isdigit(UChar(*bp)))
186 while (*bp && *bp == ' ')
188 result = (*bp == '\0');
195 if (val < low || val > high)
200 sprintf(buf, "%.*ld", (prec > 0 ? prec : 0), val);
201 set_field_buffer(field, 0, buf);
208 /*---------------------------------------------------------------------------
209 | Facility : libnform
210 | Function : static bool Check_This_Character(
214 | Description : Check a character for the integer type.
216 | Return Values : TRUE - character is valid
217 | FALSE - character is invalid
218 +--------------------------------------------------------------------------*/
220 Check_This_Character(int c, const void *argp GCC_UNUSED)
222 return ((isDigit(UChar(c)) || (c == '-')) ? TRUE : FALSE);
225 static FIELDTYPE typeTHIS =
227 _HAS_ARGS | _RESIDENT,
228 1, /* this is mutable, so we can't be const */
235 Check_This_Character,
240 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_INTEGER = &typeTHIS;
242 /* fty_int.c ends here */