Debian Codegraph @

pTk/mTk/tclGeneric/tclGet.c

raw symbols SCIP
1 /*
2 * tclGet.c --
3 *
4 * This file contains procedures to convert strings into
5 * other forms, like integers or floating-point numbers or
6 * booleans, doing syntax checking along the way.
7 *
8 * Copyright (c) 1990-1993 The Regents of the University of California.
9 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
10 *
11 * See the file "license.terms" for information on usage and redistribution
12 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 *
14 * RCS: @(#) $Id: tclGet.c,v 1.8 2002/11/19 02:34:49 hobbs Exp $
15 */
16
17 #include "tclInt.h"
18 #include "tclPort.h"
19 #include "tclMath.h"
20
21
22 /*
23 *----------------------------------------------------------------------
24 *
25 * Tcl_GetInt --
26 *
27 * Given a string, produce the corresponding integer value.
28 *
29 * Results:
30 * The return value is normally TCL_OK; in this case *intPtr
31 * will be set to the integer value equivalent to string. If
32 * string is improperly formed then TCL_ERROR is returned and
33 * an error message will be left in the interp's result.
34 *
35 * Side effects:
36 * None.
37 *
38 *----------------------------------------------------------------------
39 */
40
41 int
42 Tcl_GetInt(interp, string, intPtr)
43 Tcl_Interp *interp; /* Interpreter to use for error reporting. */
44 CONST char *string; /* String containing a (possibly signed)
45 * integer in a form acceptable to strtol. */
46 int *intPtr; /* Place to store converted result. */
47 {
48 char *end;
49 CONST char *p = string;
50 long i;
51
52 /*
53 * Note: use strtoul instead of strtol for integer conversions
54 * to allow full-size unsigned numbers, but don't depend on strtoul
55 * to handle sign characters; it won't in some implementations.
56 */
57
58 errno = 0;
59 #ifdef TCL_STRTOUL_SIGN_CHECK
60 /*
61 * This special sign check actually causes bad numbers to be allowed
62 * when strtoul. I can't find a strtoul that doesn't validly handle
63 * signed characters, and the C standard implies that this is all
64 * unnecessary. [Bug #634856]
65 */
66 for ( ; isspace(UCHAR(*p)); p++) { /* INTL: ISO space. */
67 /* Empty loop body. */
68 }
69 if (*p == '-') {
70 p++;
71 i = -((long)strtoul(p, &end, 0)); /* INTL: Tcl source. */
72 } else if (*p == '+') {
73 p++;
74 i = strtoul(p, &end, 0); /* INTL: Tcl source. */
75 } else
76 #else
77 i = strtoul(p, &end, 0); /* INTL: Tcl source. */
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 * The second test below is needed on platforms where "long" is
91 * larger than "int" to detect values that fit in a long but not in
92 * an int.
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))) { /* INTL: ISO space. */
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 * TclGetLong --
118 *
119 * Given a string, produce the corresponding long integer value.
120 * This routine is a version of Tcl_GetInt but returns a "long"
121 * instead of an "int".
122 *
123 * Results:
124 * The return value is normally TCL_OK; in this case *longPtr
125 * will be set to the long integer value equivalent to string. If
126 * string is improperly formed then TCL_ERROR is returned and
127 * an error message will be left in the interp's result if interp
128 * is non-NULL.
129 *
130 * Side effects:
131 * None.
132 *
133 *----------------------------------------------------------------------
134 */
135
136 int
137 TclGetLong(interp, string, longPtr)
138 Tcl_Interp *interp; /* Interpreter used for error reporting
139 * if not NULL. */
140 CONST char *string; /* String containing a (possibly signed)
141 * long integer in a form acceptable to
142 * strtoul. */
143 long *longPtr; /* Place to store converted long result. */
144 {
145 char *end;
146 CONST char *p = string;
147 long i;
148
149 /*
150 * Note: don't depend on strtoul to handle sign characters; it won't
151 * in some implementations.
152 */
153
154 errno = 0;
155 #ifdef TCL_STRTOUL_SIGN_CHECK
156 for ( ; isspace(UCHAR(*p)); p++) { /* INTL: ISO space. */
157 /* Empty loop body. */
158 }
159 if (*p == '-') {
160 p++;
161 i = -(int)strtoul(p, &end, 0); /* INTL: Tcl source. */
162 } else if (*p == '+') {
163 p++;
164 i = strtoul(p, &end, 0); /* INTL: Tcl source. */
165 } else
166 #else
167 i = strtoul(p, &end, 0); /* INTL: Tcl source. */
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))) { /* INTL: ISO space. */
188 end++;
189 }
190 if (*end != 0) {
191 goto badInteger;
192 }
193 *longPtr = i;
194 return TCL_OK;
195 }
196
197 /*
198 *----------------------------------------------------------------------
199 *
200 * Tcl_GetDouble --
201 *
202 * Given a string, produce the corresponding double-precision
203 * floating-point value.
204 *
205 * Results:
206 * The return value is normally TCL_OK; in this case *doublePtr
207 * will be set to the double-precision value equivalent to string.
208 * If string is improperly formed then TCL_ERROR is returned and
209 * an error message will be left in the interp's result.
210 *
211 * Side effects:
212 * None.
213 *
214 *----------------------------------------------------------------------
215 */
216
217 int
218 Tcl_GetDouble(interp, string, doublePtr)
219 Tcl_Interp *interp; /* Interpreter used for error reporting. */
220 CONST char *string; /* String containing a floating-point number
221 * in a form acceptable to strtod. */
222 double *doublePtr; /* Place to store converted result. */
223 {
224 char *end;
225 double d;
226
227 errno = 0;
228 d = strtod(string, &end); /* INTL: Tcl source. */
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))) { /* INTL: ISO space. */
245 end++;
246 }
247 if (*end != 0) {
248 goto badDouble;
249 }
250 *doublePtr = d;
251 return TCL_OK;
252 }
253
254 /*
255 *----------------------------------------------------------------------
256 *
257 * Tcl_GetBoolean --
258 *
259 * Given a string, return a 0/1 boolean value corresponding
260 * to the string.
261 *
262 * Results:
263 * The return value is normally TCL_OK; in this case *boolPtr
264 * will be set to the 0/1 value equivalent to string. If
265 * string is improperly formed then TCL_ERROR is returned and
266 * an error message will be left in the interp's result.
267 *
268 * Side effects:
269 * None.
270 *
271 *----------------------------------------------------------------------
272 */
273
274 int
275 Tcl_GetBoolean(interp, string, boolPtr)
276 Tcl_Interp *interp; /* Interpreter used for error reporting. */
277 CONST char *string; /* String containing a boolean number
278 * specified either as 1/0 or true/false or
279 * yes/no. */
280 int *boolPtr; /* Place to store converted result, which
281 * will be 0 or 1. */
282 {
283 int i;
284 char lowerCase[10], c;
285 size_t length;
286
287 /*
288 * Convert the input string to all lower-case.
289 * INTL: This code will work on UTF strings.
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 }