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