1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <string.h>
#include <malloc.h>
#include <stdio.h>
#include "namereg.h"
struct namereg_entry {
enum pa_namereg_type type;
char *name;
void *data;
};
void pa_namereg_free(struct pa_core *c) {
assert(c);
if (!c->namereg)
return;
assert(pa_hashmap_ncontents(c->namereg) == 0);
pa_hashmap_free(c->namereg, NULL, NULL);
}
const char *pa_namereg_register(struct pa_core *c, const char *name, enum pa_namereg_type type, void *data, int fail) {
struct namereg_entry *e;
char *n = NULL;
int r;
assert(c && name && data);
if (!c->namereg) {
c->namereg = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
assert(c->namereg);
}
if ((e = pa_hashmap_get(c->namereg, name)) && fail)
return NULL;
if (!e)
n = strdup(name);
else {
unsigned i;
size_t l = strlen(name);
n = malloc(l+3);
assert(n);
for (i = 1; i <= 99; i++) {
snprintf(n, l+2, "%s%u", name, i);
if (!(e = pa_hashmap_get(c->namereg, n)))
break;
}
if (e) {
free(n);
return NULL;
}
}
assert(n);
e = malloc(sizeof(struct namereg_entry));
assert(e);
e->type = type;
e->name = n;
e->data = data;
r = pa_hashmap_put(c->namereg, e->name, e);
assert (r >= 0);
return e->name;
}
void pa_namereg_unregister(struct pa_core *c, const char *name) {
struct namereg_entry *e;
int r;
assert(c && name);
e = pa_hashmap_get(c->namereg, name);
assert(e);
r = pa_hashmap_remove(c->namereg, name);
assert(r >= 0);
free(e->name);
free(e);
}
void* pa_namereg_get(struct pa_core *c, const char *name, enum pa_namereg_type type) {
struct namereg_entry *e;
uint32_t index;
char *x = NULL;
void *d = NULL;
assert(c && name);
if ((e = pa_hashmap_get(c->namereg, name)))
if (e->type == e->type)
return e->data;
index = (uint32_t) strtol(name, &x, 0);
if (!x || *x != 0)
return NULL;
if (type == PA_NAMEREG_SINK)
d = pa_idxset_get_by_index(c->sinks, index);
else if (type == PA_NAMEREG_SOURCE)
d = pa_idxset_get_by_index(c->sources, index);
return d;
}
|