Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame^] | 1 | /* Simple Osmocom System Monitor (osysmon): Name-Value tree */ |
| 2 | |
| 3 | /* (C) 2018 by Harald Welte <laforge@gnumonks.org> |
| 4 | * All Rights Reserved. |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 21 | * MA 02110-1301, USA. |
| 22 | */ |
| 23 | |
| 24 | #include <talloc.h> |
| 25 | #include <osmocom/core/utils.h> |
| 26 | |
| 27 | #include "value_node.h" |
| 28 | |
| 29 | struct value_node *value_node_add(void *ctx, struct value_node *parent, |
| 30 | const char *name, const char *value) |
| 31 | { |
| 32 | struct value_node *vn = talloc_zero(parent, struct value_node); |
| 33 | OSMO_ASSERT(vn); |
| 34 | /* we assume the name is static/const and owned by caller */ |
| 35 | vn->name = name; |
| 36 | if (value) |
| 37 | vn->value = talloc_strdup(vn, value); |
| 38 | INIT_LLIST_HEAD(&vn->children); |
| 39 | if (parent) |
| 40 | llist_add_tail(&vn->list, &parent->children); |
| 41 | else |
| 42 | INIT_LLIST_HEAD(&vn->list); |
| 43 | return vn; |
| 44 | } |
| 45 | |
| 46 | void value_node_del(struct value_node *node) |
| 47 | { |
| 48 | /* remove ourselves from the parent */ |
| 49 | llist_del(&node->list); |
| 50 | |
| 51 | #if 0 /* not actually needed, talloc should do this */ |
| 52 | struct value_node *ch, *ch2; |
| 53 | llist_for_each_entry_safe(ch, ch2, &node->children, list) |
| 54 | value_node_del(ch); |
| 55 | /* "value" is a talloc child, and "name" is not owned by us */ |
| 56 | #endif |
| 57 | /* let talloc do its magic to delete all child nodes */ |
| 58 | talloc_free(node); |
| 59 | } |