Almost all ktv stuff deleted

This commit is contained in:
2022-08-19 22:23:55 +02:00
parent 14354039e1
commit 6dc8d80102
51 changed files with 658 additions and 1880 deletions

View File

@ -79,36 +79,43 @@ CWSRC=\
# cw_process_element.c\
KTVSRC=\
cw_ktv_add.c\
cw_ktv_cast.c\
cw_ktv_replace.c\
cfg.c\
# cw_ktv_add.c\
cw_ktv_idx_get.c\
cw_ktv_mavlcmp.c\
cw_ktv_mavlcmp_type_by_name.c\
cw_ktv_mavldel.c\
# cw_ktv_parser.c\
# cw_ktv_del_sub.c\
# cw_ktv_base_exists.c\
cw_ktv_add_from_str.c\
cw_ktv_get_byte.c\
cw_ktv_read_file.c\
cw_ktv_readline.c\
cw_ktv_save.c\
cw_ktv_std_types.c\
cw_ktv_read_struct.c\
cw_ktv_write_struct.c\
# cw_ktv_get_byte.c\
cw_ktv_get_bool.c\
cw_ktv_get_bstr16.c\
cw_ktv_set_byte.c\
cw_ktv_set_word.c\
cw_ktv_set_dword.c\
cw_ktv_get.c\
cw_ktv_get_word.c\
# cw_ktv_get_word.c\
cw_ktv_get_dword.c\
cw_ktv_get_sysptr.c\
cw_ktv_get_str.c\
cw_ktv_idx_get.c\
cw_ktv_mavlcmp.c\
cw_ktv_mavlcmp_type_by_name.c\
cw_ktv_mavldel.c\
cw_ktv_read_file.c\
cw_ktv_readline.c\
cw_ktv_read_struct.c\
cw_ktv_write_struct.c\
cw_ktv_std_types.c\
cw_ktv_save.c\
cfg.c\
# cw_ktv_parser.c\
# cw_ktv_del_sub.c\
# cw_ktv_base_exists.c\
# cw_ktv_cast.c\
cw_ktv_replace.c\
cw_ktv_get.c\
LWSRC=\
lw_addelem.c\

View File

@ -495,12 +495,18 @@ int cw_cfg_write_to_file(FILE *f, cw_Cfg_t * cfg)
}
int cw_cfg_save(const char *filename, cw_Cfg_t *cfg)
int cw_cfg_save(const char *filename, cw_Cfg_t *cfg, const char *format, ...)
{
int rc;
FILE *f = fopen(filename, "wb");
if (!f)
return errno;
if (format !=NULL){
va_list args;
va_start(args,format);
vfprintf(f,format,args);
va_end(args);
}
rc = cw_cfg_write_to_file(f, cfg);
fclose(f);
@ -519,7 +525,7 @@ void cw_cfg_iter_init(cw_Cfg_t * cfg, struct cw_Cfg_iter *cfi, const char *base)
}
const char *cw_cfg_iter_next(struct cw_Cfg_iter *cfi, const char *key)
struct cw_Cfg_entry *cw_cfg_iter_next(struct cw_Cfg_iter *cfi, const char *nnkey)
{
struct cw_Cfg_entry *e;
int bl, kl;
@ -541,7 +547,7 @@ const char *cw_cfg_iter_next(struct cw_Cfg_iter *cfi, const char *key)
return NULL;
else {
mavliter_next(&(cfi->it));
return e->val;
return e;
}
}
@ -556,7 +562,7 @@ const char *cw_cfg_iter_next(struct cw_Cfg_iter *cfi, const char *key)
return NULL;
mavliter_next(&(cfi->it));
return e->val;
return e;
}
@ -686,22 +692,45 @@ void cw_cfg_copy(cw_Cfg_t *src, cw_Cfg_t *dst)
int exists;
struct cw_Cfg_entry * old_elem,*e, new_elem;
e = mavliter_get(&it);
new_elem.key = cw_strdup(e->key);
new_elem.val = cw_strdup(e->val);
/* if (1){
const char *ov;
ov = cw_cfg_get(dst,e->key,NULL);
cw_dbg(DBG_X, " REAL OV HERE: %s",ov);
}
*/
old_elem = mavl_insert(dst,&new_elem,&exists);
/* cw_dbg(DBG_X, "CPY: %s: %s -> %s [%d]",new_elem.key,new_elem.val,old_elem->val,exists);
if (exists){
const char *ov;
ov = cw_cfg_get(dst,e->key,NULL);
cw_dbg(DBG_X, " OV HERE: %s",ov);
}
*/
if (!exists){
cw_dbg(DBG_X, "New: %s: %s",new_elem.key,new_elem.val);
cw_dbg(DBG_CFG_SET, "New: %s: %s",new_elem.key,new_elem.val);
continue;
}
if (strcmp(new_elem.val,old_elem->val)==0){
free((void*)new_elem.key);
free((void*)new_elem.val);
continue;
}
cw_dbg(DBG_X, "Replace: %s: %s (old: %s)",new_elem.key, new_elem.val, old_elem->val);
cw_dbg(DBG_CFG_SET, "Replace: %s: %s (old: %s)",new_elem.key, new_elem.val, old_elem->val);
if(dst->del){
dst->del(old_elem);
}
@ -753,7 +782,7 @@ int cw_cfg_base_exists_l(cw_Cfg_t ** cfgs, const char *key)
if (cw_cfg_base_exists(cfgs[i],key))
return 1;
}
cw_dbg(DBG_X,"NOX EXISIS: %s in %d",key,i);
// cw_dbg(DBG_X,"NOX EXISIS: %s in %d",key,i);
return 0;
}

