]>
git.zerfleddert.de Git - micropolis/blob - src/tcl/tclget.c
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 1990-1991 Regents of the University of California
9 * Permission to use, copy, modify, and distribute this
10 * software and its documentation for any purpose and without
11 * fee is hereby granted, provided that the above copyright
12 * notice appear in all copies. The University of California
13 * makes no representations about the suitability of this
14 * software for any purpose. It is provided "as is" without
15 * express or implied warranty.
19 static char rcsid
[] = "$Header: /user6/ouster/tcl/RCS/tclGet.c,v 1.11 92/02/29 16:13:14 ouster Exp $ SPRITE (Berkeley)";
25 *----------------------------------------------------------------------
29 * Given a string, produce the corresponding integer value.
32 * The return value is normally TCL_OK; in this case *intPtr
33 * will be set to the integer value equivalent to string. If
34 * string is improperly formed then TCL_ERROR is returned and
35 * an error message will be left in interp->result.
40 *----------------------------------------------------------------------
44 Tcl_GetInt(interp
, string
, intPtr
)
45 Tcl_Interp
*interp
; /* Interpreter to use for error reporting. */
46 char *string
; /* String containing a (possibly signed)
47 * integer in a form acceptable to strtol. */
48 int *intPtr
; /* Place to store converted result. */
53 i
= strtol(string
, &end
, 0);
54 while ((*end
!= '\0') && isspace(*end
)) {
57 if ((end
== string
) || (*end
!= 0)) {
58 Tcl_AppendResult(interp
, "expected integer but got \"", string
,
67 *----------------------------------------------------------------------
71 * Given a string, produce the corresponding double-precision
72 * floating-point value.
75 * The return value is normally TCL_OK; in this case *doublePtr
76 * will be set to the double-precision value equivalent to string.
77 * If string is improperly formed then TCL_ERROR is returned and
78 * an error message will be left in interp->result.
83 *----------------------------------------------------------------------
87 Tcl_GetDouble(interp
, string
, doublePtr
)
88 Tcl_Interp
*interp
; /* Interpreter to use for error reporting. */
89 char *string
; /* String containing a floating-point number
90 * in a form acceptable to strtod. */
91 double *doublePtr
; /* Place to store converted result. */
96 d
= strtod(string
, &end
);
97 while ((*end
!= '\0') && isspace(*end
)) {
100 if ((end
== string
) || (*end
!= 0)) {
101 Tcl_AppendResult(interp
, "expected floating-point number but got \"",
102 string
, "\"", (char *) NULL
);
110 *----------------------------------------------------------------------
114 * Given a string, return a 0/1 boolean value corresponding
118 * The return value is normally TCL_OK; in this case *boolPtr
119 * will be set to the 0/1 value equivalent to string. If
120 * string is improperly formed then TCL_ERROR is returned and
121 * an error message will be left in interp->result.
126 *----------------------------------------------------------------------
130 Tcl_GetBoolean(interp
, string
, boolPtr
)
131 Tcl_Interp
*interp
; /* Interpreter to use for error reporting. */
132 char *string
; /* String containing a boolean number
133 * specified either as 1/0 or true/false or
135 int *boolPtr
; /* Place to store converted result, which
143 * Convert the input string to all lower-case.
146 for (i
= 0; i
< 9; i
++) {
151 if ((c
>= 'A') && (c
<= 'Z')) {
158 length
= strlen(lowerCase
);
160 if ((c
== '0') && (lowerCase
[1] == '\0')) {
162 } else if ((c
== '1') && (lowerCase
[1] == '\0')) {
164 } else if ((c
== 'y') && (strncmp(lowerCase
, "yes", length
) == 0)) {
166 } else if ((c
== 'n') && (strncmp(lowerCase
, "no", length
) == 0)) {
168 } else if ((c
== 't') && (strncmp(lowerCase
, "true", length
) == 0)) {
170 } else if ((c
== 'f') && (strncmp(lowerCase
, "false", length
) == 0)) {
172 } else if ((c
== 'o') && (length
>= 2)) {
173 if (strncmp(lowerCase
, "on", length
) == 0) {
175 } else if (strncmp(lowerCase
, "off", length
) == 0) {
179 Tcl_AppendResult(interp
, "expected boolean value but got \"",
180 string
, "\"", (char *) NULL
);