From c2189745c38d944e3b0e0c99066d67d7bc7e7744 Mon Sep 17 00:00:00 2001 From: Rich Lane Date: Thu, 25 Feb 2016 12:43:03 -0800 Subject: [PATCH] cfgfile: support looking up sections by index This is useful when sections have duplicate names. Signed-off-by: Rich Lane Acked-by: Cristian Dumitrescu --- lib/librte_cfgfile/rte_cfgfile.c | 18 ++++++++++ lib/librte_cfgfile/rte_cfgfile.h | 39 ++++++++++++++++++++++ lib/librte_cfgfile/rte_cfgfile_version.map | 7 ++++ 3 files changed, 64 insertions(+) diff --git a/lib/librte_cfgfile/rte_cfgfile.c b/lib/librte_cfgfile/rte_cfgfile.c index 1cd523f596..75625a28fc 100644 --- a/lib/librte_cfgfile/rte_cfgfile.c +++ b/lib/librte_cfgfile/rte_cfgfile.c @@ -333,6 +333,24 @@ rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname, return i; } +int +rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index, + char *sectionname, + struct rte_cfgfile_entry *entries, int max_entries) +{ + int i; + const struct rte_cfgfile_section *sect; + + if (index < 0 || index >= cfg->num_sections) + return -1; + + sect = cfg->sections[index]; + snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name); + for (i = 0; i < max_entries && i < sect->num_entries; i++) + entries[i] = *sect->entries[i]; + return i; +} + const char * rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname, const char *entryname) diff --git a/lib/librte_cfgfile/rte_cfgfile.h b/lib/librte_cfgfile/rte_cfgfile.h index d44378266f..834f82873b 100644 --- a/lib/librte_cfgfile/rte_cfgfile.h +++ b/lib/librte_cfgfile/rte_cfgfile.h @@ -126,6 +126,9 @@ int rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname); /** * Get number of entries in given config file section * +* If multiple sections have the given name this function operates on the +* first one. +* * @param cfg * Config file * @param sectionname @@ -138,6 +141,9 @@ int rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg, /** Get section entries as key-value pairs * +* If multiple sections have the given name this function operates on the +* first one. +* * @param cfg * Config file * @param sectionname @@ -155,8 +161,38 @@ int rte_cfgfile_section_entries(struct rte_cfgfile *cfg, struct rte_cfgfile_entry *entries, int max_entries); +/** Get section entries as key-value pairs +* +* The index of a section is the same as the index of its name in the +* result of rte_cfgfile_sections. This API can be used when there are +* multiple sections with the same name. +* +* @param cfg +* Config file +* @param index +* Section index +* @param sectionname +* Pre-allocated string of at least CFG_NAME_LEN characters where the +* section name is stored after successful invocation. +* @param entries +* Pre-allocated array of at least max_entries entries where the section +* entries are stored as key-value pair after successful invocation +* @param max_entries +* Maximum number of section entries to be stored in entries array +* @return +* Number of entries populated on success, negative error code otherwise +*/ +int rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, + int index, + char *sectionname, + struct rte_cfgfile_entry *entries, + int max_entries); + /** Get value of the named entry in named config file section * +* If multiple sections have the given name this function operates on the +* first one. +* * @param cfg * Config file * @param sectionname @@ -172,6 +208,9 @@ const char *rte_cfgfile_get_entry(struct rte_cfgfile *cfg, /** Check if given entry exists in named config file section * +* If multiple sections have the given name this function operates on the +* first one. +* * @param cfg * Config file * @param sectionname diff --git a/lib/librte_cfgfile/rte_cfgfile_version.map b/lib/librte_cfgfile/rte_cfgfile_version.map index bf6c6fdacf..3c2f0dbf4d 100644 --- a/lib/librte_cfgfile/rte_cfgfile_version.map +++ b/lib/librte_cfgfile/rte_cfgfile_version.map @@ -13,3 +13,10 @@ DPDK_2.0 { local: *; }; + +DPDK_16.04 { + global: + + rte_cfgfile_section_entries_by_index; + +} DPDK_2.0; -- 2.20.1