Artifact adb324193c818f7f5cf0b111431f6e749a10beee3bbec5fdcbd628bef98e13e3:
- File doc/markdown/GetIndex.md — part of check-in [bb08aafe0a] at 2025-07-15 19:41:45 on branch tip-700 — some non-change cosmetics to make the ctags implementation in Geany 2.1 partly happy again (user: Torsten, size: 5583) [annotate] [blame] [check-ins using]
CommandName: Tcl_GetIndexFromObj ManualSection: 3 Version: 8.1 TclPart: Tcl TclDescription: Tcl Library Procedures Links:
- prefix(n)
- Tcl_WrongNumArgs(3) Keywords:
- index
- option
- value
- table lookup Copyright:
- Copyright (c) 1997 Sun Microsystems, Inc. ---
Name
Tcl_GetIndexFromObj, Tcl_GetIndexFromObjStruct - lookup string in table of keywords
Synopsis
::: {.synopsis} ::: #include <tcl.h> [int]{.ret} [Tcl_GetIndexFromObj]{.lit} [interp, objPtr, tablePtr, msg, flags, indexPtr]{.arg} [int]{.ret} [Tcl_GetIndexFromObjStruct]{.lit} [interp, objPtr, structTablePtr, offset, msg, flags, indexPtr]{.arg} :::
Arguments
| .AP Tcl_Interp *interp in Interpreter to use for error reporting; if NULL, then no message is provided on errors. .AP Tcl_Obj *objPtr in/out The string value of this value is used to search through fItablePtrfR. If the fBTCL_INDEX_TEMP_TABLEfR flag is not specified, the internal representation is modified to hold the index of the matching table entry. .AP "const char *const" *tablePtr in An array of null-terminated strings. The end of the array is marked by a NULL string pointer. Note that, unless the fBTCL_INDEX_TEMP_TABLEfR flag is specified, references to the fItablePtrfR may be retained in the internal representation of fIobjPtrfR, so this should represent the address of a statically-allocated array. .AP "const void" *structTablePtr in An array of arbitrary type, typically some fBstructfR type. The first member of the structure must be a null-terminated string. The size of the structure is given by fIoffsetfR. Note that, unless the fBTCL_INDEX_TEMP_TABLEfR flag is specified, references to the fIstructTablePtrfR may be retained in the internal representation of fIobjPtrfR, so this should represent the address of a statically-allocated array of structures. .AP int offset in The offset to add to structTablePtr to get to the next entry. The end of the array is marked by a NULL string pointer. .AP "const char" *msg in Null-terminated string describing what is being looked up, such as fBoptionfR. This string is included in error messages. .AP int flags in OR-ed combination of bits providing additional information for operation. The only bits that are currently defined are fBTCL_EXACTfR , fBTCL_INDEX_TEMP_TABLEfR, and fBTCL_NULL_OKfR. .AP enum | char | short | int | long *indexPtr out If not (int *)NULL, the index of the string in fItablePtrfR that matches the value of fIobjPtrfR is returned here. The variable can be any integer type, signed or unsigned, char, short, long or long long. It can also be an enum. |
|---|
Description
These procedures provide an efficient way for looking up keywords, switch names, option names, and similar things where the literal value of a Tcl value must be chosen from a predefined set. fBTcl_GetIndexFromObjfR compares fIobjPtrfR against each of the strings in fItablePtrfR to find a match. A match occurs if fIobjPtrfR's string value is identical to one of the strings in fItablePtrfR, or if it is a non-empty unique abbreviation for exactly one of the strings in fItablePtrfR and the fBTCL_EXACTfR flag was not specified; in either case fBTCL_OKfR is returned. If fIindexPtrfR is not NULL the index of the matching entry is stored at fI*indexPtrfR.
If there is no matching entry, fBTCL_ERRORfR is returned and an error message is left in fIinterpfR's result if fIinterpfR is not NULL. fIMsgfR is included in the error message to indicate what was being looked up. For example, if fImsgfR is fBoptionfR the error message will have a form like "bad option N'34'firtN'34': must be first, second, or third".
If the fBTCL_INDEX_TEMP_TABLEfR was not specified, when fBTcl_GetIndexFromObjfR completes successfully it modifies the internal representation of fIobjPtrfR to hold the address of the table and the index of the matching entry. If fBTcl_GetIndexFromObjfR is invoked again with the same fIobjPtrfR and fItablePtrfR arguments (e.g. during a reinvocation of a Tcl command), it returns the matching index immediately without having to redo the lookup operation. Note that fBTcl_GetIndexFromObjfR assumes that the entries in fItablePtrfR are static: they must not change between invocations. This caching mechanism can be disallowed by specifying the fBTCL_INDEX_TEMP_TABLEfR flag. If the fBTCL_NULL_OKfR flag was specified, objPtr is allowed to be NULL or the empty string. The resulting index is -1. Otherwise, if the value of fIobjPtrfR is the empty string, fBTcl_GetIndexFromObjfR will treat it as a non-matching value and return fBTCL_ERRORfR.
Tcl_GetIndexFromObjStruct works just like fBTcl_GetIndexFromObjfR, except that instead of treating fItablePtrfR as an array of string pointers, it treats it as a pointer to the first string in a series of strings that have fIoffsetfR bytes between them (i.e. that there is a pointer to the first array of characters at fItablePtrfR, a pointer to the second array of characters at fItablePtrfR+fIoffsetfR bytes, etc.) This is particularly useful when processing things like fBTk_ConfigurationSpecfR, whose string keys are in the same place in each of several array elements.
Reference count management
Tcl_GetIndexFromObj and fBTcl_GetIndexFromObjStructfR do not modify the reference count of their fIobjPtrfR arguments; they only read. Note however that these functions may set the interpreter result; if that is the only place that is holding a reference to the object, it will be deleted.