1 /**************************************************************************** 2 * Copyright 2019-2020,2021 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.37 2021/03/27 23:49:58 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 struct lconv *L = argn->L; 192 bool result = FALSE; 193 194 while (*bp == ' ') 195 bp++; 196 if (*bp) 197 { 198 if (*bp == '-' || *bp == '+') 199 bp++; 200 #if USE_WIDEC_SUPPORT 201 if (*bp) 202 { 203 int len; 204 wchar_t *list = _nc_Widen_String((char *)bp, &len); 205 206 if (list != 0) 207 { 208 bool blank = FALSE; 209 int state = 0; 210 int n; 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 double val = atof(s); 268 269 if (low < high) 270 { 271 if (val < low || val > high) 272 result = FALSE; 273 } 274 if (result) 275 { 276 char buf[64]; 277 278 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf)) 279 "%.*f", (prec > 0 ? prec : 0), val); 280 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 +--------------------------------------------------------------------------*/ 298 static bool 299 Check_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 312 static 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, 321 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 328 }; 329 330 FORM_EXPORT_VAR(FIELDTYPE *) TYPE_NUMERIC = &typeTHIS; 331 332 #if NCURSES_INTEROP_FUNCS 333 /* The next routines are to simplify the use of ncurses from 334 programming languages with restrictions on interop with C level 335 constructs (e.g. variable access or va_list + ellipsis constructs) 336 */ 337 FORM_EXPORT(FIELDTYPE *) 338 _nc_TYPE_NUMERIC(void) 339 { 340 return TYPE_NUMERIC; 341 } 342 #endif 343 344 /* fty_num.c ends here */ 345