mirror of
https://gitee.com/zlgopen/awtk.git
synced 2024-11-30 02:58:26 +08:00
improve conf-io
This commit is contained in:
parent
547e74494c
commit
6accab6aca
@ -1,5 +1,10 @@
|
||||
# 最新动态
|
||||
|
||||
* 2020/06/20
|
||||
* 完善conf-io,修改json文件不存在的问题。
|
||||
* 完善conf-io,支持自读模式。
|
||||
* 完善DLL导出符号。
|
||||
|
||||
* 2020/06/18
|
||||
* 增加文档《内存分配》
|
||||
* 完善 mem allocator pool
|
||||
|
@ -59,6 +59,7 @@ ret_t app_conf_init(conf_load_t load, const char* app_name, const char* extname)
|
||||
return_value_if_fail(obj != NULL, RET_FAIL);
|
||||
|
||||
app_conf_set_instance(obj);
|
||||
OBJECT_UNREF(obj);
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
@ -35,6 +35,7 @@ typedef struct _conf_obj_t {
|
||||
conf_doc_t* doc;
|
||||
conf_doc_save_t save;
|
||||
conf_doc_load_t load;
|
||||
bool_t readonly;
|
||||
} conf_obj_t;
|
||||
|
||||
static conf_obj_t* conf_obj_cast(object_t* obj);
|
||||
@ -58,6 +59,10 @@ static ret_t conf_obj_remove_prop(object_t* obj, const char* name) {
|
||||
conf_obj_t* o = CONF_OBJ(obj);
|
||||
return_value_if_fail(o != NULL, RET_BAD_PARAMS);
|
||||
|
||||
if (o->readonly) {
|
||||
return RET_NOT_IMPL;
|
||||
}
|
||||
|
||||
return conf_doc_remove(o->doc, name);
|
||||
}
|
||||
|
||||
@ -65,6 +70,10 @@ static ret_t conf_obj_set_prop(object_t* obj, const char* name, const value_t* v
|
||||
conf_obj_t* o = CONF_OBJ(obj);
|
||||
return_value_if_fail(o != NULL, RET_BAD_PARAMS);
|
||||
|
||||
if (o->readonly) {
|
||||
return RET_NOT_IMPL;
|
||||
}
|
||||
|
||||
return conf_doc_set(o->doc, name, v);
|
||||
}
|
||||
|
||||
@ -139,6 +148,11 @@ static ret_t conf_obj_reload(object_t* 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 (o->readonly) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
if (tk_str_ieq(name, CONF_CMD_SAVE)) {
|
||||
return TRUE;
|
||||
@ -158,6 +172,10 @@ 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 (o->readonly) {
|
||||
return RET_NOT_IMPL;
|
||||
}
|
||||
|
||||
if (tk_str_ieq(name, CONF_CMD_SAVE)) {
|
||||
ret = conf_obj_save(obj);
|
||||
} else if (tk_str_ieq(name, CONF_CMD_RELOAD)) {
|
||||
@ -243,3 +261,11 @@ object_t* conf_obj_create(conf_doc_save_t save, conf_doc_load_t load, const char
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
ret_t conf_obj_set_readonly(object_t* conf, bool_t readonly) {
|
||||
conf_obj_t* o = CONF_OBJ(conf);
|
||||
return_value_if_fail(o != NULL, RET_BAD_PARAMS);
|
||||
o->readonly = readonly;
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
@ -79,6 +79,18 @@ conf_doc_t* conf_obj_get_doc(object_t* conf);
|
||||
*/
|
||||
ret_t conf_obj_save(object_t* conf);
|
||||
|
||||
/**
|
||||
* @method conf_obj_set_readonly
|
||||
*
|
||||
* 设置为只读模式。
|
||||
*
|
||||
* @param {object_t*} conf conf对象。
|
||||
* @param {bool_t} readonly 只读模式。
|
||||
*
|
||||
* @return {ret_t} 返回RET_OK表示成功,否则表示失败
|
||||
*/
|
||||
ret_t conf_obj_set_readonly(object_t* conf, bool_t readonly);
|
||||
|
||||
/*保存命令*/
|
||||
#define CONF_CMD_SAVE "save"
|
||||
|
||||
|
@ -150,8 +150,6 @@ object_t* object_locker_create(object_t* obj) {
|
||||
wrapper->obj = object_ref(obj);
|
||||
emitter_on(EMITTER(obj), EVT_ITEMS_CHANGED, object_locker_forward_events, o);
|
||||
emitter_on(EMITTER(obj), EVT_PROPS_CHANGED, object_locker_forward_events, o);
|
||||
} else {
|
||||
OBJECT_UNREF(wrapper->obj);
|
||||
}
|
||||
|
||||
return o;
|
||||
|
@ -15,7 +15,7 @@
|
||||
/**
|
||||
* History:
|
||||
* ================================================================
|
||||
* 2020-06-20 Li XianJing <xianjimli@hotmail.com> created
|
||||
* 2020-06-12 Li XianJing <xianjimli@hotmail.com> created
|
||||
*
|
||||
*/
|
||||
|
||||
|
@ -341,3 +341,20 @@ TEST(Ini, move_up) {
|
||||
|
||||
conf_doc_destroy(doc);
|
||||
}
|
||||
|
||||
TEST(Ini, readonly) {
|
||||
object_t* conf = conf_ini_load("file://./tests/testdata/test.ini", TRUE);
|
||||
|
||||
ASSERT_EQ(object_set_prop_str(conf, "tom.name", "tom"), RET_OK);
|
||||
ASSERT_EQ(object_remove_prop(conf, "tom.name"), RET_OK);
|
||||
|
||||
ASSERT_EQ(conf_obj_set_readonly(conf, TRUE), RET_OK);
|
||||
ASSERT_NE(object_set_prop_str(conf, "tom.name", "tom"), RET_OK);
|
||||
ASSERT_NE(object_remove_prop(conf, "tom.name"), RET_OK);
|
||||
|
||||
ASSERT_EQ(conf_obj_set_readonly(conf, FALSE), RET_OK);
|
||||
ASSERT_EQ(object_set_prop_str(conf, "tom.name", "tom"), RET_OK);
|
||||
ASSERT_EQ(object_remove_prop(conf, "tom.name"), RET_OK);
|
||||
|
||||
OBJECT_UNREF(conf);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user