| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "tclInt.h" |
| 18 |
#include "tclPort.h" |
| 19 |
#include "tclMath.h" |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
int |
| 42 |
Tcl_GetInt(interp, string, intPtr) |
| 43 |
Tcl_Interp *interp; |
| 44 |
CONST char *string; |
| 45 |
|
| 46 |
int *intPtr; |
| 47 |
{ |
| 48 |
char *end; |
| 49 |
CONST char *p = string; |
| 50 |
long i; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
errno = 0; |
| 59 |
#ifdef TCL_STRTOUL_SIGN_CHECK |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
for ( ; isspace(UCHAR(*p)); p++) { |
| 67 |
|
| 68 |
} |
| 69 |
if (*p == '-') { |
| 70 |
p++; |
| 71 |
i = -((long)strtoul(p, &end, 0)); |
| 72 |
} else if (*p == '+') { |
| 73 |
p++; |
| 74 |
i = strtoul(p, &end, 0); |
| 75 |
} else |
| 76 |
#else |
| 77 |
i = strtoul(p, &end, 0); |
| 78 |
#endif |
| 79 |
if (end == p) { |
| 80 |
badInteger: |
| 81 |
if (interp != (Tcl_Interp *) NULL) { |
| 82 |
Tcl_AppendResult(interp, "expected integer but got \"", string, |
| 83 |
"\"", (char *) NULL); |
| 84 |
TclCheckBadOctal(interp, string); |
| 85 |
} |
| 86 |
return TCL_ERROR; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
if ((errno == ERANGE) || (((long)(int) i) != i)) { |
| 96 |
if (interp != (Tcl_Interp *) NULL) { |
| 97 |
Tcl_SetResult(interp, "integer value too large to represent", |
| 98 |
TCL_STATIC); |
| 99 |
Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW", |
| 100 |
Tcl_GetStringResult(interp), (char *) NULL); |
| 101 |
} |
| 102 |
return TCL_ERROR; |
| 103 |
} |
| 104 |
while ((*end != '\0') && isspace(UCHAR(*end))) { |
| 105 |
end++; |
| 106 |
} |
| 107 |
if (*end != 0) { |
| 108 |
goto badInteger; |
| 109 |
} |
| 110 |
*intPtr = (int) i; |
| 111 |
return TCL_OK; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
int |
| 137 |
TclGetLong(interp, string, longPtr) |
| 138 |
Tcl_Interp *interp; |
| 139 |
|
| 140 |
CONST char *string; |
| 141 |
|
| 142 |
|
| 143 |
long *longPtr; |
| 144 |
{ |
| 145 |
char *end; |
| 146 |
CONST char *p = string; |
| 147 |
long i; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
errno = 0; |
| 155 |
#ifdef TCL_STRTOUL_SIGN_CHECK |
| 156 |
for ( ; isspace(UCHAR(*p)); p++) { |
| 157 |
|
| 158 |
} |
| 159 |
if (*p == '-') { |
| 160 |
p++; |
| 161 |
i = -(int)strtoul(p, &end, 0); |
| 162 |
} else if (*p == '+') { |
| 163 |
p++; |
| 164 |
i = strtoul(p, &end, 0); |
| 165 |
} else |
| 166 |
#else |
| 167 |
i = strtoul(p, &end, 0); |
| 168 |
#endif |
| 169 |
if (end == p) { |
| 170 |
badInteger: |
| 171 |
if (interp != (Tcl_Interp *) NULL) { |
| 172 |
Tcl_AppendResult(interp, "expected integer but got \"", string, |
| 173 |
"\"", (char *) NULL); |
| 174 |
TclCheckBadOctal(interp, string); |
| 175 |
} |
| 176 |
return TCL_ERROR; |
| 177 |
} |
| 178 |
if (errno == ERANGE) { |
| 179 |
if (interp != (Tcl_Interp *) NULL) { |
| 180 |
Tcl_SetResult(interp, "integer value too large to represent", |
| 181 |
TCL_STATIC); |
| 182 |
Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW", |
| 183 |
Tcl_GetStringResult(interp), (char *) NULL); |
| 184 |
} |
| 185 |
return TCL_ERROR; |
| 186 |
} |
| 187 |
while ((*end != '\0') && isspace(UCHAR(*end))) { |
| 188 |
end++; |
| 189 |
} |
| 190 |
if (*end != 0) { |
| 191 |
goto badInteger; |
| 192 |
} |
| 193 |
*longPtr = i; |
| 194 |
return TCL_OK; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
int |
| 218 |
Tcl_GetDouble(interp, string, doublePtr) |
| 219 |
Tcl_Interp *interp; |
| 220 |
CONST char *string; |
| 221 |
|
| 222 |
double *doublePtr; |
| 223 |
{ |
| 224 |
char *end; |
| 225 |
double d; |
| 226 |
|
| 227 |
errno = 0; |
| 228 |
d = strtod(string, &end); |
| 229 |
if (end == string) { |
| 230 |
badDouble: |
| 231 |
if (interp != (Tcl_Interp *) NULL) { |
| 232 |
Tcl_AppendResult(interp, |
| 233 |
"expected floating-point number but got \"", |
| 234 |
string, "\"", (char *) NULL); |
| 235 |
} |
| 236 |
return TCL_ERROR; |
| 237 |
} |
| 238 |
if (errno != 0 && (d == HUGE_VAL || d == -HUGE_VAL || d == 0)) { |
| 239 |
if (interp != (Tcl_Interp *) NULL) { |
| 240 |
TclExprFloatError(interp, d); |
| 241 |
} |
| 242 |
return TCL_ERROR; |
| 243 |
} |
| 244 |
while ((*end != 0) && isspace(UCHAR(*end))) { |
| 245 |
end++; |
| 246 |
} |
| 247 |
if (*end != 0) { |
| 248 |
goto badDouble; |
| 249 |
} |
| 250 |
*doublePtr = d; |
| 251 |
return TCL_OK; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
int |
| 275 |
Tcl_GetBoolean(interp, string, boolPtr) |
| 276 |
Tcl_Interp *interp; |
| 277 |
CONST char *string; |
| 278 |
|
| 279 |
|
| 280 |
int *boolPtr; |
| 281 |
|
| 282 |
{ |
| 283 |
int i; |
| 284 |
char lowerCase[10], c; |
| 285 |
size_t length; |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
for (i = 0; i < 9; i++) { |
| 293 |
c = string[i]; |
| 294 |
if (c == 0) { |
| 295 |
break; |
| 296 |
} |
| 297 |
if ((c >= 'A') && (c <= 'Z')) { |
| 298 |
c += (char) ('a' - 'A'); |
| 299 |
} |
| 300 |
lowerCase[i] = c; |
| 301 |
} |
| 302 |
lowerCase[i] = 0; |
| 303 |
|
| 304 |
length = strlen(lowerCase); |
| 305 |
c = lowerCase[0]; |
| 306 |
if ((c == '0') && (lowerCase[1] == '\0')) { |
| 307 |
*boolPtr = 0; |
| 308 |
} else if ((c == '1') && (lowerCase[1] == '\0')) { |
| 309 |
*boolPtr = 1; |
| 310 |
} else if ((c == 'y') && (strncmp(lowerCase, "yes", length) == 0)) { |
| 311 |
*boolPtr = 1; |
| 312 |
} else if ((c == 'n') && (strncmp(lowerCase, "no", length) == 0)) { |
| 313 |
*boolPtr = 0; |
| 314 |
} else if ((c == 't') && (strncmp(lowerCase, "true", length) == 0)) { |
| 315 |
*boolPtr = 1; |
| 316 |
} else if ((c == 'f') && (strncmp(lowerCase, "false", length) == 0)) { |
| 317 |
*boolPtr = 0; |
| 318 |
} else if ((c == 'o') && (length >= 2)) { |
| 319 |
if (strncmp(lowerCase, "on", length) == 0) { |
| 320 |
*boolPtr = 1; |
| 321 |
} else if (strncmp(lowerCase, "off", length) == 0) { |
| 322 |
*boolPtr = 0; |
| 323 |
} else { |
| 324 |
goto badBoolean; |
| 325 |
} |
| 326 |
} else { |
| 327 |
badBoolean: |
| 328 |
if (interp != (Tcl_Interp *) NULL) { |
| 329 |
Tcl_AppendResult(interp, "expected boolean value but got \"", |
| 330 |
string, "\"", (char *) NULL); |
| 331 |
} |
| 332 |
return TCL_ERROR; |
| 333 |
} |
| 334 |
return TCL_OK; |
| 335 |
} |