View File

@ -28,7 +28,7 @@ struct cw_Cfg_iter{
const char * cw_cfg_get(cw_Cfg_t * cfg, const char *key, const char *def);
const char *cw_cfg_iter_next(struct cw_Cfg_iter *cfi, const char *key);
struct cw_Cfg_entry *cw_cfg_iter_next(struct cw_Cfg_iter *cfi, const char *key);
void cw_cfg_iter_init(cw_Cfg_t * cfg, struct cw_Cfg_iter *cfi, const char *base);
int cw_cfg_get_bool(cw_Cfg_t * cfg, const char * key, int def);
@ -46,7 +46,7 @@ int cw_cfg_base_exists(cw_Cfg_t * cfg, const char *key);
uint8_t cw_cfg_get_byte_l(cw_Cfg_t ** cfgs, char *key, uint8_t def);
cw_Val_t * cw_cfg_get_val_l(cw_Cfg_t ** cfgs, const char *key, const struct cw_Type *type);
int cw_cfg_base_exists_l(cw_Cfg_t ** cfgs, const char *key);
int cw_cfg_save(const char *filename, cw_Cfg_t *cfg);
int cw_cfg_save(const char *filename, cw_Cfg_t *cfg, const char *format, ...);

View File

@ -59,7 +59,6 @@ void cw_conn_init(struct cw_Conn * conn)
conn->msg_callbacks = mavl_create(msg_callback_cmp,NULL,sizeof(struct msg_callback));
conn->update_cfg = cw_cfg_create();
cw_dbg(DBG_X,"Update CFG ist %p",conn->update_cfg);
conn->remote_cfg = cw_cfg_create();
conn->local_cfg = cw_cfg_create();
}

View File

@ -7,12 +7,11 @@ int cw_in_generic_with_index(struct cw_ElemHandler *eh,
{
char key[CW_CFG_MAX_KEY_LEN];
int idx;
struct cw_Type * type = (struct cw_Type*)eh->type;
cw_dbg(DBG_X,"Fix cw_in_generic_with_index");
stop();
idx = cw_get_byte(data);
sprintf(key,"%s.%d",eh->key,idx);
// cw_cfg_add(params->cfg,key,eh->type,NULL,data+1,len-1);
type->read(params->cfg,key,data+1,len-1,eh->param);
return 1;
}

View File

@ -1,43 +0,0 @@
#include "val.h"
#include "cw.h"
#include "log.h"
#include "dbg.h"
cw_Val_t * cw_ktv_add(mavl_t kvtstore, const char *key, const struct cw_Type *type,
const void * valguard,
const uint8_t * data, int len)
{
cw_Val_t mdata, *mresult;
int exists;
mdata.key=cw_strdup(key);
mdata.valguard=valguard;
if (!mdata.key){
cw_log(LOG_ERR, "Can't allocate memory for KTV 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.key);
return NULL;
}
mavl_del(kvtstore,&mdata);
mresult = mavl_insert(kvtstore, &mdata, &exists);
if (exists){
cw_log(LOG_ERR, "Element already exists %s", key);
/* element already exists */
free(mdata.key);
if (type->del)
type->del(&mdata);
return mresult;
}
return mresult;
}

View File

@ -1,45 +0,0 @@
#include "val.h"
#include "cw.h"
#include "log.h"
#include "dbg.h"
const char * cw_ktv_add_from_str(mavl_t kvtstore, const char *key,
const struct cw_Type *type,
const void * valguard,
const char * str)
{
cw_Val_t mdata, *mresult;
int exists;
/* cw_dbg(DBG_ELEM,"KVStore (%p,%d) add elem (%s): %s", kvstore, kvstore->count,
type->name, key );
*/
mdata.key=cw_strdup(key);
mdata.valguard=valguard;
if (!mdata.key){
cw_log(LOG_ERR, "Can't allocate memory for key %s: %s",
key,strerror(errno));
return NULL;
}
mresult = type->from_str(&mdata,str);
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.key);
return NULL;
}
mresult = mavl_insert(kvtstore, &mdata, &exists);
if (exists){
cw_log(LOG_ERR, "Element already exists %s", key);
/* element already exists */
free(mdata.key);
if (type->del)
type->del(&mdata);
return key;
}
return mdata.key;
}

View File

@ -1,29 +0,0 @@
#include "val.h"
/**
* @file
* @description Implementation of cw_ktv_base_exisits
*/
/**
* @brief Check if elements with e certain baskey can be found in
* in ktvstore.
* @param ktvstore ktvstore to search in
* @param basekey basekey to search for
* @return The first ktv element belonging to the base eky,
* otherwise NULL if no element was found.
*
*/
cw_Val_t * cw_ktv_base_exists(mavl_t ktvstore, const char *basekey)
{
cw_Val_t * result, search;
search.key=(char*)basekey;
result = mavl_get_first(ktvstore,&search);
if (result == NULL)
return NULL;
if (strncmp(result->key,basekey,strlen(basekey))==0)
return result;
return NULL;
}

