diff --git a/core/ini-wrapper.c b/core/ini-wrapper.c index 9bcab3f62189a9057e527975169b44ca67dbee21..4aabb21455259cb3e97c2619db07a9ecea12bad6 100644 --- a/core/ini-wrapper.c +++ b/core/ini-wrapper.c @@ -187,7 +187,7 @@ static int32_t section_set(dictionary *d, struct section_info *section) return 0; } -int32_t inifile_config_parse(const int8_t *ini_filename, struct ini_config *config) +int32_t inifile_config_parse(struct ini_config *config) { int32_t ret = 0; dictionary *d = NULL; @@ -197,16 +197,16 @@ int32_t inifile_config_parse(const int8_t *ini_filename, struct ini_config *conf int32_t i = 0; int32_t j = 0; - if ((NULL == ini_filename) || (NULL == config)) + if (NULL == config) { printf("invalid parameter\n"); return -1; } - d = iniparser_load(ini_filename); + d = iniparser_load(config->ini.filename); if (NULL == d) { - printf("parse %s failed\n", ini_filename); + printf("parse %s failed\n", config->ini.filename); return -1; } @@ -265,7 +265,7 @@ exit: return -1; } -int32_t inifile_config_save(const int8_t *ini_filename, struct ini_config *config) +int32_t inifile_config_save(struct ini_config *config) { int32_t ret = 0; dictionary *d = NULL; @@ -273,16 +273,16 @@ int32_t inifile_config_save(const int8_t *ini_filename, struct ini_config *confi int32_t i = 0; struct section_info *valid_section_ptr = NULL; - if ((NULL == ini_filename) || (NULL == config)) + if (NULL == config) { printf("invalid parameter\n"); return -1; } - d = iniparser_load(ini_filename); + d = iniparser_load(config->ini.filename); if (NULL == d) { - printf("parse %s failed\n", ini_filename); + printf("parse %s failed\n", config->ini.filename); return -1; } @@ -302,10 +302,10 @@ int32_t inifile_config_save(const int8_t *ini_filename, struct ini_config *confi valid_section_ptr = (struct section_info *)((uint8_t *)valid_section_ptr + valid_section_ptr->base.member_num * sizeof(struct member_info) + sizeof(struct section_info)); } - fp = fopen(ini_filename, "w"); + fp = fopen(config->ini.filename, "w"); if (NULL == fp) { - printf("open %s failed\n", ini_filename); + printf("open %s failed\n", config->ini.filename); goto exit; } diff --git a/core/ini-wrapper.h b/core/ini-wrapper.h index 88f913da192270bdd88685749761a51d70094d6c..a388f1d04c39b92be69e1fa1390bdb476c4343d6 100644 --- a/core/ini-wrapper.h +++ b/core/ini-wrapper.h @@ -9,6 +9,8 @@ #define MEMBER_NAME_SIZE (16) //ini 文件中,配置项的值为字符串时,字符串的最大长度 #define STRING_BUF_SIZE (64) +//ini 文件名称长度 +#define INI_FILENAME_LEN (32) //ini 文件中,配置项的值类型 enum value_type { @@ -42,6 +44,7 @@ struct section_baseinfo { //配置文件中的 section 个数 //使用 uint64_t 存储成员数量,是为了保持结构体对齐 struct ini_info { + int8_t filename[INI_FILENAME_LEN]; uint64_t section_num; }; @@ -76,26 +79,24 @@ struct ini_config { /** * 解析 ini 文件中的配置信息到结构体中 * - * @param ini_filename[in] ini 配置文件名称 * @param config[in/out] 传入配置文件中的section名称,输出配置文件中各配置项到结构体 * * @return int32_t 0:解析成功;-1:解析失败 * * @author sdc */ -extern int32_t inifile_config_parse(const int8_t *ini_filename, struct ini_config *config); +extern int32_t inifile_config_parse(struct ini_config *config); /** * 将配置信息存储到配置文件中 * - * @param ini_filename[in] ini 配置文件名称 * @param config[in] 各配置项内容 * * @return int32_t 0:配置存储成功;-1:配置存储失败 * * @author sdc */ -extern int32_t inifile_config_save(const int8_t *ini_filename, struct ini_config *config); +extern int32_t inifile_config_save(struct ini_config *config); #endif diff --git a/demo/main.c b/demo/main.c index 939dfce285751d009b822f4e119cc489fcb760f0..86ae7443dc54a828475f9aaa857a9142282116bc 100644 --- a/demo/main.c +++ b/demo/main.c @@ -12,7 +12,9 @@ #define MEMBER_NUM_OF_SECTION(section_type) ((sizeof(struct section_type) - sizeof(struct section_info)) / sizeof(struct member_info)) static struct sys_config ini_config = { - .base.ini.section_num = 2, + .base = { + .ini = {.filename = INI_FILENAME, .section_num = 2}, + }, .net = { .section.base = {.name = "ip", .member_num = MEMBER_NUM_OF_SECTION(network_info)}, .ip = {.name = "ip", .type = TYPE_STRING}, @@ -51,7 +53,7 @@ int32_t main(int argc, char *argv[]) printf("member num of ip:%ld, log:%ld\n", ini_config.net.section.base.member_num, ini_config.log.section.base.member_num); - ret = inifile_config_parse(INI_FILENAME, (struct ini_config *)&ini_config); + ret = inifile_config_parse((struct ini_config *)&ini_config); if (0 != ret) { printf("config file parse failed\n"); @@ -66,7 +68,7 @@ int32_t main(int argc, char *argv[]) ini_config.log.level.v.i32 = 5; ini_config.log.filesize.v.d64 = 10.1199; - ret = inifile_config_save(INI_FILENAME, (struct ini_config *)&ini_config); + ret = inifile_config_save((struct ini_config *)&ini_config); if (0 != ret) { printf("configuration save failed\n"); @@ -74,7 +76,7 @@ int32_t main(int argc, char *argv[]) } printf("ini config file save success\n"); - ret = inifile_config_parse(INI_FILENAME, (struct ini_config *)&ini_config); + ret = inifile_config_parse((struct ini_config *)&ini_config); if (0 != ret) { printf("config file parse failed\n"); @@ -89,7 +91,7 @@ int32_t main(int argc, char *argv[]) ini_config.log.level.v.i32 = 2; ini_config.log.filesize.v.d64 = 50.23451; - ret = inifile_config_save(INI_FILENAME, (struct ini_config *)&ini_config); + ret = inifile_config_save((struct ini_config *)&ini_config); if (0 != ret) { printf("configuration save failed\n");