missing ncurses sources
[tomato.git] / release / src / router / libncurses / form / fty_int.c
blobe643ad975fc52c5434484d59537e3742dd51ac82
1 /****************************************************************************
2 * Copyright (c) 1998-2009,2010 Free Software Foundation, Inc. *
3 * *
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: *
11 * *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
14 * *
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. *
22 * *
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 *
26 * authorization. *
27 ****************************************************************************/
29 /***************************************************************************
30 * *
31 * Author : Juergen Pfeifer *
32 * *
33 ***************************************************************************/
35 #include "form.priv.h"
37 MODULE_ID("$Id: fty_int.c,v 1.25 2010/01/23 21:14:36 tom Exp $")
39 #if USE_WIDEC_SUPPORT
40 #define isDigit(c) (iswdigit((wint_t)(c)) || isdigit(UChar(c)))
41 #else
42 #define isDigit(c) isdigit(UChar(c))
43 #endif
45 #define thisARG integerARG
47 typedef struct
49 int precision;
50 long low;
51 long high;
53 thisARG;
55 typedef struct
57 int precision;
58 long low;
59 long high;
61 integerPARM;
63 /*---------------------------------------------------------------------------
64 | Facility : libnform
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 +--------------------------------------------------------------------------*/
71 static void *
72 Generic_This_Type(void *arg)
74 thisARG *argp = (thisARG *) 0;
75 thisARG *param = (thisARG *) arg;
77 if (param)
79 argp = typeMalloc(thisARG, 1);
81 if (argp)
83 T((T_CREATE("thisARG %p"), (void *)argp));
84 *argp = *param;
87 return (void *)argp;
90 /*---------------------------------------------------------------------------
91 | Facility : libnform
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 +--------------------------------------------------------------------------*/
98 static void *
99 Make_This_Type(va_list *ap)
101 thisARG arg;
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 +--------------------------------------------------------------------------*/
118 static void *
119 Copy_This_Type(const void *argp)
121 const thisARG *ap = (const thisARG *)argp;
122 thisARG *result = (thisARG *) 0;
124 if (argp)
126 result = typeMalloc(thisARG, 1);
127 if (result)
129 T((T_CREATE("thisARG %p"), (void *)result));
130 *result = *ap;
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.
142 | Return Values : -
143 +--------------------------------------------------------------------------*/
144 static void
145 Free_This_Type(void *argp)
147 if (argp)
148 free(argp);
151 /*---------------------------------------------------------------------------
152 | Facility : libnform
153 | Function : static bool Check_This_Field(
154 | FIELD * field,
155 | const void * argp)
157 | Description : Validate buffer content to be a valid integer value
159 | Return Values : TRUE - field is valid
160 | FALSE - field is invalid
161 +--------------------------------------------------------------------------*/
162 static bool
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;
171 long val;
172 char buf[100];
173 bool result = FALSE;
175 while (*bp && *bp == ' ')
176 bp++;
177 if (*bp)
179 if (*bp == '-')
180 bp++;
181 #if USE_WIDEC_SUPPORT
182 if (*bp)
184 bool blank = FALSE;
185 int len;
186 int n;
187 wchar_t *list = _nc_Widen_String((char *)bp, &len);
189 if (list != 0)
191 result = TRUE;
192 for (n = 0; n < len; ++n)
194 if (blank)
196 if (list[n] != ' ')
198 result = FALSE;
199 break;
202 else if (list[n] == ' ')
204 blank = TRUE;
206 else if (!isDigit(list[n]))
208 result = FALSE;
209 break;
212 free(list);
215 #else
216 while (*bp)
218 if (!isdigit(UChar(*bp)))
219 break;
220 bp++;
222 while (*bp && *bp == ' ')
223 bp++;
224 result = (*bp == '\0');
225 #endif
226 if (result)
228 val = atol(s);
229 if (low < high)
231 if (val < low || val > high)
232 result = FALSE;
234 if (result)
236 sprintf(buf, "%.*ld", (prec > 0 ? prec : 0), val);
237 set_field_buffer(field, 0, buf);
241 return (result);
244 /*---------------------------------------------------------------------------
245 | Facility : libnform
246 | Function : static bool Check_This_Character(
247 | int c,
248 | const void * argp)
250 | Description : Check a character for the integer type.
252 | Return Values : TRUE - character is valid
253 | FALSE - character is invalid
254 +--------------------------------------------------------------------------*/
255 static bool
256 Check_This_Character(int c, const void *argp GCC_UNUSED)
258 return ((isDigit(UChar(c)) || (c == '-')) ? TRUE : FALSE);
261 static FIELDTYPE typeTHIS =
263 _HAS_ARGS | _RESIDENT,
264 1, /* this is mutable, so we can't be const */
265 (FIELDTYPE *)0,
266 (FIELDTYPE *)0,
267 Make_This_Type,
268 Copy_This_Type,
269 Free_This_Type,
270 INIT_FT_FUNC(Check_This_Field),
271 INIT_FT_FUNC(Check_This_Character),
272 INIT_FT_FUNC(NULL),
273 INIT_FT_FUNC(NULL),
274 #if NCURSES_INTEROP_FUNCS
275 Generic_This_Type
276 #endif
279 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_INTEGER = &typeTHIS;
281 #if NCURSES_INTEROP_FUNCS
282 /* The next routines are to simplify the use of ncurses from
283 programming languages with restictions on interop with C level
284 constructs (e.g. variable access or va_list + ellipsis constructs)
286 NCURSES_EXPORT(FIELDTYPE *)
287 _nc_TYPE_INTEGER(void)
289 return TYPE_INTEGER;
291 #endif
293 /* fty_int.c ends here */