Fix checking for NULL after usage
[pulseaudio-mirror.git] / src / pulsecore / strlist.c
blob0f4ca86730fc735fb494b8fe4e243ebb9b55f88f
1 /***
2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
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.
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.
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 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <string.h>
28 #include <pulse/xmalloc.h>
30 #include <pulsecore/strbuf.h>
31 #include <pulsecore/macro.h>
32 #include <pulsecore/core-util.h>
34 #include "strlist.h"
36 struct pa_strlist {
37 pa_strlist *next;
40 #define ITEM_TO_TEXT(c) ((char*) (c) + PA_ALIGN(sizeof(pa_strlist)))
42 pa_strlist* pa_strlist_prepend(pa_strlist *l, const char *s) {
43 pa_strlist *n;
44 size_t size;
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;
52 return n;
55 char *pa_strlist_tostring(pa_strlist *l) {
56 int first = 1;
57 pa_strbuf *b;
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));
67 return pa_strbuf_tostring_free(b);
70 pa_strlist* pa_strlist_remove(pa_strlist *l, const char *s) {
71 pa_strlist *ret = l, *prev = NULL;
73 pa_assert(l);
74 pa_assert(s);
76 while (l) {
77 if (!strcmp(ITEM_TO_TEXT(l), s)) {
78 pa_strlist *n = l->next;
80 if (!prev) {
81 pa_assert(ret == l);
82 ret = n;
83 } else
84 prev->next = n;
86 pa_xfree(l);
88 l = n;
90 } else {
91 prev = l;
92 l = l->next;
96 return ret;
99 void pa_strlist_free(pa_strlist *l) {
100 while (l) {
101 pa_strlist *c = l;
102 l = l->next;
103 pa_xfree(c);
107 pa_strlist* pa_strlist_pop(pa_strlist *l, char **s) {
108 pa_strlist *r;
110 pa_assert(s);
112 if (!l) {
113 *s = NULL;
114 return NULL;
117 *s = pa_xstrdup(ITEM_TO_TEXT(l));
118 r = l->next;
119 pa_xfree(l);
120 return r;
123 pa_strlist* pa_strlist_parse(const char *s) {
124 pa_strlist *head = NULL, *p = NULL;
125 const char *state = NULL;
126 char *r;
128 while ((r = pa_split_spaces(s, &state))) {
129 pa_strlist *n;
130 size_t size = strlen(r);
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);
137 if (p)
138 p->next = n;
139 else
140 head = n;
142 p = n;
145 return head;
148 pa_strlist *pa_strlist_reverse(pa_strlist *l) {
149 pa_strlist *r = NULL;
151 while (l) {
152 pa_strlist *n;
154 n = l->next;
155 l->next = r;
156 r = l;
157 l = n;
160 return r;
163 pa_strlist *pa_strlist_next(pa_strlist *s) {
164 pa_assert(s);
166 return s->next;
169 const char *pa_strlist_data(pa_strlist *s) {
170 pa_assert(s);
172 return ITEM_TO_TEXT(s);