opkg: adding the hash_table_remove API, not using yet.
[oweals/opkg-lede.git] / libopkg / nv_pair_list.c
1 /* nv_pair_list.c - the opkg package management system
2
3    Carl D. Worth
4
5    Copyright (C) 2001 University of Southern California
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2, or (at
10    your option) any later version.
11
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 */
17
18 #include "includes.h"
19
20 #include "nv_pair.h"
21 #include "void_list.h"
22 #include "nv_pair_list.h"
23
24 int nv_pair_list_elt_init(nv_pair_list_elt_t *elt, nv_pair_t *data)
25 {
26     return void_list_elt_init((void_list_elt_t *) elt, data);
27 }
28
29 void nv_pair_list_elt_deinit(nv_pair_list_elt_t *elt)
30 {
31     void_list_elt_deinit((void_list_elt_t *) elt);
32 }
33
34 int nv_pair_list_init(nv_pair_list_t *list)
35 {
36     return void_list_init((void_list_t *) list);
37 }
38
39 void nv_pair_list_deinit(nv_pair_list_t *list)
40 {
41     nv_pair_list_elt_t *iter;
42     nv_pair_t *nv_pair;
43
44     for (iter = list->head; iter; iter = iter->next) {
45         nv_pair = iter->data;
46         nv_pair_deinit(nv_pair);
47
48         /* malloced in nv_pair_list_append */
49         free(nv_pair);
50         iter->data = NULL;
51     }
52     void_list_deinit((void_list_t *) list);
53 }
54
55 nv_pair_t *nv_pair_list_append(nv_pair_list_t *list, const char *name, const char *value)
56 {
57     int err;
58
59     /* freed in nv_pair_list_deinit */
60     nv_pair_t *nv_pair = calloc(1, sizeof(nv_pair_t));
61
62     if (nv_pair == NULL) {
63         fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
64         return NULL;
65     }
66     nv_pair_init(nv_pair, name, value);
67
68     err = void_list_append((void_list_t *) list, nv_pair);
69     if (err) {
70         return NULL;
71     }
72
73     return nv_pair;
74 }
75
76 int nv_pair_list_push(nv_pair_list_t *list, nv_pair_t *data)
77 {
78     return void_list_push((void_list_t *) list, data);
79 }
80
81 nv_pair_list_elt_t *nv_pair_list_pop(nv_pair_list_t *list)
82 {
83     return (nv_pair_list_elt_t *) void_list_pop((void_list_t *) list);
84 }
85
86 char *nv_pair_list_find(nv_pair_list_t *list, char *name)
87 {
88      nv_pair_list_elt_t *iter;
89      nv_pair_t *nv_pair;
90
91      for (iter = list->head; iter; iter = iter->next) {
92           nv_pair = iter->data;
93           if (strcmp(nv_pair->name, name) == 0) {
94                return nv_pair->value;
95           }
96      }    
97      return NULL;
98 }