cw_kvstore_add works now

fixed a bug in mavl_add

FossilOrigin-Name: 4b040305182ef40f6f053fe1d84ea15f533e931c9d665b513b9a644592519f3e
This commit is contained in:
7u83@mail.ru 2018-03-07 09:30:40 +00:00
parent 2055a0d644
commit c93867811a
13 changed files with 108 additions and 38 deletions

View File

@ -322,6 +322,10 @@
<File Name="src/cw/cw_dbg_elem.c"/>
<File Name="src/cw/cw_kvstore_mavl_delete.c"/>
<File Name="src/cw/cw_format_version.c"/>
<File Name="src/cw/cw_strdup.c"/>
<File Name="src/cw/kvstore.h"/>
<File Name="src/cw/cw_kvstore_add.c"/>
<File Name="src/cw/cw_types_del_null.c"/>
</VirtualDirectory>
</VirtualDirectory>
<Description/>

View File

@ -469,7 +469,7 @@ extern cw_iplist_t cw_aciplist_create();
*/
char *cw_strdup(const char *s);
/**

38
src/cw/cw_kvstore_add.c Normal file
View File

@ -0,0 +1,38 @@
#include "kvstore.h"
#include "cw_types.h"
#include "cw.h"
#include "log.h"
const char * cw_kvstore_add(mavl_t kvstore, const char *key, const struct cw_Type *type,
const uint8_t * data, int len)
{
mavldata_t mdata, *mresult;
mdata.kv.key=cw_strdup(key);
if (!mdata.kv.key){
cw_log(LOG_ERR, "Can't allocate memory for key %s: %s",
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));
free(mdata.kv.key);
return NULL;
}
mresult = mavl_add(kvstore, &mdata);
if (mresult != &mdata){
cw_log(LOG_ERR, "Element already exists %s", key);
/* element already exists */
free(mdata.kv.key);
type->del(mresult);
return key;
}
return mdata.kv.key;
}

View File

