cfgfile: support global properties section
[dpdk.git] / lib / librte_cfgfile / rte_cfgfile.c
index a677dad..832fea8 100644 (file)
@@ -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));
@@ -151,6 +167,7 @@ rte_cfgfile_load(const char *filename, int flags)
                                        sizeof(*cfg) + sizeof(cfg->sections[0])
                                        * allocated_sections);
                                if (n_cfg == NULL) {
+                                       curr_section--;
                                        printf("Error - no more memory\n");
                                        goto error1;
                                }
@@ -198,6 +215,7 @@ rte_cfgfile_load(const char *filename, int flags)
                                        sizeof(sect->entries[0]) *
                                        allocated_entries);
                                if (n_sect == NULL) {
+                                       curr_entry--;
                                        printf("Error - no more memory\n");
                                        goto error1;
                                }
@@ -232,6 +250,9 @@ rte_cfgfile_load(const char *filename, int flags)
        return cfg;
 
 error1:
+       cfg->num_sections = curr_section + 1;
+       if (curr_section >= 0)
+               cfg->sections[curr_section]->num_entries = curr_entry + 1;
        rte_cfgfile_close(cfg);
 error2:
        fclose(f);
@@ -306,7 +327,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
@@ -333,6 +354,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)
@@ -352,5 +391,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;
 }