From 1cd9d1cd2e182da5bbdaace8f26399d4d3c34dd2 Mon Sep 17 00:00:00 2001 From: sdc <873892935@qq.com> Date: Wed, 24 Nov 2021 10:05:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86ini=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=90=8D=E7=A7=B0=E9=85=8D=E7=BD=AE=E4=B8=BA=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E4=BD=93=E5=86=85=E9=83=A8=EF=BC=8C=E5=A4=96=E9=83=A8?= =?UTF-8?q?=E6=97=A0=E9=9C=80=E4=BB=A5=E5=8F=82=E6=95=B0=E4=BC=A0=E5=85=A5?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/ini-wrapper.c | 20 ++++++++++---------- core/ini-wrapper.h | 9 +++++---- demo/main.c | 12 +++++++----- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/core/ini-wrapper.c b/core/ini-wrapper.c index 9bcab3f..4aabb21 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 88f913d..a388f1d 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 939dfce..86ae744 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"); -- Gitee