@ -20,6 +20,7 @@
#include "cw_types.h"
#include "dbg.h"
#include "keys.h"
#include "kvstore.h"
int cw_read_descriptor_subelems(mavl_t cfg, const char * parent_key,
uint8_t * data, int len,
@ -54,7 +55,7 @@ int cw_read_descriptor_subelems(mavl_t cfg, const char * parent_key,
errors++;
} else {
int l = sublen;
mavldata_t mdata, *mdata_result;
char dbgstr[1048];
char key[1024];
@ -68,20 +69,13 @@ int cw_read_descriptor_subelems(mavl_t cfg, const char * parent_key,
/* vendor */
sprintf(key,"%s/%s/%s",parent_key,elems[i].key,CW_KEY_VENDOR);
mdata.kv.key = strdup(key);
mdata.kv.val.dword = vendor_id;
mdata.kv.priv=CW_TYPE_DWORD;
mavl_add(cfg,&mdata);
cw_kvstore_add(cfg,key,CW_TYPE_DWORD,data + sub,4);
/* version */
sprintf(key,"%s/%s/%s",parent_key,elems[i].key,CW_KEY_VERSION);
mdata_result = cw_type_version.get(&mdata,data+sub+8,l);
mdata.kv.key = strdup(key);
mavl_add(cfg,mdata_result);
cw_type_version.to_str(mdata_result,dbgstr,90);
sprintf(dbgstr, "Storing '%s'", elems[i].key);
cw_kvstore_add(cfg,key,CW_TYPE_VERSION,data+sub+8,l);
sprintf(dbgstr, "%s", key);
cw_dbg_version_subelem(DBG_SUBELEM, dbgstr, subtype, vendor_id, data+sub+8,l);
success++;
}

10
src/cw/cw_strdup.c Normal file
View File

@ -0,0 +1,10 @@
#include <string.h>
#include <stdlib.h>
char *cw_strdup(const char *s) {
size_t size = strlen(s) + 1;
char *p = malloc(size);
if (p)
memcpy(p, s, size);
return p;
}

View File

@ -46,10 +46,10 @@ static mavldata_t *from_str(mavldata_t * data, const char *src)
const struct cw_Type cw_type_byte = {
"Byte", /* name */
NULL, /* del */
put, /* put */
get, /* get */
to_str, /* to_str */
from_str /* from_str */
"Byte", /* name */
cw_types_del_null, /* del */
put, /* put */
get, /* get */
to_str, /* to_str */
from_str /* from_str */
};

View File

@ -23,7 +23,7 @@
static mavldata_t *get(mavldata_t * data, const uint8_t * src, int len)
{
data->kv.priv = &cw_type_byte;
data->kv.priv = &cw_type_dword;
data->kv.val.dword = cw_get_dword(src);
return data;
}
@ -45,11 +45,11 @@ static mavldata_t *from_str(mavldata_t * data, const char *src)
}
const struct cw_Type cw_type_dword = {
"Dword", /* name */
NULL, /* del */
put, /* put */
get, /* get */
to_str, /* to_str */
from_str /* from_str */
"Dword", /* name */
cw_types_del_null, /* del */
put, /* put */
get, /* get */
to_str, /* to_str */
from_str /* from_str */
};

View File

@ -42,4 +42,6 @@ extern const struct cw_Type cw_type_version;
#define CW_TYPE_DWORD (&cw_type_dword)
#define CW_TYPE_VERSION (&cw_type_version)
void cw_types_del_null(mavldata_t *data);
#endif /* __CW_TYPES_H */

View File

@ -0,0 +1,6 @@
#include "mavl.h"
#include "cw_types.h"
void cw_types_del_null(mavldata_t *data){
return;
}

15
src/cw/kvstore.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef __CW_KVSTORE_H
#define __CW_KVSTORE_H
#include "mavl.h"
#include "cw_types.h"
void cw_kvstore_mavl_delete(mavldata_t *data);
const char * cw_kvstore_add(mavl_t kvstore, const char *key, const struct cw_Type *type,
const uint8_t * data, int len);
#define cw_kvstore_create()\
mavl_create(mavl_cmp_kv, cw_kvstore_mavl_delete)
#endif /* __CW_KVSTORE_H */

View File

@ -121,7 +121,7 @@ void mavlnode_destroy(struct mavl *t, struct mavlnode *n);
void mavl_del_all(struct mavl *t);
union mavldata *mavl_del(struct mavl *t, union mavldata *data);
union mavldata *mavl_add(struct mavl *t, union mavldata *data);
union mavldata *mavl_add(struct mavl *t, const union mavldata *data);
union mavldata * mavl_get(struct mavl *t ,union mavldata *data);
struct mavlnode *mavl_get_node(struct mavl *t, union mavldata *data);
void * mavl_get_ptr(mavl_t tree, void * search);
@ -228,6 +228,7 @@ extern union mavldata * mavliter_seek(mavliter_t *i,void *d);
while(NULL != (val = mavliter_next(iter)))
/**
* Get the number of elements stored in a mavl object
* @param m mavl object

View File

@ -1,6 +1,5 @@
#include "mavl.h"
static struct mavlnode *mavlnode_create(union mavldata *data)
{
struct mavlnode *n = malloc(sizeof(struct mavlnode));
@ -16,16 +15,16 @@ static struct mavlnode *mavlnode_create(union mavldata *data)
static int mavl_add0(struct mavl *t, struct mavlnode **parent, union mavldata * data)
static int mavl_add0(struct mavl *t, struct mavlnode **parent, union mavldata ** data)
{
struct mavlnode *tmp;
struct mavlnode *n = *parent;
int rc = t->cmp(data, &n->data);
int rc = t->cmp(*data, &n->data);
int bal;
if (rc == 0) {
*data = n->data;
*data = &n->data;
return 2;
}
@ -78,7 +77,7 @@ static int mavl_add0(struct mavl *t, struct mavlnode **parent, union mavldata *
}
/* n->left is 0 */
n->left = mavlnode_create(data);
n->left = mavlnode_create(*data);
if (!n->left)
return 3;
@ -141,7 +140,7 @@ static int mavl_add0(struct mavl *t, struct mavlnode **parent, union mavldata *
/* n->right is 0 */
n->right = mavlnode_create(data);
n->right = mavlnode_create(*data);
if (!n->right)
return 3;
@ -170,7 +169,7 @@ static int mavl_add0(struct mavl *t, struct mavlnode **parent, union mavldata *
* @example mavl_add_example.c
*/
union mavldata *mavl_add(struct mavl *t, union mavldata *data)
union mavldata *mavl_add(struct mavl *t, const union mavldata *data)
{
union mavldata * d;
int rc;
@ -184,7 +183,7 @@ union mavldata *mavl_add(struct mavl *t, union mavldata *data)
d = data;
rc = mavl_add0(t, &t->root, d);
rc = mavl_add0(t, &t->root, &d);
if (rc > 3)
return NULL;

View File

@ -42,11 +42,12 @@
int sock_receive(int sock, void *buf, size_t len, int flags,
struct sockaddr *srcaddr, socklen_t * srcaddrlen)
{
// socklen_t al = sizeof(struct sockaddr_storage);
int n;
/* socklen_t al = sizeof(struct sockaddr_storage); */
*srcaddrlen=sizeof(struct sockaddr);
memset(srcaddr, 0, sizeof(struct sockaddr));
int n;
while ((n = recvfrom(sock, (char *) buf, len, flags, srcaddr, srcaddrlen)) < 0) {
if (errno != EINTR)
return n;