actube/src/cw/ktv.h

81 lines
2.1 KiB
C

#ifndef __KVT_H
#define __KVT_H
#include <stdint.h>
#include <stdio.h>
#include "mavl.h"
#define CW_KTV_MAX_KEY_LEN 1024
struct cw_KTV {
char *key;
const struct cw_Type *type;
union {
uint32_t dword;
uint16_t word;
uint8_t byte;
void *ptr;
} val;
};
typedef struct cw_KTV cw_KTV_t;
struct cw_Type {
/** A human readable name for this type */
const char *name;
/** A pointer to a function to delete elements of this type */
void (*del) (struct cw_KTV * data);
/** A method to put this object to a buffer */
int (*put) (const struct cw_KTV * data, uint8_t * dst);
/** The get method */
struct cw_KTV *(*get) (struct cw_KTV * data, const uint8_t * src, int len);
/** 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 */
int (*to_str) (const struct cw_KTV * data, char *dst, int max_len);
/** Cereate an item of this type from a string, which was previously
created by the #del function. */
struct cw_KTV *(*from_str) (struct cw_KTV * data, const char *src);
/*
int (*def)(void *, void *);
*/
};
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);
*/
const char *cw_ktv_add(mavl_t kvstore, const char *key, const struct cw_Type *type,
const uint8_t * data, int len);
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 *))
int cw_ktv_read_line (FILE *f, char * key, char * type, char *val);
#endif /* __KVT_H */