Tcl_GetIndexFromObj(3tcl)
π NAME π
Tcl_GetIndexFromObj, Tcl_GetIndexFromObjStruct β bestie, it's the "lookup string in table of keywords" situation π
So like. This is a page from Tcl Library Procedures, the Tcl_GetIndexFromObj(3tcl) one, and it's honestly such a useful little duo. Let's get into it. π¦
β¨ SYNOPSIS β¨
#include <tcl.h>
int
Tcl_GetIndexFromObj(interp, objPtr, tablePtr, msg, flags, indexPtr)
int
Tcl_GetIndexFromObjStruct(interp, objPtr, structTablePtr, offset, msg, flags, indexPtr)
πΈ ARGUMENTS πΈ
Tcl_Interp *interp(in) β the interpreter we use for reporting errors. If it'sNULL, then no message gets provided on errors, so, like, screaming into the void π«Tcl_Obj *objPtr(in/out) β its string value is what we search throughtablePtrwith. As long as theTCL_INDEX_TEMP_TABLEflag is not specified, its internal representation gets modified to hold the index of the matching table entry πconst char *const *tablePtr(in) β an array of null-terminated strings, and the end of the array is marked by aNULLstring pointer. Heads up bestie: unlessTCL_INDEX_TEMP_TABLEis specified, references to thetablePtrmay be retained in the internal representation ofobjPtr, so this should literally be the address of a statically-allocated array. No local stack arrays. I said what I said πconst void *structTablePtr(in) β an array of arbitrary type, typically some struct type. The first member of the structure must be a null-terminated string. The size of the structure is given byoffset. And same deal as above: unlessTCL_INDEX_TEMP_TABLEis specified, references to thestructTablePtrmay be retained in the internal representation ofobjPtr, so this should represent the address of a statically-allocated array of structures π§int offset(in) β the offset to add tostructTablePtrto get to the next entry. The end of the array is marked by aNULLstring pointer.const char *msg(in) β a null-terminated string describing what is being looked up, such asoption. This string is included in error messages.int flags(in) β OR-ed combination of bits giving extra info for the operation. The only bits currently defined areTCL_EXACT,TCL_INDEX_TEMP_TABLE, andTCL_NULL_OK.enum|char|short|int|long *indexPtr(out) β if it's not(int *)NULL, the index of the string intablePtrthat matches the value ofobjPtrgets returned here. The variable can be any integer type, signed or unsigned,char,short,longorlong long. It can also be anenum. Flexible queen π
π DESCRIPTION π
These procedures are the efficient way to look up keywords, switch names, option names, and similar things β the whole "the literal value of a Tcl value has to be chosen from a predefined set" situation. No cap.
Tcl_GetIndexFromObj compares objPtr against each of the strings in tablePtr to find a match. A match happens if objPtr's string value is identical to one of the strings in tablePtr, or if it's a non-empty unique abbreviation for exactly one of the strings in tablePtr and the TCL_EXACT flag was not specified. Either way you get TCL_OK back. And if indexPtr is not NULL, the index of the matching entry gets stored at *indexPtr. Slay β¨
If there's no matching entry, TCL_ERROR comes back and an error message is left in interp's result, as long as interp isn't NULL. msg is included in the error message to say what was being looked up. For example, if msg is option, the error message will have a form like βbad option "firt": must be first, second, or thirdβ. Typo representation is real π
If TCL_INDEX_TEMP_TABLE was not specified, then when Tcl_GetIndexFromObj completes successfully it modifies the internal representation of objPtr to hold the address of the table and the index of the matching entry. So if Tcl_GetIndexFromObj gets invoked again with the same objPtr and tablePtr arguments (e.g. during a reinvocation of a Tcl command), it returns the matching index immediately, no redoing the lookup operation, we love a caching moment π Note that Tcl_GetIndexFromObj assumes that the entries in tablePtr are static β they must not change between invocations. This caching mechanism can be disallowed by specifying the TCL_INDEX_TEMP_TABLE flag. If the TCL_NULL_OK flag was specified, objPtr is allowed to be NULL or the empty string, and the resulting index is -1. Otherwise, if the value of objPtr is the empty string, Tcl_GetIndexFromObj will treat it as a non-matching value and return TCL_ERROR. Empty string catching strays fr fr π
Tcl_GetIndexFromObjStruct works just like Tcl_GetIndexFromObj, except that instead of treating tablePtr as an array of string pointers, it treats it as a pointer to the first string in a series of strings that have offset bytes between them β like, there's a pointer to the first array of characters at tablePtr, a pointer to the second array of characters at tablePtr+offset bytes, etc. This is particularly useful when processing things like Tk_ConfigurationSpec, whose string keys are in the same place in each of several array elements. So specific, so iconic π
π«Ά REFERENCE COUNT MANAGEMENT π«Ά
Tcl_GetIndexFromObj and Tcl_GetIndexFromObjStruct do not modify the reference count of their objPtr arguments β they only read. But note: these functions may set the interpreter result, and if that's the only place holding a reference to the object, it will be deleted. So like, keep an eye on your refcounts, bestie π
π SEE ALSO π
prefix(3tcl)Tcl_WrongNumArgs(3tcl)
π₯ KEYWORDS π₯
index, option, value, table lookup
okay that's the whole page, you ate that, go forth and index your little heart out π β¨
sending you off with love from the Tcl crew β this one's from the Tcl manual, version 8.1, dated alongside Tcl_GetIndexFromObj(3tcl) itself. bye bestie, ily π¦π
π¦ where this page comes from
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 1997 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms