C H A P T E R 2

Working with Nodes

} // PrintNodeName

Finding a Node

The sample code in Listing 2-2(page 27) demonstrates how to find the node for a specific pathname. The sample code opens an Open Directory session and gets an Open Directory reference. Then it calls its own FindNodes routine and passes to it the pathname for the node that is to be found (/NetInfo/root).

The FindNodes routine calls dsBuildFromPath to build a data list for the pathname and calls dsDataBufferAllocate to allocate a data buffer in which to store the result of calling dsFindDirNodes. The routine then calls dsFindDirNodes to find the node whose name matches the specified pathname.

Then the FindNodes routine calls dsDataListAllocate to allocate a data list and provides that data list as a parameter when it calls dsGetDirNodeName. The dsGetDirNodeName function copies the node name from the data buffer filled in by dsFindDirNodes to the data list. Then the FindNodes routine calls its PrintNodeName routine to print the node name that was found. The PrintNodeName routine is described in the section Listing 2-1(page 25).

When the PrintNodeName routine returns, the FindNodes routine cleans up by calling dsDataListDeallocate and free() to deallocate the data list.

Listing 2-2Finding the node for a pathname

void main ( )

{

long dirStatus = eDSNoErr;

dirStatus = dsOpenDirService( &gDirRef ); if ( dirStatus == eDSNoErr )

{

FindNodes("/NetInfo/root");

}

if ( gDirRef != NULL )

{

dirStatus = dsCloseDirService( gDirRef );

}

}

void FindNodes ( char* inNodePath ){ bool done = false;

long dirStatus = eDSNoErr; unsigned long index = 0; unsigned long bufferCount = 0; tDataBufferPtr dataBuffer = NULL; tDataListPtr nodeName = NULL; tContextData context = NULL;

nodeName = dsBuildFromPath( gDirRef, inNodePath, "/"); if ( nodeName != NULL )

{

//Allocate a 32k buffer.

dataBuffer = dsDataBufferAllocate( gDirRef, 32 * 1024 ); if ( dataBuffer != NULL )

{

while ( (dirStatus == eDSNoErr) && (done == false) )

{

Finding a Node

27

2007-01-08 © 2007 Apple Inc. All Rights Reserved.

Page 27
Image 27
Apple OS X manual Finding a Node