From patchwork Fri Mar 31 13:51:59 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Allain Legacy X-Patchwork-Id: 23020 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [IPv6:::1]) by dpdk.org (Postfix) with ESMTP id 9D4402C31; Fri, 31 Mar 2017 15:52:46 +0200 (CEST) Received: from mail1.windriver.com (mail1.windriver.com [147.11.146.13]) by dpdk.org (Postfix) with ESMTP id CC6062BDF for ; Fri, 31 Mar 2017 15:52:27 +0200 (CEST) Received: from ALA-HCA.corp.ad.wrs.com (ala-hca.corp.ad.wrs.com [147.11.189.40]) by mail1.windriver.com (8.15.2/8.15.1) with ESMTPS id v2VDqP2s004027 (version=TLSv1 cipher=AES128-SHA bits=128 verify=FAIL); Fri, 31 Mar 2017 06:52:25 -0700 (PDT) Received: from yow-cgts4-lx.wrs.com (128.224.145.137) by ALA-HCA.corp.ad.wrs.com (147.11.189.50) with Microsoft SMTP Server (TLS) id 14.3.294.0; Fri, 31 Mar 2017 06:52:25 -0700 From: Allain Legacy To: , CC: , , Date: Fri, 31 Mar 2017 09:51:59 -0400 Message-ID: <20170331135203.117461-3-allain.legacy@windriver.com> X-Mailer: git-send-email 2.12.1 In-Reply-To: <20170331135203.117461-1-allain.legacy@windriver.com> References: <20170330185407.61220-1-allain.legacy@windriver.com> <20170331135203.117461-1-allain.legacy@windriver.com> MIME-Version: 1.0 X-Originating-IP: [128.224.145.137] Subject: [dpdk-dev] [PATCH v5 2/6] cfgfile: add support for global properties section X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" The current implementation of the cfgfile library requires that all key=value pairs be within [SECTION] definitions. The ini file standard allows for key=value pairs in an unnamed section. https://en.wikipedia.org/wiki/INI_file#Global_properties This commit adds the capability of parsing key=value pairs from such an unnamed section. The CFG_FLAG_GLOBAL_SECTION flag must be passed to the rte_cfgfile_load() API to enable this functionality. Any key=value pairs found before the first section can be accessed in the section named "GLOBAL". Signed-off-by: Allain Legacy Acked-by: Cristian Dumitrescu --- lib/librte_cfgfile/rte_cfgfile.c | 16 ++++++++++++++++ lib/librte_cfgfile/rte_cfgfile.h | 13 ++++++++++++- test/test/test_cfgfile.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/lib/librte_cfgfile/rte_cfgfile.c b/lib/librte_cfgfile/rte_cfgfile.c index 829109a77..832fea829 100644 --- a/lib/librte_cfgfile/rte_cfgfile.c +++ b/lib/librte_cfgfile/rte_cfgfile.c @@ -107,6 +107,22 @@ rte_cfgfile_load(const char *filename, int flags) memset(cfg->sections, 0, sizeof(cfg->sections[0]) * allocated_sections); + if (flags & CFG_FLAG_GLOBAL_SECTION) { + curr_section = 0; + allocated_entries = CFG_ALLOC_ENTRY_BATCH; + cfg->sections[curr_section] = malloc( + sizeof(*cfg->sections[0]) + + sizeof(cfg->sections[0]->entries[0]) * + allocated_entries); + if (cfg->sections[curr_section] == NULL) { + printf("Error - no memory for global section\n"); + goto error1; + } + + snprintf(cfg->sections[curr_section]->name, + sizeof(cfg->sections[0]->name), "GLOBAL"); + } + while (fgets(buffer, sizeof(buffer), f) != NULL) { char *pos = NULL; size_t len = strnlen(buffer, sizeof(buffer)); diff --git a/lib/librte_cfgfile/rte_cfgfile.h b/lib/librte_cfgfile/rte_cfgfile.h index b40e6a135..2dadd75d4 100644 --- a/lib/librte_cfgfile/rte_cfgfile.h +++ b/lib/librte_cfgfile/rte_cfgfile.h @@ -66,13 +66,24 @@ struct rte_cfgfile_entry { char value[CFG_VALUE_LEN]; /**< Value */ }; +/**@{ cfgfile load operation flags */ +enum { + /** + * Indicates that the file supports key value entries before the first + * defined section. These entries can be accessed in the "GLOBAL" + * section. + */ + CFG_FLAG_GLOBAL_SECTION = 1, +}; +/**@} */ + /** * Open config file * * @param filename * Config file name * @param flags -* Config file flags, Reserved for future use. Must be set to 0. +* Config file flags * @return * Handle to configuration file on success, NULL otherwise */ diff --git a/test/test/test_cfgfile.c b/test/test/test_cfgfile.c index faca89696..a7de3706c 100644 --- a/test/test/test_cfgfile.c +++ b/test/test/test_cfgfile.c @@ -163,6 +163,36 @@ test_cfgfile_missing_section(void) } static int +test_cfgfile_global_properties(void) +{ + struct rte_cfgfile *cfgfile; + const char *value; + int ret; + + cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/missing_section.ini", + CFG_FLAG_GLOBAL_SECTION); + TEST_ASSERT_NOT_NULL(cfgfile, "Failed to load config file"); + + ret = rte_cfgfile_num_sections(cfgfile, NULL, 0); + TEST_ASSERT(ret == 1, "Unexpected number of sections: %d", ret); + + ret = rte_cfgfile_has_section(cfgfile, "GLOBAL"); + TEST_ASSERT(ret, "global section missing"); + + ret = rte_cfgfile_section_num_entries(cfgfile, "GLOBAL"); + TEST_ASSERT(ret == 1, "GLOBAL unexpected number of entries: %d", ret); + + value = rte_cfgfile_get_entry(cfgfile, "GLOBAL", "key"); + TEST_ASSERT(strcmp("value", value) == 0, + "key unexpected value: %s", value); + + ret = rte_cfgfile_close(cfgfile); + TEST_ASSERT_SUCCESS(ret, "Failed to close cfgfile"); + + return 0; +} + +static int test_cfgfile_empty_file(void) { struct rte_cfgfile *cfgfile; @@ -198,6 +228,9 @@ test_cfgfile(void) if (test_cfgfile_missing_section()) return -1; + if (test_cfgfile_global_properties()) + return -1; + if (test_cfgfile_empty_file()) return -1;