xref: /freebsd/contrib/ncurses/form/fty_num.c (revision b4e38a41f584ad4391c04b8cfec81f46176b18b0)
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.32 2020/02/02 23:34:34 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       if (result)
148 	{
149 	  T((T_CREATE("thisARG %p"), (void *)result));
150 	  *result = *ap;
151 	}
152     }
153   return (void *)result;
154 }
155 
156 /*---------------------------------------------------------------------------
157 |   Facility      :  libnform
158 |   Function      :  static void Free_This_Type(void * argp)
159 |
160 |   Description   :  Free structure for numeric type argument.
161 |
162 |   Return Values :  -
163 +--------------------------------------------------------------------------*/
164 static void
165 Free_This_Type(void *argp)
166 {
167   if (argp)
168     free(argp);
169 }
170 
171 /*---------------------------------------------------------------------------
172 |   Facility      :  libnform
173 |   Function      :  static bool Check_This_Field(FIELD * field,
174 |                                                 const void * argp)
175 |
176 |   Description   :  Validate buffer content to be a valid numeric value
177 |
178 |   Return Values :  TRUE  - field is valid
179 |                    FALSE - field is invalid
180 +--------------------------------------------------------------------------*/
181 static bool
182 Check_This_Field(FIELD *field, const void *argp)
183 {
184   const thisARG *argn = (const thisARG *)argp;
185   double low = argn->low;
186   double high = argn->high;
187   int prec = argn->precision;
188   unsigned char *bp = (unsigned char *)field_buffer(field, 0);
189   char *s = (char *)bp;
190   double val = 0.0;
191   struct lconv *L = argn->L;
192   char buf[64];
193   bool result = FALSE;
194 
195   while (*bp && *bp == ' ')
196     bp++;
197   if (*bp)
198     {
199       if (*bp == '-' || *bp == '+')
200 	bp++;
201 #if USE_WIDEC_SUPPORT
202       if (*bp)
203 	{
204 	  bool blank = FALSE;
205 	  int state = 0;
206 	  int len;
207 	  int n;
208 	  wchar_t *list = _nc_Widen_String((char *)bp, &len);
209 
210 	  if (list != 0)
211 	    {
212 	      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 	{
267 	  val = atof(s);
268 	  if (low < high)
269 	    {
270 	      if (val < low || val > high)
271 		result = FALSE;
272 	    }
273 	  if (result)
274 	    {
275 	      _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf))
276 			  "%.*f", (prec > 0 ? prec : 0), val);
277 	      set_field_buffer(field, 0, buf);
278 	    }
279 	}
280     }
281   return (result);
282 }
283 
284 /*---------------------------------------------------------------------------
285 |   Facility      :  libnform
286 |   Function      :  static bool Check_This_Character(
287 |                                      int c,
288 |                                      const void * argp)
289 |
290 |   Description   :  Check a character for the numeric type.
291 |
292 |   Return Values :  TRUE  - character is valid
293 |                    FALSE - character is invalid
294 +--------------------------------------------------------------------------*/
295 static bool
296 Check_This_Character(int c, const void *argp)
297 {
298   const thisARG *argn = (const thisARG *)argp;
299   struct lconv *L = argn->L;
300 
301   return ((isDigit(c) ||
302 	   c == '+' ||
303 	   c == '-' ||
304 	   isDecimalPoint(c))
305 	  ? TRUE
306 	  : FALSE);
307 }
308 
309 static FIELDTYPE typeTHIS =
310 {
311   _HAS_ARGS | _RESIDENT,
312   1,				/* this is mutable, so we can't be const */
313   (FIELDTYPE *)0,
314   (FIELDTYPE *)0,
315   Make_This_Type,
316   Copy_This_Type,
317   Free_This_Type,
318   INIT_FT_FUNC(Check_This_Field),
319   INIT_FT_FUNC(Check_This_Character),
320   INIT_FT_FUNC(NULL),
321   INIT_FT_FUNC(NULL),
322 #if NCURSES_INTEROP_FUNCS
323   Generic_This_Type
324 #endif
325 };
326 
327 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_NUMERIC = &typeTHIS;
328 
329 #if NCURSES_INTEROP_FUNCS
330 /* The next routines are to simplify the use of ncurses from
331    programming languages with restictions on interop with C level
332    constructs (e.g. variable access or va_list + ellipsis constructs)
333 */
334 NCURSES_EXPORT(FIELDTYPE *)
335 _nc_TYPE_NUMERIC(void)
336 {
337   return TYPE_NUMERIC;
338 }
339 #endif
340 
341 /* fty_num.c ends here */
342