2 This file is part of PulseAudio.
4 Copyright 2008 Colin Guthrie
5 Copyright 2007 Lennart Poettering
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.
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.
17 You should have received a copy of the GNU Lesser General Public
18 License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
27 #include <pulse/xmalloc.h>
29 #include <pulsecore/hashmap.h>
30 #include <pulsecore/strbuf.h>
31 #include <pulsecore/core-util.h>
33 #include "headerlist.h"
41 #define MAKE_HASHMAP(p) ((pa_hashmap*) (p))
42 #define MAKE_HEADERLIST(p) ((pa_headerlist*) (p))
44 static void header_free(struct header *hdr) {
52 pa_headerlist* pa_headerlist_new(void) {
53 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 void pa_headerlist_free(pa_headerlist* p) {
57 pa_hashmap_free(MAKE_HASHMAP(p));
60 int pa_headerlist_puts(pa_headerlist *p, const char *key, const char *value) {
67 if (!(hdr = pa_hashmap_get(MAKE_HASHMAP(p), key))) {
68 hdr = pa_xnew(struct header, 1);
69 hdr->key = pa_xstrdup(key);
74 hdr->value = pa_xstrdup(value);
75 hdr->nbytes = strlen(value)+1;
78 pa_hashmap_put(MAKE_HASHMAP(p), hdr->key, hdr);
83 int pa_headerlist_putsappend(pa_headerlist *p, const char *key, const char *value) {
90 if (!(hdr = pa_hashmap_get(MAKE_HASHMAP(p), key))) {
91 hdr = pa_xnew(struct header, 1);
92 hdr->key = pa_xstrdup(key);
93 hdr->value = pa_xstrdup(value);
96 void *newval = pa_sprintf_malloc("%s%s", (char*)hdr->value, value);
100 hdr->nbytes = strlen(hdr->value)+1;
103 pa_hashmap_put(MAKE_HASHMAP(p), hdr->key, hdr);
108 const char *pa_headerlist_gets(pa_headerlist *p, const char *key) {
114 if (!(hdr = pa_hashmap_get(MAKE_HASHMAP(p), key)))
117 if (hdr->nbytes <= 0)
120 if (((char*) hdr->value)[hdr->nbytes-1] != 0)
123 if (strlen((char*) hdr->value) != hdr->nbytes-1)
126 return (char*) hdr->value;
129 int pa_headerlist_remove(pa_headerlist *p, const char *key) {
133 return pa_hashmap_remove_and_free(MAKE_HASHMAP(p), key);
136 const char *pa_headerlist_iterate(pa_headerlist *p, void **state) {
139 if (!(hdr = pa_hashmap_iterate(MAKE_HASHMAP(p), state, NULL)))
145 char *pa_headerlist_to_string(pa_headerlist *p) {
152 buf = pa_strbuf_new();
154 while ((key = pa_headerlist_iterate(p, &state))) {
158 if ((v = pa_headerlist_gets(p, key)))
159 pa_strbuf_printf(buf, "%s: %s\r\n", key, v);
162 return pa_strbuf_to_string_free(buf);
165 int pa_headerlist_contains(pa_headerlist *p, const char *key) {
169 if (!(pa_hashmap_get(MAKE_HASHMAP(p), key)))