improve conf_io

This commit is contained in:
lixianjing 2020-06-14 17:49:37 +08:00
parent 5471d24c5e
commit 7229977695
7 changed files with 88 additions and 40 deletions

View File

@ -3,6 +3,7 @@
* 2020/06/14
* 完善 scroll view增加滚动相关事件。
* 增加文档《如何实现弹出菜单》
* 完善conf-io支持reload。
* 2020/06/13
* 完善资源生成脚本,兼容 AWTK 和 Designer感谢朝泽提供补丁

View File

@ -23,6 +23,7 @@
#include "tkc/utf8.h"
#include "tkc/mutex.h"
#include "tkc/object_locker.h"
#include "conf_io/conf_obj.h"
#include "conf_io/app_conf.h"
static object_t* s_conf;
@ -42,7 +43,11 @@ object_t* app_conf_get_instance(void) {
}
ret_t app_conf_save(void) {
return object_exec(s_conf, "save", NULL);
return object_exec(s_conf, CONF_CMD_SAVE, NULL);
}
ret_t app_conf_reload(void) {
return object_exec(s_conf, CONF_CMD_RELOAD, NULL);
}
ret_t app_conf_deinit(void) {

View File

@ -70,6 +70,16 @@ object_t* app_conf_get_instance(void);
*/
ret_t app_conf_save(void);
/**
* @method app_conf_reload
* ()
*
* @annotation ["static", "scriptable"]
*
* @return {ret_t} RET_OK表示成功
*/
ret_t app_conf_reload(void);
/**
* @method app_conf_on_changed
*

View File

@ -237,6 +237,30 @@ ret_t conf_doc_move_down(conf_doc_t* doc, const char* path);
*/
bool_t conf_doc_exists(conf_doc_t* doc, const char* path);
/**
* @method conf_doc_is_first
*
*
*
* @param {conf_doc_t*} doc
* @param {const char*} path
*
* @return {bool_t} TRUE表示是
*/
bool_t conf_doc_is_first(conf_doc_t* doc, const char* path);
/**
* @method conf_doc_is_last
*
*
*
* @param {conf_doc_t*} doc
* @param {const char*} path
*
* @return {bool_t} TRUE表示是
*/
bool_t conf_doc_is_last(conf_doc_t* doc, const char* path);
/**
* @method conf_doc_destroy
*
@ -495,30 +519,6 @@ conf_node_t* conf_node_get_first_child(conf_node_t* node);
*/
ret_t conf_node_set_first_child(conf_node_t* node, conf_node_t* child);
/**
* @method conf_doc_is_first
*
*
*
* @param {conf_node_t*} node
* @param {const char*} path
*
* @return {bool_t} TRUE表示是
*/
bool_t conf_doc_is_first(conf_doc_t* doc, const char* path);
/**
* @method conf_doc_is_last
*
*
*
* @param {conf_node_t*} node
* @param {const char*} path
*
* @return {bool_t} TRUE表示是
*/
bool_t conf_doc_is_last(conf_doc_t* doc, const char* path);
#define CONF_NODE_ROOT_NAME "root"
#define CONF_SPECIAL_ATTR_SIZE "#size"

View File

@ -75,21 +75,6 @@ static ret_t conf_obj_get_prop(object_t* obj, const char* name, value_t* v) {
return conf_doc_get(o->doc, name, v);
}
static bool_t conf_obj_can_exec(object_t* obj, const char* name, const char* args) {
conf_obj_t* o = CONF_OBJ(obj);
return_value_if_fail(o != NULL, RET_BAD_PARAMS);
if (tk_str_ieq(name, CONF_CMD_SAVE)) {
return TRUE;
} else if (tk_str_ieq(name, CONF_CMD_MOVE_UP)) {
return !conf_doc_is_first(o->doc, name);
} else if (tk_str_ieq(name, CONF_CMD_MOVE_DOWN)) {
return !conf_doc_is_last(o->doc, name);
}
return FALSE;
}
ret_t conf_obj_save(object_t* obj) {
ret_t ret = RET_FAIL;
data_writer_t* writer = NULL;
@ -123,12 +108,41 @@ static ret_t conf_obj_load(object_t* obj) {
return RET_OK;
}
static ret_t conf_obj_reload(object_t* obj) {
conf_obj_t* o = CONF_OBJ(obj);
return_value_if_fail(o != NULL, RET_BAD_PARAMS);
conf_doc_destroy(o->doc);
o->doc = NULL;
return conf_obj_load(obj);
}
static bool_t conf_obj_can_exec(object_t* obj, const char* name, const char* args) {
conf_obj_t* o = CONF_OBJ(obj);
return_value_if_fail(o != NULL, RET_BAD_PARAMS);
if (tk_str_ieq(name, CONF_CMD_SAVE)) {
return TRUE;
} else if (tk_str_ieq(name, CONF_CMD_RELOAD)) {
return TRUE;
} else if (tk_str_ieq(name, CONF_CMD_MOVE_UP)) {
return !conf_doc_is_first(o->doc, name);
} else if (tk_str_ieq(name, CONF_CMD_MOVE_DOWN)) {
return !conf_doc_is_last(o->doc, name);
}
return FALSE;
}
static ret_t conf_obj_exec(object_t* obj, const char* name, const char* args) {
conf_obj_t* o = CONF_OBJ(obj);
return_value_if_fail(o != NULL, RET_BAD_PARAMS);
if (tk_str_ieq(name, CONF_CMD_SAVE)) {
return conf_obj_save(obj);
} else if (tk_str_ieq(name, CONF_CMD_RELOAD)) {
return conf_obj_reload(obj);
} else if (tk_str_ieq(name, CONF_CMD_MOVE_UP)) {
return conf_obj_move_up(obj, args);
} else if (tk_str_ieq(name, CONF_CMD_MOVE_DOWN)) {

View File

@ -82,6 +82,9 @@ ret_t conf_obj_save(object_t* conf);
/*保存命令*/
#define CONF_CMD_SAVE "save"
/*重新加载命令*/
#define CONF_CMD_RELOAD "reload"
/*和前一个兄弟节点交换位置*/
#define CONF_CMD_MOVE_UP "move_up"

View File

@ -75,3 +75,18 @@ TEST(AppConf, wstr) {
ASSERT_EQ(app_conf_deinit(), RET_OK);
}
TEST(AppConf, reload) {
app_conf_init_ini("conf_test");
ASSERT_EQ(app_conf_set_int("int", 123), RET_OK);
ASSERT_EQ(app_conf_save(), RET_OK);
ASSERT_EQ(app_conf_set_int("int", 456), RET_OK);
ASSERT_EQ(app_conf_get_int("int", 0), 456);
ASSERT_EQ(app_conf_reload(), RET_OK);
ASSERT_EQ(app_conf_get_int("int", 0), 123);
ASSERT_EQ(app_conf_deinit(), RET_OK);
}