56 lines
1.4 KiB
C
56 lines
1.4 KiB
C
#include "Registry.h"
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
// Internal helper to ensure capacity
|
|
static int registry_ensure_capacity(Registry* r) {
|
|
if (r->entries == NULL) {
|
|
size_t cap = 4;
|
|
r->entries = (RegistryEntry*)calloc(cap, sizeof(RegistryEntry));
|
|
if (!r->entries) return -1;
|
|
r->capacity = cap;
|
|
r->count = 0;
|
|
return 0;
|
|
}
|
|
if (r->count < r->capacity) return 0;
|
|
size_t newcap = r->capacity * 2;
|
|
RegistryEntry* tmp = (RegistryEntry*)realloc(r->entries, newcap * sizeof(RegistryEntry));
|
|
if (!tmp) return -1;
|
|
// Initialize new slots to zero
|
|
for (size_t i = r->capacity; i < newcap; ++i) {
|
|
tmp[i].key = NULL;
|
|
tmp[i].value = NULL;
|
|
tmp[i].version = 0;
|
|
}
|
|
r->entries = tmp;
|
|
r->capacity = newcap;
|
|
return 0;
|
|
}
|
|
|
|
void registry_init(Registry* r) {
|
|
r->entries = NULL;
|
|
r->count = 0;
|
|
r->capacity = 0;
|
|
// lazy initialization on first register
|
|
}
|
|
|
|
int registry_register(Registry* r, const char* key, const char* value, int version) {
|
|
if (registry_ensure_capacity(r) != 0) return -1;
|
|
RegistryEntry e;
|
|
e.key = key;
|
|
e.value = value;
|
|
e.version = version;
|
|
r->entries[r->count++] = e;
|
|
return 0;
|
|
}
|
|
|
|
const char* registry_lookup(Registry* r, const char* key) {
|
|
if (!r || !r->entries) return NULL;
|
|
for (size_t i = 0; i < r->count; ++i) {
|
|
if (r->entries[i].key && strcmp(r->entries[i].key, key) == 0) {
|
|
return r->entries[i].value;
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|