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-04-04 09:25:16 +02:00
|
|
|
#include "bstr.h"
|
2018-03-19 17:26:01 +01:00
|
|
|
|
2018-04-08 16:48:13 +02:00
|
|
|
|
2018-03-19 17:26:01 +01:00
|
|
|
/**
|
2018-03-25 11:14:37 +02:00
|
|
|
* @addtogroup ALGOS
|
2018-03-19 17:26:01 +01:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-03-25 11:14:37 +02:00
|
|
|
* @defgroup KTV KTV Store (Key-Type-Value-Store)
|
2018-03-19 17:26:01 +01:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2018-03-12 11:22:06 +01:00
|
|
|
#define CW_KTV_MAX_KEY_LEN 1024
|
|
|
|
|
2018-03-25 08:34:32 +02:00
|
|
|
/**
|
|
|
|
* @struct cw_KTV
|
|
|
|
* @file ktv.h
|
|
|
|
* @brief Structure to store a key-type-value element.
|
|
|
|
*/
|
2018-03-12 11:22:06 +01:00
|
|
|
struct cw_KTV {
|
2018-03-25 08:34:32 +02:00
|
|
|
/** The key for this element. A string. */
|
2018-03-11 00:56:41 +01:00
|
|
|
char *key;
|
2018-03-25 08:34:32 +02:00
|
|
|
/** Teh type of this element. */
|
2018-03-12 11:22:06 +01:00
|
|
|
const struct cw_Type *type;
|
2018-03-25 08:34:32 +02:00
|
|
|
/** The value for this element */
|
2018-03-11 00:56:41 +01:00
|
|
|
union {
|
2018-03-25 08:34:32 +02:00
|
|
|
uint32_t dword;
|
2018-03-11 00:56:41 +01:00
|
|
|
uint16_t word;
|
|
|
|
uint8_t byte;
|
|
|
|
void *ptr;
|
2018-04-01 00:49:05 +02:00
|
|
|
char *str;
|
2018-03-17 12:32:40 +01:00
|
|
|
int boolean;
|
2018-04-17 07:46:09 +02:00
|
|
|
float float_val;
|
2018-03-11 00:56:41 +01:00
|
|
|
} val;
|
2018-04-25 10:43:27 +02:00
|
|
|
const void * valguard;
|
2018-03-11 00:56:41 +01:00
|
|
|
};
|
2018-03-12 11:22:06 +01:00
|
|
|
typedef struct cw_KTV cw_KTV_t;
|
2018-03-11 00:56:41 +01:00
|
|
|
|
2018-04-25 07:54:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-19 17:26:01 +01:00
|
|
|
/**
|
|
|
|
* @class cw_Type
|
|
|
|
* @author 7u83
|
|
|
|
* @file ktv.h
|
|
|
|
* @brief Representation of a cw_Type objetc
|
|
|
|
*/
|
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.
|
2018-03-19 17:26:01 +01:00
|
|
|
* This function is mainly used to store elements to an SQL database
|
|
|
|
* or to json strings
|
|
|
|
* @param data pointer to KTV object
|
|
|
|
* @param dst buffer where to put the string in
|
|
|
|
* @param max_len maximum length of buffer
|
|
|
|
* @return The number of bytes written to dst
|
|
|
|
*/
|
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-04-04 08:04:37 +02:00
|
|
|
|
|
|
|
void *(*data)(cw_KTV_t *);
|
2018-04-25 10:43:27 +02:00
|
|
|
const char * (*get_type_name)(cw_KTV_t*);
|
|
|
|
|
|
|
|
int (*cast)(cw_KTV_t *);
|
2018-03-15 20:07:17 +01:00
|
|
|
|
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
|
|
|
|
2018-03-25 08:34:32 +02:00
|
|
|
|
2018-04-25 10:43:27 +02:00
|
|
|
struct cw_KTVValRange {
|
|
|
|
int min,max;
|
|
|
|
const char * name;
|
|
|
|
};
|
|
|
|
typedef struct cw_KTVValRange cw_KTVValRange_t;
|
|
|
|
|
|
|
|
|
2018-03-25 08:34:32 +02:00
|
|
|
struct cw_KTVStruct {
|
|
|
|
const struct cw_Type * type;
|
|
|
|
const char * key;
|
|
|
|
int len;
|
|
|
|
int position;
|
2018-04-25 10:43:27 +02:00
|
|
|
const void * valguard;
|
2018-03-25 08:34:32 +02:00
|
|
|
};
|
2018-04-25 10:43:27 +02:00
|
|
|
typedef struct cw_KTVStruct cw_KTVStruct_t;
|
2018-04-08 16:48:13 +02:00
|
|
|
|
2018-04-25 07:54:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-08 16:48:13 +02:00
|
|
|
|
2018-04-24 07:02:23 +02:00
|
|
|
#define CW_KTVSTRUCT_L16 -2
|
|
|
|
#define CW_KTVSTRUCT_L8 -3
|
|
|
|
|
2018-04-08 16:48:13 +02:00
|
|
|
struct cw_KTVEnum{
|
|
|
|
int value;
|
|
|
|
const char * name;
|
2018-04-22 09:00:55 +02:00
|
|
|
const void * type;
|
|
|
|
int (*fun_in)();
|
|
|
|
int (*fun_out)();
|
2018-04-08 16:48:13 +02:00
|
|
|
};
|
|
|
|
typedef struct cw_KTVEnum cw_KTVEnum_t;
|
|
|
|
|
2018-04-22 09:00:55 +02:00
|
|
|
|
|
|
|
struct cw_KTVIndexed{
|
|
|
|
int idxpos;
|
|
|
|
void *type;
|
|
|
|
};
|
|
|
|
typedef struct cw_KTVIndexed cw_KTVIndexed_t;
|
|
|
|
|
2018-03-26 00:00:21 +02:00
|
|
|
int cw_ktv_read_struct(mavl_t ktv,const cw_KTVStruct_t * stru, const char *pkey,
|
2018-03-25 08:34:32 +02:00
|
|
|
uint8_t * data, int len);
|
2018-04-07 19:28:00 +02:00
|
|
|
int cw_ktv_write_struct(mavl_t ktv, const cw_KTVStruct_t * stru, const char *pkey,
|
|
|
|
uint8_t * dst);
|
|
|
|
|
|
|
|
|
2018-03-25 08:34:32 +02:00
|
|
|
|
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;
|
2018-04-01 00:49:05 +02:00
|
|
|
extern const struct cw_Type cw_type_str;
|
2018-03-21 18:50:07 +01:00
|
|
|
extern const struct cw_Type cw_type_ipaddress;
|
2018-03-30 11:12:50 +02:00
|
|
|
extern const struct cw_Type cw_type_sysptr;
|
2018-04-04 09:03:08 +02:00
|
|
|
extern const struct cw_Type cw_type_bool;
|
2018-03-11 00:56:41 +01:00
|
|
|
|
|
|
|
#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)
|
2018-03-21 18:50:07 +01:00
|
|
|
#define CW_TYPE_IPADDRESS (&cw_type_ipaddress)
|
2018-03-30 11:12:50 +02:00
|
|
|
#define CW_TYPE_SYSPTR (&cw_type_sysptr)
|
2018-04-01 00:49:05 +02:00
|
|
|
#define CW_TYPE_STR (&cw_type_str)
|
2018-04-04 09:03:08 +02:00
|
|
|
#define CW_TYPE_BOOL (&cw_type_bool)
|
2018-03-11 00:56:41 +01:00
|
|
|
/*
|
|
|
|
void cw_kvstore_mavl_delete(const void *data);
|
|
|
|
*/
|
2018-04-25 10:43:27 +02:00
|
|
|
cw_KTV_t *cw_ktv_add(mavl_t kvstore, const char *key, const struct cw_Type *type,
|
|
|
|
const void * valguard,
|
2018-03-11 00:56:41 +01:00
|
|
|
const uint8_t * data, int len);
|
|
|
|
|
2018-04-24 07:02:23 +02:00
|
|
|
void cw_ktv_del_sub(mavl_t ktvstore, const char *basekey);
|
|
|
|
|
2018-04-01 12:43:42 +02:00
|
|
|
cw_KTV_t * cw_ktv_replace(mavl_t kvtstore, const char *key, const struct cw_Type *type,
|
2018-04-25 10:43:27 +02:00
|
|
|
const void * valguard,
|
2018-04-01 12:43:42 +02:00
|
|
|
const uint8_t * data, int len);
|
|
|
|
|
2018-04-25 10:43:27 +02:00
|
|
|
const char * cw_ktv_add_from_str(mavl_t kvtstore, const char *key, const struct cw_Type *type,
|
|
|
|
const void * valguard,
|
2018-03-17 12:32:40 +01:00
|
|
|
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);
|
|
|
|
|
2018-03-19 17:26:01 +01:00
|
|
|
/**
|
|
|
|
* Create a KTV store
|
|
|
|
* @return a #mavl_t object representing the KTV store
|
|
|
|
*/
|
2018-03-12 11:22:06 +01:00
|
|
|
#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);
|
2018-04-04 10:27:56 +02:00
|
|
|
uint8_t cw_ktv_get_bool(mavl_t ktv,const char *key, uint8_t def);
|
2018-04-01 12:43:42 +02:00
|
|
|
cw_KTV_t * cw_ktv_set_byte(mavl_t ktv,const char * key, uint8_t byte);
|
2018-04-04 19:23:40 +02:00
|
|
|
cw_KTV_t * cw_ktv_set_dword(mavl_t ktv,const char * key, uint32_t dword);
|
|
|
|
cw_KTV_t * cw_ktv_set_word(mavl_t ktv,const char * key, uint16_t word);
|
|
|
|
|
2018-04-04 09:25:16 +02:00
|
|
|
bstr16_t cw_ktv_get_bstr16(mavl_t ktv,const char *key, bstr16_t def);
|
2018-04-01 12:43:42 +02:00
|
|
|
|
2018-03-17 12:32:40 +01:00
|
|
|
uint16_t cw_ktv_get_word(mavl_t ktv,const char *key, uint16_t def);
|
2018-03-31 08:37:18 +02:00
|
|
|
uint32_t cw_ktv_get_dword(mavl_t ktv,const char *key, uint32_t def);
|
|
|
|
void * cw_ktv_get_sysptr(mavl_t ktv,const char *key, void * def);
|
2018-04-01 10:06:43 +02:00
|
|
|
char * cw_ktv_get_str(mavl_t ktv,const char *key, char * def);
|
2018-03-21 20:01:36 +01:00
|
|
|
|
2018-04-14 00:50:58 +02:00
|
|
|
int cw_ktv_idx_get(mavl_t ktv, const char *key);
|
2018-04-17 07:46:09 +02:00
|
|
|
cw_KTV_t * cw_ktv_base_exists(mavl_t ktvstore, const char *basekey);
|
2018-04-19 11:03:18 +02:00
|
|
|
int cw_ktv_save(mavl_t ktvstore, const char * filename);
|
|
|
|
|
2018-03-17 12:32:40 +01:00
|
|
|
extern const cw_Type_t * cw_ktv_std_types[];
|
|
|
|
#define CW_KTV_STD_TYPES cw_ktv_std_types
|
|
|
|
|
2018-03-19 17:26:01 +01:00
|
|
|
/**
|
|
|
|
* @} KTV
|
2018-03-25 11:14:37 +02:00
|
|
|
* @} ALGOS
|
2018-03-19 17:26:01 +01:00
|
|
|
*/
|
2018-03-11 00:56:41 +01:00
|
|
|
|
|
|
|
#endif /* __KVT_H */
|