2007-07-09 04:37:16 +00:00
|
|
|
/*
|
2010-01-04 21:00:47 +00:00
|
|
|
* This file Copyright (C) 2007-2010 Mnemosyne LLC
|
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)
|
2008-09-23 19:11:04 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2007-07-09 04:37:16 +00:00
|
|
|
* 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
|
|
|
{
|
2009-01-24 02:33:25 +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
|
|
|
{
|
2009-01-24 02:33:25 +00:00
|
|
|
tr_free( node );
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
|
2007-07-09 16:30:20 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-07-09 04:37:16 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +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;
|
2008-09-23 19:11:04 +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
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_list_prepend( tr_list ** list,
|
|
|
|
void * data )
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +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 )
|
2008-09-23 19:11:04 +00:00
|
|
|
( *list )->prev = node;
|
2007-08-14 14:18:54 +00:00
|
|
|
*list = node;
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
|
2007-08-14 14:18:54 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +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( );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-07-09 16:30:20 +00:00
|
|
|
node->data = data;
|
2007-08-14 14:18:54 +00:00
|
|
|
if( !*list )
|
|
|
|
*list = node;
|
2008-09-23 19:11:04 +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;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-07-09 16:30:20 +00:00
|
|
|
l->next = node;
|
|
|
|
node->prev = l;
|
|
|
|
}
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
|
2008-12-22 17:35:10 +00:00
|
|
|
static tr_list*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_list_find_data( tr_list * list,
|
|
|
|
const void * data )
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
for( ; list; list = list->next )
|
2007-07-09 04:37:16 +00:00
|
|
|
if( list->data == data )
|
|
|
|
return list;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static void*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_list_remove_node( tr_list ** list,
|
|
|
|
tr_list * node )
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
void * data;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_list * prev = node ? node->prev : NULL;
|
|
|
|
tr_list * next = node ? node->next : NULL;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
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-09-23 19:11:04 +00:00
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
if( *list )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
ret = ( *list )->data;
|
2007-09-20 16:32:01 +00:00
|
|
|
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*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_list_remove_data( tr_list ** list,
|
|
|
|
const void * data )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
return tr_list_remove_node( list, tr_list_find_data( *list, data ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_list_remove( tr_list ** list,
|
|
|
|
const void * b,
|
|
|
|
TrListCompareFunc compare_func )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
return tr_list_remove_node( list, tr_list_find( *list, b, compare_func ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_list*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_list_find( tr_list * list,
|
|
|
|
const void * b,
|
|
|
|
TrListCompareFunc func )
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
for( ; list; list = list->next )
|
2007-07-09 04:37:16 +00:00
|
|
|
if( !func( list->data, b ) )
|
|
|
|
return list;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
int
|
|
|
|
tr_list_size( const tr_list * list )
|
|
|
|
{
|
|
|
|
int size = 0;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
while( list )
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
++size;
|
|
|
|
list = list->next;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
return size;
|
|
|
|
}
|