4.3 Creating and Using a List Iterator: Example

  /* itrlist.c 
     Example of creating a list iterator of selected class and object
     names, and printing the object names. 
  */ 
   
  #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]; 
     pBuf_T            pb = NULL; 
     NWDSCCODE         ccode; 
     nuint32           flags; 
     nuint32           iter=0; 
     nint32            iterHandle = -1; 
   
  /* Check arguments */ 
     if(argc != 4) 
     { 
        printf("\nUsage:    itrlist  <base object>  <object class>  
                <name filter>"); 
        printf("\nExample:  itrlist  myou.myorg  user a*"); 
        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("itrlist:   Sample program for NDS 8 Iterator List\n"); 
     printf("Container to list:    %s\n",   argv[1]); 
     printf("Class:                %s\n",   argv[2]); 
     printf("Name filter:          %s\n",   argv[3]); 
   
     /* 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 ————- 
        A list iterator is ordered first by Base Class, 
        then by RDN within a base class. 
     */ 
   
     ccode = NWDSItrCreateList( 
                   context,              /* context for this search */ 
                   strAbbreviatedName,   /* container to search     */ 
                   argv[2],              /* class name filter       */ 
                   argv[3],              /* subordinate name filter */ 
                   DS_ITR_ANY_SERVER,    /* scalability requirement */ 
                   0,                    /* no timeout              */ 
                   &iter);               /* returned Iterator ptr   */ 
     if (ccode) 
        goto error; 
   
     /* 
        Read all entries in the result set and dump the results. 
        If the search result is empty, -765 (ERR_EOF_HIT) is returned. 
     */ 
     do              /* Loop until Iteration handle returns -1 */ 
     { 
        nuint      i; 
        nstr8      objName[MAX_DN_CHARS+1]; 
        nuint32    objCount, attrCount; 
        Object_Info_T  objectInfo; 
   
        ccode = NWDSItrGetNext(
                       iter,          /* Iterator object handle      */
                       0,             /* # of entries to read. 0=all */
                       0,             /* no timeout                  */
                       &iterHandle,   /* Iteration handle            */
                       pb);           /* Returned data               */
        if (ccode) goto error; 
   
        ccode = NWDSGetObjectCount (context,pb,&objCount); 
        if (ccode) goto error; 
        printf("\nNext buffer.  Object Count = %d\n", objCount); 
   
        for (i=0; i<objCount; i++) 
        { 
           ccode = NWDSGetObjectName (context, 
                                      pb, 
                                      objName, 
                                      &attrCount, 
                                      &objectInfo); 
           if (ccode) goto error; 
           printf("  %-20s     Class: %s\n",
                  objName,objectInfo.baseClass); 
   
        }  /* For each object */ 
     } while (iterHandle != -1); 
   
  /* Clean up, normal or error termination. */ 
  error: 
     printf("\n\nccode = %d.",ccode); 
     if (iter) 
        NWDSItrDestroy(iter);    /* Destroy the Iterator when done. */ 
     if (pb) 
        NWDSFreeBuf(pb); 
     NWDSFreeContext(context); 
  }