Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

Commit 766475db authored by rswindell's avatar rswindell

Added functions for merging and extracting lists of nodes.

parent 9f47569a
......@@ -249,19 +249,11 @@ void* listNodeData(const list_node_t* node)
return(node->data);
}
list_node_t* listAddNode(link_list_t* list, void* data, list_node_t* after)
static list_node_t* list_add_node(link_list_t* list, list_node_t* node, list_node_t* after)
{
list_node_t* node;
if(list==NULL)
return(NULL);
if((node=(list_node_t*)malloc(sizeof(list_node_t)))==NULL)
return(NULL);
memset(node,0,sizeof(list_node_t));
node->data = data;
node->prev = after;
if(after==list->last) /* append to list */
......@@ -284,6 +276,34 @@ list_node_t* listAddNode(link_list_t* list, void* data, list_node_t* after)
return(node);
}
list_node_t* listAddNode(link_list_t* list, void* data, list_node_t* after)
{
list_node_t* node;
if(list==NULL)
return(NULL);
if((node=(list_node_t*)malloc(sizeof(list_node_t)))==NULL)
return(NULL);
return(list_add_node(list,node,after));
}
list_node_t* listAddNodes(link_list_t* list, void** data, list_node_t* after)
{
size_t i;
list_node_t* node=NULL;
if(data==NULL)
return(NULL);
for(i=0;data[i];i++)
if((node=listAddNode(list,data[i],node==NULL ? after:node))==NULL)
return(NULL);
return(node);
}
list_node_t* listAddNodeData(link_list_t* list, const void* data, size_t length, list_node_t* after)
{
list_node_t* node;
......@@ -326,20 +346,72 @@ list_node_t* listAddNodeString(link_list_t* list, const char* str, list_node_t*
return(node);
}
list_node_t* listAddStringList(link_list_t* list, str_list_t str_list, list_node_t* node)
list_node_t* listAddStringList(link_list_t* list, str_list_t str_list, list_node_t* after)
{
size_t i;
list_node_t* node=NULL;
if(str_list==NULL)
return(NULL);
for(i=0;str_list[i];i++)
if((node=listAddNodeString(list,str_list[i],node))==NULL)
if((node=listAddNodeString(list,str_list[i],node==NULL ? after:node))==NULL)
return(NULL);
return(node);
}
list_node_t* listAddNodeList(link_list_t* list, const link_list_t* src, list_node_t* after)
{
list_node_t* node=NULL;
list_node_t* src_node;
if(src==NULL)
return(NULL);
for(src_node=src->first; src_node!=NULL; src_node=src_node->next) {
if((node=listAddNode(list, src_node->data, node==NULL ? after:node))==NULL)
return(NULL);
node->flags = src_node->flags;
}
return(node);
}
list_node_t* listMerge(link_list_t* list, const link_list_t* src, list_node_t* after)
{
list_node_t* node=NULL;
list_node_t* src_node;
if(src==NULL)
return(NULL);
for(src_node=src->first; src_node!=NULL; src_node=src_node->next)
if((node=list_add_node(list, src_node, node==NULL ? after:node))==NULL)
return(NULL);
return(node);
}
link_list_t* listExtract(link_list_t* dest_list, const list_node_t* node, long max)
{
long count=0;
link_list_t* list;
if(node==NULL)
return(NULL);
if((list=listInit(dest_list))==NULL)
return(NULL);
for(count=0; count<max && node!=NULL; node=node->next) {
listAddNode(list, node->data, list->last);
count++;
}
return(list);
}
void* listRemoveNode(link_list_t* list, list_node_t* node)
{
void* data;
......
......@@ -79,6 +79,10 @@ str_list_t listStringList(const link_list_t*);
/* Return an allocated string list (which must be freed), subset of strings in linked list */
str_list_t listSubStringList(const list_node_t*, long max);
/* Extract subset (up to max number of nodes) in linked list (src_node) and place into dest_list */
/* dest_list == NULL, then allocate a return a new linked list */
link_list_t* listExtract(link_list_t* dest_list, const list_node_t* src_node, long max);
/* Simple search functions returning found node or NULL on error */
list_node_t* listNodeAt(const link_list_t*, long index);
list_node_t* listFindNode(const link_list_t*, void* data, size_t length);
......@@ -93,6 +97,9 @@ void* listNodeData(const list_node_t*);
/* Add node to list, returns pointer to new node or NULL on error */
list_node_t* listAddNode(link_list_t*, void* data, list_node_t* after /* NULL=insert */);
/* Add array of node data to list, returns pointer to last new node or NULL on error */
list_node_t* listAddNodes(link_list_t*, void** data, list_node_t* after /* NULL=insert */);
/* Add node to list, allocating and copying the data for the node */
list_node_t* listAddNodeData(link_list_t*, const void* data, size_t length, list_node_t* after);
......@@ -102,6 +109,13 @@ list_node_t* listAddNodeString(link_list_t*, const char* str, list_node_t* after
/* Add a list of strings to the linked list, allocating and copying each */
list_node_t* listAddStringList(link_list_t*, str_list_t, list_node_t* after);
/* Add a list of nodes from a source linked list */
list_node_t* listAddNodeList(link_list_t*, const link_list_t* src, list_node_t* after);
/* Merge a source linked list into the destination linked list */
/* after merging, the nodes in the source linked list should not be modified or freed */
list_node_t* listMerge(link_list_t* dest, const link_list_t* src, list_node_t* after);
/* Convenience macros for pushing, popping, and inserting nodes */
#define listPushNode(list, data) listAddNode(list, data, listLastNode(list))
#define listInsertNode(link, data) listAddNode(list, data, NULL)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment