gtpc2mfnC/C++ Language Support User's Guide

TO2_nextWithBuffer-Increment and Return the Next Element

This function returns the next element in the collection in the specified buffer and points the cursor at that element. The definition of the next element depends on the collection implementation.

Format

#include <c$to2.h>
TO2_BUF_PTR TO2_nextWithBuffer (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). If the length of the data is greater than 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. The caller must 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 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

TO2_ERROR_CURSOR

TO2_ERROR_EMPTY

TO2_ERROR_ENV

TO2_ERROR_EODAD

TO2_ERROR_METHOD

TO2_ERROR_PID

TO2_ERROR_ZERO_PID

Programming Considerations

Examples

The following example reads the next item that the cursor is currently pointing to 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  DATA_SIZE  256              /* Size of data                    */
 
struct person_record
{
  char             name[50]
  char             address[50]
};
typedef struct person_record PERSON_RECORD;
 
TO2_PID             cursor;
TO2_ENV_PTR         env_ptr;
TO2_BUF_PTR         buffer_ptr;
long                buffer_length;
PERSON_RECORD      *person_ptr;      /* Pointer to element data       */
TO2_ERR_CODE        err_code;        /* TO2 error code value          */
TO2_ERR_TEXT_PTR    err_text_ptr;    /* TO2 error code text pointer   */

  ·
  ·
  ·
/**********************************************************************/ /* Read the next item that the cursor points to. */ /**********************************************************************/ buffer_length = DATA_SIZE; if ((buffer_ptr=malloc(buffer_length)) == NULL) { printf("malloc() failed!\n"); return(0); } if ((buffer_ptr = TO2_nextWithBuffer(&cursor, env_ptr, &buffer_length, buffer_ptr)) == NULL) { err_code = TO2_getErrorCode(env_ptr); if (err_code != TO2_ERROR_EODAD) { printf("TO2_nextWithBuffer failed!\n"); process_error(env_ptr); } else printf("TO2_atCursor is at the end of the data!\n"); } else { person_ptr = buffer_ptr->data; if (buffer_length < (DATA_SIZE + sizeof(TO2_BUF_HDR))) printf("Data returned did not fit in allocated buffer!\n"); else printf("TO2_atCursorWithBuffer successful!\n"); }

Related Information