diff options
Diffstat (limited to 'test/test_key_value_load.c')
-rw-r--r-- | test/test_key_value_load.c | 124 |
1 files changed, 81 insertions, 43 deletions
diff --git a/test/test_key_value_load.c b/test/test_key_value_load.c index f020e34..afe5450 100644 --- a/test/test_key_value_load.c +++ b/test/test_key_value_load.c @@ -3,80 +3,118 @@ #include <netbytes.h> -void pr_u8( nb_u8 *u8 ) +char *alloc_new_str_s(char *str, size_t size) { - if ( !u8 ) - { - printf("u8: NULL\n"); - return; - } else + char *ret = NULL; + + if (str == NULL) { - printf("u8: t=0x%x v=0x%x\n", u8->type, u8->val ); + return NULL; } -} -void pr_u8arr( nb_u8arr *u8arr ) -{ - if (!u8arr) + //1MB is enought + if (size > (1024*1024)) { - printf("u8arr: NULL\n"); - return ; - } else + return NULL; + } + + ret = malloc(size+1); //extra for 1 zero at then end + if (ret == NULL) { - printf("u8arr: t=0x%x l=0x%x v=0x%x\n", - u8arr->type, u8arr->len, u8arr->val ); + return NULL; } + + memcpy(ret, str, size); + ret[size] = 0; //add zero at the end + + return ret; +} + +char *alloc_new_str(char *str) +{ + return alloc_new_str_s(str, strlen(str)); } -void pr_store( netbyte_store *nb ) +typedef struct kv_user_name { - int i; + int id; + char *name; + char *desc; +} kv_user_name; - if (!nb) +kv_user_name* kv_new(int id, char *name, char *desc) +{ + kv_user_name *ret = NULL; + + ret = malloc(sizeof(kv_user_name)); + if (!ret) { - printf("nb: NULL\n"); - return; + return NULL; } - printf("nb: s=0x%x c=0x%x\n", nb->size, nb->count ); - for (i=0;i<nb->count;i++) + ret->id = id; + ret->name = alloc_new_str(name); + ret->desc = alloc_new_str(desc); + + return ret; +} + +netbyte_store* kv_marsh(kv_user_name *kv) +{ + netbyte_store *nb=NULL; + nb_u8 nb_id; + nb_u8arr nb_name, nb_desc; + + nb = calloc(1,sizeof(netbyte_store)); + nb_init(nb); + + nb_u8_create(&nb_id, kv->id); + nb_add_u8(nb, &nb_id); + nb_u8arr_create(&nb_name, strlen(kv->name), kv->name); + nb_add_u8arr(nb, &nb_name); + nb_u8arr_create(&nb_desc, strlen(kv->desc), kv->desc); + nb_add_u8arr(nb, &nb_desc); + + nb_print(nb); + nb_print(nb); + + return nb; +} + +void kv_free(kv_user_name *kv) +{ + if (kv!=NULL) { - printf("\t[%x] -> ",i ); - if ( nb->types[i].type == NBT_U8 ) - { - pr_u8( (nb_u8 *)nb->types[i].nb_val ); - } else if ( nb->types[i].type == NBT_U8ARRAY ) - { - pr_u8arr( (nb_u8arr *)nb->types[i].nb_val ); - } else - { - printf("Unknown\n"); - } + free(kv->name); + free(kv->desc); + free(kv); } } int main() { - - int er; - uint8_t *res; - FILE *f; + int er=-1; + uint8_t *res=NULL; + FILE *f=NULL; int i=0; - netbyte_store nb; + netbyte_store *nb=malloc(sizeof(netbyte_store)); printf("Start test\n"); - nb_init( &nb ); - f = fopen("test_many.nb","r"); - while (nb_fread( &nb, fileno(f) ) == 0) + f = fopen("test_kv_save.md","r"); + while (nb_fread( nb, fileno(f) ) == 0) { printf("ITER %d: ", i ); - pr_store( &nb ); + nb_print( nb ); i += 1; + } fclose( f ); + nb_free(nb); + + printf("End test\n"); return 0; }
\ No newline at end of file |