e98d7188a847e0aeb0085edbb2a5c6935168d21f
[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
25 int nv_pair_list_init(nv_pair_list_t *list)
26 {
27     return void_list_init((void_list_t *) list);
28 }
29
30 void nv_pair_list_deinit(nv_pair_list_t *list)
31 {
32     nv_pair_list_elt_t *pos;
33     nv_pair_t *nv_pair;
34
35     while(!void_list_empty(list)) {
36         pos = nv_pair_list_pop(list);
37         if (!pos)
38             break;
39         nv_pair =  (nv_pair_t *) pos->data;
40         nv_pair_deinit(nv_pair);
41         /* malloced in nv_pair_list_append */
42         free(nv_pair);
43         pos->data = NULL;
44         free(pos);
45     }
46     void_list_deinit((void_list_t *) list);
47 }
48
49 nv_pair_t *nv_pair_list_append(nv_pair_list_t *list, const char *name, const char *value)
50 {
51     int err;
52
53     /* freed in nv_pair_list_deinit */
54     nv_pair_t *nv_pair = calloc(1, sizeof(nv_pair_t));
55
56     if (nv_pair == NULL) {
57         fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
58         return NULL;
59     }
60     nv_pair_init(nv_pair, name, value);
61
62     err = void_list_append((void_list_t *) list, nv_pair);
63     if (err) {
64         return NULL;
65     }
66
67     return nv_pair;
68 }
69
70 int nv_pair_list_push(nv_pair_list_t *list, nv_pair_t *data)
71 {
72     return void_list_push((void_list_t *) list, data);
73 }
74
75 nv_pair_list_elt_t *nv_pair_list_pop(nv_pair_list_t *list)
76 {
77     return (nv_pair_list_elt_t *) void_list_pop((void_list_t *) list);
78 }
79
80 char *nv_pair_list_find(nv_pair_list_t *list, char *name)
81 {
82      nv_pair_list_elt_t *iter;
83      nv_pair_t *nv_pair;
84
85      list_for_each_entry(iter, &list->head, node) {
86           nv_pair = (nv_pair_t *)iter->data;
87           if (strcmp(nv_pair->name, name) == 0) {
88                return nv_pair->value;
89           }
90      }    
91      return NULL;
92 }
93
94 nv_pair_list_elt_t *nv_pair_list_first(nv_pair_list_t *list) {
95     return (nv_pair_list_elt_t * )void_list_first((void_list_t *) list);
96 }
97
98 nv_pair_list_elt_t *nv_pair_list_prev(nv_pair_list_t *list, nv_pair_list_elt_t *node) {
99     return (nv_pair_list_elt_t * )void_list_prev((void_list_t *) list, (void_list_elt_t *)node);
100 }
101
102 nv_pair_list_elt_t *nv_pair_list_next(nv_pair_list_t *list, nv_pair_list_elt_t *node) {
103     return (nv_pair_list_elt_t * )void_list_next((void_list_t *) list, (void_list_elt_t *)node);
104 }
105
106 nv_pair_list_elt_t *nv_pair_list_last(nv_pair_list_t *list) {
107     return (nv_pair_list_elt_t * )void_list_last((void_list_t *) list);
108 }
109
110
111