View File

@ -1,14 +0,0 @@
#include "val.h"
cw_Val_t * cw_ktv_cast(cw_Val_t *v,const cw_Type_t * type)
{
if (strcmp(v->type->name,type->name)==0)
return v;
if (type->cast==NULL)
return NULL;
if (!type->cast(v))
return NULL;
return v;
}

View File

@ -1,32 +0,0 @@
#include "val.h"
#include "mavl.h"
/**
* @brief Get a ktv value from a ktv store
* @param ktv ktv store
* @param key key to search for
* @param type type to match
* @return A pointer to a #cw_Val_t element, found in the ktv store or
* NULL if no element with matching key/type is found.
*/
cw_Val_t * cw_ktv_get(mavl_t ktv, const char *key, const cw_Type_t * type)
{
cw_Val_t search, *result;
/* we can safely cast from const char * to char *, because
* we never will use the search varaiable to store ktv values */
search.key=(char*)key;
result = mavl_get(ktv,&search);
if (result == NULL){
return NULL;
}
if (type == NULL){
return result;
}
if (strcmp(type->name,result->type->name)==0){
return result;
}
return NULL;
}

View File

@ -1,11 +0,0 @@
#include "val.h"
uint8_t cw_ktv_get_bool(mavl_t ktv,const char *key, uint8_t def)
{
cw_Val_t * k;
k = cw_ktv_get(ktv,key,CW_TYPE_BOOL);
if (k != NULL){
return k->val.boolean;
}
return def;
}

View File

@ -1,13 +0,0 @@
#include "val.h"
#include "bstr.h"
bstr16_t cw_ktv_get_bstr16(mavl_t ktv,const char *key, bstr16_t def)
{
cw_Val_t * k;
k = cw_ktv_get(ktv,key,CW_TYPE_BSTR16);
if (k != NULL){
return bstr16_create(k->type->data(k),k->type->len(k));
}
return def;
}

View File

@ -1,11 +0,0 @@
#include "val.h"
uint8_t cw_ktv_get_byte(mavl_t ktv,const char *key, uint8_t def)
{
cw_Val_t * k;
k = cw_ktv_get(ktv,key,CW_TYPE_BYTE);
if (k != NULL){
return k->val.byte;
}
return def;
}

View File

@ -1,11 +0,0 @@
#include "val.h"
uint32_t cw_ktv_get_dword(mavl_t ktv,const char *key, uint32_t def)
{
cw_Val_t * k;
k = cw_ktv_get(ktv,key,CW_TYPE_DWORD);
if (k != NULL){
return k->val.dword;
}
return def;
}

View File

@ -1,11 +0,0 @@
#include "val.h"
char * cw_ktv_get_str(mavl_t ktv,const char *key, char * def)
{
cw_Val_t * k;
k = cw_ktv_get(ktv,key,CW_TYPE_STR);
if (k != NULL){
return k->val.ptr;
}
return def;
}

View File

@ -1,11 +0,0 @@
#include "val.h"
void * cw_ktv_get_sysptr(mavl_t ktv,const char *key, void * def)
{
cw_Val_t * k;
k = cw_ktv_get(ktv,key,CW_TYPE_SYSPTR);
if (k != NULL){
return k->val.ptr;
}
return def;
}

View File

@ -1,11 +0,0 @@
#include "val.h"
uint16_t cw_ktv_get_word(mavl_t ktv,const char *key, uint16_t def)
{
cw_Val_t * k;
k = cw_ktv_get(ktv,key,CW_TYPE_WORD);
if (k != NULL){
return k->val.word;
}
return def;
}

View File

@ -1,126 +0,0 @@
#include "val.h"
#include "cfg.h"
void * ktvn(struct mavl *t ,const void *search)
{
struct mavlnode *n,/**lastl,*/*lastb;
lastb = NULL; /*lastl=NULL;*/
n = t->root;
while(n){
int rc;
/* const cw_Val_t;*//* *c1,*c2;*/
/*c1=search;
c2=mavlnode_dataptr(n);
*/
rc = t->cmp(search,mavlnode_dataptr(n));
/*printf("Compare: %s %s = %d\n",c1->key,c2->key, rc);*/
if (rc==0){
return NULL;
}
if (rc<0){
/*lastl = n;*/
if (n->s[0]==NULL){
return mavlnode_dataptr(lastb);
}
n=n->s[0];
}
else{
lastb=n;
if(n->s[1]==NULL){
return mavlnode_dataptr(lastb);
}
n=n->s[1];
}
}
return NULL;
}
int cw_ktv_idx_get(mavl_t ktv, const char *key)
{
char ikey[CW_CFG_MAX_KEY_LEN];
cw_Val_t search, * result;
char *d;
sprintf(ikey,"%s.%d",key,65536);
search.key=ikey;
/*//result = ktvn(ktv,&search);*/
result = mavl_get_last(ktv,&search);
if (result == NULL){
return -1;
}
d = strchr(result->key,'.');
if (d==NULL){
return -1;
}
if (strncmp(result->key,ikey,d-result->key)!=0)
return -1;
return atoi(d+1);
}
int cw_ktv_idx_get_next(mavl_t ktv, const char *key, int n)
{
char ikey[CW_CFG_MAX_KEY_LEN];
cw_Val_t search, * result;
char *d;
int i;
sprintf(ikey,"%s.%d",key,n);
search.key=ikey;
/*//result = ktvn(ktv,&search);*/
result = mavl_get_first(ktv,&search);
if (result == NULL){
return -1;
}
/*d = strchr(result->key,'.');*/
d=NULL;
/* for (i = strlen(result->key); i>=0; i--){
if (result->key[i]=='/')
break;
}
*/ for (i = strlen(ikey); i>=0; i--){
if (ikey[i]=='.'){
d = result->key+i;
break;
}
}
if (d==NULL){
return -1;
}
if(result->key[i]!='.'){
return -1;
}
/* if (strncmp(result->key,ikey,d-result->key)!=0)
return -1;
*/
if (strncmp(result->key,ikey,i)!=0)
return -1;
return atoi(result->key+i+1);
}

