myxce/src/lib/configobj.c
2026-03-04 18:18:53 +08:00

91 lines
1.8 KiB
C

#include <errno.h>
#include <rtti.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "configobj.h"
#include "mylock.h"
#include "utils.h"
#include "xce/types.h"
#include "xstring.h"
static size_t xconfig_datasize(va_list ap)
{
size_t sz = sizeof(xce_config_t);
return sz;
}
static int xconfig_init(meta_obj_t* obj, va_list ap)
{
xce_config_t* dst = RTTI_OBJ_TO_DATA(xce_config_t, obj);
xce_config_t* src = va_arg(ap, xce_config_t*);
if (src == NULL) {
return -EINVAL;
}
dst->name = makeString(src->name);
dst->value = makeString(src->value);
return 1;
}
static void xconfig_destroy(meta_obj_t* obj)
{
xce_config_t* c = RTTI_OBJ_TO_DATA(xce_config_t, obj);
dereference(c->name);
dereference(c->value);
}
static typeinfo_t xconfig_type = {
.name = "xce config item",
.privateSize = 0,
.dataSize = (datasize_fn_t)xconfig_datasize,
.init = (obj_init_fn_t)xconfig_init,
.destroy = (obj_destroy_fn_t)xconfig_destroy,
};
static xce_config_t* makeConfig_(var configmgr, xce_config_t* c)
{
xce_config_t* c1 = NULL;
int rc = 0;
rc = key_find(configmgr, (void*)c->name, (void**)&c1);
if ((rc < 0) && (rc != -ENOENT)) {
errno = -rc;
return NULL;
}
if (rc > 0) {
errno = EEXIST;
return NULL;
}
c1 = makeInstance(&xconfig_type, c);
if (c1 != NULL) {
rc = key_add(configmgr, (void*)c1->name, (void*)c1);
if (rc < 0) {
destroy(c1);
errno = -rc;
return NULL;
}
}
return c1;
}
xce_config_t* makeConfig(var configmgr, xce_config_t* c)
{
xce_config_t* c1 = NULL;
lock(configmgr, RTTI_LOCK_WRITE);
c1 = makeConfig_(configmgr, c);
unlock(configmgr);
return c1;
}