]> cvs.zerfleddert.de Git - micropolis/blob - src/tcl/tclget.c
Fixes for compilation with gcc 15
[micropolis] / src / tcl / tclget.c
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 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.
16 */
17
18 #ifndef lint
19 static char rcsid[] = "$Header: /user6/ouster/tcl/RCS/tclGet.c,v 1.11 92/02/29 16:13:14 ouster Exp $ SPRITE (Berkeley)";
20 #endif /* not lint */
21
22 #include "tclint.h"
23 \f
24 /*
25 *----------------------------------------------------------------------
26 *
27 * Tcl_GetInt --
28 *
29 * Given a string, produce the corresponding integer value.
30 *
31 * Results:
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.
36 *
37 * Side effects:
38 * None.
39 *
40 *----------------------------------------------------------------------
41 */
42
43 int
44 Tcl_GetInt (
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. */
49 )
50 {
51 char *end;
52 int i;
53
54 i = strtol(string, &end, 0);
55 while ((*end != '\0') && isspace(*end)) {
56 end++;
57 }
58 if ((end == string) || (*end != 0)) {
59 Tcl_AppendResult(interp, "expected integer but got \"", string,
60 "\"", (char *) NULL);
61 return TCL_ERROR;
62 }
63 *intPtr = i;
64 return TCL_OK;
65 }
66 \f
67 /*
68 *----------------------------------------------------------------------
69 *
70 * Tcl_GetDouble --
71 *
72 * Given a string, produce the corresponding double-precision
73 * floating-point value.
74 *
75 * Results:
76 * The return value is normally TCL_OK; in this case *doublePtr
77 * will be set to the double-precision value equivalent to string.
78 * If string is improperly formed then TCL_ERROR is returned and
79 * an error message will be left in interp->result.
80 *
81 * Side effects:
82 * None.
83 *
84 *----------------------------------------------------------------------
85 */
86
87 int
88 Tcl_GetDouble (
89 Tcl_Interp *interp, /* Interpreter to use for error reporting. */
90 char *string, /* String containing a floating-point number
91 * in a form acceptable to strtod. */
92 double *doublePtr /* Place to store converted result. */
93 )
94 {
95 char *end;
96 double d;
97
98 d = strtod(string, &end);
99 while ((*end != '\0') && isspace(*end)) {
100 end++;
101 }
102 if ((end == string) || (*end != 0)) {
103 Tcl_AppendResult(interp, "expected floating-point number but got \"",
104 string, "\"", (char *) NULL);
105 return TCL_ERROR;
106 }
107 *doublePtr = d;
108 return TCL_OK;
109 }
110 \f
111 /*
112 *----------------------------------------------------------------------
113 *
114 * Tcl_GetBoolean --
115 *
116 * Given a string, return a 0/1 boolean value corresponding
117 * to the string.
118 *
119 * Results:
120 * The return value is normally TCL_OK; in this case *boolPtr
121 * will be set to the 0/1 value equivalent to string. If
122 * string is improperly formed then TCL_ERROR is returned and
123 * an error message will be left in interp->result.
124 *
125 * Side effects:
126 * None.
127 *
128 *----------------------------------------------------------------------
129 */
130
131 int
132 Tcl_GetBoolean (
133 Tcl_Interp *interp, /* Interpreter to use for error reporting. */
134 char *string, /* String containing a boolean number
135 * specified either as 1/0 or true/false or
136 * yes/no. */
137 int *boolPtr /* Place to store converted result, which
138 * will be 0 or 1. */
139 )
140 {
141 char c;
142 char lowerCase[10];
143 int i, length;
144
145 /*
146 * Convert the input string to all lower-case.
147 */
148
149 for (i = 0; i < 9; i++) {
150 c = string[i];
151 if (c == 0) {
152 break;
153 }
154 if ((c >= 'A') && (c <= 'Z')) {
155 c += 'a' - 'A';
156 }
157 lowerCase[i] = c;
158 }
159 lowerCase[i] = 0;
160
161 length = strlen(lowerCase);
162 c = lowerCase[0];
163 if ((c == '0') && (lowerCase[1] == '\0')) {
164 *boolPtr = 0;
165 } else if ((c == '1') && (lowerCase[1] == '\0')) {
166 *boolPtr = 1;
167 } else if ((c == 'y') && (strncmp(lowerCase, "yes", length) == 0)) {
168 *boolPtr = 1;
169 } else if ((c == 'n') && (strncmp(lowerCase, "no", length) == 0)) {
170 *boolPtr = 0;
171 } else if ((c == 't') && (strncmp(lowerCase, "true", length) == 0)) {
172 *boolPtr = 1;
173 } else if ((c == 'f') && (strncmp(lowerCase, "false", length) == 0)) {
174 *boolPtr = 0;
175 } else if ((c == 'o') && (length >= 2)) {
176 if (strncmp(lowerCase, "on", length) == 0) {
177 *boolPtr = 1;
178 } else if (strncmp(lowerCase, "off", length) == 0) {
179 *boolPtr = 0;
180 }
181 } else {
182 Tcl_AppendResult(interp, "expected boolean value but got \"",
183 string, "\"", (char *) NULL);
184 return TCL_ERROR;
185 }
186 return TCL_OK;
187 }
Impressum, Datenschutz