View File

@ -1,67 +0,0 @@
#include <ctype.h>
#include "val.h"
/**
* @brief Default function to compare two values of type #cw_Val_t.
*
* @param v1
* @param v2
* @return
*/
int cw_ktv_mavlcmp (const void *v1, const void *v2)
{
char *d1, *d2, *k1, *k2;
int l1, l2, rc, i1, i2;
k1 = ( (cw_Val_t *) v1)->key;
k2 = ( (cw_Val_t *) v2)->key;
while (1) {
/* Find dots in both keys */
d1 = strchr (k1, '.');
d2 = strchr (k2, '.');
/* if there are no dots, compare keys as normal */
if (d1 == NULL || d2 == NULL)
return strcmp (k1, k2);
/* calculate the length of the key till dots */
l1 = d1 - k1; /*((cw_Val_t *) v1)->key;*/
l2 = d2 - k2; /*((cw_Val_t *) v2)->key;*/
/* if length differs do a normal compare */
if (l1 != l2) {
return strcmp (k1, k2); /*((cw_Val_t *) v1)->key, ((cw_Val_t *) v2)->key);*/
}
rc = strncmp (k1, k2, l1); /*((cw_Val_t *) v1)->key,((cw_Val_t *) v2)->key,l1);*/
if (rc != 0) {
return rc;
}
d1++;
d2++;
i1 = atoi (d1);
i2 = atoi (d2);
rc = i1 - i2;
if (rc != 0)
return rc;
while (isdigit (*d1))
d1++;
while (isdigit (*d2))
d2++;
k1=d1;
k2=d2;
/*return strcmp(d1,d2);*/
/*return cw_ktv_mavlcmp(d1,d2);*/
}
}

View File

@ -1,8 +0,0 @@
#include "val.h"
int cw_ktv_mavlcmp_type_by_name (const void *v1, const void *v2)
{
return strcmp ( (*((struct cw_Type**) v1))->name,
(*((struct cw_Type**) v2))->name);
}

View File

@ -1,14 +0,0 @@
#include <stdlib.h>
#include "val.h"
void cw_ktv_mavldel(void *data)
{
struct cw_Val *ktv = data;
if (ktv->type->del) {
ktv->type->del(data);
}
free(ktv->key);
}

View File

