1 /**************************************************************************** 2 * Copyright (c) 1998-2010,2012 Free Software Foundation, Inc. * 3 * * 4 * Permission is hereby granted, free of charge, to any person obtaining a * 5 * copy of this software and associated documentation files (the * 6 * "Software"), to deal in the Software without restriction, including * 7 * without limitation the rights to use, copy, modify, merge, publish, * 8 * distribute, distribute with modifications, sublicense, and/or sell * 9 * copies of the Software, and to permit persons to whom the Software is * 10 * furnished to do so, subject to the following conditions: * 11 * * 12 * The above copyright notice and this permission notice shall be included * 13 * in all copies or substantial portions of the Software. * 14 * * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 22 * * 23 * Except as contained in this notice, the name(s) of the above copyright * 24 * holders shall not be used in advertising or otherwise to promote the * 25 * sale, use or other dealings in this Software without prior written * 26 * authorization. * 27 ****************************************************************************/ 28 29 /*************************************************************************** 30 * * 31 * Author : Juergen Pfeifer * 32 * * 33 ***************************************************************************/ 34 35 #include "form.priv.h" 36 37 MODULE_ID("$Id: fty_int.c,v 1.26 2012/02/23 10:02:15 tom Exp $") 38 39 #if USE_WIDEC_SUPPORT 40 #define isDigit(c) (iswdigit((wint_t)(c)) || isdigit(UChar(c))) 41 #else 42 #define isDigit(c) isdigit(UChar(c)) 43 #endif 44 45 #define thisARG integerARG 46 47 typedef struct 48 { 49 int precision; 50 long low; 51 long high; 52 } 53 thisARG; 54 55 typedef struct 56 { 57 int precision; 58 long low; 59 long high; 60 } 61 integerPARM; 62 63 /*--------------------------------------------------------------------------- 64 | Facility : libnform 65 | Function : static void *Generic_This_Type( void * arg ) 66 | 67 | Description : Allocate structure for integer type argument. 68 | 69 | Return Values : Pointer to argument structure or NULL on error 70 +--------------------------------------------------------------------------*/ 71 static void * 72 Generic_This_Type(void *arg) 73 { 74 thisARG *argp = (thisARG *) 0; 75 thisARG *param = (thisARG *) arg; 76 77 if (param) 78 { 79 argp = typeMalloc(thisARG, 1); 80 81 if (argp) 82 { 83 T((T_CREATE("thisARG %p"), (void *)argp)); 84 *argp = *param; 85 } 86 } 87 return (void *)argp; 88 } 89 90 /*--------------------------------------------------------------------------- 91 | Facility : libnform 92 | Function : static void *Make_This_Type( va_list * ap ) 93 | 94 | Description : Allocate structure for integer type argument. 95 | 96 | Return Values : Pointer to argument structure or NULL on error 97 +--------------------------------------------------------------------------*/ 98 static void * 99 Make_This_Type(va_list *ap) 100 { 101 thisARG arg; 102 103 arg.precision = va_arg(*ap, int); 104 arg.low = va_arg(*ap, long); 105 arg.high = va_arg(*ap, long); 106 107 return Generic_This_Type((void *)&arg); 108 } 109 110 /*--------------------------------------------------------------------------- 111 | Facility : libnform 112 | Function : static void *Copy_This_Type(const void * argp) 113 | 114 | Description : Copy structure for integer type argument. 115 | 116 | Return Values : Pointer to argument structure or NULL on error. 117 +--------------------------------------------------------------------------*/ 118 static void * 119 Copy_This_Type(const void *argp) 120 { 121 const thisARG *ap = (const thisARG *)argp; 122 thisARG *result = (thisARG *) 0; 123 124 if (argp) 125 { 126 result = typeMalloc(thisARG, 1); 127 if (result) 128 { 129 T((T_CREATE("thisARG %p"), (void *)result)); 130 *result = *ap; 131 } 132 } 133 return (void *)result; 134 } 135 136 /*--------------------------------------------------------------------------- 137 | Facility : libnform 138 | Function : static void Free_This_Type(void * argp) 139 | 140 | Description : Free structure for integer type argument. 141 | 142 | Return Values : - 143 +--------------------------------------------------------------------------*/ 144 static void 145 Free_This_Type(void *argp) 146 { 147 if (argp) 148 free(argp); 149 } 150 151 /*--------------------------------------------------------------------------- 152 | Facility : libnform 153 | Function : static bool Check_This_Field( 154 | FIELD * field, 155 | const void * argp) 156 | 157 | Description : Validate buffer content to be a valid integer value 158 | 159 | Return Values : TRUE - field is valid 160 | FALSE - field is invalid 161 +--------------------------------------------------------------------------*/ 162 static bool 163 Check_This_Field(FIELD *field, const void *argp) 164 { 165 const thisARG *argi = (const thisARG *)argp; 166 long low = argi->low; 167 long high = argi->high; 168 int prec = argi->precision; 169 unsigned char *bp = (unsigned char *)field_buffer(field, 0); 170 char *s = (char *)bp; 171 long val; 172 char buf[100]; 173 bool result = FALSE; 174 175 while (*bp && *bp == ' ') 176 bp++; 177 if (*bp) 178 { 179 if (*bp == '-') 180 bp++; 181 #if USE_WIDEC_SUPPORT 182 if (*bp) 183 { 184 bool blank = FALSE; 185 int len; 186 int n; 187 wchar_t *list = _nc_Widen_String((char *)bp, &len); 188 189 if (list != 0) 190 { 191 result = TRUE; 192 for (n = 0; n < len; ++n) 193 { 194 if (blank) 195 { 196 if (list[n] != ' ') 197 { 198 result = FALSE; 199 break; 200 } 201 } 202 else if (list[n] == ' ') 203 { 204 blank = TRUE; 205 } 206 else if (!isDigit(list[n])) 207 { 208 result = FALSE; 209 break; 210 } 211 } 212 free(list); 213 } 214 } 215 #else 216 while (*bp) 217 { 218 if (!isdigit(UChar(*bp))) 219 break; 220 bp++; 221 } 222 while (*bp && *bp == ' ') 223 bp++; 224 result = (*bp == '\0'); 225 #endif 226 if (result) 227 { 228 val = atol(s); 229 if (low < high) 230 { 231 if (val < low || val > high) 232 result = FALSE; 233 } 234 if (result) 235 { 236 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf)) 237 "%.*ld", (prec > 0 ? prec : 0), val); 238 set_field_buffer(field, 0, buf); 239 } 240 } 241 } 242 return (result); 243 } 244 245 /*--------------------------------------------------------------------------- 246 | Facility : libnform 247 | Function : static bool Check_This_Character( 248 | int c, 249 | const void * argp) 250 | 251 | Description : Check a character for the integer type. 252 | 253 | Return Values : TRUE - character is valid 254 | FALSE - character is invalid 255 +--------------------------------------------------------------------------*/ 256 static bool 257 Check_This_Character(int c, const void *argp GCC_UNUSED) 258 { 259 return ((isDigit(UChar(c)) || (c == '-')) ? TRUE : FALSE); 260 } 261 262 static FIELDTYPE typeTHIS = 263 { 264 _HAS_ARGS | _RESIDENT, 265 1, /* this is mutable, so we can't be const */ 266 (FIELDTYPE *)0, 267 (FIELDTYPE *)0, 268 Make_This_Type, 269 Copy_This_Type, 270 Free_This_Type, 271 INIT_FT_FUNC(Check_This_Field), 272 INIT_FT_FUNC(Check_This_Character), 273 INIT_FT_FUNC(NULL), 274 INIT_FT_FUNC(NULL), 275 #if NCURSES_INTEROP_FUNCS 276 Generic_This_Type 277 #endif 278 }; 279 280 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_INTEGER = &typeTHIS; 281 282 #if NCURSES_INTEROP_FUNCS 283 /* The next routines are to simplify the use of ncurses from 284 programming languages with restictions on interop with C level 285 constructs (e.g. variable access or va_list + ellipsis constructs) 286 */ 287 NCURSES_EXPORT(FIELDTYPE *) 288 _nc_TYPE_INTEGER(void) 289 { 290 return TYPE_INTEGER; 291 } 292 #endif 293 294 /* fty_int.c ends here */ 295