summaryrefslogtreecommitdiffstats
path: root/generic/tclDictObj.c
diff options
context:
space:
mode:
Diffstat (limited to 'generic/tclDictObj.c')
-rw-r--r--generic/tclDictObj.c159
1 files changed, 153 insertions, 6 deletions
diff --git a/generic/tclDictObj.c b/generic/tclDictObj.c
index 8c34bb8..a0016df 100644
--- a/generic/tclDictObj.c
+++ b/generic/tclDictObj.c
@@ -718,7 +718,7 @@ SetDictFromAny(
if (interp != NULL) {
Tcl_SetObjResult(interp, Tcl_NewStringObj(
"missing value to go with key", -1));
- Tcl_SetErrorCode(interp, "TCL", "VALUE", "DICTIONARY", (void *)NULL);
+ Tcl_SetErrorCode(interp, "TCL", "VALUE", "DICTIONARY", (char *)NULL);
}
errorInFindDictElement:
DeleteChainTable(dict);
@@ -813,7 +813,7 @@ TclTraceDictPath(
"key \"%s\" not known in dictionary",
TclGetString(keyv[i])));
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "DICT",
- TclGetString(keyv[i]), (void *)NULL);
+ TclGetString(keyv[i]), (char *)NULL);
}
return NULL;
}
@@ -1487,6 +1487,153 @@ Tcl_DbNewDictObj(
}
#endif
+/***** START OF FUNCTIONS ACTING AS HELPERS *****/
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * TclDictGet --
+ *
+ * Given a key, get its value from the dictionary (or NULL if key is not
+ * found in dictionary.)
+ *
+ * Results:
+ * A standard Tcl result. The variable pointed to by valuePtrPtr is
+ * updated with the value for the key. Note that it is not an error for
+ * the key to have no mapping in the dictionary.
+ *
+ * Side effects:
+ * The object pointed to by dictPtr is converted to a dictionary if it is
+ * not already one.
+ *
+ *----------------------------------------------------------------------
+ */
+int
+TclDictGet(
+ Tcl_Interp *interp,
+ Tcl_Obj *dictPtr,
+ const char *key, /* The key in a C string. */
+ Tcl_Obj **valuePtrPtr) /* Where to write the value. */
+{
+ Tcl_Obj *keyPtr = Tcl_NewStringObj(key, -1);
+ int code;
+
+ Tcl_IncrRefCount(keyPtr);
+ code = Tcl_DictObjGet(interp, dictPtr, keyPtr, valuePtrPtr);
+ Tcl_DecrRefCount(keyPtr);
+ return code;
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * TclDictPut --
+ *
+ * Add a key,value pair to a dictionary, or update the value for a key if
+ * that key already has a mapping in the dictionary.
+ *
+ * If valuePtr is a zero-count object and is not written into the
+ * dictionary because of an error, it is freed by this routine. The caller
+ * does NOT need to do reference count management.
+ *
+ * Results:
+ * A standard Tcl result.
+ *
+ * Side effects:
+ * The object pointed to by dictPtr is converted to a dictionary if it is
+ * not already one, and any string representation that it has is
+ * invalidated.
+ *
+ *----------------------------------------------------------------------
+ */
+int
+TclDictPut(
+ Tcl_Interp *interp,
+ Tcl_Obj *dictPtr,
+ const char *key, /* The key in a C string. */
+ Tcl_Obj *valuePtr) /* The value to write in. */
+{
+ Tcl_Obj *keyPtr = Tcl_NewStringObj(key, -1);
+ int code;
+
+ Tcl_IncrRefCount(keyPtr);
+ Tcl_IncrRefCount(valuePtr);
+ code = Tcl_DictObjPut(interp, dictPtr, keyPtr, valuePtr);
+ Tcl_DecrRefCount(keyPtr);
+ Tcl_DecrRefCount(valuePtr);
+ return code;
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * TclDictPutString --
+ *
+ * Add a key,value pair to a dictionary, or update the value for a key if
+ * that key already has a mapping in the dictionary.
+ *
+ * Results:
+ * A standard Tcl result.
+ *
+ * Side effects:
+ * The object pointed to by dictPtr is converted to a dictionary if it is
+ * not already one, and any string representation that it has is
+ * invalidated.
+ *
+ *----------------------------------------------------------------------
+ */
+int
+TclDictPutString(
+ Tcl_Interp *interp,
+ Tcl_Obj *dictPtr,
+ const char *key, /* The key in a C string. */
+ const char *value) /* The value in a C string. */
+{
+ Tcl_Obj *keyPtr = Tcl_NewStringObj(key, -1);
+ Tcl_Obj *valuePtr = Tcl_NewStringObj(value, -1);
+ int code;
+
+ Tcl_IncrRefCount(keyPtr);
+ Tcl_IncrRefCount(valuePtr);
+ code = Tcl_DictObjPut(interp, dictPtr, keyPtr, valuePtr);
+ Tcl_DecrRefCount(keyPtr);
+ Tcl_DecrRefCount(valuePtr);
+ return code;
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * TclDictRemove --
+ *
+ * Remove the key,value pair with the given key from the dictionary; the
+ * key does not need to be present in the dictionary.
+ *
+ * Results:
+ * A standard Tcl result.
+ *
+ * Side effects:
+ * The object pointed to by dictPtr is converted to a dictionary if it is
+ * not already one, and any string representation that it has is
+ * invalidated.
+ *
+ *----------------------------------------------------------------------
+ */
+int
+TclDictRemove(
+ Tcl_Interp *interp,
+ Tcl_Obj *dictPtr,
+ const char *key) /* The key in a C string. */
+{
+ Tcl_Obj *keyPtr = Tcl_NewStringObj(key, -1);
+ int code;
+
+ Tcl_IncrRefCount(keyPtr);
+ code = Tcl_DictObjRemove(interp, dictPtr, keyPtr);
+ Tcl_DecrRefCount(keyPtr);
+ return code;
+}
+
/***** START OF FUNCTIONS IMPLEMENTING TCL COMMANDS *****/
/*
@@ -1625,7 +1772,7 @@ DictGetCmd(
"key \"%s\" not known in dictionary",
TclGetString(objv[objc-1])));
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "DICT",
- TclGetString(objv[objc-1]), (void *)NULL);
+ TclGetString(objv[objc-1]), (char *)NULL);
return TCL_ERROR;
}
Tcl_SetObjResult(interp, valuePtr);
@@ -2556,7 +2703,7 @@ DictForNRCmd(
if (varc != 2) {
Tcl_SetObjResult(interp, Tcl_NewStringObj(
"must have exactly two variable names", -1));
- Tcl_SetErrorCode(interp, "TCL", "SYNTAX", "dict", "for", (void *)NULL);
+ Tcl_SetErrorCode(interp, "TCL", "SYNTAX", "dict", "for", (char *)NULL);
return TCL_ERROR;
}
searchPtr = (Tcl_DictSearch *)TclStackAlloc(interp, sizeof(Tcl_DictSearch));
@@ -2751,7 +2898,7 @@ DictMapNRCmd(
if (varc != 2) {
Tcl_SetObjResult(interp, Tcl_NewStringObj(
"must have exactly two variable names", -1));
- Tcl_SetErrorCode(interp, "TCL", "SYNTAX", "dict", "map", (void *)NULL);
+ Tcl_SetErrorCode(interp, "TCL", "SYNTAX", "dict", "map", (char *)NULL);
return TCL_ERROR;
}
storagePtr = (DictMapStorage *)TclStackAlloc(interp, sizeof(DictMapStorage));
@@ -3191,7 +3338,7 @@ DictFilterCmd(
if (varc != 2) {
Tcl_SetObjResult(interp, Tcl_NewStringObj(
"must have exactly two variable names", -1));
- Tcl_SetErrorCode(interp, "TCL", "SYNTAX", "dict", "filter", (void *)NULL);
+ Tcl_SetErrorCode(interp, "TCL", "SYNTAX", "dict", "filter", (char *)NULL);
return TCL_ERROR;
}
keyVarObj = varv[0];