2018-03-12 11:22:06 +01:00
|
|
|
#include "ktv.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
|
|
|
|
2018-03-19 17:26:01 +01:00
|
|
|
cw_KTV_t * cw_ktv_add(mavl_t kvtstore, const char *key, const struct cw_Type *type,
|
2018-03-07 10:30:40 +01:00
|
|
|
const uint8_t * data, int len)
|
|
|
|
{
|
2018-03-12 11:22:06 +01:00
|
|
|
cw_KTV_t mdata, *mresult;
|
2018-03-11 00:56:41 +01:00
|
|
|
int exists;
|
2018-03-09 07:44:17 +01:00
|
|
|
|
|
|
|
/* cw_dbg(DBG_ELEM,"KVStore (%p,%d) add elem (%s): %s", kvstore, kvstore->count,
|
|
|
|
type->name, key );
|
|
|
|
*/
|
2018-03-11 00:56:41 +01:00
|
|
|
mdata.key=cw_strdup(key);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-11 00:56:41 +01:00
|
|
|
mresult = mavl_add(kvtstore, &mdata, &exists);
|
|
|
|
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
|
|
|
}
|