~ubuntu-branches/ubuntu/trusty/net-snmp/trusty

« back to all changes in this revision

Viewing changes to snmplib/container.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2007-05-10 22:20:23 UTC
  • mto: (1.4.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: james.westby@ubuntu.com-20070510222023-3fr07xb9i17xvq32
Tags: upstream-5.3.1
ImportĀ upstreamĀ versionĀ 5.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
typedef struct container_type_s {
13
13
   const char                 *name;
14
14
   netsnmp_factory            *factory;
 
15
   netsnmp_container_compare  *compare;
15
16
} container_type;
16
17
 
17
18
netsnmp_factory *
65
66
                               netsnmp_container_get_factory("sorted_singly_linked_list"));
66
67
    netsnmp_container_register("ssll_container",
67
68
                               netsnmp_container_get_factory("sorted_singly_linked_list"));
 
69
 
 
70
    netsnmp_container_register_with_compare
 
71
        ("string", netsnmp_container_get_factory("binary_array"),
 
72
         netsnmp_compare_cstring);
 
73
    netsnmp_container_register_with_compare
 
74
        ("string:binary_array", netsnmp_container_get_factory("binary_array"),
 
75
         netsnmp_compare_cstring);
 
76
 
68
77
}
69
78
 
70
79
void
77
86
    /*
78
87
     * free memory used by each factory entry
79
88
     */
80
 
    CONTAINER_FOR_EACH(containers, _factory_free, NULL);
 
89
    CONTAINER_FOR_EACH(containers, ((netsnmp_container_obj_func *)_factory_free), NULL);
81
90
 
82
91
    /*
83
92
     * free factory container
87
96
}
88
97
 
89
98
int
90
 
netsnmp_container_register(const char* name, netsnmp_factory *f)
 
99
netsnmp_container_register_with_compare(const char* name, netsnmp_factory *f,
 
100
                                        netsnmp_container_compare  *c)
91
101
{
92
102
    container_type *ct, tmp;
93
103
 
94
 
    tmp.name = name;
 
104
    tmp.name = (char *)name;
95
105
    ct = CONTAINER_FIND(containers, &tmp);
96
106
    if (NULL!=ct) {
97
107
        DEBUGMSGT(("container_registry",
104
114
            return -1;
105
115
        ct->name = strdup(name);
106
116
        ct->factory = f;
 
117
        ct->compare = c;
107
118
        CONTAINER_INSERT(containers, ct);
108
119
    }
109
120
    DEBUGMSGT(("container_registry", "registered container factory %s (%s)\n",
112
123
    return 0;
113
124
}
114
125
 
 
126
int
 
127
netsnmp_container_register(const char* name, netsnmp_factory *f)
 
128
{
 
129
    return netsnmp_container_register_with_compare(name, f, NULL);
 
130
}
 
131
 
115
132
/*------------------------------------------------------------------
116
133
 */
117
134
netsnmp_factory *
150
167
 
151
168
/*------------------------------------------------------------------
152
169
 */
 
170
static container_type *
 
171
netsnmp_container_get_ct(const char *type)
 
172
{
 
173
    container_type ct;
 
174
    
 
175
    ct.name = type;
 
176
    return CONTAINER_FIND(containers, &ct);
 
177
}
 
178
 
 
179
static container_type *
 
180
netsnmp_container_find_ct(const char *type_list)
 
181
{
 
182
    container_type    *ct = NULL;
 
183
    char              *list, *entry;
 
184
    char              *st;
 
185
 
 
186
    if (NULL==type_list)
 
187
        return NULL;
 
188
 
 
189
    list = strdup(type_list);
 
190
    entry = strtok_r(list, ":", &st);
 
191
    while(entry) {
 
192
        ct = netsnmp_container_get_ct(entry);
 
193
        if (NULL != ct)
 
194
            break;
 
195
        entry = strtok_r(NULL, ":", &st);
 
196
    }
 
197
 
 
198
    free(list);
 
199
    return ct;
 
200
}
 
