2022-07-31 17:15:32 +02:00
|
|
|
#include "val.h"
|
2018-03-07 10:30:40 +01:00
|
|
|
#include "cw.h"
|
|
|
|
|
|
|
|
#include "log.h"
|
2018-03-09 07:44:17 +01:00
|
|
|
#include "dbg.h"
|
2018-03-07 10:30:40 +01:00
|
|
|
|
2022-07-31 17:15:32 +02:00
|
|
|
cw_Val_t * cw_ktv_add(mavl_t kvtstore, const char *key, const struct cw_Type *type,
|
2018-04-25 10:43:27 +02:00
|
|
|
const void * valguard,
|
2018-03-07 10:30:40 +01:00
|
|
|
const uint8_t * data, int len)
|
|
|
|
{
|
2022-07-31 17:15:32 +02:00
|
|
|
cw_Val_t mdata, *mresult;
|
2018-03-11 00:56:41 +01:00
|
|
|
int exists;
|
|
|
|
mdata.key=cw_strdup(key);
|
2018-04-25 10:43:27 +02:00
|
|
|
mdata.valguard=valguard;
|
|
|
|
|
2018-03-11 00:56:41 +01:00
|
|
|
if (!mdata.key){
|
2018-03-19 17:26:01 +01:00
|
|
|
cw_log(LOG_ERR, "Can't allocate memory for KTV key %s: %s",
|
2018-03-07 10:30:40 +01:00
|
|
|
key,strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-04-25 10:43:27 +02:00
|
|
|
|
2018-03-07 10:30:40 +01:00
|
|
|
mresult = type->get(&mdata,data,len);
|
|
|
|
if (!mresult){
|
|
|
|
cw_log(LOG_ERR, "Can't create kvstore element for key %s of type %s: %s",
|
|
|
|
key,type->name, strerror(errno));
|
2018-03-11 00:56:41 +01:00
|
|
|
free(mdata.key);
|
2018-03-07 10:30:40 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-04-07 21:47:35 +02:00
|
|
|
|
|
|
|
mavl_del(kvtstore,&mdata);
|
2022-07-18 01:15:17 +02:00
|
|
|
mresult = mavl_insert(kvtstore, &mdata, &exists);
|
2018-03-11 00:56:41 +01:00
|
|
|
if (exists){
|
2018-03-07 10:30:40 +01:00
|
|
|
cw_log(LOG_ERR, "Element already exists %s", key);
|
|
|
|
/* element already exists */
|
2018-03-11 00:56:41 +01:00
|
|
|
free(mdata.key);
|
|
|
|
if (type->del)
|
|
|
|
type->del(&mdata);
|
2018-03-19 17:26:01 +01:00
|
|
|
return mresult;
|
2018-03-07 10:30:40 +01:00
|
|
|
}
|
2018-03-09 07:44:17 +01:00
|
|
|
|
2018-03-19 17:26:01 +01:00
|
|
|
return mresult;
|
2018-03-07 10:30:40 +01:00
|
|
|
}
|