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