]> code.delx.au - pulseaudio/blob - polyp/strlist.c
df7278d2310140657fee95ea06cd52616e1c7ff4
[pulseaudio] / polyp / strlist.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio 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 General Public License
17 along with polypaudio; 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 #include <assert.h>
28
29 #include "strlist.h"
30 #include "xmalloc.h"
31 #include "strbuf.h"
32 #include "util.h"
33
34 struct pa_strlist {
35 struct pa_strlist *next;
36 char *str;
37 };
38
39 struct pa_strlist* pa_strlist_prepend(struct pa_strlist *l, const char *s) {
40 struct pa_strlist *n;
41 assert(s);
42 n = pa_xmalloc(sizeof(struct pa_strlist));
43 n->str = pa_xstrdup(s);
44 n->next = l;
45 return n;
46 }
47
48 char *pa_strlist_tostring(struct pa_strlist *l) {
49 int first = 1;
50 struct pa_strbuf *b;
51
52 b = pa_strbuf_new();
53 for (; l; l = l->next) {
54 if (!first)
55 pa_strbuf_puts(b, " ");
56 first = 0;
57 pa_strbuf_puts(b, l->str);
58 }
59
60 return pa_strbuf_tostring_free(b);
61 }
62
63 struct pa_strlist* pa_strlist_remove(struct pa_strlist *l, const char *s) {
64 struct pa_strlist *ret = l, *prev = NULL;
65 assert(l && s);
66
67 while (l) {
68 if (!strcmp(l->str, s)) {
69 struct pa_strlist *n = l->next;
70
71 if (!prev) {
72 assert(ret == l);
73 ret = n;
74 } else
75 prev->next = n;
76
77 pa_xfree(l->str);
78 pa_xfree(l);
79
80 l = n;
81
82 } else {
83 prev = l;
84 l = l->next;
85 }
86 }
87
88 return ret;
89 }
90
91 void pa_strlist_free(struct pa_strlist *l) {
92 while (l) {
93 struct pa_strlist *c = l;
94 l = l->next;
95
96 pa_xfree(c->str);
97 pa_xfree(c);
98 }
99 }
100
101 struct pa_strlist* pa_strlist_pop(struct pa_strlist *l, char **s) {
102 struct pa_strlist *r;
103 assert(s);
104
105 if (!l) {
106 *s = NULL;
107 return NULL;
108 }
109
110 *s = l->str;
111 r = l->next;
112 pa_xfree(l);
113 return r;
114 }
115
116 struct pa_strlist* pa_strlist_parse(const char *s) {
117 struct pa_strlist *head = NULL, *p = NULL;
118 const char *state = NULL;
119 char *r;
120
121 while ((r = pa_split_spaces(s, &state))) {
122 struct pa_strlist *n;
123
124 n = pa_xmalloc(sizeof(struct pa_strlist));
125 n->str = r;
126 n->next = NULL;
127
128 if (p)
129 p->next = n;
130 else
131 head = n;
132
133 p = n;
134 }
135
136 return head;
137 }