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