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