summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordkf <donal.k.fellows@manchester.ac.uk>2024-05-25 09:13:47 (GMT)
committerdkf <donal.k.fellows@manchester.ac.uk>2024-05-25 09:13:47 (GMT)
commitd0f4560014d2fd2def1e2860ec52259ef80495f1 (patch)
treeb305f32aa914a475dd1a6ab9b0c071044fcde621
parent0201d4b3ee5ec24ea7bb96ef5acb9d52fc65b9e7 (diff)
downloadtcl-d0f4560014d2fd2def1e2860ec52259ef80495f1.zip
tcl-d0f4560014d2fd2def1e2860ec52259ef80495f1.tar.gz
tcl-d0f4560014d2fd2def1e2860ec52259ef80495f1.tar.bz2
Add some more C functions for working with dicts [656fe3c816]
-rw-r--r--generic/tclDictObj.c114
-rw-r--r--generic/tclInt.h6
2 files changed, 120 insertions, 0 deletions
diff --git a/generic/tclDictObj.c b/generic/tclDictObj.c
index de3547e..b44e437 100644
--- a/generic/tclDictObj.c
+++ b/generic/tclDictObj.c
@@ -1440,6 +1440,120 @@ 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;
+}
+
/***** START OF FUNCTIONS IMPLEMENTING TCL COMMANDS *****/
/*
diff --git a/generic/tclInt.h b/generic/tclInt.h
index 5890bcb..a3761e5 100644
--- a/generic/tclInt.h
+++ b/generic/tclInt.h
@@ -2918,6 +2918,12 @@ MODULE_SCOPE int TclFindDictElement(Tcl_Interp *interp,
const char *dict, int dictLength,
const char **elementPtr, const char **nextPtr,
int *sizePtr, int *literalPtr);
+MODULE_SCOPE int TclDictGet(Tcl_Interp *interp, Tcl_Obj *dictPtr,
+ const char *key, Tcl_Obj **valuePtrPtr);
+MODULE_SCOPE int TclDictPut(Tcl_Interp *interp, Tcl_Obj *dictPtr,
+ const char *key, Tcl_Obj *valuePtr);
+MODULE_SCOPE int TclDictPutString(Tcl_Interp *interp, Tcl_Obj *dictPtr,
+ const char *key, const char *value);
/* TIP #280 - Modified token based evaluation, with line information. */
MODULE_SCOPE int TclEvalEx(Tcl_Interp *interp, const char *script,
int numBytes, int flags, int line,