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, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <string.h>
25
26 #include <pulse/xmalloc.h>
27
28 #include <pulsecore/strbuf.h>
29 #include <pulsecore/macro.h>
30 #include <pulsecore/core-util.h>
31
32 #include "strlist.h"
33
34 struct pa_strlist {
35 pa_strlist *next;
36 };
37
38 #define ITEM_TO_TEXT(c) ((char*) (c) + PA_ALIGN(sizeof(pa_strlist)))
39
pa_strlist_prepend(pa_strlist * l,const char * s)40 pa_strlist* pa_strlist_prepend(pa_strlist *l, const char *s) {
41 pa_strlist *n;
42 size_t size;
43
44 pa_assert(s);
45 size = strlen(s);
46 n = pa_xmalloc(PA_ALIGN(sizeof(pa_strlist)) + size + 1);
47 memcpy(ITEM_TO_TEXT(n), s, size + 1);
48 n->next = l;
49
50 return n;
51 }
52
pa_strlist_to_string(pa_strlist * l)53 char *pa_strlist_to_string(pa_strlist *l) {
54 int first = 1;
55 pa_strbuf *b;
56
57 b = pa_strbuf_new();
58 for (; l; l = l->next) {
59 if (!first)
60 pa_strbuf_puts(b, " ");
61 first = 0;
62 pa_strbuf_puts(b, ITEM_TO_TEXT(l));
63 }
64
65 return pa_strbuf_to_string_free(b);
66 }
67
pa_strlist_remove(pa_strlist * l,const char * s)68 pa_strlist* pa_strlist_remove(pa_strlist *l, const char *s) {
69 pa_strlist *ret = l, *prev = NULL;
70
71 pa_assert(l);
72 pa_assert(s);
73
74 while (l) {
75 if (pa_streq(ITEM_TO_TEXT(l), s)) {
76 pa_strlist *n = l->next;
77
78 if (!prev) {
79 pa_assert(ret == l);
80 ret = n;
81 } else
82 prev->next = n;
83
84 pa_xfree(l);
85
86 l = n;
87
88 } else {
89 prev = l;
90 l = l->next;
91 }
92 }
93
94 return ret;
95 }
96
pa_strlist_free(pa_strlist * l)97 void pa_strlist_free(pa_strlist *l) {
98 while (l) {
99 pa_strlist *c = l;
100 l = l->next;
101 pa_xfree(c);
102 }
103 }
104
pa_strlist_pop(pa_strlist * l,char ** s)105 pa_strlist* pa_strlist_pop(pa_strlist *l, char **s) {
106 pa_strlist *r;
107
108 pa_assert(s);
109
110 if (!l) {
111 *s = NULL;
112 return NULL;
113 }
114
115 *s = pa_xstrdup(ITEM_TO_TEXT(l));
116 r = l->next;
117 pa_xfree(l);
118 return r;
119 }
120
pa_strlist_parse(const char * s)121 pa_strlist* pa_strlist_parse(const char *s) {
122 pa_strlist *head = NULL, *p = NULL;
123 const char *state = NULL;
124 char *r;
125
126 while ((r = pa_split_spaces(s, &state))) {
127 pa_strlist *n;
128 size_t size = strlen(r);
129
130 n = pa_xmalloc(PA_ALIGN(sizeof(pa_strlist)) + size + 1);
131 n->next = NULL;
132 memcpy(ITEM_TO_TEXT(n), r, size+1);
133 pa_xfree(r);
134
135 if (p)
136 p->next = n;
137 else
138 head = n;
139
140 p = n;
141 }
142
143 return head;
144 }
145
pa_strlist_reverse(pa_strlist * l)146 pa_strlist *pa_strlist_reverse(pa_strlist *l) {
147 pa_strlist *r = NULL;
148
149 while (l) {
150 pa_strlist *n;
151
152 n = l->next;
153 l->next = r;
154 r = l;
155 l = n;
156 }
157
158 return r;
159 }
160
pa_strlist_next(pa_strlist * s)161 pa_strlist *pa_strlist_next(pa_strlist *s) {
162 pa_assert(s);
163
164 return s->next;
165 }
166
pa_strlist_data(pa_strlist * s)167 const char *pa_strlist_data(pa_strlist *s) {
168 pa_assert(s);
169
170 return ITEM_TO_TEXT(s);
171 }
172