OpenClovis Logo

API Usage Examples
Container Service

Code Examples. More...

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);
}
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 .
}
}

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;
}
}

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