4
4
* This file is licensed by the GPL version 2. Works owned by the
5
5
* Transmission project are granted a special exemption to clause 2(b)
6
* so that the bulk of its code can remain under the MIT license.
6
* so that the bulk of its code can remain under the MIT license.
7
7
* This exemption does not extend to derived works not owned by
8
8
* the Transmission project.
10
* $Id: list.c 6425 2008-08-01 16:43:22Z charles $
10
* $Id: list.c 6892 2008-10-13 22:26:02Z charles $
13
13
#include "transmission.h"
34
tr_list_free( tr_list** list, TrListForeachFunc data_free_func )
34
tr_list_free( tr_list** list,
35
TrListForeachFunc data_free_func )
38
39
tr_list *node = *list;
39
*list = (*list)->next;
40
*list = ( *list )->next;
40
41
if( data_free_func )
41
42
data_free_func( node->data );
47
tr_list_prepend( tr_list ** list, void * data )
48
tr_list_prepend( tr_list ** list,
49
tr_list * node = node_alloc ();
51
tr_list * node = node_alloc ( );
51
54
node->next = *list;
56
( *list )->prev = node;
58
tr_list_append( tr_list ** list, void * data )
61
tr_list_append( tr_list ** list,
60
64
tr_list * node = node_alloc( );
65
71
tr_list * l = *list;
74
tr_list_insert_sorted( tr_list ** list,
76
int compare(const void*,const void*) )
81
tr_list_insert_sorted( tr_list ** list,
83
TrListCompareFunc compare )
78
85
/* find l, the node that we'll insert this data before */
80
for( l=*list; l!=NULL; l=l->next ) {
88
for( l = *list; l != NULL; l = l->next )
81
90
const int c = (compare)( data, l->data );
87
96
tr_list_append( list, data );
88
97
else if( l == *list )
89
98
tr_list_prepend( list, data );
91
101
tr_list * node = node_alloc( );
92
102
node->data = data;
93
if( l->prev ) { node->prev = l->prev; node->prev->next = node; }
103
if( l->prev ){ node->prev = l->prev; node->prev->next = node; }
101
tr_list_find_data ( tr_list * list, const void * data )
110
tr_list_find_data( tr_list * list,
103
for(; list; list=list->next )
113
for( ; list; list = list->next )
104
114
if( list->data == data )
111
tr_list_remove_node ( tr_list ** list, tr_list * node )
121
tr_list_remove_node( tr_list ** list,
114
125
tr_list * prev = node ? node->prev : NULL;
115
126
tr_list * next = node ? node->next : NULL;
116
128
if( prev ) prev->next = next;
117
129
if( next ) next->prev = prev;
118
130
if( *list == node ) *list = next;
125
137
tr_list_pop_front( tr_list ** list )
127
139
void * ret = NULL;
143
ret = ( *list )->data;
131
144
tr_list_remove_node( list, *list );
137
tr_list_remove_data ( tr_list ** list, const void * data )
150
tr_list_remove_data( tr_list ** list,
139
153
return tr_list_remove_node( list, tr_list_find_data( *list, data ) );
143
tr_list_remove( tr_list ** list,
145
TrListCompareFunc compare_func )
157
tr_list_remove( tr_list ** list,
159
TrListCompareFunc compare_func )
147
161
return tr_list_remove_node( list, tr_list_find( *list, b, compare_func ) );
152
tr_list_find ( tr_list * list , const void * b, TrListCompareFunc func )
165
tr_list_find( tr_list * list,
167
TrListCompareFunc func )
154
for( ; list; list=list->next )
169
for( ; list; list = list->next )
155
170
if( !func( list->data, b ) )