]> code.delx.au - pulseaudio/blob - src/pulsecore/props.c
4a39f0fb989f91f8f10e0917be006a43c23a52f4
[pulseaudio] / src / pulsecore / props.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #include <assert.h>
25
26 #include <pulse/xmalloc.h>
27
28 #include <pulsecore/log.h>
29
30 #include "props.h"
31
32 typedef struct pa_property {
33 char *name; /* Points to memory allocated by the property subsystem */
34 void *data; /* Points to memory maintained by the caller */
35 } pa_property;
36
37 /* Allocate a new property object */
38 static pa_property* property_new(const char *name, void *data) {
39 pa_property* p;
40 assert(name && data);
41
42 p = pa_xmalloc(sizeof(pa_property));
43 p->name = pa_xstrdup(name);
44 p->data = data;
45
46 return p;
47 }
48
49 /* Free a property object */
50 static void property_free(pa_property *p) {
51 assert(p);
52
53 pa_xfree(p->name);
54 pa_xfree(p);
55 }
56
57 void* pa_property_get(pa_core *c, const char *name) {
58 pa_property *p;
59 assert(c && name && c->properties);
60
61 if (!(p = pa_hashmap_get(c->properties, name)))
62 return NULL;
63
64 return p->data;
65 }
66
67 int pa_property_set(pa_core *c, const char *name, void *data) {
68 pa_property *p;
69 assert(c && name && data && c->properties);
70
71 if (pa_hashmap_get(c->properties, name))
72 return -1;
73
74 p = property_new(name, data);
75 pa_hashmap_put(c->properties, p->name, p);
76 return 0;
77 }
78
79 int pa_property_remove(pa_core *c, const char *name) {
80 pa_property *p;
81 assert(c && name && c->properties);
82
83 if (!(p = pa_hashmap_remove(c->properties, name)))
84 return -1;
85
86 property_free(p);
87 return 0;
88 }
89
90 void pa_property_init(pa_core *c) {
91 assert(c);
92
93 c->properties = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
94 }
95
96 void pa_property_cleanup(pa_core *c) {
97 assert(c);
98
99 if (!c->properties)
100 return;
101
102 assert(!pa_hashmap_size(c->properties));
103
104 pa_hashmap_free(c->properties, NULL, NULL);
105 c->properties = NULL;
106
107 }
108
109 void pa_property_dump(pa_core *c, pa_strbuf *s) {
110 void *state = NULL;
111 pa_property *p;
112 assert(c && s);
113
114 while ((p = pa_hashmap_iterate(c->properties, &state, NULL)))
115 pa_strbuf_printf(s, "[%s] -> [%p]\n", p->name, p->data);
116 }
117
118 int pa_property_replace(pa_core *c, const char *name, void *data) {
119 assert(c && name);
120
121 pa_property_remove(c, name);
122 return pa_property_set(c, name, data);
123 }