@ -1,359 +0,0 @@
#include <stdint.h>
#include "val.h"
static int str_getc(struct cw_Val_Reader * r)
{
if (r->next==r->maxlen)
return EOF;
return *((uint8_t*)(r->data)+r->next++);
}
static void str_ungetc(struct cw_Val_Reader * r, int c)
{
if (r->next>0)
r->next--;
}
void cw_ktv_init_str_reader(struct cw_Val_Reader *r, const char * str, int len)
{
memset(r,0,sizeof(struct cw_Val_Reader));
r->data = str;
r->xgetchar=str_getc;
r->ungetchar=str_ungetc;
r->maxlen=len;
}
#include <ctype.h>
#include "val.h"
/*
struct parser {
int line;
int pos;
int prevpos;
char error[256];
int quote;
FILE *f;
int (*getc)(struct parser *);
void (*ungetc)(struct parser *)
};
*/
static int get_char(struct cw_Val_Reader *r)
{
int c;
c = r->xgetchar (r);
r->pos++;
if (c=='\n'){
r->prevpos=r->pos;
r->line ++;
r->pos=0;
}
return c;
}
static void unget_char(struct cw_Val_Reader * r,int c){
r->ungetchar(r,c);
if (c=='\n'){
r->line--;
r->pos=r->prevpos;
}
else
r->pos--;
}
static int get_char_q(struct cw_Val_Reader *p)
{
int c;
while(1) {
c = get_char(p);
if (c==EOF || c=='\n')
return c;
if(c=='"' && !p->quote){
p->quote=1;
continue;
}
if(c=='"' && p->quote){
p->quote=0;
continue;
}
break;
}
if (!p->quote)
return c;
if (c!='\\')
return c;
c = get_char(p);
switch(c){
case EOF:
return c;
case 'n':
return '\n';
case '\\':
return '\\';
case '"':
return '"';
default:
unget_char(p,c);
return '\\';
}
/* We will never reach here */
/* return c;*/
}
static int skip_chars (struct cw_Val_Reader *r, const char * chars)
{
int c;
while ( (c = get_char (r)) != EOF) {
if (strchr (chars, c))
continue;
return c;
}
return c;
}
static int skip_to_chars (struct cw_Val_Reader *r, const char *chars)
{
int c;
while ( (c = get_char (r)) != EOF) {
if (strchr (chars, c))
return c;
}
return c;
}
static int read_key (struct cw_Val_Reader *r, char *key, int max_len)
{
int c,n;
do {
c = skip_chars (r, " \t\n\a\v");
if (c == '#') {
c = skip_to_chars (r, "\n\a");
} else {
break;
}
} while (c != EOF);
unget_char(r,c);
c=get_char_q(r);
n=0;
while(c!=EOF && n<max_len){
if (!r->quote && !isalnum(c) && !strchr("._/-()@#|{}[]\\",c)){
unget_char(r,c);
break;
}
key[n]=c;
c=get_char_q(r);
n++;
}
key[n]=0;
return n;
}
static int skip_to_colon(struct cw_Val_Reader * r)
{
int c;
c = skip_chars (r, " \t");
if (c!=':' && c!='='){
if (c=='\n'){
unget_char(r,c);
sprintf(r->error,"Unexpected EOL, colon or equal sign expected.");
return -1;
}
sprintf(r->error,"Colon or equal sign expected.");
return -1;
}
return c;
}
static int read_type(struct cw_Val_Reader * r, char *type, int max_len)
{
int c,n;
c = skip_to_colon(r);
if (c==-1)
return -1;
if (c=='='){
unget_char(r,c);
return sprintf(type,"%s","Str");
}
c = skip_chars (r, " \t");
if (c==':'){
unget_char(r,c);
sprintf(type,"%s","Str");
return 0;
}
if (!isalpha(c)){
if (c=='\n'){
unget_char(r,c);
/*sprintf(p->error,"Error at line %d, pos %d: Unexpected EOL.", p->line, p->pos);*/
return -1;
}
/*sprintf(p->error,"Error at line %d, pos %d: Letter expected.", p->line, p->pos);*/
return -1;
}
n=0;
while(c!=EOF && n<max_len){
if (!isalnum(c) && !strchr("_/-.()@#|{}[]",c)/*strchr(": \t\n\a",c)*/){
unget_char(r,c);
break;
}
type[n]=c;
c=get_char(r);
n++;
}
type[n]=0;
return n;
}
static int read_val(struct cw_Val_Reader *r, char *val, int max_len){
int c,n,quote;
c = skip_to_colon(r);
if (c==-1)
return -1;
c = skip_chars (r, " \t");
if (c=='"'){
quote=1;
c=get_char(r);
}
else{
quote=0;
}
n=0;
while(c!=EOF && n<max_len){
if (quote && c=='"'){
break;
}
if (c=='\n'){
break;
}
if (quote){
if (c=='\\'){
c = get_char(r);
switch(c){
case 'n':
c='\n';
break;
case '\\':
break;
case '"':
break;
default:
unget_char(r,c);
c='\\';
}
}
}
val[n++]=c;
c=get_char(r);
}
if(!quote && n>0){
while(n>0){
if (isspace(val[n-1]))
n--;
else
break;
}
}
val[n]=0;
return n;
}
/*
int cw_ktv_parse_line (FILE *f, char * key, char * type, char *val)
{
int n;
struct parser p;
p.line=1;
p.pos=0;
p.prevpos=0;
p.quote=0;
p.f=f;
n = read_key (&p,key,CW_KTV_MAX_KEY_LEN);
n = read_type (&p,type,CW_KTV_MAX_KEY_LEN);
if (n==-1){
return -1;
}
n = read_val (&p,val,CW_KTV_MAX_KEY_LEN);
if (n==-1){
return -1;
}
return 0;
}
*/
int cw_ktv_parse_line(struct cw_Val_Reader * r)
{
}
int cw_ktv_parse_string(struct cw_Val_Reader *r, char *key, char *type, char *val)
{
int n;
n = read_key (r,key,CW_KTV_MAX_KEY_LEN);
n = read_type(r,type,200);
if (n==1)
return -1;
n = read_val(r,val,200);
return n;
}

View File

@ -1,40 +0,0 @@
#include "val.h"
#include "cfg.h"
int cw_ktv_read_file(FILE * file, mavl_t ktv, mavl_t types)
{
char key[CW_CFG_MAX_KEY_LEN];
char type[256];
char val[2048];
int rc;
struct cw_Type typesearch, *cwtype;
do {
rc = cw_ktv_read_line(file,key,type,val);
if (rc != 0){
continue;
}
typesearch.name=type;
cwtype = mavl_get_ptr(types,&typesearch);
if (cwtype != NULL){
/*// printf("Yes! I know the type: %s\n",cwtype->name);*/
}
else {
printf("Unknown type: %s\n",typesearch.name);
continue;
}
cw_ktv_add_from_str(ktv,key,cwtype,NULL,val);
}while(rc==0);
return 0;
}

View File

