1 /****************************************************************************
2 * Copyright (c) 1998-2009,2010 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_num.c,v 1.28 2010/01/23 21:14:36 tom Exp $")
44 #define isDecimalPoint(c) ((c) == ((L && L->decimal_point) ? *(L->decimal_point) : '.'))
46 #define isDecimalPoint(c) ((c) == '.')
50 #define isDigit(c) (iswdigit((wint_t)(c)) || isdigit(UChar(c)))
52 #define isDigit(c) isdigit(UChar(c))
55 #define thisARG numericARG
74 /*---------------------------------------------------------------------------
76 | Function : static void *Generic_This_Type(void * arg)
78 | Description : Allocate structure for numeric type argument.
80 | Return Values : Pointer to argument structure or NULL on error
81 +--------------------------------------------------------------------------*/
83 Generic_This_Type(void *arg
)
85 thisARG
*argn
= (thisARG
*) 0;
86 thisPARM
*args
= (thisPARM
*) arg
;
90 argn
= typeMalloc(thisARG
, 1);
94 T((T_CREATE("thisARG %p"), (void *)argn
));
95 argn
->precision
= args
->precision
;
96 argn
->low
= args
->low
;
97 argn
->high
= args
->high
;
100 argn
->L
= localeconv();
109 /*---------------------------------------------------------------------------
110 | Facility : libnform
111 | Function : static void *Make_This_Type(va_list * ap)
113 | Description : Allocate structure for numeric type argument.
115 | Return Values : Pointer to argument structure or NULL on error
116 +--------------------------------------------------------------------------*/
118 Make_This_Type(va_list *ap
)
122 arg
.precision
= va_arg(*ap
, int);
123 arg
.low
= va_arg(*ap
, double);
124 arg
.high
= va_arg(*ap
, double);
126 return Generic_This_Type((void *)&arg
);
129 /*---------------------------------------------------------------------------
130 | Facility : libnform
131 | Function : static void *Copy_This_Type(const void * argp)
133 | Description : Copy structure for numeric type argument.
135 | Return Values : Pointer to argument structure or NULL on error.
136 +--------------------------------------------------------------------------*/
138 Copy_This_Type(const void *argp
)
140 const thisARG
*ap
= (const thisARG
*)argp
;
141 thisARG
*result
= (thisARG
*) 0;
145 result
= typeMalloc(thisARG
, 1);
148 T((T_CREATE("thisARG %p"), (void *)result
));
152 return (void *)result
;
155 /*---------------------------------------------------------------------------
156 | Facility : libnform
157 | Function : static void Free_This_Type(void * argp)
159 | Description : Free structure for numeric type argument.
162 +--------------------------------------------------------------------------*/
164 Free_This_Type(void *argp
)
170 /*---------------------------------------------------------------------------
171 | Facility : libnform
172 | Function : static bool Check_This_Field(FIELD * field,
175 | Description : Validate buffer content to be a valid numeric value
177 | Return Values : TRUE - field is valid
178 | FALSE - field is invalid
179 +--------------------------------------------------------------------------*/
181 Check_This_Field(FIELD
*field
, const void *argp
)
183 const thisARG
*argn
= (const thisARG
*)argp
;
184 double low
= argn
->low
;
185 double high
= argn
->high
;
186 int prec
= argn
->precision
;
187 unsigned char *bp
= (unsigned char *)field_buffer(field
, 0);
188 char *s
= (char *)bp
;
190 struct lconv
*L
= argn
->L
;
194 while (*bp
&& *bp
== ' ')
198 if (*bp
== '-' || *bp
== '+')
200 #if USE_WIDEC_SUPPORT
207 wchar_t *list
= _nc_Widen_String((char *)bp
, &len
);
212 for (n
= 0; n
< len
; ++n
)
222 else if (list
[n
] == ' ')
226 else if (isDecimalPoint(list
[n
]))
234 else if (!isDigit(list
[n
]))
246 if (!isdigit(UChar(*bp
)))
250 if (isDecimalPoint(*bp
))
255 if (!isdigit(UChar(*bp
)))
260 while (*bp
&& *bp
== ' ')
262 result
= (*bp
== '\0');
269 if (val
< low
|| val
> high
)
274 sprintf(buf
, "%.*f", (prec
> 0 ? prec
: 0), val
);
275 set_field_buffer(field
, 0, buf
);
282 /*---------------------------------------------------------------------------
283 | Facility : libnform
284 | Function : static bool Check_This_Character(
288 | Description : Check a character for the numeric type.
290 | Return Values : TRUE - character is valid
291 | FALSE - character is invalid
292 +--------------------------------------------------------------------------*/
294 Check_This_Character(int c
, const void *argp
)
296 const thisARG
*argn
= (const thisARG
*)argp
;
297 struct lconv
*L
= argn
->L
;
299 return ((isDigit(c
) ||
307 static FIELDTYPE typeTHIS
=
309 _HAS_ARGS
| _RESIDENT
,
310 1, /* this is mutable, so we can't be const */
316 INIT_FT_FUNC(Check_This_Field
),
317 INIT_FT_FUNC(Check_This_Character
),
320 #if NCURSES_INTEROP_FUNCS
325 NCURSES_EXPORT_VAR(FIELDTYPE
*) TYPE_NUMERIC
= &typeTHIS
;
327 #if NCURSES_INTEROP_FUNCS
328 /* The next routines are to simplify the use of ncurses from
329 programming languages with restictions on interop with C level
330 constructs (e.g. variable access or va_list + ellipsis constructs)
332 NCURSES_EXPORT(FIELDTYPE
*)
333 _nc_TYPE_NUMERIC(void)
339 /* fty_num.c ends here */