os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/generic/tclGet.c
Update contrib.
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.
8 * Copyright (c) 1990-1993 The Regents of the University of California.
9 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
10 * Portions Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiaries. All rights reserved.
12 * See the file "license.terms" for information on usage and redistribution
13 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15 * RCS: @(#) $Id: tclGet.c,v 1.8.2.1 2005/04/20 16:06:17 dgp Exp $
24 *----------------------------------------------------------------------
28 * Given a string, produce the corresponding integer value.
31 * The return value is normally TCL_OK; in this case *intPtr
32 * will be set to the integer value equivalent to string. If
33 * string is improperly formed then TCL_ERROR is returned and
34 * an error message will be left in the interp's result.
39 *----------------------------------------------------------------------
43 Tcl_GetInt(interp, string, intPtr)
44 Tcl_Interp *interp; /* Interpreter to use for error reporting. */
45 CONST char *string; /* String containing a (possibly signed)
46 * integer in a form acceptable to strtol. */
47 int *intPtr; /* Place to store converted result. */
50 CONST char *p = string;
54 * Note: use strtoul instead of strtol for integer conversions
55 * to allow full-size unsigned numbers, but don't depend on strtoul
56 * to handle sign characters; it won't in some implementations.
60 #ifdef TCL_STRTOUL_SIGN_CHECK
62 * This special sign check actually causes bad numbers to be allowed
63 * when strtoul. I can't find a strtoul that doesn't validly handle
64 * signed characters, and the C standard implies that this is all
65 * unnecessary. [Bug #634856]
67 for ( ; isspace(UCHAR(*p)); p++) { /* INTL: ISO space. */
68 /* Empty loop body. */
72 i = -((long)strtoul(p, &end, 0)); /* INTL: Tcl source. */
73 } else if (*p == '+') {
75 i = strtoul(p, &end, 0); /* INTL: Tcl source. */
78 i = strtoul(p, &end, 0); /* INTL: Tcl source. */
82 if (interp != (Tcl_Interp *) NULL) {
83 Tcl_AppendResult(interp, "expected integer but got \"", string,
85 TclCheckBadOctal(interp, string);
91 * The second test below is needed on platforms where "long" is
92 * larger than "int" to detect values that fit in a long but not in
97 #if (LONG_MAX > INT_MAX)
98 || (i > UINT_MAX) || (i < -(long)UINT_MAX)
101 if (interp != (Tcl_Interp *) NULL) {
102 Tcl_SetResult(interp, "integer value too large to represent",
104 Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW",
105 Tcl_GetStringResult(interp), (char *) NULL);
109 while ((*end != '\0') && isspace(UCHAR(*end))) { /* INTL: ISO space. */
120 *----------------------------------------------------------------------
124 * Given a string, produce the corresponding long integer value.
125 * This routine is a version of Tcl_GetInt but returns a "long"
126 * instead of an "int".
129 * The return value is normally TCL_OK; in this case *longPtr
130 * will be set to the long integer value equivalent to string. If
131 * string is improperly formed then TCL_ERROR is returned and
132 * an error message will be left in the interp's result if interp
138 *----------------------------------------------------------------------
142 TclGetLong(interp, string, longPtr)
143 Tcl_Interp *interp; /* Interpreter used for error reporting
145 CONST char *string; /* String containing a (possibly signed)
146 * long integer in a form acceptable to
148 long *longPtr; /* Place to store converted long result. */
151 CONST char *p = string;
155 * Note: don't depend on strtoul to handle sign characters; it won't
156 * in some implementations.
160 #ifdef TCL_STRTOUL_SIGN_CHECK
161 for ( ; isspace(UCHAR(*p)); p++) { /* INTL: ISO space. */
162 /* Empty loop body. */
166 i = -(int)strtoul(p, &end, 0); /* INTL: Tcl source. */
167 } else if (*p == '+') {
169 i = strtoul(p, &end, 0); /* INTL: Tcl source. */
172 i = strtoul(p, &end, 0); /* INTL: Tcl source. */
176 if (interp != (Tcl_Interp *) NULL) {
177 Tcl_AppendResult(interp, "expected integer but got \"", string,
178 "\"", (char *) NULL);
179 TclCheckBadOctal(interp, string);
183 if (errno == ERANGE) {
184 if (interp != (Tcl_Interp *) NULL) {
185 Tcl_SetResult(interp, "integer value too large to represent",
187 Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW",
188 Tcl_GetStringResult(interp), (char *) NULL);
192 while ((*end != '\0') && isspace(UCHAR(*end))) { /* INTL: ISO space. */
203 *----------------------------------------------------------------------
207 * Given a string, produce the corresponding double-precision
208 * floating-point value.
211 * The return value is normally TCL_OK; in this case *doublePtr
212 * will be set to the double-precision value equivalent to string.
213 * If string is improperly formed then TCL_ERROR is returned and
214 * an error message will be left in the interp's result.
219 *----------------------------------------------------------------------
223 Tcl_GetDouble(interp, string, doublePtr)
224 Tcl_Interp *interp; /* Interpreter used for error reporting. */
225 CONST char *string; /* String containing a floating-point number
226 * in a form acceptable to strtod. */
227 double *doublePtr; /* Place to store converted result. */
233 d = strtod(string, &end); /* INTL: Tcl source. */
236 if (interp != (Tcl_Interp *) NULL) {
237 Tcl_AppendResult(interp,
238 "expected floating-point number but got \"",
239 string, "\"", (char *) NULL);
243 if (errno != 0 && (d == HUGE_VAL || d == -HUGE_VAL || d == 0)) {
244 if (interp != (Tcl_Interp *) NULL) {
245 TclExprFloatError(interp, d);
249 while ((*end != 0) && isspace(UCHAR(*end))) { /* INTL: ISO space. */
260 *----------------------------------------------------------------------
264 * Given a string, return a 0/1 boolean value corresponding
268 * The return value is normally TCL_OK; in this case *boolPtr
269 * will be set to the 0/1 value equivalent to string. If
270 * string is improperly formed then TCL_ERROR is returned and
271 * an error message will be left in the interp's result.
276 *----------------------------------------------------------------------
280 Tcl_GetBoolean(interp, string, boolPtr)
281 Tcl_Interp *interp; /* Interpreter used for error reporting. */
282 CONST char *string; /* String containing a boolean number
283 * specified either as 1/0 or true/false or
285 int *boolPtr; /* Place to store converted result, which
289 char lowerCase[10], c;
293 * Convert the input string to all lower-case.
294 * INTL: This code will work on UTF strings.
297 for (i = 0; i < 9; i++) {
302 if ((c >= 'A') && (c <= 'Z')) {
303 c += (char) ('a' - 'A');
309 length = strlen(lowerCase);
311 if ((c == '0') && (lowerCase[1] == '\0')) {
313 } else if ((c == '1') && (lowerCase[1] == '\0')) {
315 } else if ((c == 'y') && (strncmp(lowerCase, "yes", length) == 0)) {
317 } else if ((c == 'n') && (strncmp(lowerCase, "no", length) == 0)) {
319 } else if ((c == 't') && (strncmp(lowerCase, "true", length) == 0)) {
321 } else if ((c == 'f') && (strncmp(lowerCase, "false", length) == 0)) {
323 } else if ((c == 'o') && (length >= 2)) {
324 if (strncmp(lowerCase, "on", length) == 0) {
326 } else if (strncmp(lowerCase, "off", length) == 0) {
333 if (interp != (Tcl_Interp *) NULL) {
334 Tcl_AppendResult(interp, "expected boolean value but got \"",
335 string, "\"", (char *) NULL);