source: trunk/libtransmission/list.c @ 7467

Last change on this file since 7467 was 7467, checked in by charles, 13 years ago

(trunk libT) remove unused functions tr_list_insert_sorted(), tr_list_foreach(). make tr_list_find_data() private.

  • Property svn:keywords set to Date Rev Author Id
File size: 3.0 KB
Line 
1/*
2 * This file Copyright (C) 2007-2008 Charles Kerr <charles@transmissionbt.com>
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
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.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: list.c 7467 2008-12-22 17:35:10Z charles $
11 */
12
13#include "transmission.h"
14#include "list.h"
15#include "utils.h"
16
17static tr_list*
18node_alloc( void )
19{
20    return tr_new0( tr_list, 1 );
21}
22
23static void
24node_free( tr_list* node )
25{
26    tr_free( node );
27}
28
29/***
30****
31***/
32
33void
34tr_list_free( tr_list**         list,
35              TrListForeachFunc data_free_func )
36{
37    while( *list )
38    {
39        tr_list *node = *list;
40        *list = ( *list )->next;
41        if( data_free_func )
42            data_free_func( node->data );
43        node_free( node );
44    }
45}
46
47void
48tr_list_prepend( tr_list ** list,
49                 void *     data )
50{
51    tr_list * node = node_alloc ( );
52
53    node->data = data;
54    node->next = *list;
55    if( *list )
56        ( *list )->prev = node;
57    *list = node;
58}
59
60void
61tr_list_append( tr_list ** list,
62                void *     data )
63{
64    tr_list * node = node_alloc( );
65
66    node->data = data;
67    if( !*list )
68        *list = node;
69    else
70    {
71        tr_list * l = *list;
72        while( l->next )
73            l = l->next;
74
75        l->next = node;
76        node->prev = l;
77    }
78}
79
80static tr_list*
81tr_list_find_data( tr_list *    list,
82                   const void * data )
83{
84    for( ; list; list = list->next )
85        if( list->data == data )
86            return list;
87
88    return NULL;
89}
90
91static void*
92tr_list_remove_node( tr_list ** list,
93                     tr_list *  node )
94{
95    void *    data;
96    tr_list * prev = node ? node->prev : NULL;
97    tr_list * next = node ? node->next : NULL;
98
99    if( prev ) prev->next = next;
100    if( next ) next->prev = prev;
101    if( *list == node ) *list = next;
102    data = node ? node->data : NULL;
103    node_free( node );
104    return data;
105}
106
107void*
108tr_list_pop_front( tr_list ** list )
109{
110    void * ret = NULL;
111
112    if( *list )
113    {
114        ret = ( *list )->data;
115        tr_list_remove_node( list, *list );
116    }
117    return ret;
118}
119
120void*
121tr_list_remove_data( tr_list **   list,
122                     const void * data )
123{
124    return tr_list_remove_node( list, tr_list_find_data( *list, data ) );
125}
126
127void*
128tr_list_remove( tr_list **        list,
129                const void *      b,
130                TrListCompareFunc compare_func )
131{
132    return tr_list_remove_node( list, tr_list_find( *list, b, compare_func ) );
133}
134
135tr_list*
136tr_list_find( tr_list *         list,
137              const void *      b,
138              TrListCompareFunc func )
139{
140    for( ; list; list = list->next )
141        if( !func( list->data, b ) )
142            return list;
143
144    return NULL;
145}
146
147int
148tr_list_size( const tr_list * list )
149{
150    int size = 0;
151
152    while( list )
153    {
154        ++size;
155        list = list->next;
156    }
157
158    return size;
159}
160
Note: See TracBrowser for help on using the repository browser.