TO2_atKeyWithBuffer: Return the specified element
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.
Last updated
Changed for PUT00.
Format
#include <tpf/c_to2.h>
TO2_BUF_PTR TO2_atKeyWithBuffer(const TO2_PID_PTR pid_ptr,
TO2_ENV_PTR env_ptr,
const void *key,
const int *keylength,
const int *bufferLength,
TO2_BUF_PTR buffer); - pid_ptr
- The pointer to the persistent identifier (PID) assigned to the collection.
- env_ptr
- The pointer to the environment as returned by the TO2_createEnv function.
- key
- The pointer to the value that will be used as the key to locate the specific entry.
- keyLength
- The pointer to the length of the key. The maximum length is 248 bytes. For key sorted set collections, the key length can be shorter than the defined key to allow a locate by partial key. The key comparison is always from the first character of the key for the specified length. For all other keyed collections, the key length must equal the maximum defined key length of the collection.
- bufferLength
- The pointer to the length of the specified buffer. The buffer should be large enough to hold the data to be returned plus 16 additional bytes for a header. The minimum buffer length is 20 bytes.
- buffer
- The pointer to the buffer where the element will be returned.
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.
- 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 z/TPF transaction services on behalf of the caller.
Examples
#include <tpf/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 */
int keylength;
int 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
{
if (elem_ptr->dataL != bufL - (sizeof(TO2_BUF_HDR))
printf("Data returned did not fit in allocated buffer!\n");
else
printf("TO2_atKeyWithBuffer is successful. Key found!\n");
}
Related information
- TO2_atCursor: Return the element pointed to by the cursor
- TO2_atKey: Return the specified element by key
- TO2_atKeyPut: Update the specified element using a key
- TO2_atNewKeyPut: Add a new key and element.
See z/TPF collection support: non-cursor overview for more information about this function and z/TPF C/C++ language support.