> -----Original Message----- > From: Allain Legacy [mailto:allain.leg...@windriver.com] > Sent: Wednesday, March 29, 2017 1:48 AM > To: Dumitrescu, Cristian <cristian.dumitre...@intel.com>; Richardson, Bruce > <bruce.richard...@intel.com> > Cc: dev@dpdk.org; yuanhan....@linux.intel.com; > thomas.monja...@6wind.com > Subject: [PATCH v3 2/6] cfgfile: add support for global properties section > > 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 <allain.leg...@windriver.com> > --- > lib/librte_cfgfile/rte_cfgfile.c | 16 ++++++++++++++++ > lib/librte_cfgfile/rte_cfgfile.h | 10 +++++++++- > test/test/test_cfgfile.c | 33 +++++++++++++++++++++++++++++++++ > 3 files changed, 58 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..0e805c268 100644 > --- a/lib/librte_cfgfile/rte_cfgfile.h > +++ b/lib/librte_cfgfile/rte_cfgfile.h > @@ -66,13 +66,21 @@ struct rte_cfgfile_entry { > char value[CFG_VALUE_LEN]; /**< Value */ > }; > > +/**@{ cfgfile load operation flags */ > +/** > + * Indicates that the file supports key value entries before the first > defined > + * section. These entries can be accessed in the "GLOBAL" section. > + */ > +#define CFG_FLAG_GLOBAL_SECTION (1 << 0) > +/**@} */ > + > /** > * 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..ad293cc89 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, "Expected failured did not occur"); > + > + 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; > +} > +
Can we please add a test to check that error is triggered for keys defined outside any section when the CFG_FLAG_GLOBAL_SECTION is NOT provided to rte_cfgfile_load()? Thank you! > +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; > > -- > 2.12.1