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