@ -1,62 +0,0 @@
#include "val.h"
#include "dbg.h"
#include "cfg.h"
int cw_ktv_read_struct(mavl_t ktv,const cw_ValStruct_t * stru, const char *pkey,
uint8_t * data, int len)
{
char key[CW_CFG_MAX_KEY_LEN];
int pos, i,l;
cw_Val_t * result;
pos=0; i=0;
while (stru[i].type != NULL){
char dbstr[100];
if(stru[i].position!=-1)
pos=stru[i].position;
if (stru[i].key!=NULL)
sprintf(key,"%s/%s",pkey,stru[i].key);
else
sprintf(key,"%s",pkey);
switch (stru[i].len){
case CW_KTVSTRUCT_L8:
l = cw_get_byte(data+pos);
pos ++;
break;
case CW_KTVSTRUCT_L16:
l = cw_get_word(data+pos);
pos ++;
break;
case -1:
l = len-pos;
break;
default:
l = stru[i].len;
if (pos+l > len){
l = len-pos;
}
}
result = cw_ktv_add(ktv,key,stru[i].type,stru[i].valguard,data+pos,l);
stru[i].type->to_str(result,dbstr,100);
cw_dbg(DBG_ELEM_DETAIL, "Read (%d): %s: %s",pos,key,dbstr);
printf("READ STRUCT (%d): %s: %s\n",pos,key,dbstr);
if (stru[i].len==-1)
l = result->type->len(result);
else
l = stru[i].len;
if(stru[i].position == -1)
pos+=l;
i++;
}
return pos;
}

View File

@ -1,306 +0,0 @@
#include <ctype.h>
#include "val.h"
#include "cfg.h"
struct parser {
int line;
int pos;
int prevpos;
char error[256];
int quote;
FILE *f;
int (*getc)(struct parser *);
void (*ungetc)(struct parser *);
};
/*
static int pgetc(struct parser *parser)
{
return fgetc(parser->f);
}*/
static int get_char(struct parser *p)
{
int c;
c = fgetc (p->f);
p->pos++;
if (c=='\n'){
p->prevpos=p->pos;
p->line ++;
p->pos=0;
}
return c;
}
static void unget_char(struct parser *p,int c){
ungetc(c,p->f);
if (c=='\n'){
p->line--;
p->pos=p->prevpos;
}
else
p->pos--;
}
static int get_char_q(struct parser *p)
{
int c;
while(1) {
c = get_char(p);
if (c==EOF || c=='\n')
return c;
if(c=='"' && !p->quote){
p->quote=1;
continue;
}
if(c=='"' && p->quote){
p->quote=0;
continue;
}
break;
}
if (!p->quote)
return c;
if (c!='\\')
return c;
c = get_char(p);
switch(c){
case EOF:
return c;
case 'n':
return '\n';
case '\\':
return '\\';
case '"':
return '"';
default:
unget_char(p,c);
return '\\';
}
/* We will never reach here */
/* return c;*/
}
static int skip_chars (struct parser *p, const char * chars)
{
int c;
while ( (c = get_char (p)) != EOF) {
if (strchr (chars, c))
continue;
return c;
}
return c;
}
static int skip_to_chars (struct parser *p, const char *chars)
{
int c;
while ( (c = get_char (p)) != EOF) {
if (strchr (chars, c))
return c;
}
return c;
}
static int read_key (struct parser *p, char *key, int max_len)
{
int c,n;
do {
c = skip_chars (p, " \t\n\a\v");
if (c == '#') {
c = skip_to_chars (p, "\n\a");
} else {
break;
}
} while (c != EOF);
unget_char(p,c);
c=get_char_q(p);
n=0;
while(c!=EOF && n<max_len){
if (!p->quote && !isalnum(c) && !strchr("._/-()@#|{}[]\\",c)/*strchr(": \t\n\a",c)*/){
unget_char(p,c);
break;
}
key[n]=c;
c=get_char_q(p);
n++;
}
key[n]=0;
return n;
}
static int skip_to_colon(FILE *f,struct parser * p)
{
int c;
c = skip_chars (p, " \t");
if (c!=':'){
if (c=='\n'){
unget_char(p,c);
sprintf(p->error,"Error at line %d, pos %d: Unexpected EOL, collon expected.", p->line, p->pos);
return 0;
}
sprintf(p->error,"Error at line %d, pos %d: Collon expected.", p->line, p->pos);
return 0;
}
return 1;
}
static int read_type(struct parser *p, char *type, int max_len)
{
int c,n;
if (!skip_to_colon(p->f,p))
return -1;
c = skip_chars (p, " \t");
if (c==':'){
unget_char(p,c);
sprintf(type,"%s","");
return 0;
}
if (!isalpha(c)){
if (c=='\n'){
unget_char(p,c);
sprintf(p->error,"Error at line %d, pos %d: Unexpected EOL.", p->line, p->pos);
return -1;
}
sprintf(p->error,"Error at line %d, pos %d: Letter expected.", p->line, p->pos);
return -1;
}
n=0;
while(c!=EOF && n<max_len){
if (!isalnum(c) && !strchr("_/-.()@#|{}[]",c)/*strchr(": \t\n\a",c)*/){
unget_char(p,c);
break;
}
type[n]=c;
c=get_char(p);
n++;
}
type[n]=0;
return n;
}
static int read_val(struct parser *p, char *val, int max_len){
int c,n,quote;
if (!skip_to_colon(p->f,p))
return -1;
c = skip_chars (p, " \t");
if (c=='"'){
quote=1;
c=get_char(p);
}
else{
quote=0;
}
n=0;
while(c!=EOF && n<max_len){
if (quote && c=='"'){
break;
}
if (c=='\n'){
break;
}
if (quote){
if (c=='\\'){
c = get_char(p);
switch(c){
case 'n':
c='\n';
break;
case '\\':
break;
case '"':
break;
default:
unget_char(p,c);
c='\\';
}
}
}
val[n++]=c;
c=get_char(p);
}
if(!quote && n>0){
while(n>0){
if (isspace(val[n-1]))
n--;
else
break;
}
}
val[n]=0;
return n;
}
int cw_ktv_read_line (FILE *f, char * key, char * type, char *val)
{
int n;
struct parser p;
p.line=1;
p.pos=0;
p.prevpos=0;
p.quote=0;
p.f=f;
n = read_key (&p,key,CW_CFG_MAX_KEY_LEN);
n = read_type (&p,type,CW_CFG_MAX_KEY_LEN);
if (n==-1){
return -1;
}
n = read_val (&p,val,CW_CFG_MAX_KEY_LEN);
if (n==-1){
return -1;
}
return 0;
}

