| 1 | '\" |
|---|
| 2 | '\" Copyright (c) 1997 Sun Microsystems, Inc. |
|---|
| 3 | '\" |
|---|
| 4 | '\" See the file "license.terms" for information on usage and redistribution |
|---|
| 5 | '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
|---|
| 6 | '\" |
|---|
| 7 | '\" RCS: @(#) $Id: GetIndex.3,v 1.22 2007/12/13 15:22:31 dgp Exp $ |
|---|
| 8 | '\" |
|---|
| 9 | .so man.macros |
|---|
| 10 | .TH Tcl_GetIndexFromObj 3 8.1 Tcl "Tcl Library Procedures" |
|---|
| 11 | .BS |
|---|
| 12 | .SH NAME |
|---|
| 13 | Tcl_GetIndexFromObj, Tcl_GetIndexFromObjStruct \- lookup string in table of keywords |
|---|
| 14 | .SH SYNOPSIS |
|---|
| 15 | .nf |
|---|
| 16 | \fB#include <tcl.h>\fR |
|---|
| 17 | .sp |
|---|
| 18 | int |
|---|
| 19 | \fBTcl_GetIndexFromObj\fR(\fIinterp, objPtr, tablePtr, msg, flags, |
|---|
| 20 | indexPtr\fR) |
|---|
| 21 | .sp |
|---|
| 22 | int |
|---|
| 23 | \fBTcl_GetIndexFromObjStruct\fR(\fIinterp, objPtr, structTablePtr, offset, |
|---|
| 24 | msg, flags, indexPtr\fR) |
|---|
| 25 | .SH ARGUMENTS |
|---|
| 26 | .AS "const char" *structTablePtr in/out |
|---|
| 27 | .AP Tcl_Interp *interp in |
|---|
| 28 | Interpreter to use for error reporting; if NULL, then no message is |
|---|
| 29 | provided on errors. |
|---|
| 30 | .AP Tcl_Obj *objPtr in/out |
|---|
| 31 | The string value of this object is used to search through \fItablePtr\fR. |
|---|
| 32 | The internal representation is modified to hold the index of the matching |
|---|
| 33 | table entry. |
|---|
| 34 | .AP "const char" **tablePtr in |
|---|
| 35 | An array of null-terminated strings. The end of the array is marked |
|---|
| 36 | by a NULL string pointer. |
|---|
| 37 | .AP "const void" *structTablePtr in |
|---|
| 38 | An array of arbitrary type, typically some \fBstruct\fR type. |
|---|
| 39 | The first member of the structure must be a null-terminated string. |
|---|
| 40 | The size of the structure is given by \fIoffset\fR. |
|---|
| 41 | .AP int offset in |
|---|
| 42 | The offset to add to structTablePtr to get to the next entry. |
|---|
| 43 | The end of the array is marked by a NULL string pointer. |
|---|
| 44 | .AP "const char" *msg in |
|---|
| 45 | Null-terminated string describing what is being looked up, such as |
|---|
| 46 | \fBoption\fR. This string is included in error messages. |
|---|
| 47 | .AP int flags in |
|---|
| 48 | OR-ed combination of bits providing additional information for |
|---|
| 49 | operation. The only bit that is currently defined is \fBTCL_EXACT\fR. |
|---|
| 50 | .AP int *indexPtr out |
|---|
| 51 | The index of the string in \fItablePtr\fR that matches the value of |
|---|
| 52 | \fIobjPtr\fR is returned here. |
|---|
| 53 | .BE |
|---|
| 54 | |
|---|
| 55 | .SH DESCRIPTION |
|---|
| 56 | .PP |
|---|
| 57 | This procedure provides an efficient way for looking up keywords, |
|---|
| 58 | switch names, option names, and similar things where the value of |
|---|
| 59 | an object must be one of a predefined set of values. |
|---|
| 60 | \fIObjPtr\fR is compared against each of |
|---|
| 61 | the strings in \fItablePtr\fR to find a match. A match occurs if |
|---|
| 62 | \fIobjPtr\fR's string value is identical to one of the strings in |
|---|
| 63 | \fItablePtr\fR, or if it is a non-empty unique abbreviation |
|---|
| 64 | for exactly one of the strings in \fItablePtr\fR and the |
|---|
| 65 | \fBTCL_EXACT\fR flag was not specified; in either case |
|---|
| 66 | the index of the matching entry is stored at \fI*indexPtr\fR |
|---|
| 67 | and \fBTCL_OK\fR is returned. |
|---|
| 68 | .PP |
|---|
| 69 | If there is no matching entry, |
|---|
| 70 | \fBTCL_ERROR\fR is returned and an error message is left in \fIinterp\fR's |
|---|
| 71 | result if \fIinterp\fR is not NULL. \fIMsg\fR is included in the |
|---|
| 72 | error message to indicate what was being looked up. For example, |
|---|
| 73 | if \fImsg\fR is \fBoption\fR the error message will have a form like |
|---|
| 74 | .QW "\fBbad option \N'34'firt\N'34': must be first, second, or third\fR" . |
|---|
| 75 | .PP |
|---|
| 76 | If \fBTcl_GetIndexFromObj\fR completes successfully it modifies the |
|---|
| 77 | internal representation of \fIobjPtr\fR to hold the address of |
|---|
| 78 | the table and the index of the matching entry. If \fBTcl_GetIndexFromObj\fR |
|---|
| 79 | is invoked again with the same \fIobjPtr\fR and \fItablePtr\fR |
|---|
| 80 | arguments (e.g. during a reinvocation of a Tcl command), it returns |
|---|
| 81 | the matching index immediately without having to redo the lookup |
|---|
| 82 | operation. Note: \fBTcl_GetIndexFromObj\fR assumes that the entries |
|---|
| 83 | in \fItablePtr\fR are static: they must not change between |
|---|
| 84 | invocations. If the value of \fIobjPtr\fR is the empty string, |
|---|
| 85 | \fBTcl_GetIndexFromObj\fR will treat it as a non-matching value |
|---|
| 86 | and return \fBTCL_ERROR\fR. |
|---|
| 87 | .PP |
|---|
| 88 | \fBTcl_GetIndexFromObjStruct\fR works just like |
|---|
| 89 | \fBTcl_GetIndexFromObj\fR, except that instead of treating |
|---|
| 90 | \fItablePtr\fR as an array of string pointers, it treats it as a |
|---|
| 91 | pointer to the first string in a series of strings that have |
|---|
| 92 | \fIoffset\fR bytes between them (i.e. that there is a pointer to the |
|---|
| 93 | first array of characters at \fItablePtr\fR, a pointer to the second |
|---|
| 94 | array of characters at \fItablePtr\fR+\fIoffset\fR bytes, etc.) |
|---|
| 95 | This is particularly useful when processing things like |
|---|
| 96 | \fBTk_ConfigurationSpec\fR, whose string keys are in the same place in |
|---|
| 97 | each of several array elements. |
|---|
| 98 | |
|---|
| 99 | .SH "SEE ALSO" |
|---|
| 100 | Tcl_WrongNumArgs |
|---|
| 101 | |
|---|
| 102 | .SH KEYWORDS |
|---|
| 103 | index, object, table lookup |
|---|