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