gtpc2mc1 | 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 in the specified buffer. If the collection contains duplicate keys equal to the specified key, the first one found will be returned. The key is not returned if it is only partially matched.
Format
#include <c$to2.h> TO2_BUF_PTR TO2_atKeyWithBuffer(const TO2_PID_PTR pid_ptr, TO2_ENV_PTR env_ptr, const void *key, const long *keylength, const long *bufferLength, TO2_BUF_PTR buffer);
Normal Return
The normal return value is the address of the same buffer that you specified when calling the function. The normal return is a pointer (TO2_BUF_PTR) to a structure (buffer) of type TO2_BUF_HDR (see Type Definitions).
If the length of the data is greater then the length of the buffer, only the amount of data that will fit is placed in the buffer. The length is returned in the buffer and is the actual length of the data element. It is the responsibility of the caller to check the returned length against the length of the supplied buffer to determine if the entire element has been returned.
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_BUFFER_SIZE
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_ZERO_PID
Programming Considerations
This function does not use TPF transaction services on behalf of the caller.
Examples
The following example searches a keyed collection to determine if it contains an element with the specified key. If an element with that key is found, it is copied into the specified buffer.
#include <c$to2.h> /* Needed for TO2 API functions */ #include <stdio.h> /* APIs for standard I/O functions */ char buf[50]; TO2_BUF_PTR elem_ptr; TO2_BUF_PTR buf_ptr=&buf; TO2_PID collect; /* will set to KeyBag PID */ TO2_ENV_PTR env_ptr; /* Pointer to the TO2 environment */ char keya[] = "KeyA"; /* key to search for */ long keylength; long bufL;
·
·
·
/************************************************************************/ /* Test to see if collection contains entry for key in keya */ /************************************************************************/ keylength = sizeof(keya); bufL = sizeof(buf); elem_ptr = TO2_atKeyWithBuffer(&collect, env_ptr, keya, &keylength, &bufL, buf_ptr ); if (elem_ptr == NULL) { printf("TO2_atKeyWithBuffer failed!\n"); process_error(env_ptr); } else { printf("TO2_atKeyWithBuffer is successful. Key found!\n"); }
Related Information