libopkg: implement lightweight package listing logic
[oweals/opkg-lede.git] / libopkg / str_list.c
1 /* str_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 "str_list.h"
19 #include "libbb/libbb.h"
20
21 void str_list_elt_init(str_list_elt_t * elt, char *data)
22 {
23         void_list_elt_init((void_list_elt_t *) elt, data);
24 }
25
26 void str_list_elt_deinit(str_list_elt_t * elt)
27 {
28         if (elt->data)
29                 free(elt->data);
30         void_list_elt_deinit((void_list_elt_t *) elt);
31 }
32
33 str_list_t *str_list_alloc()
34 {
35         str_list_t *list = xcalloc(1, sizeof(str_list_t));
36         str_list_init(list);
37         return list;
38 }
39
40 void str_list_init(str_list_t * list)
41 {
42         void_list_init((void_list_t *) list);
43 }
44
45 void str_list_deinit(str_list_t * list)
46 {
47         str_list_elt_t *elt;
48         while (!void_list_empty(list)) {
49                 elt = str_list_first(list);
50                 if (!elt)
51                         return;
52                 list_del_init(&elt->node);
53                 free(elt->data);
54                 elt->data = NULL;
55                 free(elt);
56         }
57 }
58
59 void str_list_append(str_list_t * list, char *data)
60 {
61         void_list_append((void_list_t *) list, xstrdup(data));
62 }
63
64 str_list_elt_t *str_list_pop(str_list_t * list)
65 {
66         return (str_list_elt_t *) void_list_pop((void_list_t *) list);
67 }
68
69 void str_list_remove(str_list_t * list, str_list_elt_t ** iter)
70 {
71         char *str = void_list_remove((void_list_t *) list,
72                                      (void_list_elt_t **) iter);
73
74         if (str)
75                 free(str);
76 }
77
78 void str_list_remove_elt(str_list_t * list, const char *target_str)
79 {
80         char *str = void_list_remove_elt((void_list_t *) list,
81                                          (void *)target_str,
82                                          (void_list_cmp_t) strcmp);
83         if (str)
84                 free(str);
85 }
86
87 str_list_elt_t *str_list_first(str_list_t * list)
88 {
89         return (str_list_elt_t *) void_list_first((void_list_t *) list);
90 }
91
92 str_list_elt_t *str_list_next(str_list_t * list, str_list_elt_t * node)
93 {
94         return (str_list_elt_t *) void_list_next((void_list_t *) list,
95                                                  (void_list_elt_t *) node);
96 }
97
98 void str_list_purge(str_list_t * list)
99 {
100         str_list_deinit(list);
101         free(list);
102 }