summaryrefslogtreecommitdiff
path: root/src/core/registry.c
blob: 107cc66c60d97be3c2d8dca520c43d7c78c73648 (plain)
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
#include "hybbx/registry.h"
#include "hybbx/limits.h"

#include <string.h>

typedef struct registry_entry {
    const hybbx_transport_plugin_t *plugin;
} registry_entry_t;

static registry_entry_t g_registry[HYBBX_MAX_PLUGINS];
static size_t g_registry_count = 0;

hybbx_result_t hybbx_registry_register(const hybbx_transport_plugin_t *plugin)
{
    if (plugin == NULL || plugin->name == NULL) {
        return HYBBX_ERR_INVALID;
    }

    if (hybbx_registry_find(plugin->name) != NULL) {
        return HYBBX_ERR_BUSY;
    }

    if (g_registry_count >= HYBBX_MAX_PLUGINS) {
        return HYBBX_ERR_NOMEM;
    }

    g_registry[g_registry_count].plugin = plugin;
    g_registry_count++;
    return HYBBX_OK;
}

hybbx_result_t hybbx_registry_unregister(const char *name)
{
    size_t i;

    if (name == NULL) {
        return HYBBX_ERR_INVALID;
    }

    for (i = 0; i < g_registry_count; i++) {
        if (strcmp(g_registry[i].plugin->name, name) == 0) {
            size_t remaining = g_registry_count - i - 1;
            if (remaining > 0) {
                memmove(&g_registry[i], &g_registry[i + 1],
                        remaining * sizeof(registry_entry_t));
            }
            g_registry_count--;
            return HYBBX_OK;
        }
    }

    return HYBBX_ERR_NOT_FOUND;
}

const hybbx_transport_plugin_t *hybbx_registry_find(const char *name)
{
    size_t i;

    if (name == NULL) {
        return NULL;
    }

    for (i = 0; i < g_registry_count; i++) {
        if (strcmp(g_registry[i].plugin->name, name) == 0) {
            return g_registry[i].plugin;
        }
    }

    return NULL;
}

void hybbx_registry_foreach(hybbx_registry_iter_fn fn, void *userdata)
{
    size_t i;

    if (fn == NULL) {
        return;
    }

    for (i = 0; i < g_registry_count; i++) {
        fn(g_registry[i].plugin, userdata);
    }
}
git clone -b <branch> https://cgit.mode42.com/<repo>.git
git clone -b <branch> git://cgit.mode42.com/<repo>.git

info@mode42.com