4.4 Creating a Search Iterator and Displaying the Results: Example
/* itrsrch.c
Example of creating a search iterator and displaying all text
attributes.
*/
#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 pFilter = NULL;
pBuf_T pb = NULL;
pFilter_Cursor_T pCur = NULL;
NWDSCCODE ccode;
nuint32 flags;
nuint32 iter=0;
nint32 iterHandle = -1;
/* Check arguments */
if(argc != 3)
{
printf("\nUsage: itrsrch <base object> <object class>");
printf("\nExample: itrsrch myou.myorg user");
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("itrsrch: Sample program for NDS 8 Iterator Search\n");
printf("Container to search: %s\n", argv[1]);
printf("Class: %s\n", argv[2]);
/* 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;
/* Allocate space for the search filter cursor. */
ccode = NWDSAllocFilter(&pCur);
if(ccode) goto error;
/* Allocate the buffer to hold the search filter. */
ccode = NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &pFilter);
if(ccode) goto error;
/* Initialize filter for a DSV_SEARCH_FILTER operation */
ccode = NWDSInitBuf(context, DSV_SEARCH_FILTER, pFilter);
if(ccode) goto error;
/* Filter expression "FTOK_BASECLS OBJCLASS". */
ccode = NWDSAddFilterToken(pCur, FTOK_BASECLS, NULL,0);
if(ccode) goto error;
ccode = NWDSAddFilterToken(pCur, FTOK_ANAME, argv[2],
SYN_CLASS_NAME);
if(ccode) goto error;
ccode = NWDSAddFilterToken(pCur, FTOK_END, NULL, 0);
if(ccode) goto error;
/* Place finished search filter into the search input buffer */
ccode = NWDSPutFilter(context, pFilter, pCur, NULL);
pCur = NULL; /* NWDSPutFilter frees the expression tree memory. */
if(ccode) goto error;
/* ———— Create the Iterator ————-
Since indexSelect is NULL, the server chooses the optimum
index for this query. The index chosen will determine the
sort order of the result set. You may also explicitly specify
which index to use.
*/
ccode = NWDSItrCreateSearch(
context, /* context for this search */
strAbbreviatedName, /* container to search */
DS_SEARCH_SUBORDINATES, /* scope */
N_FALSE, /* deref alias false */
pFilter, /* search filter */
NULL, /* Time Stamp filter */
DS_ATTRIBUTE_VALUES, /* info type to return */
N_TRUE, /* return all attributes */
NULL, /* attribute list to ret */
NULL, /* index select */
NULL, /* emulation mode sort key */
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,j,k;
nstr8 objName[MAX_DN_CHARS+1];
nstr8 attrName[MAX_DN_CHARS+1];
nuint8 attrVal[MAX_MESSAGE_LEN];
nuint32 objCount, attrCount, attrValCount, syntaxID;
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("\n %s Class: %s\n", objName,objectInfo.baseClass);
for (j=0; j<attrCount; j++) /* If we returned attrs,
print them. */
{
ccode = NWDSGetAttrName (context,
pb,
attrName,
&attrValCount,
&syntaxID);
printf(" Attr: %s syntax=%d ValCount=%d\n",
attrName,syntaxID,attrValCount);
if (ccode) goto error;
for (k=0; k<attrValCount; k++)
{
ccode = NWDSGetAttrVal(context,
pb,
syntaxID,
attrVal);
if (ccode) goto error;
/* If this is a text attribute, print it. */
if ((syntaxID >= 1 && syntaxID <= 5) || syntaxID == 20)
printf(" Value: %s\n",attrVal);
} /* For each value */
} /* For each attr */
} /* 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);
if (pCur)
NWDSFreeFilter(pCur,NULL);
if (pFilter)
NWDSFreeBuf(pFilter);
NWDSFreeContext(context);
}