-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscriber_table.c
120 lines (91 loc) · 2.61 KB
/
subscriber_table.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Author: Shuji Ishii, Kazushi SUGYO
*
* Copyright (C) 2008-2011 NEC Corporation
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "trema.h"
#include "subscriber_table.h"
static list_element *subscriber_table;
static subscriber_entry *
allocate_subscriber_entry( const char *name ) {
subscriber_entry *new_entry;
new_entry = xmalloc( sizeof( subscriber_entry ) );
new_entry->name = xstrdup( name );
return new_entry;
}
static void
free_subscriber_entry( subscriber_entry *entry ) {
xfree( entry->name );
xfree( entry );
}
bool
insert_subscriber_entry( const char *name ) {
subscriber_entry *entry = lookup_subscriber_entry( name );
if ( entry != NULL ) {
debug( "subscriber '%s' already subscribed", name );
return false;
}
entry = allocate_subscriber_entry( name );
insert_in_front( &subscriber_table, entry );
return true;
}
void
delete_subscriber_entry( subscriber_entry *entry ) {
delete_element( &subscriber_table, entry );
free_subscriber_entry( entry );
}
subscriber_entry *
lookup_subscriber_entry( const char *name ) {
subscriber_entry *entry;
list_element *list;
for ( list = subscriber_table; list != NULL; list = list->next ) {
entry = list->data;
if ( strcmp( entry->name, name ) == 0 ) {
// found
return entry;
}
}
return NULL;
}
void
foreach_subscriber( void function( subscriber_entry *entry, void *user_data ),
void *user_data ) {
list_element *list, *next;
for ( list = subscriber_table; list != NULL; list = next ) {
next = list->next;
function( list->data, user_data );
}
}
void
init_subscriber_table( void ) {
create_list( &subscriber_table );
}
void
finalize_subscriber_table( void ) {
list_element *list, *next;
for ( list = subscriber_table; list != NULL; list = next ) {
next = list->next;
free_subscriber_entry( list->data );
}
delete_list( subscriber_table );
subscriber_table = NULL;
}
/*
* Local variables:
* c-basic-offset: 2
* indent-tabs-mode: nil
* End:
*/