OpenClovis Logo

API Usage Examples

Code Examples.

Code Examples.

The Container library is used as a repository of data. The following scenarios can be used to perform various functions with a linked list Container:

//A simple key compare function for integer keys, that returns an integer.
//The signature of a key compare function should be as shown below.
int KeyCompare(ClCntKeyHandleT key1, ClCntKeyHandleT key2)
{
return (key1 - key2);
}
//A simple delete callback function which frees the memory I allocate
void myDeleteCallback(ClCntKeyHandleT key, ClCntDataHandleT userData)
{
void *ptr = (void *)userData;
free(ptr);
}
//A simple hash function for integer keys, that returns the hash value.
//The signature of a hash function should be as shown below.
int
HashFunction(ClCntKeyHandleT key)
{
return (key % NUMBER_OF_BUCKETS);
}
ClPtrT ClCntDataHandleT
Handle of the data.
Definition: clCntApi.h:97
ClPtrT ClCntKeyHandleT
Handle of the key handle.
Definition: clCntApi.h:105
ClCntHandleT Container Handle;
ClCntNodeHandleT nodeHandle;
void* key = XXXX; // user key can be of any type.
void* data = malloc(10); // this may be any user data type.
int retCode;
//The create API accepts the first argument as a pointer to the
//key compare function, the second argument is a pointer to the
//delete callback function, third is a pointer to the destroy
//callback function
if (clCntLlistCreate(KeyCompare, myDeleteCallback, myDeleteCallback,
CL_CNT_NON_UNIQUE_KEY, &containerHandle) != CL_OK){
//Container creation failed
//containerHandle will be equal to 0
}
retCode = clCntNodeAdd(containerHandle, key, data, NULL);
if(retCode != CL_OK){
//the API has failed to add the node
}
if( clCntNodeFind(containerHandle, key, &nodeHandle) != CL_OK)
{
//Node with specified key was not found;
//nodeHandle will be equal to 0
}
else {
//If a node with specified key is found, use following API to
//retrieve data from the node.
retCode = clCntNodeUserDataGet(containerHandle, nodeHandle, &data);
if (retCode != CL_OK){
//API has failed to retrieve the user data from the node.
}
//Use the below API to get the user Key associated with the node.
retCode = clCntNodeUserKeyGet(containerHandle, nodeHandle, &key);
if(retCode != CL_OK){
//API has failed to get the key from node.
}
//Use the below API to delete all the data associted with
//a key from the Container
retCode = cclKeyDelete(containerHandle, key);
if(retCode != CL_OK){
//API has failed to delete the node from Container .
}
}
ClRcT clCntNodeUserDataGet(CL_IN ClCntHandleT containerHandle, CL_IN ClCntNodeHandleT nodeHandle, CL_OUT ClCntDataHandleT *pUserDataHandle)
Returns the user-data from the node.
ClRcT clCntNodeUserKeyGet(CL_IN ClCntHandleT containerHandle, CL_IN ClCntNodeHandleT nodeHandle, CL_OUT ClCntKeyHandleT *pUserKey)
Returns the user-key from the node.
ClPtrT ClCntNodeHandleT
Handle of the container Node.
Definition: clCntApi.h:93
ClPtrT ClCntHandleT
Handle of the container.
Definition: clCntApi.h:88
ClRcT clCntNodeAdd(CL_IN ClCntHandleT containerHandle, CL_IN ClCntKeyHandleT userKey, CL_IN ClCntDataHandleT userData, CL_IN ClRuleExprT *rbeExpression)
Adds a new node to Container.
ClRcT clCntLlistCreate(CL_IN ClCntKeyCompareCallbackT fpKeyCompare, CL_IN ClCntDeleteCallbackT fpUserDeleteCallback, CL_IN ClCntDeleteCallbackT fpUserDestroyCallback, CL_IN ClCntKeyTypeT containerKeyType, CL_OUT ClCntHandleT *pContainerHandle)
Creates the container doubly linked list.
ClRcT clCntNodeFind(CL_IN ClCntHandleT containerHandle, CL_IN ClCntKeyHandleT userKey, CL_IN ClCntNodeHandleT *pNodeHandle)
Finds a specific node in the Container.
@ CL_CNT_NON_UNIQUE_KEY
Container contains non unique key.
Definition: clCntApi.h:119
#define CL_OK
Every thing is OK.
Definition: clCommonErrors.h:68

Use the following scenario to traverse through the Container .

ClCntHandleT containerHandle;
ClCntNodeHandleT nodeHandle;
unsigned int key = XXXX; //user key should be unsigned int.
void* data = YYYYYY; // this may be any user data type.
int retCode;
// Inorder to traverse to the 100th node in the Container
if(clCntFirstNodeGet(containerHandle, &nodeHandle) != CL_OK){
//API failed to get the first node
}
for(i = 0; i < 100; i++)
{
if(clCntNextNodeGet(containerHandle,nodeHandle,&nodeHanlde)
!= CL_OK){
//no next node exists
//take appropriate action
break;
}
}
// To get the 100th node's key and/or data
// need the following scenario.
if(nodeHandle !=0){
retCode = clCntNodeUserDataGet(containerHandle,
nodeHandle, &data);
if(retCode != CL_OK){
//API failed to get the data from node.
}
retCode = clCntNodeUserKeyGet(containerHandle,
nodeHandle, &key);
if(retCode != CL_OK){
//API failed to get the key from node.
}
}
// To taraverse in backward direction upto 100 elements.
clCntLastNodeGet(containerHandle, &nodeHandle);
for ( i = 0; i < 100; i++)
{
clCntPreviousNodeGet(containerHandle,nodeHandle,
&nodeHandle);
if(nodeHankle == 0){
break;
}
}
ClRcT clCntFirstNodeGet(CL_IN ClCntHandleT containerHandle, CL_OUT ClCntNodeHandleT *pNodeHandle)
Returns the first node from the Container.
ClRcT clCntNextNodeGet(CL_IN ClCntHandleT containerHandle, CL_IN ClCntNodeHandleT currentNodeHandle, CL_OUT ClCntNodeHandleT *pNextNodeHandle)
Returns the next node from the Container.
ClRcT clCntPreviousNodeGet(CL_IN ClCntHandleT containerHandle, CL_IN ClCntNodeHandleT currentNodeHandle, CL_OUT ClCntNodeHandleT *pPreviousNodeHandle)
Returns the previous node from the Container.
ClRcT clCntLastNodeGet(CL_IN ClCntHandleT containerHandle, CL_OUT ClCntNodeHandleT *pNodeHandle)
Returns the last node from the Container.

Generated on Tue Jan 10 10:29:15 PST 2012 for OpenClovis SDK using Doxygen