201
 
 
202
 
 
203
 
 
204
/*------------------------------------------------------------------
 
205
 */
153
206
netsnmp_container *
154
207
netsnmp_container_get(const char *type)
155
208
{
156
 
    netsnmp_factory *f = netsnmp_container_get_factory(type);
157
 
    if (f)
158
 
        return f->produce();
 
209
    netsnmp_container *c;
 
210
    container_type *ct = netsnmp_container_get_ct(type);
 
211
    if (ct) {
 
212
        c = ct->factory->produce();
 
213
        if (c && ct->compare)
 
214
            c->compare = ct->compare;
 
215
        return c;
 
216
    }
159
217
 
160
218
    return NULL;
161
219
}
165
223
netsnmp_container *
166
224
netsnmp_container_find(const char *type)
167
225
{
168
 
    netsnmp_factory *f = netsnmp_container_find_factory(type);
169
 
    netsnmp_container *c = f ? f->produce() : NULL;
 
226
    container_type *ct = netsnmp_container_find_ct(type);
 
227
    netsnmp_container *c = ct ? ct->factory->produce() : NULL;
170
228
 
171
229
    /*
172
230
     * provide default compare
173
231
     */
174
 
    if (c && (NULL == c->compare))
175
 
        c->compare = netsnmp_compare_netsnmp_index;
 
232
    if (c) {
 
233
        if (ct->compare)
 
234
            c->compare = ct->compare;
 
235
        else if (NULL == c->compare)
 
236
            c->compare = netsnmp_compare_netsnmp_index;
 
237
    }
176
238
 
177
239
    return c;
178
240
}
201
263
 
202
264
/*------------------------------------------------------------------
203
265
 * These functions should EXACTLY match the inline version in
204
 
 * container.h. If you chance one, change them both.
 
266
 * container.h. If you change one, change them both.
205
267
 */
206
268
int CONTAINER_INSERT(netsnmp_container *x, const void *k)
207
269
210
272
    /** start at first container */
211
273
    while(x->prev)
212
274
        x = x->prev;
213
 
    while(x) {
 
275
    for(; x; x = x->next) {
 
276
        if ((NULL != x->insert_filter) &&
 
277
            (x->insert_filter(x,k) == 1))
 
278
            continue;
214
279
        rc2 = x->insert(x,k);
215
280
        if (rc2) {
216
 
            snmp_log(LOG_ERR,"error on subcontainer insert (%d)\n", rc2);
 
281
            snmp_log(LOG_ERR,"error on subcontainer '%s' insert (%d)\n",
 
282
                     x->container_name ? x->container_name : "", rc2);
217
283
            rc = rc2;
218
284
        }
219
 
        x = x->next;
220
285
    }
221
286
    return rc;
222
287
}
223
288
 
224
289
/*------------------------------------------------------------------
225
290
 * These functions should EXACTLY match the inline version in
226
 
 * container.h. If you chance one, change them both.
 
291
 * container.h. If you change one, change them both.
227
292
 */
228
293
int CONTAINER_REMOVE(netsnmp_container *x, const void *k)
229
294
{
234
299
        x = x->next;
235
300
    while(x) {
236
301
        rc2 = x->remove(x,k);
237
 
        if (rc2) {
 
302
        /** ignore remove errors if there is a filter in place */
 
303
        if ((rc2) && (NULL == x->insert_filter)) {
238
304
            snmp_log(LOG_ERR,"error on subcontainer remove (%d)\n", rc2);
239
305
            rc = rc2;
240
306
        }
246
312
 
247
313
/*------------------------------------------------------------------
248
314
 * These functions should EXACTLY match the inline version in
249
 
 * container.h. If you chance one, change them both.
 
315
 * container.h. If you change one, change them both.
250
316
 */
251
317
int CONTAINER_FREE(netsnmp_container *x)
252
318
{