1 /* $OpenBSD: fty_num.c,v 1.9 2015/01/23 22:48:51 krw Exp $ */ 2 /**************************************************************************** 3 * Copyright (c) 1998-2006,2007 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.9 2015/01/23 22:48:51 krw Exp $") 39 40 #if HAVE_LOCALE_H 41 #include <locale.h> 42 #endif 43 44 #if HAVE_LOCALE_H 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 /*--------------------------------------------------------------------------- 68 | Facility : libnform 69 | Function : static void *Make_This_Type(va_list * ap) 70 | 71 | Description : Allocate structure for numeric type argument. 72 | 73 | Return Values : Pointer to argument structure or NULL on error 74 +--------------------------------------------------------------------------*/ 75 static void * 76 Make_This_Type(va_list *ap) 77 { 78 thisARG *argn = typeMalloc(thisARG, 1); 79 80 if (argn) 81 { 82 T((T_CREATE("thisARG %p"), argn)); 83 argn->precision = va_arg(*ap, int); 84 argn->low = va_arg(*ap, double); 85 argn->high = va_arg(*ap, double); 86 87 #if HAVE_LOCALE_H 88 argn->L = localeconv(); 89 #else 90 argn->L = NULL; 91 #endif 92 } 93 return (void *)argn; 94 } 95 96 /*--------------------------------------------------------------------------- 97 | Facility : libnform 98 | Function : static void *Copy_This_Type(const void * argp) 99 | 100 | Description : Copy structure for numeric type argument. 101 | 102 | Return Values : Pointer to argument structure or NULL on error. 103 +--------------------------------------------------------------------------*/ 104 static void * 105 Copy_This_Type(const void *argp) 106 { 107 const thisARG *ap = (const thisARG *)argp; 108 thisARG *result = (thisARG *) 0; 109 110 if (argp) 111 { 112 result = typeMalloc(thisARG, 1); 113 if (result) 114 { 115 T((T_CREATE("thisARG %p"), result)); 116 *result = *ap; 117 } 118 } 119 return (void *)result; 120 } 121 122 /*--------------------------------------------------------------------------- 123 | Facility : libnform 124 | Function : static void Free_This_Type(void * argp) 125 | 126 | Description : Free structure for numeric type argument. 127 | 128 | Return Values : - 129 +--------------------------------------------------------------------------*/ 130 static void 131 Free_This_Type(void *argp) 132 { 133 if (argp) 134 free(argp); 135 } 136 137 /*--------------------------------------------------------------------------- 138 | Facility : libnform 139 | Function : static bool Check_This_Field(FIELD * field, 140 | const void * argp) 141 | 142 | Description : Validate buffer content to be a valid numeric value 143 | 144 | Return Values : TRUE - field is valid 145 | FALSE - field is invalid 146 +--------------------------------------------------------------------------*/ 147 static bool 148 Check_This_Field(FIELD *field, const void *argp) 149 { 150 const thisARG *argn = (const thisARG *)argp; 151 double low = argn->low; 152 double high = argn->high; 153 int prec = argn->precision; 154 unsigned char *bp = (unsigned char *)field_buffer(field, 0); 155 char *s = (char *)bp; 156 double val = 0.0; 157 struct lconv *L = argn->L; 158 char buf[64]; 159 bool result = FALSE; 160 161 while (*bp && *bp == ' ') 162 bp++; 163 if (*bp) 164 { 165 if (*bp == '-' || *bp == '+') 166 bp++; 167 #if USE_WIDEC_SUPPORT 168 if (*bp) 169 { 170 bool blank = FALSE; 171 int state = 0; 172 int len; 173 int n; 174 wchar_t *list = _nc_Widen_String((char *)bp, &len); 175 176 if (list != 0) 177 { 178 result = TRUE; 179 for (n = 0; n < len; ++n) 180 { 181 if (blank) 182 { 183 if (list[n] != ' ') 184 { 185 result = FALSE; 186 break; 187 } 188 } 189 else if (list[n] == ' ') 190 { 191 blank = TRUE; 192 } 193 else if (isDecimalPoint(list[n])) 194 { 195 if (++state > 1) 196 { 197 result = FALSE; 198 break; 199 } 200 } 201 else if (!isDigit(list[n])) 202 { 203 result = FALSE; 204 break; 205 } 206 } 207 free(list); 208 } 209 } 210 #else 211 while (*bp) 212 { 213 if (!isdigit(UChar(*bp))) 214 break; 215 bp++; 216 } 217 if (isDecimalPoint(*bp)) 218 { 219 bp++; 220 while (*bp) 221 { 222 if (!isdigit(UChar(*bp))) 223 break; 224 bp++; 225 } 226 } 227 while (*bp && *bp == ' ') 228 bp++; 229 result = (*bp == '\0'); 230 #endif 231 if (result) 232 { 233 val = atof(s); 234 if (low < high) 235 { 236 if (val < low || val > high) 237 result = FALSE; 238 } 239 if (result) 240 { 241 snprintf(buf, sizeof(buf), "%.*f", (prec > 0 ? prec : 0), val); 242 set_field_buffer(field, 0, buf); 243 } 244 } 245 } 246 return (result); 247 } 248 249 /*--------------------------------------------------------------------------- 250 | Facility : libnform 251 | Function : static bool Check_This_Character( 252 | int c, 253 | const void * argp) 254 | 255 | Description : Check a character for the numeric type. 256 | 257 | Return Values : TRUE - character is valid 258 | FALSE - character is invalid 259 +--------------------------------------------------------------------------*/ 260 static bool 261 Check_This_Character(int c, const void *argp) 262 { 263 const thisARG *argn = (const thisARG *)argp; 264 struct lconv *L = argn->L; 265 266 return ((isDigit(c) || 267 c == '+' || 268 c == '-' || 269 isDecimalPoint(c)) 270 ? TRUE 271 : FALSE); 272 } 273 274 static FIELDTYPE typeTHIS = 275 { 276 _HAS_ARGS | _RESIDENT, 277 1, /* this is mutable, so we can't be const */ 278 (FIELDTYPE *)0, 279 (FIELDTYPE *)0, 280 Make_This_Type, 281 Copy_This_Type, 282 Free_This_Type, 283 Check_This_Field, 284 Check_This_Character, 285 NULL, 286 NULL 287 }; 288 289 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_NUMERIC = &typeTHIS; 290 291 /* fty_num.c ends here */ 292