added files
[bcm963xx.git] / userapps / opensource / net-snmp / snmplib / data_list.c
1 /*
2  * netsnmp_data_list.c
3  *
4  * $Id: data_list.c,v 5.1.2.1 2003/03/14 17:37:36 rstory Exp $
5  */
6 #include <net-snmp/net-snmp-config.h>
7 #include <sys/types.h>
8 #include <stdlib.h>
9
10 #if HAVE_STRING_H
11 #include <string.h>
12 #else
13 #include <strings.h>
14 #endif
15
16 #if HAVE_DMALLOC_H
17 #include <dmalloc.h>
18 #endif
19
20 #include <net-snmp/library/data_list.h>
21
22 /***********************************************************************/
23 /*
24  * New Handler based API 
25  */
26 /***********************************************************************/
27
28 NETSNMP_INLINE void
29 netsnmp_free_list_data(netsnmp_data_list *node)
30 {
31     Netsnmp_Free_List_Data *beer;
32     if (!node)
33         return;
34
35     beer = node->free_func;
36     if (beer)
37         (beer) (node->data);
38     SNMP_FREE(node->name);
39 }
40
41 NETSNMP_INLINE void
42 netsnmp_free_all_list_data(netsnmp_data_list *head)
43 {
44     netsnmp_data_list *tmpptr;
45     for (; head;) {
46         netsnmp_free_list_data(head);
47         tmpptr = head;
48         head = head->next;
49         SNMP_FREE(tmpptr);
50     }
51 }
52
53 NETSNMP_INLINE netsnmp_data_list *
54 netsnmp_create_data_list(const char *name, void *data,
55                          Netsnmp_Free_List_Data * beer)
56 {
57     netsnmp_data_list *node = SNMP_MALLOC_TYPEDEF(netsnmp_data_list);
58     if (!node)
59         return NULL;
60     node->name = strdup(name);
61     node->data = data;
62     node->free_func = beer;
63     return node;
64 }
65
66
67 NETSNMP_INLINE void
68 netsnmp_add_list_data(netsnmp_data_list **head, netsnmp_data_list *node)
69 {
70     netsnmp_data_list *ptr;
71     if (!*head) {
72         *head = node;
73         return;
74     }
75
76     /*
77      * xxx-rks: check for duplicate names? 
78      */
79     for (ptr = *head; ptr->next != NULL; ptr = ptr->next) {
80         /*
81          * noop 
82          */
83     }
84
85     if (ptr)                    /* should always be true */
86         ptr->next = node;
87 }
88
89 NETSNMP_INLINE void    *
90 netsnmp_get_list_data(netsnmp_data_list *head, const char *name)
91 {
92     for (; head; head = head->next)
93         if (head->name && strcmp(head->name, name) == 0)
94             break;
95     if (head)
96         return head->data;
97     return NULL;
98 }
99
100 NETSNMP_INLINE void    *
101 netsnmp_get_list_node(netsnmp_data_list *head, const char *name)
102 {
103     for (; head; head = head->next)
104         if (head->name && strcmp(head->name, name) == 0)
105             break;
106     if (head)
107         return head;
108     return NULL;
109 }
110
111 int
112 netsnmp_remove_list_node(netsnmp_data_list **realhead, const char *name)
113 {
114     netsnmp_data_list *head, *prev;
115     for (head = *realhead, prev = NULL; head;
116          prev = head, head = head->next) {
117         if (head->name && strcmp(head->name, name) == 0) {
118             if (prev)
119                 prev->next = head->next;
120             else
121                 *realhead = head->next;
122             netsnmp_free_list_data(head);
123             free(head);
124             return 0;
125         }
126     }
127     return 1;
128 }