| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#include "form.priv.h" |
| 36 |
|
| 37 |
MODULE_ID("$Id: fty_num.c,v 1.28 2010/01/23 21:14:36 tom Exp $") |
| 38 |
|
| 39 |
#if HAVE_LOCALE_H |
| 40 |
#include <locale.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
#if HAVE_LOCALE_H |
| 44 |
#define isDecimalPoint(c) ((c) == ((L && L->decimal_point) ? *(L->decimal_point) : '.')) |
| 45 |
#else |
| 46 |
#define isDecimalPoint(c) ((c) == '.') |
| 47 |
#endif |
| 48 |
|
| 49 |
#if USE_WIDEC_SUPPORT |
| 50 |
#define isDigit(c) (iswdigit((wint_t)(c)) || isdigit(UChar(c))) |
| 51 |
#else |
| 52 |
#define isDigit(c) isdigit(UChar(c)) |
| 53 |
#endif |
| 54 |
|
| 55 |
#define thisARG numericARG |
| 56 |
|
| 57 |
typedef struct |
| 58 |
{ |
| 59 |
int precision; |
| 60 |
double low; |
| 61 |
double high; |
| 62 |
struct lconv *L; |
| 63 |
} |
| 64 |
thisARG; |
| 65 |
|
| 66 |
typedef struct |
| 67 |
{ |
| 68 |
int precision; |
| 69 |
double low; |
| 70 |
double high; |
| 71 |
} |
| 72 |
thisPARM; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
static void * |
| 83 |
Generic_This_Type(void *arg) |
| 84 |
{ |
| 85 |
thisARG *argn = (thisARG *) 0; |
| 86 |
thisPARM *args = (thisPARM *) arg; |
| 87 |
|
| 88 |
if (args) |
| 89 |
{ |
| 90 |
argn = typeMalloc(thisARG, 1); |
| 91 |
|
| 92 |
if (argn) |
| 93 |
{ |
| 94 |
T((T_CREATE("thisARG %p"), (void *)argn)); |
| 95 |
argn->precision = args->precision; |
| 96 |
argn->low = args->low; |
| 97 |
argn->high = args->high; |
| 98 |
|
| 99 |
#if HAVE_LOCALE_H |
| 100 |
argn->L = localeconv(); |
| 101 |
#else |
| 102 |
argn->L = NULL; |
| 103 |
#endif |
| 104 |
} |
| 105 |
} |
| 106 |
return (void *)argn; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
static void * |
| 118 |
Make_This_Type(va_list *ap) |
| 119 |
{ |
| 120 |
thisPARM arg; |
| 121 |
|
| 122 |
arg.precision = va_arg(*ap, int); |
| 123 |
arg.low = va_arg(*ap, double); |
| 124 |
arg.high = va_arg(*ap, double); |
| 125 |
|
| 126 |
return Generic_This_Type((void *)&arg); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
static void * |
| 138 |
Copy_This_Type(const void *argp) |
| 139 |
{ |
| 140 |
const thisARG *ap = (const thisARG *)argp; |
| 141 |
thisARG *result = (thisARG *) 0; |
| 142 |
|
| 143 |
if (argp) |
| 144 |
{ |
| 145 |
result = typeMalloc(thisARG, 1); |
| 146 |
if (result) |
| 147 |
{ |
| 148 |
T((T_CREATE("thisARG %p"), (void *)result)); |
| 149 |
*result = *ap; |
| 150 |
} |
| 151 |
} |
| 152 |
return (void *)result; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
static void |
| 164 |
Free_This_Type(void *argp) |
| 165 |
{ |
| 166 |
if (argp) |
| 167 |
free(argp); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
static bool |
| 181 |
Check_This_Field(FIELD *field, const void *argp) |
| 182 |
{ |
| 183 |
const thisARG *argn = (const thisARG *)argp; |
| 184 |
double low = argn->low; |
| 185 |
double high = argn->high; |
| 186 |
int prec = argn->precision; |
| 187 |
unsigned char *bp = (unsigned char *)field_buffer(field, 0); |
| 188 |
char *s = (char *)bp; |
| 189 |
double val = 0.0; |
| 190 |
struct lconv *L = argn->L; |
| 191 |
char buf[64]; |
| 192 |
bool result = FALSE; |
| 193 |
|
| 194 |
while (*bp && *bp == ' ') |
| 195 |
bp++; |
| 196 |
if (*bp) |
| 197 |
{ |
| 198 |
if (*bp == '-' || *bp == '+') |
| 199 |
bp++; |
| 200 |
#if USE_WIDEC_SUPPORT |
| 201 |
if (*bp) |
| 202 |
{ |
| 203 |
bool blank = FALSE; |
| 204 |
int state = 0; |
| 205 |
int len; |
| 206 |
int n; |
| 207 |
wchar_t *list = _nc_Widen_String((char *)bp, &len); |
| 208 |
|
| 209 |
if (list != 0) |
| 210 |
{ |
| 211 |
result = TRUE; |
| 212 |
for (n = 0; n < len; ++n) |
| 213 |
{ |
| 214 |
if (blank) |
| 215 |
{ |
| 216 |
if (list[n] != ' ') |
| 217 |
{ |
| 218 |
result = FALSE; |
| 219 |
break; |
| 220 |
} |
| 221 |
} |
| 222 |
else if (list[n] == ' ') |
| 223 |
{ |
| 224 |
blank = TRUE; |
| 225 |
} |
| 226 |
else if (isDecimalPoint(list[n])) |
| 227 |
{ |
| 228 |
if (++state > 1) |
| 229 |
{ |
| 230 |
result = FALSE; |
| 231 |
break; |
| 232 |
} |
| 233 |
} |
| 234 |
else if (!isDigit(list[n])) |
| 235 |
{ |
| 236 |
result = FALSE; |
| 237 |
break; |
| 238 |
} |
| 239 |
} |
| 240 |
free(list); |
| 241 |
} |
| 242 |
} |
| 243 |
#else |
| 244 |
while (*bp) |
| 245 |
{ |
| 246 |
if (!isdigit(UChar(*bp))) |
| 247 |
break; |
| 248 |
bp++; |
| 249 |
} |
| 250 |
if (isDecimalPoint(*bp)) |
| 251 |
{ |
| 252 |
bp++; |
| 253 |
while (*bp) |
| 254 |
{ |
| 255 |
if (!isdigit(UChar(*bp))) |
| 256 |
break; |
| 257 |
bp++; |
| 258 |
} |
| 259 |
} |
| 260 |
while (*bp && *bp == ' ') |
| 261 |
bp++; |
| 262 |
result = (*bp == '\0'); |
| 263 |
#endif |
| 264 |
if (result) |
| 265 |
{ |
| 266 |
val = atof(s); |
| 267 |
if (low < high) |
| 268 |
{ |
| 269 |
if (val < low || val > high) |
| 270 |
result = FALSE; |
| 271 |
} |
| 272 |
if (result) |
| 273 |
{ |
| 274 |
sprintf(buf, "%.*f", (prec > 0 ? prec : 0), val); |
| 275 |
set_field_buffer(field, 0, buf); |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
return (result); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
static bool |
| 294 |
Check_This_Character(int c, const void *argp) |
| 295 |
{ |
| 296 |
const thisARG *argn = (const thisARG *)argp; |
| 297 |
struct lconv *L = argn->L; |
| 298 |
|
| 299 |
return ((isDigit(c) || |
| 300 |
c == '+' || |
| 301 |
c == '-' || |
| 302 |
isDecimalPoint(c)) |
| 303 |
? TRUE |
| 304 |
: FALSE); |
| 305 |
} |
| 306 |
|
| 307 |
static FIELDTYPE typeTHIS = |
| 308 |
{ |
| 309 |
_HAS_ARGS | _RESIDENT, |
| 310 |
1, |
| 311 |
(FIELDTYPE *)0, |
| 312 |
(FIELDTYPE *)0, |
| 313 |
Make_This_Type, |
| 314 |
Copy_This_Type, |
| 315 |
Free_This_Type, |
| 316 |
INIT_FT_FUNC(Check_This_Field), |
| 317 |
INIT_FT_FUNC(Check_This_Character), |
| 318 |
INIT_FT_FUNC(NULL), |
| 319 |
INIT_FT_FUNC(NULL), |
| 320 |
#if NCURSES_INTEROP_FUNCS |
| 321 |
Generic_This_Type |
| 322 |
#endif |
| 323 |
}; |
| 324 |
|
| 325 |
NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_NUMERIC = &typeTHIS; |
| 326 |
|
| 327 |
#if NCURSES_INTEROP_FUNCS |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
NCURSES_EXPORT(FIELDTYPE *) |
| 333 |
_nc_TYPE_NUMERIC(void) |
| 334 |
{ |
| 335 |
return TYPE_NUMERIC; |
| 336 |
} |
| 337 |
#endif |
| 338 |
|
| 339 |
|