Branch data Line data Source code
1 : : /***
2 : : This file is part of PulseAudio.
3 : :
4 : : Copyright 2004-2006 Lennart Poettering
5 : :
6 : : PulseAudio is free software; you can redistribute it and/or modify
7 : : it under the terms of the GNU Lesser General Public License as published
8 : : by the Free Software Foundation; either version 2.1 of the License,
9 : : or (at your option) any later version.
10 : :
11 : : PulseAudio is distributed in the hope that it will be useful, but
12 : : WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : : General Public License for more details.
15 : :
16 : : You should have received a copy of the GNU Lesser General Public License
17 : : along with PulseAudio; if not, write to the Free Software
18 : : Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 : : USA.
20 : : ***/
21 : :
22 : : #ifdef HAVE_CONFIG_H
23 : : #include <config.h>
24 : : #endif
25 : :
26 : : #include <string.h>
27 : :
28 : : #include <pulse/xmalloc.h>
29 : :
30 : : #include <pulsecore/strbuf.h>
31 : : #include <pulsecore/macro.h>
32 : : #include <pulsecore/core-util.h>
33 : :
34 : : #include "strlist.h"
35 : :
36 : : struct pa_strlist {
37 : : pa_strlist *next;
38 : : };
39 : :
40 : : #define ITEM_TO_TEXT(c) ((char*) (c) + PA_ALIGN(sizeof(pa_strlist)))
41 : :
42 : 5 : pa_strlist* pa_strlist_prepend(pa_strlist *l, const char *s) {
43 : : pa_strlist *n;
44 : : size_t size;
45 : :
46 [ - + ]: 5 : pa_assert(s);
47 : 5 : size = strlen(s);
48 : 5 : n = pa_xmalloc(PA_ALIGN(sizeof(pa_strlist)) + size + 1);
49 : 5 : memcpy(ITEM_TO_TEXT(n), s, size + 1);
50 : 5 : n->next = l;
51 : :
52 : 5 : return n;
53 : : }
54 : :
55 : 3 : char *pa_strlist_tostring(pa_strlist *l) {
56 : 3 : int first = 1;
57 : : pa_strbuf *b;
58 : :
59 : 3 : b = pa_strbuf_new();
60 [ + + ]: 16 : for (; l; l = l->next) {
61 [ + + ]: 13 : if (!first)
62 : 10 : pa_strbuf_puts(b, " ");
63 : 13 : first = 0;
64 : 13 : pa_strbuf_puts(b, ITEM_TO_TEXT(l));
65 : : }
66 : :
67 : 3 : return pa_strbuf_tostring_free(b);
68 : : }
69 : :
70 : 1 : pa_strlist* pa_strlist_remove(pa_strlist *l, const char *s) {
71 : 1 : pa_strlist *ret = l, *prev = NULL;
72 : :
73 [ - + ]: 1 : pa_assert(l);
74 [ + - ]: 1 : pa_assert(s);
75 : :
76 [ + + ]: 5 : while (l) {
77 [ + + ]: 4 : if (pa_streq(ITEM_TO_TEXT(l), s)) {
78 : 1 : pa_strlist *n = l->next;
79 : :
80 [ - + ]: 1 : if (!prev) {
81 [ # # ]: 0 : pa_assert(ret == l);
82 : : ret = n;
83 : : } else
84 : 1 : prev->next = n;
85 : :
86 : 1 : pa_xfree(l);
87 : :
88 : 1 : l = n;
89 : :
90 : : } else {
91 : 3 : prev = l;
92 : 4 : l = l->next;
93 : : }
94 : : }
95 : :
96 : 1 : return ret;
97 : : }
98 : :
99 : 2 : void pa_strlist_free(pa_strlist *l) {
100 [ + + ]: 10 : while (l) {
101 : 8 : pa_strlist *c = l;
102 : 8 : l = l->next;
103 : 8 : pa_xfree(c);
104 : : }
105 : 2 : }
106 : :
107 : 1 : pa_strlist* pa_strlist_pop(pa_strlist *l, char **s) {
108 : : pa_strlist *r;
109 : :
110 [ - + ]: 1 : pa_assert(s);
111 : :
112 [ - + ]: 1 : if (!l) {
113 : 0 : *s = NULL;
114 : 0 : return NULL;
115 : : }
116 : :
117 : 1 : *s = pa_xstrdup(ITEM_TO_TEXT(l));
118 : 1 : r = l->next;
119 : 1 : pa_xfree(l);
120 : 1 : return r;
121 : : }
122 : :
123 : 1 : pa_strlist* pa_strlist_parse(const char *s) {
124 : 1 : pa_strlist *head = NULL, *p = NULL;
125 : 1 : const char *state = NULL;
126 : : char *r;
127 : :
128 [ + + ]: 6 : while ((r = pa_split_spaces(s, &state))) {
129 : : pa_strlist *n;
130 : 5 : size_t size = strlen(r);
131 : :
132 : 5 : n = pa_xmalloc(PA_ALIGN(sizeof(pa_strlist)) + size + 1);
133 : 5 : n->next = NULL;
134 : 5 : memcpy(ITEM_TO_TEXT(n), r, size+1);
135 : 5 : pa_xfree(r);
136 : :
137 [ + + ]: 5 : if (p)
138 : 4 : p->next = n;
139 : : else
140 : : head = n;
141 : :
142 : 5 : p = n;
143 : : }
144 : :
145 : 1 : return head;
146 : : }
147 : :
148 : 0 : pa_strlist *pa_strlist_reverse(pa_strlist *l) {
149 : 0 : pa_strlist *r = NULL;
150 : :
151 [ # # ]: 0 : while (l) {
152 : : pa_strlist *n;
153 : :
154 : 0 : n = l->next;
155 : 0 : l->next = r;
156 : 0 : r = l;
157 : 0 : l = n;
158 : : }
159 : :
160 : 0 : return r;
161 : : }
162 : :
163 : 0 : pa_strlist *pa_strlist_next(pa_strlist *s) {
164 [ # # ]: 0 : pa_assert(s);
165 : :
166 : 0 : return s->next;
167 : : }
168 : :
169 : 0 : const char *pa_strlist_data(pa_strlist *s) {
170 [ # # ]: 0 : pa_assert(s);
171 : :
172 : 0 : return ITEM_TO_TEXT(s);
173 : : }
|