-/* @(#) $Id: store_op.c,v 1.11 2009/06/24 18:53:09 dcid Exp $ */
+/* @(#) $Id: ./src/shared/store_op.c, 2011/09/08 dcid Exp $
+ */
/* Copyright (C) 2009 Trend Micro Inc.
* All right reserved.
*
* This program is a free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
- * License (version 3) as published by the FSF - Free Software
+ * License (version 2) as published by the FSF - Free Software
* Foundation
*/
/* Common API for dealing with ordered lists.
* Provides a fast search on average (n/2).
- */
+ */
#include "shared.h"
-/* Create the list storage
+/* Create the list storage
* Return NULL on error
*/
OSStore *OSStore_Create()
my_list = calloc(1, sizeof(OSStore));
if(!my_list)
return(NULL);
-
+
my_list->first_node = NULL;
my_list->last_node = NULL;
my_list->cur_node = NULL;
my_list->currently_size = 0;
my_list->max_size = 0;
my_list->free_data_function = NULL;
-
+
return(my_list);
}
-/* Deletes the list storage
+/* Deletes the list storage
* Return NULL on error
*/
OSStore *OSStore_Free(OSStore *list)
free(list);
list = NULL;
-
+
return(list);
}
{
return(0);
}
-
+
/* Minimum size is 1 */
if(max_size <= 1)
{
{
return(0);
}
-
+
list->free_data_function = free_data_function;
return(1);
}
/* In here, this node should stay where it is. */
else if(movenode == list->cur_node->prev)
{
- break;
+ break;
}
/* In here we need to replace the nodes. */
else
{
newnode = list->cur_node;
-
+
if(list->cur_node->prev)
list->cur_node->prev->next = list->cur_node->next;
-
+
if(list->cur_node->next)
list->cur_node->next->prev = list->cur_node->prev;
else
- list->last_node = list->cur_node->prev;
-
- list->cur_node = list->cur_node->prev;
+ list->last_node = list->cur_node->prev;
+
+ list->cur_node = list->cur_node->prev;
+
-
newnode->next = movenode->next;
newnode->prev = movenode;
if(movenode->next)
movenode->next->prev = newnode;
-
+
movenode->next = newnode;
-
+
break;
}
}
if(list->cur_node->prev)
list->cur_node->prev->next = list->cur_node->next;
-
+
if(list->cur_node->next)
list->cur_node->next->prev = list->cur_node->prev;
- else
- list->last_node = list->cur_node->prev;
-
+ else
+ list->last_node = list->cur_node->prev;
+
list->cur_node = list->cur_node->prev;
-
+
newnode->prev = NULL;
newnode->next = list->first_node;
list->first_node->prev = newnode;
-
- list->first_node = newnode;
+
+ list->first_node = newnode;
}
-
+
list->cur_node = list->cur_node->next;
}
{
int chk_rc;
list->cur_node = list->first_node;
-
+
while(list->cur_node)
{
if((chk_rc = strcmp(list->cur_node->key, key)) >= 0)
if(chk_rc == 0)
return(list->cur_node->data);
- /* Not found */
+ /* Not found */
return(NULL);
}
{
int chk_rc;
list->cur_node = list->first_node;
-
+
while(list->cur_node)
{
if((chk_rc = strcmp(list->cur_node->key, key)) >= 0)
if(chk_rc == 0)
return(1);
- /* Not found */
+ /* Not found */
return(0);
}
while(list->cur_node)
{
- if((chk_rc = strncmp(list->cur_node->key, key,
+ if((chk_rc = strncmp(list->cur_node->key, key,
list->cur_node->key_size)) >= 0)
{
/* Found */
int OSStore_Put(OSStore *list, char *key, void *data)
{
int chk_rc;
- OSStoreNode *newnode;
+ OSStoreNode *newnode;
/* Allocating memory for new node */
list->first_node = newnode;
list->last_node = newnode;
}
-
-
- /* Store the data in order */
+
+
+ /* Store the data in order */
else
{
list->cur_node = list->first_node;
{
return(1);
}
-
+
/* If there is no prev node, it is because
- * this is the first node.
+ * this is the first node.
*/
if(list->cur_node->prev)
list->cur_node->prev->next = newnode;
else
list->first_node = newnode;
-
-
+
+
newnode->prev = list->cur_node->prev;
-
+
list->cur_node->prev = newnode;
newnode->next = list->cur_node;
break;
list->last_node = newnode;
}
}
-
+
/* Increment list size */
list->currently_size++;
-
+
return(1);
}