| 1 | /* |
| 2 | * Copyright 2010 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * "Daniel Kopecek" <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <stddef.h> |
| 28 | #include <errno.h> |
| 29 | |
| 30 | #include "_seap-command.h" |
| 31 | #include "generic/rbt/rbt.h" |
| 32 | #include "seap-command-backendT.h" |
| 33 | |
| 34 | typedef rbt_t Ttable_t; |
| 35 | |
| 36 | static Ttable_t *Ttable_new (size_t capacity) |
| 37 | { |
| 38 | Ttable_t *t; |
| 39 | |
| 40 | t = rbt_i32_new(); |
| 41 | |
| 42 | return (t); |
| 43 | } |
| 44 | |
| 45 | static int Ttable_add (Ttable_t *t, SEAP_cmdrec_t *r) |
| 46 | { |
| 47 | return rbt_i32_add(t, (int32_t) r->code, (void *)r, NULL); |
| 48 | } |
| 49 | |
| 50 | static SEAP_cmdrec_t *Ttable_get (Ttable_t *t, SEAP_cmdcode_t c) |
| 51 | { |
| 52 | SEAP_cmdrec_t *r = NULL; |
| 53 | |
| 54 | rbt_i32_get(t, (int32_t)c, (void *)&r); |
| 55 | |
| 56 | return (r); |
| 57 | } |
| 58 | |
| 59 | int SEAP_cmdtbl_backendT_add (SEAP_cmdtbl_t *t, SEAP_cmdrec_t *r) |
| 60 | { |
| 61 | if (t->table == NULL) |
| 62 | t->table = Ttable_new(-1); |
| 63 | |
| 64 | if (t->table == NULL) { |
| 65 | return -1; |
| 66 | } |
| 67 | |
| 68 | return Ttable_add ((Ttable_t *)t->table, r); |
| 69 | } |
| 70 | |
| 71 | int SEAP_cmdtbl_backendT_ins (SEAP_cmdtbl_t *t, SEAP_cmdrec_t *r) |
| 72 | { |
| 73 | errno = EOPNOTSUPP; |
| 74 | return (-1); |
| 75 | } |
| 76 | |
| 77 | int SEAP_cmdtbl_backendT_del (SEAP_cmdtbl_t *t, SEAP_cmdcode_t c) |
| 78 | { |
| 79 | SEAP_cmdrec_t *tr = NULL; |
| 80 | |
| 81 | if (rbt_i32_del((Ttable_t*)t->table, (int32_t)c, (void *)&tr) != 0) |
| 82 | return (-1); |
| 83 | |
| 84 | SEAP_cmdrec_free(tr); |
| 85 | |
| 86 | return(0); |
| 87 | } |
| 88 | |
| 89 | SEAP_cmdrec_t *SEAP_cmdtbl_backendT_get (SEAP_cmdtbl_t *t, SEAP_cmdcode_t c) |
| 90 | { |
| 91 | return (t->table == NULL ? NULL : Ttable_get ((Ttable_t *)t->table, c)); |
| 92 | } |
| 93 | |
| 94 | static void backendT_free_callback(struct rbt_i32_node *n) |
| 95 | { |
| 96 | SEAP_cmdrec_free(n->data); |
| 97 | } |
| 98 | |
| 99 | void SEAP_cmdtbl_backendT_free (SEAP_cmdtbl_t *t) |
| 100 | { |
| 101 | if (t->table != NULL) |
| 102 | rbt_i32_free_cb((Ttable_t *)(t->table), &backendT_free_callback); |
| 103 | } |
| 104 | |
| 105 | int SEAP_cmdtbl_backendT_apply (SEAP_cmdtbl_t *t, int (*func) (SEAP_cmdrec_t *r, void *), void *arg) |
| 106 | { |
| 107 | /* |
| 108 | size_t i, l; |
| 109 | int ret; |
| 110 | |
| 111 | Ttable_t *St; |
| 112 | |
| 113 | St = (Ttable_t *)(t->table); |
| 114 | */ |
| 115 | |
| 116 | return (-1); |
| 117 | } |