blob: 34cef6364bdb7bd2537365380dfd1b01a0d17ceb [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2019-2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2010,2012 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29
30/***************************************************************************
31* *
32* Author : Juergen Pfeifer *
33* *
34***************************************************************************/
35
36#include "form.priv.h"
37
micky3879b9f5e72025-07-08 18:04:53 -040038MODULE_ID("$Id: fty_num.c,v 1.37 2021/03/27 23:49:58 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053039
40#if HAVE_LOCALE_H
41#include <locale.h>
42#endif
43
micky3879b9f5e72025-07-08 18:04:53 -040044#if HAVE_LOCALE_H && HAVE_LOCALECONV
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053045#define isDecimalPoint(c) ((c) == ((L && L->decimal_point) ? *(L->decimal_point) : '.'))
46#else
47#define isDecimalPoint(c) ((c) == '.')
48#endif
49
50#if USE_WIDEC_SUPPORT
51#define isDigit(c) (iswdigit((wint_t)(c)) || isdigit(UChar(c)))
52#else
53#define isDigit(c) isdigit(UChar(c))
54#endif
55
56#define thisARG numericARG
57
58typedef struct
59 {
60 int precision;
61 double low;
62 double high;
63 struct lconv *L;
64 }
65thisARG;
66
Steve Kondikae271bc2015-11-15 02:50:53 +010067typedef struct
68 {
69 int precision;
70 double low;
71 double high;
72 }
73thisPARM;
74
75/*---------------------------------------------------------------------------
76| Facility : libnform
77| Function : static void *Generic_This_Type(void * arg)
78|
79| Description : Allocate structure for numeric type argument.
80|
81| Return Values : Pointer to argument structure or NULL on error
82+--------------------------------------------------------------------------*/
83static void *
84Generic_This_Type(void *arg)
85{
micky3879b9f5e72025-07-08 18:04:53 -040086 thisARG *argn = (thisARG *)0;
87 thisPARM *args = (thisPARM *)arg;
Steve Kondikae271bc2015-11-15 02:50:53 +010088
89 if (args)
90 {
91 argn = typeMalloc(thisARG, 1);
92
93 if (argn)
94 {
95 T((T_CREATE("thisARG %p"), (void *)argn));
96 argn->precision = args->precision;
97 argn->low = args->low;
98 argn->high = args->high;
99
micky3879b9f5e72025-07-08 18:04:53 -0400100#if HAVE_LOCALE_H && HAVE_LOCALECONV
Steve Kondikae271bc2015-11-15 02:50:53 +0100101 argn->L = localeconv();
102#else
103 argn->L = NULL;
104#endif
105 }
106 }
107 return (void *)argn;
108}
109
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530110/*---------------------------------------------------------------------------
111| Facility : libnform
112| Function : static void *Make_This_Type(va_list * ap)
113|
114| Description : Allocate structure for numeric type argument.
115|
116| Return Values : Pointer to argument structure or NULL on error
117+--------------------------------------------------------------------------*/
118static void *
119Make_This_Type(va_list *ap)
120{
Steve Kondikae271bc2015-11-15 02:50:53 +0100121 thisPARM arg;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530122
Steve Kondikae271bc2015-11-15 02:50:53 +0100123 arg.precision = va_arg(*ap, int);
124 arg.low = va_arg(*ap, double);
125 arg.high = va_arg(*ap, double);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530126
Steve Kondikae271bc2015-11-15 02:50:53 +0100127 return Generic_This_Type((void *)&arg);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530128}
129
130/*---------------------------------------------------------------------------
131| Facility : libnform
132| Function : static void *Copy_This_Type(const void * argp)
133|
134| Description : Copy structure for numeric type argument.
135|
136| Return Values : Pointer to argument structure or NULL on error.
137+--------------------------------------------------------------------------*/
138static void *
139Copy_This_Type(const void *argp)
140{
141 const thisARG *ap = (const thisARG *)argp;
micky3879b9f5e72025-07-08 18:04:53 -0400142 thisARG *result = (thisARG *)0;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530143
144 if (argp)
145 {
146 result = typeMalloc(thisARG, 1);
micky3879b9f5e72025-07-08 18:04:53 -0400147
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530148 if (result)
149 {
Steve Kondikae271bc2015-11-15 02:50:53 +0100150 T((T_CREATE("thisARG %p"), (void *)result));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530151 *result = *ap;
152 }
153 }
154 return (void *)result;
155}
156
157/*---------------------------------------------------------------------------
158| Facility : libnform
159| Function : static void Free_This_Type(void * argp)
160|
161| Description : Free structure for numeric type argument.
162|
163| Return Values : -
164+--------------------------------------------------------------------------*/
165static void
166Free_This_Type(void *argp)
167{
168 if (argp)
169 free(argp);
170}
171
172/*---------------------------------------------------------------------------
173| Facility : libnform
174| Function : static bool Check_This_Field(FIELD * field,
175| const void * argp)
176|
177| Description : Validate buffer content to be a valid numeric value
178|
179| Return Values : TRUE - field is valid
180| FALSE - field is invalid
181+--------------------------------------------------------------------------*/
182static bool
183Check_This_Field(FIELD *field, const void *argp)
184{
185 const thisARG *argn = (const thisARG *)argp;
186 double low = argn->low;
187 double high = argn->high;
188 int prec = argn->precision;
189 unsigned char *bp = (unsigned char *)field_buffer(field, 0);
190 char *s = (char *)bp;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530191 struct lconv *L = argn->L;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530192 bool result = FALSE;
193
micky3879b9f5e72025-07-08 18:04:53 -0400194 while (*bp == ' ')
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530195 bp++;
196 if (*bp)
197 {
198 if (*bp == '-' || *bp == '+')
199 bp++;
200#if USE_WIDEC_SUPPORT
201 if (*bp)
202 {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530203 int len;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530204 wchar_t *list = _nc_Widen_String((char *)bp, &len);
205
206 if (list != 0)
207 {
micky3879b9f5e72025-07-08 18:04:53 -0400208 bool blank = FALSE;
209 int state = 0;
210 int n;
211
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530212 result = TRUE;
213 for (n = 0; n < len; ++n)
214 {
215 if (blank)
216 {
217 if (list[n] != ' ')
218 {
219 result = FALSE;
220 break;
221 }
222 }
223 else if (list[n] == ' ')
224 {
225 blank = TRUE;
226 }
227 else if (isDecimalPoint(list[n]))
228 {
229 if (++state > 1)
230 {
231 result = FALSE;
232 break;
233 }
234 }
235 else if (!isDigit(list[n]))
236 {
237 result = FALSE;
238 break;
239 }
240 }
241 free(list);
242 }
243 }
244#else
245 while (*bp)
246 {
247 if (!isdigit(UChar(*bp)))
248 break;
249 bp++;
250 }
251 if (isDecimalPoint(*bp))
252 {
253 bp++;
254 while (*bp)
255 {
256 if (!isdigit(UChar(*bp)))
257 break;
258 bp++;
259 }
260 }
261 while (*bp && *bp == ' ')
262 bp++;
263 result = (*bp == '\0');
264#endif
265 if (result)
266 {
micky3879b9f5e72025-07-08 18:04:53 -0400267 double val = atof(s);
268
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530269 if (low < high)
270 {
271 if (val < low || val > high)
272 result = FALSE;
273 }
274 if (result)
275 {
micky3879b9f5e72025-07-08 18:04:53 -0400276 char buf[64];
277
Steve Kondikae271bc2015-11-15 02:50:53 +0100278 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf))
279 "%.*f", (prec > 0 ? prec : 0), val);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530280 set_field_buffer(field, 0, buf);
281 }
282 }
283 }
284 return (result);
285}
286
287/*---------------------------------------------------------------------------
288| Facility : libnform
289| Function : static bool Check_This_Character(
290| int c,
291| const void * argp)
292|
293| Description : Check a character for the numeric type.
294|
295| Return Values : TRUE - character is valid
296| FALSE - character is invalid
297+--------------------------------------------------------------------------*/
298static bool
299Check_This_Character(int c, const void *argp)
300{
301 const thisARG *argn = (const thisARG *)argp;
302 struct lconv *L = argn->L;
303
304 return ((isDigit(c) ||
305 c == '+' ||
306 c == '-' ||
307 isDecimalPoint(c))
308 ? TRUE
309 : FALSE);
310}
311
312static FIELDTYPE typeTHIS =
313{
314 _HAS_ARGS | _RESIDENT,
315 1, /* this is mutable, so we can't be const */
316 (FIELDTYPE *)0,
317 (FIELDTYPE *)0,
318 Make_This_Type,
319 Copy_This_Type,
320 Free_This_Type,
Steve Kondikae271bc2015-11-15 02:50:53 +0100321 INIT_FT_FUNC(Check_This_Field),
322 INIT_FT_FUNC(Check_This_Character),
323 INIT_FT_FUNC(NULL),
324 INIT_FT_FUNC(NULL),
325#if NCURSES_INTEROP_FUNCS
326 Generic_This_Type
327#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530328};
329
micky3879b9f5e72025-07-08 18:04:53 -0400330FORM_EXPORT_VAR(FIELDTYPE *) TYPE_NUMERIC = &typeTHIS;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530331
Steve Kondikae271bc2015-11-15 02:50:53 +0100332#if NCURSES_INTEROP_FUNCS
333/* The next routines are to simplify the use of ncurses from
micky3879b9f5e72025-07-08 18:04:53 -0400334 programming languages with restrictions on interop with C level
Steve Kondikae271bc2015-11-15 02:50:53 +0100335 constructs (e.g. variable access or va_list + ellipsis constructs)
336*/
micky3879b9f5e72025-07-08 18:04:53 -0400337FORM_EXPORT(FIELDTYPE *)
Steve Kondikae271bc2015-11-15 02:50:53 +0100338_nc_TYPE_NUMERIC(void)
339{
340 return TYPE_NUMERIC;
341}
342#endif
343
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530344/* fty_num.c ends here */