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> |
Harald Welte | 3a964a4 | 2018-06-04 19:40:26 +0200 | [diff] [blame] | 25 | #include <string.h> |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 26 | #include <osmocom/core/utils.h> |
| 27 | |
| 28 | #include "value_node.h" |
| 29 | |
| 30 | struct value_node *value_node_add(void *ctx, struct value_node *parent, |
| 31 | const char *name, const char *value) |
| 32 | { |
| 33 | struct value_node *vn = talloc_zero(parent, struct value_node); |
Harald Welte | 3a964a4 | 2018-06-04 19:40:26 +0200 | [diff] [blame] | 34 | |
| 35 | if (parent && value_node_find(parent, name)) { |
| 36 | /* duplicate name not permitted! */ |
| 37 | return NULL; |
| 38 | } |
| 39 | |
| 40 | vn = talloc_zero(parent, struct value_node); |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 41 | OSMO_ASSERT(vn); |
Harald Welte | 3a964a4 | 2018-06-04 19:40:26 +0200 | [diff] [blame] | 42 | |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 43 | /* we assume the name is static/const and owned by caller */ |
| 44 | vn->name = name; |
| 45 | if (value) |
| 46 | vn->value = talloc_strdup(vn, value); |
| 47 | INIT_LLIST_HEAD(&vn->children); |
| 48 | if (parent) |
| 49 | llist_add_tail(&vn->list, &parent->children); |
| 50 | else |
| 51 | INIT_LLIST_HEAD(&vn->list); |
| 52 | return vn; |
| 53 | } |
| 54 | |
Harald Welte | 3a964a4 | 2018-06-04 19:40:26 +0200 | [diff] [blame] | 55 | struct value_node *value_node_find(struct value_node *parent, const char *name) |
| 56 | { |
| 57 | struct value_node *vn; |
| 58 | llist_for_each_entry(vn, &parent->children, list) { |
| 59 | if (!strcmp(name, vn->name)) |
| 60 | return vn; |
| 61 | } |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | struct value_node *value_node_find_or_add(struct value_node *parent, const char *name) |
| 66 | { |
| 67 | struct value_node *vn; |
| 68 | vn = value_node_find(parent, name); |
| 69 | if (!vn) |
| 70 | vn = value_node_add(parent, parent, name, NULL); |
| 71 | return vn; |
| 72 | } |
| 73 | |
Harald Welte | b6718f7 | 2018-06-04 20:08:21 +0200 | [diff] [blame^] | 74 | struct value_node *value_node_find_by_idx(struct value_node *parent, int idx) |
| 75 | { |
| 76 | struct value_node *vn; |
| 77 | llist_for_each_entry(vn, &parent->children, list) { |
| 78 | if (idx == vn->idx) |
| 79 | return vn; |
| 80 | } |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 85 | void value_node_del(struct value_node *node) |
| 86 | { |
| 87 | /* remove ourselves from the parent */ |
| 88 | llist_del(&node->list); |
| 89 | |
| 90 | #if 0 /* not actually needed, talloc should do this */ |
| 91 | struct value_node *ch, *ch2; |
| 92 | llist_for_each_entry_safe(ch, ch2, &node->children, list) |
| 93 | value_node_del(ch); |
| 94 | /* "value" is a talloc child, and "name" is not owned by us */ |
| 95 | #endif |
| 96 | /* let talloc do its magic to delete all child nodes */ |
| 97 | talloc_free(node); |
| 98 | } |