2018-03-11 00:56:41 +01:00
|
|
|
#ifndef __KVT_H
|
|
|
|
#define __KVT_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2018-03-12 11:22:06 +01:00
|
|
|
#include <stdio.h>
|
2018-03-11 00:56:41 +01:00
|
|
|
|
|
|
|
#include "mavl.h"
|
|
|
|
|
2018-03-12 11:22:06 +01:00
|
|
|
#define CW_KTV_MAX_KEY_LEN 1024
|
|
|
|
|
|
|
|
struct cw_KTV {
|
2018-03-11 00:56:41 +01:00
|
|
|
char *key;
|
2018-03-12 11:22:06 +01:00
|
|
|
const struct cw_Type *type;
|
2018-03-11 00:56:41 +01:00
|
|
|
union {
|
|
|
|
uint32_t dword;
|
|
|
|
uint16_t word;
|
|
|
|
uint8_t byte;
|
|
|
|
void *ptr;
|
2018-03-17 12:32:40 +01:00
|
|
|
int boolean;
|
2018-03-11 00:56:41 +01:00
|
|
|
} val;
|
|
|
|
};
|
2018-03-12 11:22:06 +01:00
|
|
|
typedef struct cw_KTV cw_KTV_t;
|
2018-03-11 00:56:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
struct cw_Type {
|
|
|
|
/** A human readable name for this type */
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
/** A pointer to a function to delete elements of this type */
|
2018-03-12 11:22:06 +01:00
|
|
|
void (*del) (struct cw_KTV * data);
|
2018-03-11 00:56:41 +01:00
|
|
|
|
|
|
|
/** A method to put this object to a buffer */
|
2018-03-12 11:22:06 +01:00
|
|
|
int (*put) (const struct cw_KTV * data, uint8_t * dst);
|
2018-03-11 00:56:41 +01:00
|
|
|
|
|
|
|
/** The get method */
|
2018-03-12 11:22:06 +01:00
|
|
|
struct cw_KTV *(*get) (struct cw_KTV * data, const uint8_t * src, int len);
|
2018-03-11 00:56:41 +01:00
|
|
|
|
|
|
|
/** A pointer to a function to convert elements of this type to a string.
|
|
|
|
This function is mainly used to store elements to an SQL database
|
|
|
|
or to json strings */
|
2018-03-12 11:22:06 +01:00
|
|
|
int (*to_str) (const struct cw_KTV * data, char *dst, int max_len);
|
2018-03-11 00:56:41 +01:00
|
|
|
|
|
|
|
/** Cereate an item of this type from a string, which was previously
|
|
|
|
created by the #del function. */
|
2018-03-12 11:22:06 +01:00
|
|
|
struct cw_KTV *(*from_str) (struct cw_KTV * data, const char *src);
|
2018-03-11 00:56:41 +01:00
|
|
|
|
2018-03-15 20:07:17 +01:00
|
|
|
|
|
|
|
int (*len)(cw_KTV_t *);
|
|
|
|
|
2018-03-11 00:56:41 +01:00
|
|
|
};
|
2018-03-15 20:07:17 +01:00
|
|
|
typedef struct cw_Type cw_Type_t;
|
2018-03-11 00:56:41 +01:00
|
|
|
|
|
|
|
extern const struct cw_Type cw_type_byte;
|
|
|
|
extern const struct cw_Type cw_type_word;
|
|
|
|
extern const struct cw_Type cw_type_dword;
|
|
|
|
extern const struct cw_Type cw_type_bstr16;
|
|
|
|
|
|
|
|
#define CW_TYPE_BYTE (&cw_type_byte)
|
|
|
|
#define CW_TYPE_WORD (&cw_type_word)
|
|
|
|
#define CW_TYPE_DWORD (&cw_type_dword)
|
|
|
|
#define CW_TYPE_BSTR16 (&cw_type_bstr16)
|
|
|
|
|
|
|
|
/*
|
|
|
|
void cw_kvstore_mavl_delete(const void *data);
|
|
|
|
*/
|
2018-03-12 11:22:06 +01:00
|
|
|
const char *cw_ktv_add(mavl_t kvstore, const char *key, const struct cw_Type *type,
|
2018-03-11 00:56:41 +01:00
|
|
|
const uint8_t * data, int len);
|
|
|
|
|
2018-03-17 12:32:40 +01:00
|
|
|
const char * cw_ktv_add_from_str(mavl_t kvtstore, const char *key, const struct cw_Type *type,
|
|
|
|
const char * str);
|
|
|
|
|
2018-03-12 11:22:06 +01:00
|
|
|
int cw_ktv_mavlcmp(const void *v1, const void *v2);
|
|
|
|
int cw_ktv_mavlcmp_type_by_name(const void *v1,const void *v2);
|
|
|
|
|
|
|
|
void cw_ktv_mavldel(void *data);
|
|
|
|
|
|
|
|
#define cw_ktv_create()\
|
|
|
|
mavl_create(cw_ktv_mavlcmp, cw_ktv_mavldel, sizeof(cw_KTV_t))
|
|
|
|
|
|
|
|
#define cw_ktv_create_types_tree()\
|
|
|
|
mavl_create(cw_ktv_mavlcmp_type_by_name,NULL,sizeof(struct cw_Type *))
|
2018-03-11 00:56:41 +01:00
|
|
|
|
2018-03-12 11:22:06 +01:00
|
|
|
int cw_ktv_read_line (FILE *f, char * key, char * type, char *val);
|
2018-03-15 20:07:17 +01:00
|
|
|
int cw_ktv_read_file(FILE * file, mavl_t ktv, mavl_t types);
|
|
|
|
cw_KTV_t * cw_ktv_get(mavl_t ktv, const char *key, const cw_Type_t * type);
|
2018-03-17 12:32:40 +01:00
|
|
|
uint8_t cw_ktv_get_byte(mavl_t ktv,const char *key, uint8_t def);
|
|
|
|
uint16_t cw_ktv_get_word(mavl_t ktv,const char *key, uint16_t def);
|
|
|
|
|
|
|
|
void cw_ktv_dump(mavl_t ktv, uint32_t dbglevel,
|
|
|
|
const char *header, const char *prefix, const char *footer );
|
|
|
|
|
|
|
|
extern const cw_Type_t * cw_ktv_std_types[];
|
|
|
|
#define CW_KTV_STD_TYPES cw_ktv_std_types
|
|
|
|
|
2018-03-11 00:56:41 +01:00
|
|
|
|
|
|
|
#endif /* __KVT_H */
|