1/*
2 * tclGet.c --
3 *
4 *	This file contains functions to convert strings into other forms, like
5 *	integers or floating-point numbers or booleans, doing syntax checking
6 *	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 of
12 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 *
14 * RCS: @(#) $Id: tclGet.c,v 1.20.2.1 2008/07/08 17:53:14 dgp Exp $
15 */
16
17#include "tclInt.h"
18
19/*
20 *----------------------------------------------------------------------
21 *
22 * Tcl_GetInt --
23 *
24 *	Given a string, produce the corresponding integer value.
25 *
26 * Results:
27 *	The return value is normally TCL_OK; in this case *intPtr will be set
28 *	to the integer value equivalent to src.  If src is improperly formed
29 *	then TCL_ERROR is returned and an error message will be left in the
30 *	interp's result.
31 *
32 * Side effects:
33 *	None.
34 *
35 *----------------------------------------------------------------------
36 */
37
38int
39Tcl_GetInt(
40    Tcl_Interp *interp,		/* Interpreter to use for error reporting. */
41    CONST char *src,		/* String containing a (possibly signed)
42				 * integer in a form acceptable to
43				 * Tcl_GetIntFromObj(). */
44    int *intPtr)		/* Place to store converted result. */
45{
46    Tcl_Obj obj;
47    int code;
48
49    obj.refCount = 1;
50    obj.bytes = (char *) src;
51    obj.length = strlen(src);
52    obj.typePtr = NULL;
53
54    code = Tcl_GetIntFromObj(interp, &obj, intPtr);
55    if (obj.refCount > 1) {
56	Tcl_Panic("invalid sharing of Tcl_Obj on C stack");
57    }
58    return code;
59}
60
61/*
62 *----------------------------------------------------------------------
63 *
64 * TclGetLong --
65 *
66 *	Given a string, produce the corresponding long integer value. This
67 *	routine is a version of Tcl_GetInt but returns a "long" instead of an
68 *	"int" (a difference that matters on 64-bit architectures).
69 *
70 * Results:
71 *	The return value is normally TCL_OK; in this case *longPtr will be set
72 *	to the long integer value equivalent to src. If src is improperly
73 *	formed then TCL_ERROR is returned and an error message will be left in
74 *	the interp's result if interp is non-NULL.
75 *
76 * Side effects:
77 *	None.
78 *
79 *----------------------------------------------------------------------
80 */
81
82int
83TclGetLong(
84    Tcl_Interp *interp,		/* Interpreter used for error reporting if not
85				 * NULL. */
86    CONST char *src,		/* String containing a (possibly signed) long
87				 * integer in a form acceptable to
88				 * Tcl_GetLongFromObj(). */
89    long *longPtr)		/* Place to store converted long result. */
90{
91    Tcl_Obj obj;
92    int code;
93
94    obj.refCount = 1;
95    obj.bytes = (char *) src;
96    obj.length = strlen(src);
97    obj.typePtr = NULL;
98
99    code = Tcl_GetLongFromObj(interp, &obj, longPtr);
100    if (obj.refCount > 1) {
101	Tcl_Panic("invalid sharing of Tcl_Obj on C stack");
102    }
103    return code;
104}
105
106/*
107 *----------------------------------------------------------------------
108 *
109 * Tcl_GetDouble --
110 *
111 *	Given a string, produce the corresponding double-precision
112 *	floating-point value.
113 *
114 * Results:
115 *	The return value is normally TCL_OK; in this case *doublePtr will be
116 *	set to the double-precision value equivalent to src. If src is
117 *	improperly formed then TCL_ERROR is returned and an error message will
118 *	be left in the interp's result.
119 *
120 * Side effects:
121 *	None.
122 *
123 *----------------------------------------------------------------------
124 */
125
126int
127Tcl_GetDouble(
128    Tcl_Interp *interp,		/* Interpreter used for error reporting. */
129    CONST char *src,		/* String containing a floating-point number
130				 * in a form acceptable to
131				 * Tcl_GetDoubleFromObj(). */
132    double *doublePtr)		/* Place to store converted result. */
133{
134    Tcl_Obj obj;
135    int code;
136
137    obj.refCount = 1;
138    obj.bytes = (char *) src;
139    obj.length = strlen(src);
140    obj.typePtr = NULL;
141
142    code = Tcl_GetDoubleFromObj(interp, &obj, doublePtr);
143    if (obj.refCount > 1) {
144	Tcl_Panic("invalid sharing of Tcl_Obj on C stack");
145    }
146    return code;
147}
148
149/*
150 *----------------------------------------------------------------------
151 *
152 * Tcl_GetBoolean --
153 *
154 *	Given a string, return a 0/1 boolean value corresponding to the
155 *	string.
156 *
157 * Results:
158 *	The return value is normally TCL_OK; in this case *boolPtr will be set
159 *	to the 0/1 value equivalent to src. If src is improperly formed then
160 *	TCL_ERROR is returned and an error message will be left in the
161 *	interp's result.
162 *
163 * Side effects:
164 *	None.
165 *
166 *----------------------------------------------------------------------
167 */
168
169int
170Tcl_GetBoolean(
171    Tcl_Interp *interp,		/* Interpreter used for error reporting. */
172    CONST char *src,		/* String containing one of the boolean values
173				 * 1, 0, true, false, yes, no, on off. */
174    int *boolPtr)		/* Place to store converted result, which will
175				 * be 0 or 1. */
176{
177    Tcl_Obj obj;
178    int code;
179
180    obj.refCount = 1;
181    obj.bytes = (char *) src;
182    obj.length = strlen(src);
183    obj.typePtr = NULL;
184
185    code = Tcl_ConvertToType(interp, &obj, &tclBooleanType);
186    if (obj.refCount > 1) {
187	Tcl_Panic("invalid sharing of Tcl_Obj on C stack");
188    }
189    if (code == TCL_OK) {
190	*boolPtr = obj.internalRep.longValue;
191    }
192    return code;
193}
194
195/*
196 * Local Variables:
197 * mode: c
198 * c-basic-offset: 4
199 * fill-column: 78
200 * End:
201 */
202