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, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <pulse/xmalloc.h>
31 #include <pulsecore/hashmap.h>
32 #include <pulsecore/strbuf.h>
33 #include <pulsecore/core-util.h>
35 #include "headerlist.h"
43 #define MAKE_HASHMAP(p) ((pa_hashmap*) (p))
44 #define MAKE_HEADERLIST(p) ((pa_headerlist*) (p))
46 static void header_free(struct header *hdr) {
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));
58 void pa_headerlist_free(pa_headerlist* p) {
59 pa_hashmap_free(MAKE_HASHMAP(p));
62 int pa_headerlist_puts(pa_headerlist *p, const char *key, const char *value) {
69 if (!(hdr = pa_hashmap_get(MAKE_HASHMAP(p), key))) {
70 hdr = pa_xnew(struct header, 1);
71 hdr->key = pa_xstrdup(key);
76 hdr->value = pa_xstrdup(value);
77 hdr->nbytes = strlen(value)+1;
80 pa_hashmap_put(MAKE_HASHMAP(p), hdr->key, hdr);
85 int pa_headerlist_putsappend(pa_headerlist *p, const char *key, const char *value) {
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);
98 void *newval = pa_sprintf_malloc("%s%s", (char*)hdr->value, value);
102 hdr->nbytes = strlen(hdr->value)+1;
105 pa_hashmap_put(MAKE_HASHMAP(p), hdr->key, hdr);
110 const char *pa_headerlist_gets(pa_headerlist *p, const char *key) {
116 if (!(hdr = pa_hashmap_get(MAKE_HASHMAP(p), key)))
119 if (hdr->nbytes <= 0)
122 if (((char*) hdr->value)[hdr->nbytes-1] != 0)
125 if (strlen((char*) hdr->value) != hdr->nbytes-1)
128 return (char*) hdr->value;
131 int pa_headerlist_remove(pa_headerlist *p, const char *key) {
137 if (!(hdr = pa_hashmap_remove(MAKE_HASHMAP(p), key)))
144 const char *pa_headerlist_iterate(pa_headerlist *p, void **state) {
147 if (!(hdr = pa_hashmap_iterate(MAKE_HASHMAP(p), state, NULL)))
153 char *pa_headerlist_to_string(pa_headerlist *p) {
160 buf = pa_strbuf_new();
162 while ((key = pa_headerlist_iterate(p, &state))) {
166 if ((v = pa_headerlist_gets(p, key)))
167 pa_strbuf_printf(buf, "%s: %s\r\n", key, v);
170 return pa_strbuf_tostring_free(buf);
173 int pa_headerlist_contains(pa_headerlist *p, const char *key) {
177 if (!(pa_hashmap_get(MAKE_HASHMAP(p), key)))