4.1 Cloning an Iterator Object: Example

  /* itrclone.c 
     Example of NDS 8 Iterator clone function. 
  */ 
   
  #include <stdio.h> 
  #include <stdlib.h> 
  #include <nwnet.h> 
  #include <nwcalls.h> 
  #include <assert.h> 
   
  void main(int argc, char *argv[]) 
  { 
     NWDSContextHandle context =
           (NWDSContextHandle)ERR_CONTEXT_CREATION; 
     nstr8         strAbbreviatedName[MAX_DN_CHARS+1]; 
     pBuf_T        pb = NULL; 
     NWDSCCODE     ccode; 
     nuint32       flags; 
     nuint32       origIter=0; 
     nuint32       cloneIter=0; 
     nuint32        timeout = 0;     /* Timeout=0 means no time limit. */ 
   
  /* Check arguments */ 
     if(argc < 2) 
     { 
        printf("\nUsage:    itrclone  <base object>\n"); 
        printf("\nExample:  itrclone  myou.myorg\n"); 
        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("itrclone:   Sample program for Iterator informational
             functions.\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; 
   
     /* Allocate the output buffer.  */ 
     ccode = NWDSAllocBuf(MAX_MESSAGE_LEN, &pb); 
     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          */ 
                   &origIter);           /* returned Iterator ptr   */ 
     if (ccode) goto error; 
   
     /* Create a clone of the original iterator */ 
     ccode = NWDSItrClone(origIter, &cloneIter); 
     if (ccode) goto error; 
   
     /* Position the clone to EOF */ 
     ccode = NWDSItrSetPosition(cloneIter, DS_ITR_EOF, timeout); 
     if (ccode) goto error; 
   
     /* The original iter should still be at the first position. */ 
     /* The clone iterator should be at EOF. */ 
     assert(NWDSItrAtFirst(origIter) == N_TRUE); 
     assert(NWDSItrAtEOF(cloneIter) == N_TRUE); 
   
     /* Position the clone iterator to the same position as the 
        original. */ 
     ccode = NWDSItrSetPositionFromIterator(cloneIter, origIter,
             timeout); 
     if (ccode) goto error; 
     assert(NWDSItrAtFirst(cloneIter) == N_TRUE); 
   
  /* Clean up, normal or error termination. */ 
  error: 
     printf("\n\nccode = %d.",ccode); 
     if (origIter) 
        NWDSItrDestroy(origIter);   /* Destroy the Iterator when done. */ 
     if (cloneIter) 
        NWDSItrDestroy(cloneIter);  /* Destroy the clone also. */ 
     if (pb) 
        NWDSFreeBuf(pb); 
     NWDSFreeContext(context); 
  }