]> code.delx.au - pulseaudio/blob - src/polypcore/strlist.c
Cleaned up the includes after the restructuring. Indicate which headers are
[pulseaudio] / src / polypcore / 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 Lesser 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 Lesser 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 <polypcore/xmalloc.h>
30 #include <polypcore/strbuf.h>
31 #include <polypcore/util.h>
32
33 #include "strlist.h"
34
35 struct pa_strlist {
36 pa_strlist *next;
37 char *str;
38 };
39
40 pa_strlist* pa_strlist_prepend(pa_strlist *l, const char *s) {
41 pa_strlist *n;
42 assert(s);
43 n = pa_xmalloc(sizeof(pa_strlist));
44 n->str = pa_xstrdup(s);
45 n->next = l;
46 return n;
47 }
48
49 char *pa_strlist_tostring(pa_strlist *l) {
50 int first = 1;
51 pa_strbuf *b;
52
53 b = pa_strbuf_new();
54 for (; l; l = l->next) {
55 if (!first)
56 pa_strbuf_puts(b, " ");
57 first = 0;
58 pa_strbuf_puts(b, l->str);
59 }
60
61 return pa_strbuf_tostring_free(b);
62 }
63
64 pa_strlist* pa_strlist_remove(pa_strlist *l, const char *s) {
65 pa_strlist *ret = l, *prev = NULL;
66 assert(l && s);
67
68 while (l) {
69 if (!strcmp(l->str, s)) {
70 pa_strlist *n = l->next;
71
72 if (!prev) {
73 assert(ret == l);
74 ret = n;
75 } else
76 prev->next = n;
77
78 pa_xfree(l->str);
79 pa_xfree(l);
80
81 l = n;
82
83 } else {
84 prev = l;
85 l = l->next;
86 }
87 }
88
89 return ret;
90 }
91
92 void pa_strlist_free(pa_strlist *l) {
93 while (l) {
94 pa_strlist *c = l;
95 l = l->next;
96
97 pa_xfree(c->str);
98 pa_xfree(c);
99 }
100 }
101
102 pa_strlist* pa_strlist_pop(pa_strlist *l, char **s) {
103 pa_strlist *r;
104 assert(s);
105
106 if (!l) {
107 *s = NULL;
108 return NULL;
109 }
110
111 *s = l->str;
112 r = l->next;
113 pa_xfree(l);
114 return r;
115 }
116
117 pa_strlist* pa_strlist_parse(const char *s) {
118 pa_strlist *head = NULL, *p = NULL;
119 const char *state = NULL;
120 char *r;
121
122 while ((r = pa_split_spaces(s, &state))) {
123 pa_strlist *n;
124
125 n = pa_xmalloc(sizeof(pa_strlist));
126 n->str = r;
127 n->next = NULL;
128
129 if (p)
130 p->next = n;
131 else
132 head = n;
133
134 p = n;
135 }
136
137 return head;
138 }