]> code.delx.au - pulseaudio/blob - src/modules/rtp/headerlist.c
11e076eed8bae5cba624b6447628777649ea2147
[pulseaudio] / src / modules / rtp / headerlist.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2008 Colin Guthrie
5 Copyright 2007 Lennart Poettering
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of the
10 License, or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28
29 #include <pulse/xmalloc.h>
30
31 #include <pulsecore/hashmap.h>
32 #include <pulsecore/strbuf.h>
33 #include <pulsecore/core-util.h>
34
35 #include "headerlist.h"
36
37 struct header {
38 char *key;
39 void *value;
40 size_t nbytes;
41 };
42
43 #define MAKE_HASHMAP(p) ((pa_hashmap*) (p))
44 #define MAKE_HEADERLIST(p) ((pa_headerlist*) (p))
45
46 static void header_free(struct header *hdr) {
47 pa_assert(hdr);
48
49 pa_xfree(hdr->key);
50 pa_xfree(hdr->value);
51 pa_xfree(hdr);
52 }
53
54 pa_headerlist* pa_headerlist_new(void) {
55 return MAKE_HEADERLIST(pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, (pa_free_cb_t) header_free));
56 }
57
58 void pa_headerlist_free(pa_headerlist* p) {
59 pa_hashmap_free(MAKE_HASHMAP(p));
60 }
61
62 int pa_headerlist_puts(pa_headerlist *p, const char *key, const char *value) {
63 struct header *hdr;
64 bool add = false;
65
66 pa_assert(p);
67 pa_assert(key);
68
69 if (!(hdr = pa_hashmap_get(MAKE_HASHMAP(p), key))) {
70 hdr = pa_xnew(struct header, 1);
71 hdr->key = pa_xstrdup(key);
72 add = true;
73 } else
74 pa_xfree(hdr->value);
75
76 hdr->value = pa_xstrdup(value);
77 hdr->nbytes = strlen(value)+1;
78
79 if (add)
80 pa_hashmap_put(MAKE_HASHMAP(p), hdr->key, hdr);
81
82 return 0;
83 }
84
85 int pa_headerlist_putsappend(pa_headerlist *p, const char *key, const char *value) {
86 struct header *hdr;
87 bool add = false;
88
89 pa_assert(p);
90 pa_assert(key);
91
92 if (!(hdr = pa_hashmap_get(MAKE_HASHMAP(p), key))) {
93 hdr = pa_xnew(struct header, 1);
94 hdr->key = pa_xstrdup(key);
95 hdr->value = pa_xstrdup(value);
96 add = true;
97 } else {
98 void *newval = pa_sprintf_malloc("%s%s", (char*)hdr->value, value);
99 pa_xfree(hdr->value);
100 hdr->value = newval;
101 }
102 hdr->nbytes = strlen(hdr->value)+1;
103
104 if (add)
105 pa_hashmap_put(MAKE_HASHMAP(p), hdr->key, hdr);
106
107 return 0;
108 }
109
110 const char *pa_headerlist_gets(pa_headerlist *p, const char *key) {
111 struct header *hdr;
112
113 pa_assert(p);
114 pa_assert(key);
115
116 if (!(hdr = pa_hashmap_get(MAKE_HASHMAP(p), key)))
117 return NULL;
118
119 if (hdr->nbytes <= 0)
120 return NULL;
121
122 if (((char*) hdr->value)[hdr->nbytes-1] != 0)
123 return NULL;
124
125 if (strlen((char*) hdr->value) != hdr->nbytes-1)
126 return NULL;
127
128 return (char*) hdr->value;
129 }
130
131 int pa_headerlist_remove(pa_headerlist *p, const char *key) {
132 struct header *hdr;
133
134 pa_assert(p);
135 pa_assert(key);
136
137 if (!(hdr = pa_hashmap_remove(MAKE_HASHMAP(p), key)))
138 return -1;
139
140 header_free(hdr);
141 return 0;
142 }
143
144 const char *pa_headerlist_iterate(pa_headerlist *p, void **state) {
145 struct header *hdr;
146
147 if (!(hdr = pa_hashmap_iterate(MAKE_HASHMAP(p), state, NULL)))
148 return NULL;
149
150 return hdr->key;
151 }
152
153 char *pa_headerlist_to_string(pa_headerlist *p) {
154 const char *key;
155 void *state = NULL;
156 pa_strbuf *buf;
157
158 pa_assert(p);
159
160 buf = pa_strbuf_new();
161
162 while ((key = pa_headerlist_iterate(p, &state))) {
163
164 const char *v;
165
166 if ((v = pa_headerlist_gets(p, key)))
167 pa_strbuf_printf(buf, "%s: %s\r\n", key, v);
168 }
169
170 return pa_strbuf_tostring_free(buf);
171 }
172
173 int pa_headerlist_contains(pa_headerlist *p, const char *key) {
174 pa_assert(p);
175 pa_assert(key);
176
177 if (!(pa_hashmap_get(MAKE_HASHMAP(p), key)))
178 return 0;
179
180 return 1;
181 }