gtpc2mc1C/C++ Language Support User's Guide

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.

Note:
This function does not support all collections. See Table 47 for collections that are supported for this function.

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);

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.

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