2007-07-09 04:37:16 +00:00
|
|
|
/*
|
2008-01-01 17:20:20 +00:00
|
|
|
* This file Copyright (C) 2007-2008 Charles Kerr <charles@rebelbase.com>
|
2007-07-09 04:37:16 +00:00
|
|
|
*
|
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
2007-08-18 17:19:49 +00:00
|
|
|
*
|
|
|
|
* $Id$
|
2007-07-09 04:37:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static tr_list*
|
2007-07-09 16:30:20 +00:00
|
|
|
node_alloc( void )
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
return tr_new0( tr_list, 1 );
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
|
2007-07-09 16:30:20 +00:00
|
|
|
static void
|
2007-09-20 16:32:01 +00:00
|
|
|
node_free( tr_list* node )
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
|
|
|
tr_free( node );
|
|
|
|
}
|
|
|
|
|
2007-07-09 16:30:20 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-07-09 04:37:16 +00:00
|
|
|
void
|
2007-09-28 14:27:56 +00:00
|
|
|
tr_list_free( tr_list** list, TrListForeachFunc data_free_func )
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2007-08-14 14:18:54 +00:00
|
|
|
while( *list )
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2007-09-28 14:27:56 +00:00
|
|
|
tr_list *node = *list;
|
2007-08-14 14:18:54 +00:00
|
|
|
*list = (*list)->next;
|
2008-08-01 16:43:22 +00:00
|
|
|
if( data_free_func )
|
2007-09-28 14:27:56 +00:00
|
|
|
data_free_func( node->data );
|
2007-07-09 16:30:20 +00:00
|
|
|
node_free( node );
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-14 14:18:54 +00:00
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_list_prepend( tr_list ** list, void * data )
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_list * node = node_alloc ();
|
2007-07-09 04:37:16 +00:00
|
|
|
node->data = data;
|
2007-08-14 14:18:54 +00:00
|
|
|
node->next = *list;
|
|
|
|
if( *list )
|
|
|
|
(*list)->prev = node;
|
|
|
|
*list = node;
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
|
2007-08-14 14:18:54 +00:00
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_list_append( tr_list ** list, void * data )
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_list * node = node_alloc( );
|
2007-07-09 16:30:20 +00:00
|
|
|
node->data = data;
|
2007-08-14 14:18:54 +00:00
|
|
|
if( !*list )
|
|
|
|
*list = node;
|
2007-07-09 16:30:20 +00:00
|
|
|
else {
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_list * l = *list;
|
2007-07-09 16:30:20 +00:00
|
|
|
while( l->next )
|
|
|
|
l = l->next;
|
|
|
|
l->next = node;
|
|
|
|
node->prev = l;
|
|
|
|
}
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
|
2007-08-14 14:18:54 +00:00
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_list_insert_sorted( tr_list ** list,
|
2007-08-14 14:18:54 +00:00
|
|
|
void * data,
|
|
|
|
int compare(const void*,const void*) )
|
|
|
|
{
|
|
|
|
/* find l, the node that we'll insert this data before */
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_list * l;
|
2007-08-14 14:18:54 +00:00
|
|
|
for( l=*list; l!=NULL; l=l->next ) {
|
|
|
|
const int c = (compare)( data, l->data );
|
|
|
|
if( c <= 0 )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( l == NULL)
|
|
|
|
tr_list_append( list, data );
|
|
|
|
else if( l == *list )
|
|
|
|
tr_list_prepend( list, data );
|
|
|
|
else {
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_list * node = node_alloc( );
|
2007-08-14 14:18:54 +00:00
|
|
|
node->data = data;
|
|
|
|
if( l->prev ) { node->prev = l->prev; node->prev->next = node; }
|
|
|
|
node->next = l;
|
|
|
|
l->prev = node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_list*
|
|
|
|
tr_list_find_data ( tr_list * list, const void * data )
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
|
|
|
for(; list; list=list->next )
|
|
|
|
if( list->data == data )
|
|
|
|
return list;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static void*
|
|
|
|
tr_list_remove_node ( tr_list ** list, tr_list * node )
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
void * data;
|
|
|
|
tr_list * prev = node ? node->prev : NULL;
|
|
|
|
tr_list * next = node ? node->next : NULL;
|
2007-07-09 04:37:16 +00:00
|
|
|
if( prev ) prev->next = next;
|
|
|
|
if( next ) next->prev = prev;
|
2007-08-14 14:18:54 +00:00
|
|
|
if( *list == node ) *list = next;
|
2007-09-20 16:32:01 +00:00
|
|
|
data = node ? node->data : NULL;
|
2007-07-09 16:30:20 +00:00
|
|
|
node_free( node );
|
2007-09-20 16:32:01 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
tr_list_pop_front( tr_list ** list )
|
|
|
|
{
|
|
|
|
void * ret = NULL;
|
2008-08-01 16:43:22 +00:00
|
|
|
if( *list )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
ret = (*list)->data;
|
|
|
|
tr_list_remove_node( list, *list );
|
|
|
|
}
|
|
|
|
return ret;
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void*
|
|
|
|
tr_list_remove_data ( tr_list ** list, const void * data )
|
|
|
|
{
|
|
|
|
return tr_list_remove_node( list, tr_list_find_data( *list, data ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
tr_list_remove( tr_list ** list,
|
|
|
|
const void * b,
|
|
|
|
TrListCompareFunc compare_func )
|
|
|
|
{
|
|
|
|
return tr_list_remove_node( list, tr_list_find( *list, b, compare_func ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tr_list*
|
|
|
|
tr_list_find ( tr_list * list , const void * b, TrListCompareFunc func )
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
|
|
|
for( ; list; list=list->next )
|
|
|
|
if( !func( list->data, b ) )
|
|
|
|
return list;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_list_foreach( tr_list * list, TrListForeachFunc func )
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2008-08-01 16:43:22 +00:00
|
|
|
while( list ) {
|
2007-07-09 04:37:16 +00:00
|
|
|
func( list->data );
|
|
|
|
list = list->next;
|
|
|
|
}
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
tr_list_size( const tr_list * list )
|
|
|
|
{
|
|
|
|
int size = 0;
|
2008-08-01 16:43:22 +00:00
|
|
|
while( list ) {
|
2007-09-20 16:32:01 +00:00
|
|
|
++size;
|
|
|
|
list = list->next;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|