Debian Codegraph @

generic/tclGet.c

raw
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
15 #include "tclInt.h"
16
17 /*
18 *----------------------------------------------------------------------
19 *
20 * Tcl_GetInt --
21 *
22 * Given a string, produce the corresponding integer value.
23 *
24 * Results:
25 * The return value is normally TCL_OK; in this case *intPtr will be set
26 * to the integer value equivalent to src. If src is improperly formed
27 * then TCL_ERROR is returned and an error message will be left in the
28 * interp's result.
29 *
30 * Side effects:
31 * None.
32 *
33 *----------------------------------------------------------------------
34 */
35
36 int
37 Tcl_GetInt(
38 Tcl_Interp *interp, /* Interpreter to use for error reporting. */
39 const char *src, /* String containing a (possibly signed)
40 * integer in a form acceptable to
41 * Tcl_GetIntFromObj(). */
42 int *intPtr) /* Place to store converted result. */
43 {
44 Tcl_Obj obj;
45 int code;
46
47 obj.refCount = 1;
48 obj.bytes = (char *) src;
49 obj.length = strlen(src);
50 obj.typePtr = NULL;
51
52 code = Tcl_GetIntFromObj(interp, &obj, intPtr);
53 if (obj.refCount > 1) {
54 Tcl_Panic("invalid sharing of Tcl_Obj on C stack");
55 }
56 TclFreeIntRep(&obj);
57 return code;
58 }
59
60 /*
61 *----------------------------------------------------------------------
62 *
63 * Tcl_GetDouble --
64 *
65 * Given a string, produce the corresponding double-precision
66 * floating-point value.
67 *
68 * Results:
69 * The return value is normally TCL_OK; in this case *doublePtr will be
70 * set to the double-precision value equivalent to src. If src is
71 * improperly formed then TCL_ERROR is returned and an error message will
72 * be left in the interp's result.
73 *
74 * Side effects:
75 * None.
76 *
77 *----------------------------------------------------------------------
78 */
79
80 int
81 Tcl_GetDouble(
82 Tcl_Interp *interp, /* Interpreter used for error reporting. */
83 const char *src, /* String containing a floating-point number
84 * in a form acceptable to
85 * Tcl_GetDoubleFromObj(). */
86 double *doublePtr) /* Place to store converted result. */
87 {
88 Tcl_Obj obj;
89 int code;
90
91 obj.refCount = 1;
92 obj.bytes = (char *) src;
93 obj.length = strlen(src);
94 obj.typePtr = NULL;
95
96 code = Tcl_GetDoubleFromObj(interp, &obj, doublePtr);
97 if (obj.refCount > 1) {
98 Tcl_Panic("invalid sharing of Tcl_Obj on C stack");
99 }
100 TclFreeIntRep(&obj);
101 return code;
102 }
103
104 /*
105 *----------------------------------------------------------------------
106 *
107 * Tcl_GetBoolean --
108 *
109 * Given a string, return a 0/1 boolean value corresponding to the
110 * string.
111 *
112 * Results:
113 * The return value is normally TCL_OK; in this case *intPtr will be set
114 * to the 0/1 value equivalent to src. If src is improperly formed then
115 * TCL_ERROR is returned and an error message will be left in the
116 * interp's result.
117 *
118 * Side effects:
119 * None.
120 *
121 *----------------------------------------------------------------------
122 */
123
124 int
125 Tcl_GetBoolean(
126 Tcl_Interp *interp, /* Interpreter used for error reporting. */
127 const char *src, /* String containing one of the boolean values
128 * 1, 0, true, false, yes, no, on, off. */
129 int *intPtr) /* Place to store converted result, which will
130 * be 0 or 1. */
131 {
132 Tcl_Obj obj;
133 int code;
134
135 obj.refCount = 1;
136 obj.bytes = (char *) src;
137 obj.length = strlen(src);
138 obj.typePtr = NULL;
139
140 code = TclSetBooleanFromAny(interp, &obj);
141 if (obj.refCount > 1) {
142 Tcl_Panic("invalid sharing of Tcl_Obj on C stack");
143 }
144 if (code == TCL_OK) {
145 *intPtr = obj.internalRep.longValue;
146 }
147 return code;
148 }
149
150 /*
151 * Local Variables:
152 * mode: c
153 * c-basic-offset: 4
154 * fill-column: 78
155 * End:
156 */