4.6 Getting and Setting the Iterator's Position: Example
/* itrpos.c
Example of NDS 8 Iterator GetPosition and SetPosition calls.
*/
#include <stdio.h>
#include <stdlib.h>
#include <nwnet.h>
#include <nwcalls.h>
void main(int argc, char *argv[])
{
NWDSContextHandle context =
(NWDSContextHandle)ERR_CONTEXT_CREATION;
nstr8 strAbbreviatedName[MAX_DN_CHARS+1];
NWDSCCODE ccode;
nuint32 flags;
nuint32 iter=0;
nint32 iterHandle = -1;
nuint32 timeout = 0; /* Timeout=0 means no time limit. */
nuint32 pos;
/* Check arguments */
if(argc != 2)
{
printf("\nUsage: itrpos <base object>");
printf("\nExample: itrpos myou.myorg");
exit(1);
}
/* Initialize NWCalls and unicode tables */
ccode = NWCallsInit(NULL,NULL);
if(ccode) exit(1);
/* Create NDS Context */
ccode = NWDSCreateContextHandle(&context);
if(ccode) exit(1);
/* Set flags to get object names in typeless format. */
NWDSGetContext(context, DCK_FLAGS, &flags);
flags |= DCV_TYPELESS_NAMES;
NWDSSetContext(context, DCK_FLAGS, &flags);
printf("itrpos: Sample program for NDS 8 NWDSItrGet/
SetPosition\n");
printf("Container: %s\n", argv[1]);
/* Convert the directory name (passed in as first arg) to its
shortest form relative to the name context */
ccode = NWDSAbbreviateName(context, argv[1], strAbbreviatedName);
if(ccode) goto error;
/* ———— Create the Iterator ————- */
ccode = NWDSItrCreateList(
context, /* context for this search */
strAbbreviatedName, /* container to search */
NULL, /* class name filter */
NULL, /* subordinate name filter */
DS_ITR_ANY_SERVER, /* scalability requirement */
timeout, /* 0 = no timeout */
&iter); /* returned Iterator ptr */
if (ccode)
goto error;
/* Do several Get and SetPosition calls. Iterator positions are
always "logical positions" in the range 0-1001. Special
symbols defined:
DS_ITR_FIRST = 0
DS_ITR_LAST = 1000
DS_ITR_EOF = 1001
*/
/* Initial position should be 0. If list is empty,
position will be 1001 (EOF). */
printf("\nInitial position is: ");
ccode = NWDSItrGetPosition(iter, &pos, timeout);
if (ccode == ERR_NOT_IMPLEMENTED)
printf(" *** Iterator not positionable.\n");
else if (ccode)
goto error;
else
printf("%d\n",pos);
printf("Setting position 500.\n");
ccode = NWDSItrSetPosition(iter, 500, timeout);
if (ccode == ERR_NOT_IMPLEMENTED)
printf(" *** Iterator not positionable.\n");
else if (ccode)
goto error;
printf("Current position is now: ");
ccode = NWDSItrGetPosition(iter, &pos, timeout);
if (ccode == ERR_NOT_IMPLEMENTED)
printf(" *** Iterator not positionable.\n");
else if (ccode)
goto error;
else
printf("%d\n",pos);
/* You should always be able to set positions 0, 1000, and 1001,
even if the iterator is not positionable. */
printf("Setting position 1000.\n");
ccode = NWDSItrSetPosition(iter, DS_ITR_LAST, timeout);
if (ccode) goto error;
printf("Current position is now: ");
ccode = NWDSItrGetPosition(iter, &pos, timeout);
if (ccode == ERR_NOT_IMPLEMENTED)
printf(" *** Iterator not positionable.\n");
else if (ccode)
goto error;
else
printf("%d\n",pos);
/* Clean up, normal or error termination. */
error:
printf("\n\nccode = %d.",ccode);
if (iter)
NWDSItrDestroy(iter); /* Destroy the Iterator when done. */
NWDSFreeContext(context);
}