View File

@ -1,32 +0,0 @@
#include "val.h"
#include "cw.h"
#include "log.h"
#include "dbg.h"
cw_Val_t * cw_ktv_replace(mavl_t kvtstore, const char *key, const struct cw_Type *type,
const void * valguard,
const uint8_t * data, int len)
{
cw_Val_t mdata, *mresult;
int exists;
mdata.key=cw_strdup(key);
if (!mdata.key){
cw_log(LOG_ERR, "Can't allocate memory for KTV 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.key);
return NULL;
}
mresult = mavl_replace(kvtstore, &mdata, &exists);
return mresult;
}

View File

@ -1,58 +0,0 @@
#include "val.h"
static int write_str(FILE *outfile,const char *str)
{
if ( (strchr(str,'\\')!=NULL) || (strchr(str,'"')!=NULL) || (strchr(str,'\n')!=NULL)){
fprintf(outfile,"\"");
while (*str != 0){
switch(*str){
case '\n':
fprintf(outfile,"\\n");
break;
case '\\':
fprintf(outfile,"\\\\");
break;
case '"':
fprintf(outfile,"\\\"");
break;
default:
fprintf(outfile,"%c",*str);
break;
}
str++;
}
fprintf(outfile,"\"");
return 1;
}
fprintf(outfile,"%s",str);
return 1;
}
int cw_ktv_save(mavl_t ktvstore, const char * filename){
mavliter_t it;
FILE * outfile;
outfile = fopen(filename,"w");
if (outfile == NULL)
return 0;
mavliter_init(&it,ktvstore);
mavliter_foreach(&it){
cw_Val_t * val;
char buf[4000];
val = mavliter_get(&it);
val->type->to_str(val,buf,4000);
write_str(outfile,val->key);
fprintf(outfile," :%s: ",val->type->name);
write_str(outfile,buf);
fprintf(outfile,"\n");
}
fclose(outfile);
return 1;
}

View File

@ -1,6 +0,0 @@
#include "val.h"
cw_Val_t * cw_ktv_set_byte(mavl_t ktv,const char * key, uint8_t byte)
{
return cw_ktv_replace(ktv,key,CW_TYPE_BYTE,NULL,&byte,1);
}

View File

@ -1,6 +0,0 @@
#include "val.h"
cw_Val_t * cw_ktv_set_dword(mavl_t ktv,const char * key, uint32_t dword)
{
return cw_ktv_replace(ktv,key,CW_TYPE_DWORD,NULL,(void*)&dword,4);
}

View File

@ -1,6 +0,0 @@
#include "val.h"
cw_Val_t * cw_ktv_set_word(mavl_t ktv,const char * key, uint16_t word)
{
return cw_ktv_replace(ktv,key,CW_TYPE_WORD,NULL,(void*)&word,2);
}

View File

@ -1,13 +0,0 @@
#include "val.h"
const cw_Type_t * cw_ktv_std_types[] = {
CW_TYPE_BYTE,
CW_TYPE_WORD,
CW_TYPE_DWORD,
CW_TYPE_BSTR16,
CW_TYPE_IPADDRESS,
CW_TYPE_STR,
CW_TYPE_SYSPTR,
CW_TYPE_BOOL,
NULL
};

View File

@ -1,77 +0,0 @@
#include "val.h"
#include "dbg.h"
#include "log.h"
#include "cfg.h"
int cw_ktv_write_struct(mavl_t ktv, mavl_t def, const cw_ValStruct_t * stru, const char *pkey,
uint8_t * dst)
{
char key[CW_CFG_MAX_KEY_LEN];
int pos, i;
cw_Val_t * result;
pos=0; i=0;
for(i=0; stru[i].type != NULL;i++){
if (stru[i].position!=-1){
pos=stru[i].position;
}
if (stru[i].len!=-1)
memset(dst+pos,0,stru[i].len);
if (stru[i].key!=NULL)
sprintf(key,"%s/%s",pkey,stru[i].key);
else
sprintf(key,"%s",pkey);
printf("Get Key: %s\n",key);
result = cw_ktv_get(ktv,key,NULL);
if(result)
{
char s[129];
result->type->to_str(result,s,128);
printf("Content: '%s'\n",s);
}
if (result == NULL && def != NULL){
result = cw_ktv_get(def,key,NULL);
}
if (result == NULL){
cw_log(LOG_ERR,"Can't put %s, no value found, filling zero.",key);
memset(dst+pos,0,stru[i].len);
}
else{
result->valguard=stru[i].valguard;
if (cw_ktv_cast(result,stru[i].type)==NULL){
cw_log(LOG_ERR,"Can't cast key '%s' from %s to %s",key,result->type->name,stru[i].type->name);
}
/* if (strcmp(stru[i].type->name,result->type->name)){
printf("Type mismatch: %s != %s\n",stru[i].type->name,result->type->name);
if (stru[i].type->cast != NULL){
if (!stru[i].type->cast(result)){
cw_log(LOG_ERR,"Can't cast '%s' from %s to %s",key,result->type->name,stru[i].type->name);
exit(0);
}
}
}
*/
result->type->put(result,dst+pos);
}
if (stru[i].len!=-1)
pos+=stru[i].len;
else
pos+=result->type->len(result);
}
return pos;
}

View File

@ -1,22 +1,52 @@
#include "cw.h"
#include "dbg.h"
#include "cfg.h"
int cw_out_generic_with_index(struct cw_ElemHandler * eh,
struct cw_ElemHandlerParams * params, uint8_t * dst)
{
stop();
char key[CW_CFG_MAX_KEY_LEN];
int idx;
cw_Val_t * result, search;
int len,start;
uint8_t * ob;
struct cw_Cfg_entry *e;
struct cw_Cfg_iter cfi;
struct cw_Type * type = (struct cw_Type *)eh->type;
int i,l;
idx = 0;
ob = dst;
cw_cfg_iter_init(params->cfg_list[0], &cfi, eh->key);
for (i=0; (e = cw_cfg_iter_next(&cfi, NULL)) != NULL; i++){
start = params->msgset->header_len(eh);
len = cw_put_byte(ob+start,i);
cw_dbg(DBG_X,"KV: %s - %s",e->key,e->val);
// int bwrite(cw_Cfg_t ** cfgs, const char *key, uint8_t *dst, const void * param)
l = type->write(params->cfg_list,e->key,ob+start+len,eh->param);
if (l==-1)
continue;
len += l;
//len += result->type->put(result,ob+start+len);
ob += params->msgset->write_header(eh,ob,len);
}
return ob-dst;
stop();
return 0;
do {
sprintf(key,"%s.%d",eh->key,idx);
search.key=key;

View File

@ -10,7 +10,8 @@ static int get_psk(struct cw_Conn *conn, const char *username, uint8_t ** psk,
char key[CW_CFG_MAX_KEY_LEN];
cw_Val_t *result;
sprintf(key, "%s/%s", "psk", username);
result = cw_ktv_get(conn->local_cfg, key, CW_TYPE_BSTR16);
stop();
// result = cw_ktv_get(conn->local_cfg, key, CW_TYPE_BSTR16);
if (result == NULL) {
if (conn->dtls_psk != NULL) {
*psk = bstr16_data(conn->dtls_psk);

View File

@ -96,7 +96,7 @@ int cw_compose_message(struct cw_Conn *conn, uint8_t * rawout)
dst = msgptr+8;
len =0;
cw_dbg(DBG_X,"setting with update CFG");
//cw_dbg(DBG_X,"setting with update CFG");
params.conn=conn;
params.cfg_list[0]=conn->update_cfg;
params.cfg_list[1]=conn->local_cfg;

View File

@ -217,6 +217,33 @@ static int update_msgdata(struct cw_MsgSet *set, struct cw_MsgData *msgdata,
ed.vendor = elemdef->vendor;
ed.mand = elemdef->mand;
/* add/delete/replace message elemeent to/from/in the elements list */
switch ( elemdef->op & 0xff){
case CW_IGNORE:
break;
continue;
case CW_DELETE:
cw_dbg(DBG_MOD, " deleting message element %d %d %d - %s",
elemdef->proto,
elemdef->vendor, elemdef->id, handler->name);
mlist_delete(msgdata->elements_list, &ed);
mavl_del(msgdata->elements_tree,&ed);
continue;
break;
case CW_APPEND:
mlist_append(msgdata->elements_list, &ed);
break;
default:
case CW_REPLACE:
if (mlist_replace(msgdata->elements_list, &ed)==NULL){
mlist_append(msgdata->elements_list, &ed);
}
break;
}
/* add message element to the elements tree */
result = mavl_replace(msgdata->elements_tree, &ed, &replaced);
@ -230,23 +257,6 @@ static int update_msgdata(struct cw_MsgSet *set, struct cw_MsgData *msgdata,
elemdef->vendor, elemdef->id, handler->name);
}
/* add/delete/replace message elemeent to/from/in the elements list */
switch ( elemdef->op & 0xff){
case CW_IGNORE:
break;
case CW_DELETE:
mlist_delete(msgdata->elements_list, &ed);
break;
case CW_APPEND:
mlist_append(msgdata->elements_list, &ed);
break;
default:
case CW_REPLACE:
if (mlist_replace(msgdata->elements_list, &ed)==NULL){
mlist_append(msgdata->elements_list, &ed);
}
break;
}
}