2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: tclGet.c /main/2 1996/08/08 14:44:07 cde-hp $ */
27 * This file contains procedures to convert strings into
28 * other forms, like integers or floating-point numbers or
29 * booleans, doing syntax checking along the way.
31 * Copyright (c) 1990-1993 The Regents of the University of California.
32 * Copyright (c) 1994-1995 Sun Microsystems, Inc.
34 * See the file "license.terms" for information on usage and redistribution
35 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
37 * SCCS: @(#) tclGet.c 1.24 96/02/15 11:42:47
45 *----------------------------------------------------------------------
49 * Given a string, produce the corresponding integer value.
52 * The return value is normally TCL_OK; in this case *intPtr
53 * will be set to the integer value equivalent to string. If
54 * string is improperly formed then TCL_ERROR is returned and
55 * an error message will be left in interp->result.
60 *----------------------------------------------------------------------
64 Tcl_GetInt(interp, string, intPtr)
65 Tcl_Interp *interp; /* Interpreter to use for error reporting. */
66 char *string; /* String containing a (possibly signed)
67 * integer in a form acceptable to strtol. */
68 int *intPtr; /* Place to store converted result. */
74 * Note: use strtoul instead of strtol for integer conversions
75 * to allow full-size unsigned numbers, but don't depend on strtoul
76 * to handle sign characters; it won't in some implementations.
80 for (p = string; isspace(UCHAR(*p)); p++) {
81 /* Empty loop body. */
85 i = -(int)strtoul(p, &end, 0);
86 } else if (*p == '+') {
88 i = strtoul(p, &end, 0);
90 i = strtoul(p, &end, 0);
94 if (interp != (Tcl_Interp *) NULL) {
95 Tcl_AppendResult(interp, "expected integer but got \"", string,
100 if (errno == ERANGE) {
101 if (interp != (Tcl_Interp *) NULL) {
102 interp->result = "integer value too large to represent";
103 Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW",
104 interp->result, (char *) NULL);
108 while ((*end != '\0') && isspace(UCHAR(*end))) {
119 *----------------------------------------------------------------------
123 * Given a string, produce the corresponding double-precision
124 * floating-point value.
127 * The return value is normally TCL_OK; in this case *doublePtr
128 * will be set to the double-precision value equivalent to string.
129 * If string is improperly formed then TCL_ERROR is returned and
130 * an error message will be left in interp->result.
135 *----------------------------------------------------------------------
139 Tcl_GetDouble(interp, string, doublePtr)
140 Tcl_Interp *interp; /* Interpreter to use for error reporting. */
141 char *string; /* String containing a floating-point number
142 * in a form acceptable to strtod. */
143 double *doublePtr; /* Place to store converted result. */
149 d = strtod(string, &end);
152 if (interp != (Tcl_Interp *) NULL) {
153 Tcl_AppendResult(interp,
154 "expected floating-point number but got \"",
155 string, "\"", (char *) NULL);
160 if (interp != (Tcl_Interp *) NULL) {
161 TclExprFloatError(interp, d);
165 while ((*end != 0) && isspace(UCHAR(*end))) {
176 *----------------------------------------------------------------------
180 * Given a string, return a 0/1 boolean value corresponding
184 * The return value is normally TCL_OK; in this case *boolPtr
185 * will be set to the 0/1 value equivalent to string. If
186 * string is improperly formed then TCL_ERROR is returned and
187 * an error message will be left in interp->result.
192 *----------------------------------------------------------------------
196 Tcl_GetBoolean(interp, string, boolPtr)
197 Tcl_Interp *interp; /* Interpreter to use for error reporting. */
198 char *string; /* String containing a boolean number
199 * specified either as 1/0 or true/false or
201 int *boolPtr; /* Place to store converted result, which
205 char lowerCase[10], c;
209 * Convert the input string to all lower-case.
212 for (i = 0; i < 9; i++) {
217 if ((c >= 'A') && (c <= 'Z')) {
218 c += (char) ('a' - 'A');
224 length = strlen(lowerCase);
226 if ((c == '0') && (lowerCase[1] == '\0')) {
228 } else if ((c == '1') && (lowerCase[1] == '\0')) {
230 } else if ((c == 'y') && (strncmp(lowerCase, "yes", length) == 0)) {
232 } else if ((c == 'n') && (strncmp(lowerCase, "no", length) == 0)) {
234 } else if ((c == 't') && (strncmp(lowerCase, "true", length) == 0)) {
236 } else if ((c == 'f') && (strncmp(lowerCase, "false", length) == 0)) {
238 } else if ((c == 'o') && (length >= 2)) {
239 if (strncmp(lowerCase, "on", length) == 0) {
241 } else if (strncmp(lowerCase, "off", length) == 0) {
248 if (interp != (Tcl_Interp *) NULL) {
249 Tcl_AppendResult(interp, "expected boolean value but got \"",
250 string, "\"", (char *) NULL);