cw_kvstore_add works now
fixed a bug in mavl_add FossilOrigin-Name: 4b040305182ef40f6f053fe1d84ea15f533e931c9d665b513b9a644592519f3e
This commit is contained in:
parent
2055a0d644
commit
c93867811a
@ -322,6 +322,10 @@
|
|||||||
<File Name="src/cw/cw_dbg_elem.c"/>
|
<File Name="src/cw/cw_dbg_elem.c"/>
|
||||||
<File Name="src/cw/cw_kvstore_mavl_delete.c"/>
|
<File Name="src/cw/cw_kvstore_mavl_delete.c"/>
|
||||||
<File Name="src/cw/cw_format_version.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>
|
||||||
</VirtualDirectory>
|
</VirtualDirectory>
|
||||||
<Description/>
|
<Description/>
|
||||||
|
@ -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
38
src/cw/cw_kvstore_add.c
Normal 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;
|
||||||
|
}
|
@ -20,6 +20,7 @@
|
|||||||
#include "cw_types.h"
|
#include "cw_types.h"
|
||||||
#include "dbg.h"
|
#include "dbg.h"
|
||||||
#include "keys.h"
|
#include "keys.h"
|
||||||
|
#include "kvstore.h"
|
||||||
|
|
||||||
int cw_read_descriptor_subelems(mavl_t cfg, const char * parent_key,
|
int cw_read_descriptor_subelems(mavl_t cfg, const char * parent_key,
|
||||||
uint8_t * data, int len,
|
uint8_t * data, int len,
|
||||||
@ -54,7 +55,7 @@ int cw_read_descriptor_subelems(mavl_t cfg, const char * parent_key,
|
|||||||
errors++;
|
errors++;
|
||||||
} else {
|
} else {
|
||||||
int l = sublen;
|
int l = sublen;
|
||||||
mavldata_t mdata, *mdata_result;
|
|
||||||
char dbgstr[1048];
|
char dbgstr[1048];
|
||||||
char key[1024];
|
char key[1024];
|
||||||
|
|
||||||
@ -68,20 +69,13 @@ int cw_read_descriptor_subelems(mavl_t cfg, const char * parent_key,
|
|||||||
|
|
||||||
/* vendor */
|
/* vendor */
|
||||||
sprintf(key,"%s/%s/%s",parent_key,elems[i].key,CW_KEY_VENDOR);
|
sprintf(key,"%s/%s/%s",parent_key,elems[i].key,CW_KEY_VENDOR);
|
||||||
mdata.kv.key = strdup(key);
|
cw_kvstore_add(cfg,key,CW_TYPE_DWORD,data + sub,4);
|
||||||
mdata.kv.val.dword = vendor_id;
|
|
||||||
mdata.kv.priv=CW_TYPE_DWORD;
|
|
||||||
mavl_add(cfg,&mdata);
|
|
||||||
|
|
||||||
|
|
||||||
/* version */
|
/* version */
|
||||||
sprintf(key,"%s/%s/%s",parent_key,elems[i].key,CW_KEY_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);
|
cw_kvstore_add(cfg,key,CW_TYPE_VERSION,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, "%s", key);
|
||||||
sprintf(dbgstr, "Storing '%s'", elems[i].key);
|
|
||||||
cw_dbg_version_subelem(DBG_SUBELEM, dbgstr, subtype, vendor_id, data+sub+8,l);
|
cw_dbg_version_subelem(DBG_SUBELEM, dbgstr, subtype, vendor_id, data+sub+8,l);
|
||||||
success++;
|
success++;
|
||||||
}
|
}
|
||||||
|
10
src/cw/cw_strdup.c
Normal file
10
src/cw/cw_strdup.c
Normal 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;
|
||||||
|
}
|
@ -46,10 +46,10 @@ static mavldata_t *from_str(mavldata_t * data, const char *src)
|
|||||||
|
|
||||||
|
|
||||||
const struct cw_Type cw_type_byte = {
|
const struct cw_Type cw_type_byte = {
|
||||||
"Byte", /* name */
|
"Byte", /* name */
|
||||||
NULL, /* del */
|
cw_types_del_null, /* del */
|
||||||
put, /* put */
|
put, /* put */
|
||||||
get, /* get */
|
get, /* get */
|
||||||
to_str, /* to_str */
|
to_str, /* to_str */
|
||||||
from_str /* from_str */
|
from_str /* from_str */
|
||||||
};
|
};
|
||||||
|
@ -23,7 +23,7 @@
|
|||||||
|
|
||||||
static mavldata_t *get(mavldata_t * data, const uint8_t * src, int len)
|
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);
|
data->kv.val.dword = cw_get_dword(src);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
@ -45,11 +45,11 @@ static mavldata_t *from_str(mavldata_t * data, const char *src)
|
|||||||
}
|
}
|
||||||
|
|
||||||
const struct cw_Type cw_type_dword = {
|
const struct cw_Type cw_type_dword = {
|
||||||
"Dword", /* name */
|
"Dword", /* name */
|
||||||
NULL, /* del */
|
cw_types_del_null, /* del */
|
||||||
put, /* put */
|
put, /* put */
|
||||||
get, /* get */
|
get, /* get */
|
||||||
to_str, /* to_str */
|
to_str, /* to_str */
|
||||||
from_str /* from_str */
|
from_str /* from_str */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -42,4 +42,6 @@ extern const struct cw_Type cw_type_version;
|
|||||||
#define CW_TYPE_DWORD (&cw_type_dword)
|
#define CW_TYPE_DWORD (&cw_type_dword)
|
||||||
#define CW_TYPE_VERSION (&cw_type_version)
|
#define CW_TYPE_VERSION (&cw_type_version)
|
||||||
|
|
||||||
|
void cw_types_del_null(mavldata_t *data);
|
||||||
|
|
||||||
#endif /* __CW_TYPES_H */
|
#endif /* __CW_TYPES_H */
|
||||||
|
6
src/cw/cw_types_del_null.c
Normal file
6
src/cw/cw_types_del_null.c
Normal 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
15
src/cw/kvstore.h
Normal 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 */
|
@ -121,7 +121,7 @@ void mavlnode_destroy(struct mavl *t, struct mavlnode *n);
|
|||||||
|
|
||||||
void mavl_del_all(struct mavl *t);
|
void mavl_del_all(struct mavl *t);
|
||||||
union mavldata *mavl_del(struct mavl *t, union mavldata *data);
|
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);
|
union mavldata * mavl_get(struct mavl *t ,union mavldata *data);
|
||||||
struct mavlnode *mavl_get_node(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);
|
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)))
|
while(NULL != (val = mavliter_next(iter)))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the number of elements stored in a mavl object
|
* Get the number of elements stored in a mavl object
|
||||||
* @param m mavl object
|
* @param m mavl object
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
#include "mavl.h"
|
#include "mavl.h"
|
||||||
|
|
||||||
|
|
||||||
static struct mavlnode *mavlnode_create(union mavldata *data)
|
static struct mavlnode *mavlnode_create(union mavldata *data)
|
||||||
{
|
{
|
||||||
struct mavlnode *n = malloc(sizeof(struct mavlnode));
|
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 *tmp;
|
||||||
|
|
||||||
struct mavlnode *n = *parent;
|
struct mavlnode *n = *parent;
|
||||||
int rc = t->cmp(data, &n->data);
|
int rc = t->cmp(*data, &n->data);
|
||||||
|
|
||||||
int bal;
|
int bal;
|
||||||
if (rc == 0) {
|
if (rc == 0) {
|
||||||
*data = n->data;
|
*data = &n->data;
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,7 +77,7 @@ static int mavl_add0(struct mavl *t, struct mavlnode **parent, union mavldata *
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* n->left is 0 */
|
/* n->left is 0 */
|
||||||
n->left = mavlnode_create(data);
|
n->left = mavlnode_create(*data);
|
||||||
if (!n->left)
|
if (!n->left)
|
||||||
return 3;
|
return 3;
|
||||||
|
|
||||||
@ -141,7 +140,7 @@ static int mavl_add0(struct mavl *t, struct mavlnode **parent, union mavldata *
|
|||||||
|
|
||||||
/* n->right is 0 */
|
/* n->right is 0 */
|
||||||
|
|
||||||
n->right = mavlnode_create(data);
|
n->right = mavlnode_create(*data);
|
||||||
if (!n->right)
|
if (!n->right)
|
||||||
return 3;
|
return 3;
|
||||||
|
|
||||||
@ -170,7 +169,7 @@ static int mavl_add0(struct mavl *t, struct mavlnode **parent, union mavldata *
|
|||||||
* @example mavl_add_example.c
|
* @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;
|
union mavldata * d;
|
||||||
int rc;
|
int rc;
|
||||||
@ -184,7 +183,7 @@ union mavldata *mavl_add(struct mavl *t, union mavldata *data)
|
|||||||
|
|
||||||
d = data;
|
d = data;
|
||||||
|
|
||||||
rc = mavl_add0(t, &t->root, d);
|
rc = mavl_add0(t, &t->root, &d);
|
||||||
|
|
||||||
if (rc > 3)
|
if (rc > 3)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -42,11 +42,12 @@
|
|||||||
int sock_receive(int sock, void *buf, size_t len, int flags,
|
int sock_receive(int sock, void *buf, size_t len, int flags,
|
||||||
struct sockaddr *srcaddr, socklen_t * srcaddrlen)
|
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);
|
*srcaddrlen=sizeof(struct sockaddr);
|
||||||
memset(srcaddr, 0, sizeof(struct sockaddr));
|
memset(srcaddr, 0, sizeof(struct sockaddr));
|
||||||
|
|
||||||
int n;
|
|
||||||
while ((n = recvfrom(sock, (char *) buf, len, flags, srcaddr, srcaddrlen)) < 0) {
|
while ((n = recvfrom(sock, (char *) buf, len, flags, srcaddr, srcaddrlen)) < 0) {
|
||||||
if (errno != EINTR)
|
if (errno != EINTR)
|
||||||
return n;
|
return n;
|
||||||
|
Loading…
Reference in New Issue
Block a user