tclGet.c

Go to the documentation of this file.
00001 /*
00002  * tclGet.c --
00003  *
00004  *      This file contains functions to convert strings into other forms, like
00005  *      integers or floating-point numbers or booleans, doing syntax checking
00006  *      along the way.
00007  *
00008  * Copyright (c) 1990-1993 The Regents of the University of California.
00009  * Copyright (c) 1994-1997 Sun Microsystems, Inc.
00010  *
00011  * See the file "license.terms" for information on usage and redistribution of
00012  * this file, and for a DISCLAIMER OF ALL WARRANTIES.
00013  *
00014  * RCS: @(#) $Id: tclGet.c,v 1.20 2007/12/13 15:23:17 dgp Exp $
00015  */
00016 
00017 #include "tclInt.h"
00018 
00019 /*
00020  *----------------------------------------------------------------------
00021  *
00022  * Tcl_GetInt --
00023  *
00024  *      Given a string, produce the corresponding integer value.
00025  *
00026  * Results:
00027  *      The return value is normally TCL_OK; in this case *intPtr will be set
00028  *      to the integer value equivalent to src.  If src is improperly formed
00029  *      then TCL_ERROR is returned and an error message will be left in the
00030  *      interp's result.
00031  *
00032  * Side effects:
00033  *      None.
00034  *
00035  *----------------------------------------------------------------------
00036  */
00037 
00038 int
00039 Tcl_GetInt(
00040     Tcl_Interp *interp,         /* Interpreter to use for error reporting. */
00041     CONST char *src,            /* String containing a (possibly signed)
00042                                  * integer in a form acceptable to strtoul. */
00043     int *intPtr)                /* Place to store converted result. */
00044 {
00045     Tcl_Obj obj;
00046     int code;
00047 
00048     obj.refCount = 1;
00049     obj.bytes = (char *) src;
00050     obj.length = strlen(src);
00051     obj.typePtr = NULL;
00052 
00053     code = Tcl_GetIntFromObj(interp, &obj, intPtr);
00054     if (obj.refCount > 1) {
00055         Tcl_Panic("invalid sharing of Tcl_Obj on C stack");
00056     }
00057     return code;
00058 }
00059 
00060 /*
00061  *----------------------------------------------------------------------
00062  *
00063  * TclGetLong --
00064  *
00065  *      Given a string, produce the corresponding long integer value. This
00066  *      routine is a version of Tcl_GetInt but returns a "long" instead of an
00067  *      "int" (a difference that matters on 64-bit architectures).
00068  *
00069  * Results:
00070  *      The return value is normally TCL_OK; in this case *longPtr will be set
00071  *      to the long integer value equivalent to src. If src is improperly
00072  *      formed then TCL_ERROR is returned and an error message will be left in
00073  *      the interp's result if interp is non-NULL.
00074  *
00075  * Side effects:
00076  *      None.
00077  *
00078  *----------------------------------------------------------------------
00079  */
00080 
00081 int
00082 TclGetLong(
00083     Tcl_Interp *interp,         /* Interpreter used for error reporting if not
00084                                  * NULL. */
00085     CONST char *src,            /* String containing a (possibly signed) long
00086                                  * integer in a form acceptable to strtoul. */
00087     long *longPtr)              /* Place to store converted long result. */
00088 {
00089     Tcl_Obj obj;
00090     int code;
00091 
00092     obj.refCount = 1;
00093     obj.bytes = (char *) src;
00094     obj.length = strlen(src);
00095     obj.typePtr = NULL;
00096 
00097     code = Tcl_GetLongFromObj(interp, &obj, longPtr);
00098     if (obj.refCount > 1) {
00099         Tcl_Panic("invalid sharing of Tcl_Obj on C stack");
00100     }
00101     return code;
00102 }
00103 
00104 /*
00105  *----------------------------------------------------------------------
00106  *
00107  * Tcl_GetDouble --
00108  *
00109  *      Given a string, produce the corresponding double-precision
00110  *      floating-point value.
00111  *
00112  * Results:
00113  *      The return value is normally TCL_OK; in this case *doublePtr will be
00114  *      set to the double-precision value equivalent to src. If src is
00115  *      improperly formed then TCL_ERROR is returned and an error message will
00116  *      be left in the interp's result.
00117  *
00118  * Side effects:
00119  *      None.
00120  *
00121  *----------------------------------------------------------------------
00122  */
00123 
00124 int
00125 Tcl_GetDouble(
00126     Tcl_Interp *interp,         /* Interpreter used for error reporting. */
00127     CONST char *src,            /* String containing a floating-point number
00128                                  * in a form acceptable to strtod. */
00129     double *doublePtr)          /* Place to store converted result. */
00130 {
00131     Tcl_Obj obj;
00132     int code;
00133 
00134     obj.refCount = 1;
00135     obj.bytes = (char *) src;
00136     obj.length = strlen(src);
00137     obj.typePtr = NULL;
00138 
00139     code = Tcl_GetDoubleFromObj(interp, &obj, doublePtr);
00140     if (obj.refCount > 1) {
00141         Tcl_Panic("invalid sharing of Tcl_Obj on C stack");
00142     }
00143     return code;
00144 }
00145 
00146 /*
00147  *----------------------------------------------------------------------
00148  *
00149  * Tcl_GetBoolean --
00150  *
00151  *      Given a string, return a 0/1 boolean value corresponding to the
00152  *      string.
00153  *
00154  * Results:
00155  *      The return value is normally TCL_OK; in this case *boolPtr will be set
00156  *      to the 0/1 value equivalent to src. If src is improperly formed then
00157  *      TCL_ERROR is returned and an error message will be left in the
00158  *      interp's result.
00159  *
00160  * Side effects:
00161  *      None.
00162  *
00163  *----------------------------------------------------------------------
00164  */
00165 
00166 int
00167 Tcl_GetBoolean(
00168     Tcl_Interp *interp,         /* Interpreter used for error reporting. */
00169     CONST char *src,            /* String containing a boolean number
00170                                  * specified either as 1/0 or true/false or
00171                                  * yes/no. */
00172     int *boolPtr)               /* Place to store converted result, which will
00173                                  * be 0 or 1. */
00174 {
00175     Tcl_Obj obj;
00176     int code;
00177 
00178     obj.refCount = 1;
00179     obj.bytes = (char *) src;
00180     obj.length = strlen(src);
00181     obj.typePtr = NULL;
00182 
00183     code = Tcl_ConvertToType(interp, &obj, &tclBooleanType);
00184     if (obj.refCount > 1) {
00185         Tcl_Panic("invalid sharing of Tcl_Obj on C stack");
00186     }
00187     if (code == TCL_OK) {
00188         *boolPtr = obj.internalRep.longValue;
00189     }
00190     return code;
00191 }
00192 
00193 /*
00194  * Local Variables:
00195  * mode: c
00196  * c-basic-offset: 4
00197  * fill-column: 78
00198  * End:
00199  */



Generated on Wed Mar 12 12:18:16 2008 by  doxygen 1.5.1