tclvar.c

来自「tcl是工具命令语言」· C语言 代码 · 共 1,908 行 · 第 1/5 页

C
1,908
字号
 *	Return the value of a Tcl variable as a Tcl object, given the *      pointers to the variable's (and possibly containing array's)  *      VAR structure. * * Results: *	The return value points to the current object value of the variable *	given by varPtr. If the specified variable doesn't exist, or if there  *      is a clash in array usage, then NULL is returned and a message will be  *      left in the interpreter's result if the TCL_LEAVE_ERR_MSG flag is set. * * Side effects: *	The ref count for the returned object is _not_ incremented to *	reflect the returned reference; if you want to keep a reference to *	the object you must increment its ref count yourself. * *---------------------------------------------------------------------- */Tcl_Obj *TclPtrGetVar(interp, varPtr, arrayPtr, part1, part2, flags)    Tcl_Interp *interp;		/* Command interpreter in which variable is				 * to be looked up. */    register Var *varPtr;       /* The variable to be read.*/    Var *arrayPtr;              /* NULL for scalar variables, pointer to				 * the containing array otherwise. */    CONST char *part1;		/* Name of an array (if part2 is non-NULL)				 * or the name of a variable. */    CONST char *part2;		/* If non-NULL, gives the name of an element				 * in the array part1. */    CONST int flags;		/* OR-ed combination of TCL_GLOBAL_ONLY,				 * and TCL_LEAVE_ERR_MSG bits. */{    Interp *iPtr = (Interp *) interp;    CONST char *msg;    /*     * Invoke any traces that have been set for the variable.     */    if ((varPtr->tracePtr != NULL)	    || ((arrayPtr != NULL) && (arrayPtr->tracePtr != NULL))) {	if (TCL_ERROR == CallVarTraces(iPtr, arrayPtr, varPtr, part1, part2,		(flags & (TCL_NAMESPACE_ONLY|TCL_GLOBAL_ONLY))		| TCL_TRACE_READS, (flags & TCL_LEAVE_ERR_MSG))) {	    goto errorReturn;	}    }    /*     * Return the element if it's an existing scalar variable.     */        if (TclIsVarScalar(varPtr) && !TclIsVarUndefined(varPtr)) {	return varPtr->value.objPtr;    }        if (flags & TCL_LEAVE_ERR_MSG) {	if (TclIsVarUndefined(varPtr) && (arrayPtr != NULL)	        && !TclIsVarUndefined(arrayPtr)) {	    msg = noSuchElement;	} else if (TclIsVarArray(varPtr)) {	    msg = isArray;	} else {	    msg = noSuchVar;	}	VarErrMsg(interp, part1, part2, "read", msg);    }    /*     * An error. If the variable doesn't exist anymore and no-one's using     * it, then free up the relevant structures and hash table entries.     */    errorReturn:    if (TclIsVarUndefined(varPtr)) {	CleanupVar(varPtr, arrayPtr);    }    return NULL;}/* *---------------------------------------------------------------------- * * Tcl_SetObjCmd -- * *	This procedure is invoked to process the "set" Tcl command. *	See the user documentation for details on what it does. * * Results: *	A standard Tcl result value. * * Side effects: *	A variable's value may be changed. * *---------------------------------------------------------------------- */	/* ARGSUSED */intTcl_SetObjCmd(dummy, interp, objc, objv)    ClientData dummy;			/* Not used. */    register Tcl_Interp *interp;	/* Current interpreter. */    int objc;				/* Number of arguments. */    Tcl_Obj *CONST objv[];		/* Argument objects. */{    Tcl_Obj *varValueObj;    if (objc == 2) {	varValueObj = Tcl_ObjGetVar2(interp, objv[1], NULL, TCL_LEAVE_ERR_MSG);	if (varValueObj == NULL) {	    return TCL_ERROR;	}	Tcl_SetObjResult(interp, varValueObj);	return TCL_OK;    } else if (objc == 3) {	varValueObj = Tcl_ObjSetVar2(interp, objv[1], NULL, objv[2],		TCL_LEAVE_ERR_MSG);	if (varValueObj == NULL) {	    return TCL_ERROR;	}	Tcl_SetObjResult(interp, varValueObj);	return TCL_OK;    } else {	Tcl_WrongNumArgs(interp, 1, objv, "varName ?newValue?");	return TCL_ERROR;    }}/* *---------------------------------------------------------------------- * * Tcl_SetVar -- * *	Change the value of a variable. * * Results: *	Returns a pointer to the malloc'ed string which is the character *	representation of the variable's new value. The caller must not *	modify this string. If the write operation was disallowed then NULL *	is returned; if the TCL_LEAVE_ERR_MSG flag is set, then an *	explanatory message will be left in the interp's result. Note that the *	returned string may not be the same as newValue; this is because *	variable traces may modify the variable's value. * * Side effects: *	If varName is defined as a local or global variable in interp, *	its value is changed to newValue. If varName isn't currently *	defined, then a new global variable by that name is created. * *---------------------------------------------------------------------- */CONST char *Tcl_SetVar(interp, varName, newValue, flags)    Tcl_Interp *interp;		/* Command interpreter in which varName is				 * to be looked up. */    CONST char *varName;	/* Name of a variable in interp. */    CONST char *newValue;	/* New value for varName. */    int flags;			/* Various flags that tell how to set value:				 * any of TCL_GLOBAL_ONLY,				 * TCL_NAMESPACE_ONLY, TCL_APPEND_VALUE,				 * TCL_LIST_ELEMENT, TCL_LEAVE_ERR_MSG. */{    return Tcl_SetVar2(interp, varName, (char *) NULL, newValue, flags);}/* *---------------------------------------------------------------------- * * Tcl_SetVar2 -- * *      Given a two-part variable name, which may refer either to a *      scalar variable or an element of an array, change the value *      of the variable.  If the named scalar or array or element *      doesn't exist then create one. * * Results: *	Returns a pointer to the malloc'ed string which is the character *	representation of the variable's new value. The caller must not *	modify this string. If the write operation was disallowed because an *	array was expected but not found (or vice versa), then NULL is *	returned; if the TCL_LEAVE_ERR_MSG flag is set, then an explanatory *	message will be left in the interp's result. Note that the returned *	string may not be the same as newValue; this is because variable *	traces may modify the variable's value. * * Side effects: *      The value of the given variable is set. If either the array *      or the entry didn't exist then a new one is created. * *---------------------------------------------------------------------- */CONST char *Tcl_SetVar2(interp, part1, part2, newValue, flags)    Tcl_Interp *interp;         /* Command interpreter in which variable is                                 * to be looked up. */    CONST char *part1;          /* If part2 is NULL, this is name of scalar                                 * variable. Otherwise it is the name of                                 * an array. */    CONST char *part2;		/* Name of an element within an array, or				 * NULL. */    CONST char *newValue;       /* New value for variable. */    int flags;                  /* Various flags that tell how to set value:				 * any of TCL_GLOBAL_ONLY,				 * TCL_NAMESPACE_ONLY, TCL_APPEND_VALUE,				 * TCL_LIST_ELEMENT, or TCL_LEAVE_ERR_MSG */{    register Tcl_Obj *valuePtr;    Tcl_Obj *varValuePtr;    /*     * Create an object holding the variable's new value and use     * Tcl_SetVar2Ex to actually set the variable.     */    valuePtr = Tcl_NewStringObj(newValue, -1);    Tcl_IncrRefCount(valuePtr);    varValuePtr = Tcl_SetVar2Ex(interp, part1, part2, valuePtr, flags);    Tcl_DecrRefCount(valuePtr); /* done with the object */        if (varValuePtr == NULL) {	return NULL;    }    return TclGetString(varValuePtr);}/* *---------------------------------------------------------------------- * * Tcl_SetVar2Ex -- * *	Given a two-part variable name, which may refer either to a scalar *	variable or an element of an array, change the value of the variable *	to a new Tcl object value. If the named scalar or array or element *	doesn't exist then create one. * * Results: *	Returns a pointer to the Tcl_Obj holding the new value of the *	variable. If the write operation was disallowed because an array was *	expected but not found (or vice versa), then NULL is returned; if *	the TCL_LEAVE_ERR_MSG flag is set, then an explanatory message will *	be left in the interpreter's result. Note that the returned object *	may not be the same one referenced by newValuePtr; this is because *	variable traces may modify the variable's value. * * Side effects: *	The value of the given variable is set. If either the array or the *	entry didn't exist then a new variable is created. * *	The reference count is decremented for any old value of the variable *	and incremented for its new value. If the new value for the variable *	is not the same one referenced by newValuePtr (perhaps as a result *	of a variable trace), then newValuePtr's ref count is left unchanged *	by Tcl_SetVar2Ex. newValuePtr's ref count is also left unchanged if *	we are appending it as a string value: that is, if "flags" includes *	TCL_APPEND_VALUE but not TCL_LIST_ELEMENT. * *	The reference count for the returned object is _not_ incremented: if *	you want to keep a reference to the object you must increment its *	ref count yourself. * *---------------------------------------------------------------------- */Tcl_Obj *Tcl_SetVar2Ex(interp, part1, part2, newValuePtr, flags)    Tcl_Interp *interp;		/* Command interpreter in which variable is				 * to be found. */    CONST char *part1;		/* Name of an array (if part2 is non-NULL)				 * or the name of a variable. */    CONST char *part2;		/* If non-NULL, gives the name of an element				 * in the array part1. */    Tcl_Obj *newValuePtr;	/* New value for variable. */    int flags;			/* Various flags that tell how to set value:				 * any of TCL_GLOBAL_ONLY,				 * TCL_NAMESPACE_ONLY, TCL_APPEND_VALUE,				 * TCL_LIST_ELEMENT or TCL_LEAVE_ERR_MSG. */{    Var *varPtr, *arrayPtr;    varPtr = TclLookupVar(interp, part1, part2, flags, "set",	    /*createPart1*/ 1, /*createPart2*/ 1, &arrayPtr);    if (varPtr == NULL) {	return NULL;    }    return TclPtrSetVar(interp, varPtr, arrayPtr, part1, part2,             newValuePtr, flags);}/* *---------------------------------------------------------------------- * * Tcl_ObjSetVar2 -- * *	This function is the same as Tcl_SetVar2Ex above, except the *	variable names are passed in Tcl object instead of strings. * * Results: *	Returns a pointer to the Tcl_Obj holding the new value of the *	variable. If the write operation was disallowed because an array was *	expected but not found (or vice versa), then NULL is returned; if *	the TCL_LEAVE_ERR_MSG flag is set, then an explanatory message will *	be left in the interpreter's result. Note that the returned object *	may not be the same one referenced by newValuePtr; this is because *	variable traces may modify the variable's value. * * Side effects: *	The value of the given variable is set. If either the array or the *	entry didn't exist then a new variable is created. * *---------------------------------------------------------------------- */Tcl_Obj *Tcl_ObjSetVar2(interp, part1Ptr, part2Ptr, newValuePtr, flags)    Tcl_Interp *interp;		/* Command interpreter in which variable is				 * to be found. */    register Tcl_Obj *part1Ptr;	/* Points to an object holding the name of				 * an array (if part2 is non-NULL) or the				 * name of a variable. */    register Tcl_Obj *part2Ptr;	/* If non-null, points to an object holding				 * the name of an element in the array				 * part1Ptr. */    Tcl_Obj *newValuePtr;	/* New value for variable. */    int flags;			/* Various flags that tell how to set value:				 * any of TCL_GLOBAL_ONLY,				 * TCL_NAMESPACE_ONLY, TCL_APPEND_VALUE,				 * TCL_LIST_ELEMENT, or TCL_LEAVE_ERR_MSG. */{    Var *varPtr, *arrayPtr;    char *part1, *part2;    part1 = TclGetString(part1Ptr);    part2 = ((part2Ptr == NULL) ? NULL : Tcl_GetString(part2Ptr));        varPtr = TclObjLookupVar(interp, part1Ptr, part2, flags, "set",	    /*createPart1*/ 1, /*createPart2*/ 1, &arrayPtr);    if (varPtr == NULL) {	return NULL;    }    return TclPtrSetVar(interp, varPtr, arrayPtr, part1, part2,             newValuePtr, flags);}/* *---------------------------------------------------------------------- * * TclPtrSetVar -- * *	This function is the same as Tcl_SetVar2Ex above, except that *      it requires pointers to the variable's Var structs in addition *	to the variable names. * * Results: *	Returns a pointer to the Tcl_Obj holding the new value of the *	variable. If the write operation was disallowed because an array was *	expected but not found (or vice versa), then NULL is returned; if *	the TCL_LEAVE_ERR_MSG flag is set, then an explanatory message will *	be left in the interpreter's result. Note that the returned object *	may not be the same one referenced by newValuePtr; this is because *	variable traces may modify the variable's value. * * Side effects: *	The value of the given variable is set. If either the array or the *	entry didn't exist then a new variable is created. * *---------------------------------------------------------------------- */Tcl_Obj *TclPtrSetVar(interp, varPtr, arrayPtr, part1, part2, newValuePtr, flags)    Tcl_Interp *interp;		/* Command interpreter in which variable is				 * to be looked up. */    register Var *varPtr;    Var *arrayPtr;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?