]> code.delx.au - pulseaudio/blob - polyp/namereg.c
rename src to polyp
[pulseaudio] / polyp / namereg.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 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 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 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <string.h>
30 #include <malloc.h>
31 #include <stdio.h>
32
33 #include "namereg.h"
34
35 struct namereg_entry {
36 enum pa_namereg_type type;
37 char *name;
38 void *data;
39 };
40
41 void pa_namereg_free(struct pa_core *c) {
42 assert(c);
43 if (!c->namereg)
44 return;
45 assert(pa_hashmap_ncontents(c->namereg) == 0);
46 pa_hashmap_free(c->namereg, NULL, NULL);
47 }
48
49 const char *pa_namereg_register(struct pa_core *c, const char *name, enum pa_namereg_type type, void *data, int fail) {
50 struct namereg_entry *e;
51 char *n = NULL;
52 int r;
53
54 assert(c && name && data);
55
56 if (!c->namereg) {
57 c->namereg = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
58 assert(c->namereg);
59 }
60
61 if ((e = pa_hashmap_get(c->namereg, name)) && fail)
62 return NULL;
63
64 if (!e)
65 n = strdup(name);
66 else {
67 unsigned i;
68 size_t l = strlen(name);
69 n = malloc(l+3);
70 assert(n);
71
72 for (i = 1; i <= 99; i++) {
73 snprintf(n, l+2, "%s%u", name, i);
74
75 if (!(e = pa_hashmap_get(c->namereg, n)))
76 break;
77 }
78
79 if (e) {
80 free(n);
81 return NULL;
82 }
83 }
84
85 assert(n);
86 e = malloc(sizeof(struct namereg_entry));
87 assert(e);
88 e->type = type;
89 e->name = n;
90 e->data = data;
91
92 r = pa_hashmap_put(c->namereg, e->name, e);
93 assert (r >= 0);
94
95 return e->name;
96
97 }
98
99 void pa_namereg_unregister(struct pa_core *c, const char *name) {
100 struct namereg_entry *e;
101 int r;
102 assert(c && name);
103
104 e = pa_hashmap_get(c->namereg, name);
105 assert(e);
106
107 r = pa_hashmap_remove(c->namereg, name);
108 assert(r >= 0);
109
110 free(e->name);
111 free(e);
112 }
113
114 void* pa_namereg_get(struct pa_core *c, const char *name, enum pa_namereg_type type) {
115 struct namereg_entry *e;
116 uint32_t index;
117 char *x = NULL;
118 void *d = NULL;
119 assert(c && name);
120
121 if ((e = pa_hashmap_get(c->namereg, name)))
122 if (e->type == e->type)
123 return e->data;
124
125 index = (uint32_t) strtol(name, &x, 0);
126
127 if (!x || *x != 0)
128 return NULL;
129
130 if (type == PA_NAMEREG_SINK)
131 d = pa_idxset_get_by_index(c->sinks, index);
132 else if (type == PA_NAMEREG_SOURCE)
133 d = pa_idxset_get_by_index(c->sources, index);
134
135 return d;
136 }