X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=lib%2Flibrte_cfgfile%2Frte_cfgfile.c;h=d72052a08e88147b7c3771ccfea7c394280c10a1;hb=26faac80327ff088d296da5e2c2553e381af5e25;hp=26052d0c2fa67e0840037f1129d937782a51c1c8;hpb=eaafbad419bf224fe656f5edeb99f5ca33199835;p=dpdk.git diff --git a/lib/librte_cfgfile/rte_cfgfile.c b/lib/librte_cfgfile/rte_cfgfile.c index 26052d0c2f..d72052a08e 100644 --- a/lib/librte_cfgfile/rte_cfgfile.c +++ b/lib/librte_cfgfile/rte_cfgfile.c @@ -92,7 +92,7 @@ rte_cfgfile_load(const char *filename, int flags) int allocated_entries = 0; int curr_section = -1; int curr_entry = -1; - char buffer[256]; + char buffer[256] = {0}; int lineno = 0; struct rte_cfgfile *cfg = NULL; @@ -169,7 +169,7 @@ rte_cfgfile_load(const char *filename, int flags) goto error1; } - rte_snprintf(cfg->sections[curr_section]->name, + snprintf(cfg->sections[curr_section]->name, sizeof(cfg->sections[0]->name), "%s", &buffer[1]); } else { @@ -213,9 +213,9 @@ rte_cfgfile_load(const char *filename, int flags) struct rte_cfgfile_entry *entry = sect->entries[ curr_entry]; - rte_snprintf(entry->name, sizeof(entry->name), "%s", + snprintf(entry->name, sizeof(entry->name), "%s", split[0]); - rte_snprintf(entry->value, sizeof(entry->value), "%s", + snprintf(entry->value, sizeof(entry->value), "%s", split[1]); _strip(entry->name, strnlen(entry->name, sizeof(entry->name))); @@ -225,11 +225,14 @@ rte_cfgfile_load(const char *filename, int flags) } fclose(f); cfg->flags = flags; - cfg->sections[curr_section]->num_entries = curr_entry + 1; cfg->num_sections = curr_section + 1; + /* curr_section will still be -1 if we have an empty file */ + if (curr_section >= 0) + cfg->sections[curr_section]->num_entries = curr_entry + 1; return cfg; error1: + cfg->num_sections = curr_section + 1; rte_cfgfile_close(cfg); error2: fclose(f); @@ -283,7 +286,7 @@ rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[], int i; for (i = 0; i < cfg->num_sections && i < max_sections; i++) - rte_snprintf(sections[i], CFG_NAME_LEN, "%s", + snprintf(sections[i], CFG_NAME_LEN, "%s", cfg->sections[i]->name); return i; @@ -304,7 +307,7 @@ _get_section(struct rte_cfgfile *cfg, const char *sectionname) int rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname) { - return (_get_section(cfg, sectionname) != NULL); + return _get_section(cfg, sectionname) != NULL; } int @@ -331,6 +334,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) @@ -350,5 +371,5 @@ int rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname, const char *entryname) { - return (rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL); + return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL; }