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