1 #ifndef R2_CONFIG_H
2 #define R2_CONFIG_H
3 
4 #include "r_types.h"
5 #include "r_util.h"
6 
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 
11 R_LIB_VERSION_HEADER(r_config);
12 
13 #define CN_BOOL  0x000001
14 #define CN_INT   0x000002
15 #define CN_STR   0x000008
16 #define CN_RO    0x000010
17 #define CN_RW    0x000020
18 
19 #if LEFTOVER_FOR_MY_FUTURE_SELF
20 // type != attrib
21 #define R_CONFIG_NODE_TYPE_BOOL  0x000001
22 #define R_CONFIG_NODE_TYPE_INT   0x000002
23 #define R_CONFIG_NODE_TYPE_STR   0x000008
24 #define R_CONFIG_NODE_TYPE_RO    0x000010
25 #define R_CONFIG_NODE_TYPE_RW    0x000020
26 #endif
27 
28 typedef bool (*RConfigCallback)(void *user, void *data);
29 
30 typedef struct r_config_node_t {
31 	char *name;
32 	ut32 flags;
33 	char *value;
34 	ut64 i_value;
35 	ut64 *cb_ptr_q;
36 	int *cb_ptr_i;
37 	char **cb_ptr_s;
38 	RConfigCallback getter;
39 	RConfigCallback setter;
40 	char *desc;
41 	RList *options;
42 } RConfigNode;
43 
44 R_API const char *r_config_node_type(RConfigNode *node);
45 
46 typedef struct r_config_t {
47 	void *user;
48 	RNum *num;
49 	PrintfCallback cb_printf;
50 	RList *nodes;
51 	HtPP *ht;
52 	bool lock;
53 } RConfig;
54 
55 typedef struct r_config_hold_t {
56 	RConfig *cfg;
57 	RList *list;
58 } RConfigHold;
59 
60 #ifdef R_API
61 
62 // TODO: simplify this API?
63 R_API RConfigHold* r_config_hold_new(RConfig *cfg);
64 R_API bool r_config_hold(RConfigHold *h, ...);
65 R_API void r_config_hold_free(RConfigHold *h);
66 R_API void r_config_hold_restore(RConfigHold *h);
67 
68 R_API RConfig *r_config_new(void *user);
69 R_API RConfig *r_config_clone(RConfig *cfg);
70 R_API void r_config_free(RConfig *cfg);
71 
72 R_API void r_config_lock(RConfig *cfg, bool lock);
73 R_API bool r_config_eval(RConfig *cfg, const char *str, bool many);
74 R_API void r_config_bump(RConfig *cfg, const char *key);
75 R_API bool r_config_get_b(RConfig *cfg, const char *name);
76 R_API RConfigNode* r_config_set_b(RConfig *cfg, const char *name, bool b);
77 R_API RConfigNode *r_config_set_i(RConfig *cfg, const char *name, const ut64 i);
78 R_API RConfigNode *r_config_set_cb(RConfig *cfg, const char *name, const char *value, RConfigCallback cb);
79 R_API RConfigNode *r_config_set_i_cb(RConfig *cfg, const char *name, int ivalue, RConfigCallback cb);
80 R_API RConfigNode *r_config_set(RConfig *cfg, const char *name, const char *value);
81 R_API bool r_config_rm(RConfig *cfg, const char *name);
82 R_API ut64 r_config_get_i(RConfig *cfg, const char *name);
83 R_API const char *r_config_get(RConfig *cfg, const char *name);
84 R_API const char *r_config_desc(RConfig *cfg, const char *name, const char *desc);
85 R_API void r_config_list(RConfig *cfg, const char *str, int rad);
86 
87 R_API bool r_config_toggle(RConfig *cfg, const char *name);
88 R_API bool r_config_readonly(RConfig *cfg, const char *key);
89 
90 // TODO Move to RConfigNode for consistency
91 R_API bool r_config_set_setter(RConfig *cfg, const char *key, RConfigCallback cb);
92 R_API bool r_config_set_getter(RConfig *cfg, const char *key, RConfigCallback cb);
93 
94 R_API void r_config_serialize(R_NONNULL RConfig *config, R_NONNULL Sdb *db);
95 R_API bool r_config_unserialize(R_NONNULL RConfig *config, R_NONNULL Sdb *db, R_NULLABLE char **err);
96 
97 // RConfigNode
98 R_API const char *r_config_node_desc(RConfigNode *node, const char *desc);
99 R_API char *r_config_node_to_string(RConfigNode *node);
100 R_API void r_config_node_add_option(RConfigNode *node, const char *option);
101 R_API void r_config_node_purge_options(RConfigNode *node);
102 
103 R_API RConfigNode *r_config_node_get(RConfig *cfg, const char *name);
104 R_API RConfigNode *r_config_node_new(const char *name, const char *value);
105 R_API void r_config_node_free(void *n);
106 R_API void r_config_node_value_format_i(char *buf, size_t buf_size, const ut64 i, R_NULLABLE RConfigNode *node);
107 
r_config_node_is_bool(RConfigNode * node)108 static inline bool r_config_node_is_bool(RConfigNode *node) {
109 	return node->flags & CN_BOOL;
110 }
r_config_node_is_int(RConfigNode * node)111 static inline bool r_config_node_is_int(RConfigNode *node) {
112 	return node->flags & CN_INT;
113 }
r_config_node_is_ro(RConfigNode * node)114 static inline bool r_config_node_is_ro(RConfigNode *node) {
115 	return node->flags & CN_RO;
116 }
r_config_node_is_str(RConfigNode * node)117 static inline bool r_config_node_is_str(RConfigNode *node) {
118 	return node->flags & CN_STR;
119 }
120 #endif
121 
122 #ifdef __cplusplus
123 }
124 #endif
125 
126 #endif
127