xref: /freebsd/contrib/ncurses/form/fty_num.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /****************************************************************************
2  * Copyright 2019,2020 Thomas E. Dickey                                     *
3  * Copyright 1998-2010,2012 Free Software Foundation, Inc.                  *
4  *                                                                          *
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 
38 MODULE_ID("$Id: fty_num.c,v 1.36 2020/12/12 01:15:37 tom Exp $")
39 
40 #if HAVE_LOCALE_H
41 #include <locale.h>
42 #endif
43 
44 #if HAVE_LOCALE_H && HAVE_LOCALECONV
45 #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 
58 typedef struct
59   {
60     int precision;
61     double low;
62     double high;
63     struct lconv *L;
64   }
65 thisARG;
66 
67 typedef struct
68   {
69     int precision;
70     double low;
71     double high;
72   }
73 thisPARM;
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 +--------------------------------------------------------------------------*/
83 static void *
84 Generic_This_Type(void *arg)
85 {
86   thisARG *argn = (thisARG *)0;
87   thisPARM *args = (thisPARM *)arg;
88 
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 
100 #if HAVE_LOCALE_H && HAVE_LOCALECONV
101 	  argn->L = localeconv();
102 #else
103 	  argn->L = NULL;
104 #endif
105 	}
106     }
107   return (void *)argn;
108 }
109 
110 /*---------------------------------------------------------------------------
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 +--------------------------------------------------------------------------*/
118 static void *
119 Make_This_Type(va_list *ap)
120 {
121   thisPARM arg;
122 
123   arg.precision = va_arg(*ap, int);
124   arg.low = va_arg(*ap, double);
125   arg.high = va_arg(*ap, double);
126 
127   return Generic_This_Type((void *)&arg);
128 }
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 +--------------------------------------------------------------------------*/
138 static void *
139 Copy_This_Type(const void *argp)
140 {
141   const thisARG *ap = (const thisARG *)argp;
142   thisARG *result = (thisARG *)0;
143 
144   if (argp)
145     {
146       result = typeMalloc(thisARG, 1);
147 
148       if (result)
149 	{
150 	  T((T_CREATE("thisARG %p"), (void *)result));
151 	  *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 +--------------------------------------------------------------------------*/
165 static void
166 Free_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 +--------------------------------------------------------------------------*/
182 static bool
183 Check_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;
191   double val = 0.0;
192   struct lconv *L = argn->L;
193   char buf[64];
194   bool result = FALSE;
195 
196   while (*bp && *bp == ' ')
197     bp++;
198   if (*bp)
199     {
200       if (*bp == '-' || *bp == '+')
201 	bp++;
202 #if USE_WIDEC_SUPPORT
203       if (*bp)
204 	{
205 	  bool blank = FALSE;
206 	  int state = 0;
207 	  int len;
208 	  int n;
209 	  wchar_t *list = _nc_Widen_String((char *)bp, &len);
210 
211 	  if (list != 0)
212 	    {
213 	      result = TRUE;
214 	      for (n = 0; n < len; ++n)
215 		{
216 		  if (blank)
217 		    {
218 		      if (list[n] != ' ')
219 			{
220 			  result = FALSE;
221 			  break;
222 			}
223 		    }
224 		  else if (list[n] == ' ')
225 		    {
226 		      blank = TRUE;
227 		    }
228 		  else if (isDecimalPoint(list[n]))
229 		    {
230 		      if (++state > 1)
231 			{
232 			  result = FALSE;
233 			  break;
234 			}
235 		    }
236 		  else if (!isDigit(list[n]))
237 		    {
238 		      result = FALSE;
239 		      break;
240 		    }
241 		}
242 	      free(list);
243 	    }
244 	}
245 #else
246       while (*bp)
247 	{
248 	  if (!isdigit(UChar(*bp)))
249 	    break;
250 	  bp++;
251 	}
252       if (isDecimalPoint(*bp))
253 	{
254 	  bp++;
255 	  while (*bp)
256 	    {
257 	      if (!isdigit(UChar(*bp)))
258 		break;
259 	      bp++;
260 	    }
261 	}
262       while (*bp && *bp == ' ')
263 	bp++;
264       result = (*bp == '\0');
265 #endif
266       if (result)
267 	{
268 	  val = atof(s);
269 	  if (low < high)
270 	    {
271 	      if (val < low || val > high)
272 		result = FALSE;
273 	    }
274 	  if (result)
275 	    {
276 	      _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf))
277 			  "%.*f", (prec > 0 ? prec : 0), val);
278 	      set_field_buffer(field, 0, buf);
279 	    }
280 	}
281     }
282   return (result);
283 }
284 
285 /*---------------------------------------------------------------------------
286 |   Facility      :  libnform
287 |   Function      :  static bool Check_This_Character(
288 |                                      int c,
289 |                                      const void * argp)
290 |
291 |   Description   :  Check a character for the numeric type.
292 |
293 |   Return Values :  TRUE  - character is valid
294 |                    FALSE - character is invalid
295 +--------------------------------------------------------------------------*/
296 static bool
297 Check_This_Character(int c, const void *argp)
298 {
299   const thisARG *argn = (const thisARG *)argp;
300   struct lconv *L = argn->L;
301 
302   return ((isDigit(c) ||
303 	   c == '+' ||
304 	   c == '-' ||
305 	   isDecimalPoint(c))
306 	  ? TRUE
307 	  : FALSE);
308 }
309 
310 static FIELDTYPE typeTHIS =
311 {
312   _HAS_ARGS | _RESIDENT,
313   1,				/* this is mutable, so we can't be const */
314   (FIELDTYPE *)0,
315   (FIELDTYPE *)0,
316   Make_This_Type,
317   Copy_This_Type,
318   Free_This_Type,
319   INIT_FT_FUNC(Check_This_Field),
320   INIT_FT_FUNC(Check_This_Character),
321   INIT_FT_FUNC(NULL),
322   INIT_FT_FUNC(NULL),
323 #if NCURSES_INTEROP_FUNCS
324   Generic_This_Type
325 #endif
326 };
327 
328 FORM_EXPORT_VAR(FIELDTYPE *) TYPE_NUMERIC = &typeTHIS;
329 
330 #if NCURSES_INTEROP_FUNCS
331 /* The next routines are to simplify the use of ncurses from
332    programming languages with restrictions on interop with C level
333    constructs (e.g. variable access or va_list + ellipsis constructs)
334 */
335 FORM_EXPORT(FIELDTYPE *)
336 _nc_TYPE_NUMERIC(void)
337 {
338   return TYPE_NUMERIC;
339 }
340 #endif
341 
342 /* fty_num.c ends here */
343