gtpc2mbz | C/C++ Language Support User's Guide |
This function searches the specified collection for the specified key and, if found, returns the associated element value. If the collection contains duplicate keys equal to the specified key, the first one found will be returned.
Format
#include <c$to2.h> TO2_BUF_PTR TO2_atKey (const TO2_PID_PTR pid_ptr, TO2_ENV_PTR env_ptr, const void *key, const long *keyLength);
Normal Return
The normal return is a pointer (TO2_BUF_PTR) to a structure (buffer) of type TO2_BUF_HDR (see Type Definitions).
Error Return
An error return is indicated by a NULL pointer. When a NULL pointer is returned, use the TO2_getErrorCode function to determine the specific error code. For more information, see Error Handling.
The following error codes are common for this function:
TO2_ERROR_EMPTY
TO2_ERROR_ENV
TO2_ERROR_LOCATOR_LGH
TO2_ERROR_LOCATOR_NOT_FOUND
TO2_ERROR_METHOD
TO2_ERROR_NOT_INIT
TO2_ERROR_PID
TO2_ERROR_REREAD
TO2_ERROR_ZERO_PID
Programming Considerations
Examples
The following example searches a keyed collection to determine if the collection contains an element with the specified key. If an element with that key is found, a buffer is obtained with a copy of the element.
#include <c$to2.h> /* Needed for TO2 API functions */ #include <stdio.h> /* APIs for standard I/O functions*/ TO2_BUF_PTR elem_ptr; TO2_PID keyb; /* will set to KeyBag PID */ TO2_ENV_PTR env_ptr; /* Pointer to the TO2 environment */ char keya[] = "KeyA"; /* key to search for */ long keylength; char *dataField;
·
·
·
/************************************************************************/ /* Test to see if collection contains entry for key in keya */ /************************************************************************/ keylength = sizeof(keya); if ((elem_ptr = TO2_atKey(&keyb, env_ptr, keya, &keylength)) == 0) { printf("TO2_atKey failed!\n"); process_error(env_ptr); return; } else { dataField = elem_ptr->data; printf("TO2_atKey is successful. Key found!\n"); }
·
·
·
free(elem_ptr);
Related Information