From 10b31c61d6d48fc4ed2df85d9dbf054827f179f5 Mon Sep 17 00:00:00 2001 From: "7u83@mail.ru" <7u83@mail.ru@noemail.net> Date: Fri, 1 May 2015 10:49:11 +0000 Subject: [PATCH] itemheap functions added. FossilOrigin-Name: 6636f179aa11185d2ae593e20a31251e4f9ca37291c02d52aa9685ff73abbae1 --- src/capwap/item.c | 79 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 63 insertions(+), 16 deletions(-) diff --git a/src/capwap/item.c b/src/capwap/item.c index 5821b605..25bfe215 100644 --- a/src/capwap/item.c +++ b/src/capwap/item.c @@ -1,29 +1,76 @@ - -#include "item.h" -#include "capwap_items.h" - /* -struct cw_item * cw_item_get_by_id(int id,struct cw_item *table) -{ - int i; - while (table->id != CW_ITEM_NONE) { - if (table->id == id) - return table; - table++; + This file is part of libcapwap. + + libcapwap is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + libcapwap 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 Foobar. If not, see . - } - return NULL; -} */ -struct cw_item * cw_item_get_by_name(const char *id,struct cw_item *table) + +#include "item.h" +#include "log.h" + + +const char CW_ITEM_NONE[] = ""; + +struct cw_itemdef *cw_item_get_by_name(const char *id, struct cw_itemdef *table) { int i; while (table->id != CW_ITEM_NONE) { - if (!strcmp(table->id,id)) + if (!strcmp(table->id, id)) return table; table++; } return NULL; } + + + +static int cmp(const void *x1, const void *x2) +{ + cw_itemdef_t *i1, *i2; + i1 = (cw_itemdef_t *) x1; + i2 = (cw_itemdef_t *) x2; + int rc = strcmp(i1->id, i2->id); + if (rc != 0) + return rc; + return strcmp(i1->sub_id, i2->sub_id); +} + +const cw_itemdef_t * cw_itemdef_get(cw_itemdefheap_t t, const char *id, const char *sub_id) +{ + cw_itemdef_t idef; + idef.id = id; + idef.sub_id = !sub_id ? CW_ITEM_NONE : sub_id; + return mavl_get(t, &idef); +} + +cw_itemdefheap_t cw_itemdefheap_create() +{ + return mavl_create(cmp, NULL); +} + +int cw_itemdefheap_register(cw_itemdefheap_t t, cw_itemdef_t * defs) +{ + int n = 0; + while (defs->id != CW_ITEM_NONE) { + cw_itemdef_t *a = mavl_add(t, defs); + if (a != defs) { + cw_log(LOG_ERR, "Duplicated item: %s", defs->id); + } else + n++; + defs++; + } + return n; +}