Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1 | /**************************************************************************** |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 2 | * Copyright 2019-2020,2021 Thomas E. Dickey * |
| 3 | * Copyright 1998-2010,2012 Free Software Foundation, Inc. * |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 38 | MODULE_ID("$Id: fty_num.c,v 1.37 2021/03/27 23:49:58 tom Exp $") |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 39 | |
| 40 | #if HAVE_LOCALE_H |
| 41 | #include <locale.h> |
| 42 | #endif |
| 43 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 44 | #if HAVE_LOCALE_H && HAVE_LOCALECONV |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 86 | thisARG *argn = (thisARG *)0; |
| 87 | thisPARM *args = (thisPARM *)arg; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 100 | #if HAVE_LOCALE_H && HAVE_LOCALECONV |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 101 | argn->L = localeconv(); |
| 102 | #else |
| 103 | argn->L = NULL; |
| 104 | #endif |
| 105 | } |
| 106 | } |
| 107 | return (void *)argn; |
| 108 | } |
| 109 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 121 | thisPARM arg; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 122 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 123 | arg.precision = va_arg(*ap, int); |
| 124 | arg.low = va_arg(*ap, double); |
| 125 | arg.high = va_arg(*ap, double); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 126 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 127 | return Generic_This_Type((void *)&arg); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 142 | thisARG *result = (thisARG *)0; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 143 | |
| 144 | if (argp) |
| 145 | { |
| 146 | result = typeMalloc(thisARG, 1); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 147 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 148 | if (result) |
| 149 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 150 | T((T_CREATE("thisARG %p"), (void *)result)); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 191 | struct lconv *L = argn->L; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 192 | bool result = FALSE; |
| 193 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 194 | while (*bp == ' ') |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 195 | bp++; |
| 196 | if (*bp) |
| 197 | { |
| 198 | if (*bp == '-' || *bp == '+') |
| 199 | bp++; |
| 200 | #if USE_WIDEC_SUPPORT |
| 201 | if (*bp) |
| 202 | { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 203 | int len; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 204 | wchar_t *list = _nc_Widen_String((char *)bp, &len); |
| 205 | |
| 206 | if (list != 0) |
| 207 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 208 | bool blank = FALSE; |
| 209 | int state = 0; |
| 210 | int n; |
| 211 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 267 | double val = atof(s); |
| 268 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 269 | if (low < high) |
| 270 | { |
| 271 | if (val < low || val > high) |
| 272 | result = FALSE; |
| 273 | } |
| 274 | if (result) |
| 275 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 276 | char buf[64]; |
| 277 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 278 | _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf)) |
| 279 | "%.*f", (prec > 0 ? prec : 0), val); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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, |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 328 | }; |
| 329 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 330 | FORM_EXPORT_VAR(FIELDTYPE *) TYPE_NUMERIC = &typeTHIS; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 331 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 332 | #if NCURSES_INTEROP_FUNCS |
| 333 | /* The next routines are to simplify the use of ncurses from |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 334 | programming languages with restrictions on interop with C level |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 335 | constructs (e.g. variable access or va_list + ellipsis constructs) |
| 336 | */ |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 337 | FORM_EXPORT(FIELDTYPE *) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 338 | _nc_TYPE_NUMERIC(void) |
| 339 | { |
| 340 | return TYPE_NUMERIC; |
| 341 | } |
| 342 | #endif |
| 343 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 344 | /* fty_num.c ends here */ |