2004-06-29 20:37:24 +00:00
|
|
|
#include <stdlib.h>
|
2004-06-27 22:42:17 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <malloc.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
#include "namereg.h"
|
|
|
|
|
|
|
|
|
|
struct namereg_entry {
|
2004-07-03 23:35:12 +00:00
|
|
|
enum pa_namereg_type type;
|
2004-06-27 22:42:17 +00:00
|
|
|
char *name;
|
|
|
|
|
void *data;
|
|
|
|
|
};
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
void pa_namereg_free(struct pa_core *c) {
|
2004-06-27 22:42:17 +00:00
|
|
|
assert(c);
|
|
|
|
|
if (!c->namereg)
|
|
|
|
|
return;
|
2004-07-03 23:35:12 +00:00
|
|
|
assert(pa_hashset_ncontents(c->namereg) == 0);
|
|
|
|
|
pa_hashset_free(c->namereg, NULL, NULL);
|
2004-06-27 22:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
const char *pa_namereg_register(struct pa_core *c, const char *name, enum pa_namereg_type type, void *data, int fail) {
|
2004-06-27 22:42:17 +00:00
|
|
|
struct namereg_entry *e;
|
|
|
|
|
char *n = NULL;
|
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
assert(c && name && data);
|
|
|
|
|
|
|
|
|
|
if (!c->namereg) {
|
2004-07-03 23:35:12 +00:00
|
|
|
c->namereg = pa_hashset_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
|
2004-06-27 22:42:17 +00:00
|
|
|
assert(c->namereg);
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
if ((e = pa_hashset_get(c->namereg, name)) && fail)
|
2004-06-27 22:42:17 +00:00
|
|
|
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);
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
if (!(e = pa_hashset_get(c->namereg, n)))
|
2004-06-27 22:42:17 +00:00
|
|
|
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;
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
r = pa_hashset_put(c->namereg, e->name, e);
|
2004-06-27 22:42:17 +00:00
|
|
|
assert (r >= 0);
|
|
|
|
|
|
|
|
|
|
return e->name;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
void pa_namereg_unregister(struct pa_core *c, const char *name) {
|
2004-06-27 22:42:17 +00:00
|
|
|
struct namereg_entry *e;
|
|
|
|
|
int r;
|
|
|
|
|
assert(c && name);
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
e = pa_hashset_get(c->namereg, name);
|
2004-06-27 22:42:17 +00:00
|
|
|
assert(e);
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
r = pa_hashset_remove(c->namereg, name);
|
2004-06-27 22:42:17 +00:00
|
|
|
assert(r >= 0);
|
|
|
|
|
|
|
|
|
|
free(e->name);
|
|
|
|
|
free(e);
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
void* pa_namereg_get(struct pa_core *c, const char *name, enum pa_namereg_type type) {
|
2004-06-27 22:42:17 +00:00
|
|
|
struct namereg_entry *e;
|
2004-06-29 20:37:24 +00:00
|
|
|
uint32_t index;
|
|
|
|
|
char *x = NULL;
|
|
|
|
|
void *d = NULL;
|
2004-06-27 22:42:17 +00:00
|
|
|
assert(c && name);
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
if ((e = pa_hashset_get(c->namereg, name)))
|
2004-06-27 22:42:17 +00:00
|
|
|
if (e->type == e->type)
|
|
|
|
|
return e->data;
|
|
|
|
|
|
2004-06-29 20:37:24 +00:00
|
|
|
index = (uint32_t) strtol(name, &x, 0);
|
|
|
|
|
|
|
|
|
|
if (!x || *x != 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
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);
|
2004-06-29 20:37:24 +00:00
|
|
|
|
|
|
|
|
return d;
|
2004-06-27 22:42:17 +00:00
|
|
|
}
|