]> code.delx.au - pulseaudio/blob - polyp/props.c
Make the whole stuff LGPL only
[pulseaudio] / polyp / props.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 #include <assert.h>
23
24 #include "xmalloc.h"
25 #include "props.h"
26 #include "log.h"
27
28 struct pa_property {
29 char *name; /* Points to memory allocated by the property subsystem */
30 void *data; /* Points to memory maintained by the caller */
31 };
32
33 /* Allocate a new property object */
34 static struct pa_property* property_new(const char *name, void *data) {
35 struct pa_property* p;
36 assert(name && data);
37
38 p = pa_xmalloc(sizeof(struct pa_property));
39 p->name = pa_xstrdup(name);
40 p->data = data;
41
42 return p;
43 }
44
45 /* Free a property object */
46 static void property_free(struct pa_property *p) {
47 assert(p);
48
49 pa_xfree(p->name);
50 pa_xfree(p);
51 }
52
53 void* pa_property_get(struct pa_core *c, const char *name) {
54 struct pa_property *p;
55 assert(c && name && c->properties);
56
57 if (!(p = pa_hashmap_get(c->properties, name)))
58 return NULL;
59
60 return p->data;
61 }
62
63 int pa_property_set(struct pa_core *c, const char *name, void *data) {
64 struct pa_property *p;
65 assert(c && name && data && c->properties);
66
67 if (pa_hashmap_get(c->properties, name))
68 return -1;
69
70 p = property_new(name, data);
71 pa_hashmap_put(c->properties, p->name, p);
72 return 0;
73 }
74
75 int pa_property_remove(struct pa_core *c, const char *name) {
76 struct pa_property *p;
77 assert(c && name && c->properties);
78
79 if (!(p = pa_hashmap_remove(c->properties, name)))
80 return -1;
81
82 property_free(p);
83 return 0;
84 }
85
86 void pa_property_init(struct pa_core *c) {
87 assert(c);
88
89 c->properties = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
90 }
91
92 void pa_property_cleanup(struct pa_core *c) {
93 assert(c);
94
95 if (!c->properties)
96 return;
97
98 assert(!pa_hashmap_ncontents(c->properties));
99
100 pa_hashmap_free(c->properties, NULL, NULL);
101 c->properties = NULL;
102
103 }
104
105 void pa_property_dump(struct pa_core *c, struct pa_strbuf *s) {
106 void *state = NULL;
107 struct pa_property *p;
108 assert(c && s);
109
110 while ((p = pa_hashmap_iterate(c->properties, &state, NULL)))
111 pa_strbuf_printf(s, "[%s] -> [%p]\n", p->name, p->data);
112 }
113
114 int pa_property_replace(struct pa_core *c, const char *name, void *data) {
115 assert(c && name);
116
117 pa_property_remove(c, name);
118 return pa_property_set(c, name, data);
119 }