gtpc2mfgC/C++ Language Support User's Guide

TO2_keyWithBuffer-Return Current Key Value in the Buffer

This function retrieves the key value from the item that the cursor is currently pointing to and returns it in the specified buffer. The current key is returned for dictionaries, key bags, key sets, and key sorted bags.

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

Format

#include <c$to2.h>
TO2_BUF_PTR TO2_keyWithBuffer (const TO2_PID_PTR  cursorPidPtr,
                                     TO2_ENV_PTR  env_ptr,
                               const long        *bufferLength,
                                     TO2_BUF_PTR  buffer);

cursorPidPtr
The pointer to the cursor persistent identifier (PID) created by one of the TPF collection support (TPFCS) create cursor application programming interfaces (APIs).

env_ptr
The pointer to the environment as returned by the TO2_createEnv function.

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

For a normal return, this buffer contains a C header file followed by a copy of the data requested. 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 zero. When zero 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

Programming Considerations

Examples

The following example shows how the key value is retrieved from the item to which the cursor is currently pointing and returns it in the specified buffer.

#include <c$to2.h>               /* Needed for TO2 API Functions      */
#include <stdio.h>               /* APIs for standard I/O functions   */
#define  KEY_SIZE   16
 
TO2_PID             cursor;
TO2_ENV_PTR         env_ptr;
TO2_BUF_PTR         buffer_ptr;   /* Pointer to TO2 environment       */
u_char             *key=NULL;     /* The key of the element           */
TO2_ERR_CODE        err_code;     /* TO2 error code value             */
TO2_ERR_TEXT_PTR    err_text_ptr; /* TO2 error code text pointer      */
long                buffer_length;

  ·
  ·
  ·
/**********************************************************************/ /* Retrieve the key from the item pointed to by the cursor */ /* and return it in the supplied buffer. */ /**********************************************************************/ buffer_length = KEY_SIZE + sizeof(TO2_BUF_HDR); if ((buffer_ptr=malloc(buffer_length)) == NULL) { printf("malloc() failed!\n"); return(0); } if ((buffer_ptr=TO2_keyWithBuffer(&cursor, env_ptr, &buffer_length, buffer_ptr)) == NULL) { printf("TO2_keyWithBuffer failed!\n"); process_error(env_ptr); } else { if (buffer_length < (KEY_SIZE + sizeof(TO2_BUF_HDR))) { printf("Key returned did not fit in allocated buffer.\n"); } else { printf("TO2_keyWithBuffer successful!\n"); key = buffer_ptr->data; } }

Related Information

TO2_key-Return Current Key Value.