cfgfile: support runtime modification
[dpdk.git] / lib / librte_cfgfile / rte_cfgfile.c
index 63e34bb..ad9bb2e 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
+#include <errno.h>
 #include <rte_common.h>
-#include <rte_string_fns.h>
 
 #include "rte_cfgfile.h"
 
 struct rte_cfgfile_section {
        char name[CFG_NAME_LEN];
        int num_entries;
-       struct rte_cfgfile_entry *entries[0];
+       int allocated_entries;
+       struct rte_cfgfile_entry *entries;
 };
 
 struct rte_cfgfile {
        int flags;
        int num_sections;
-       struct rte_cfgfile_section *sections[0];
+       int allocated_sections;
+       struct rte_cfgfile_section *sections;
 };
 
 /** when we resize a file structure, how many extra entries
@@ -105,6 +107,49 @@ _strip(char *str, unsigned len)
        return newlen;
 }
 
+static struct rte_cfgfile_section *
+_get_section(struct rte_cfgfile *cfg, const char *sectionname)
+{
+       int i;
+
+       for (i = 0; i < cfg->num_sections; i++) {
+               if (strncmp(cfg->sections[i].name, sectionname,
+                               sizeof(cfg->sections[0].name)) == 0)
+                       return &cfg->sections[i];
+       }
+       return NULL;
+}
+
+static int
+_add_entry(struct rte_cfgfile_section *section, const char *entryname,
+               const char *entryvalue)
+{
+       /* resize entry structure if we don't have room for more entries */
+       if (section->num_entries == section->allocated_entries) {
+               struct rte_cfgfile_entry *n_entries = realloc(
+                               section->entries,
+                               sizeof(struct rte_cfgfile_entry) *
+                               ((section->allocated_entries) +
+                                               CFG_ALLOC_ENTRY_BATCH));
+
+               if (n_entries == NULL)
+                       return -ENOMEM;
+
+               section->entries = n_entries;
+               section->allocated_entries += CFG_ALLOC_ENTRY_BATCH;
+       }
+       /* fill up entry fields with key name and value */
+       struct rte_cfgfile_entry *curr_entry =
+                                       &section->entries[section->num_entries];
+
+       snprintf(curr_entry->name, sizeof(curr_entry->name), "%s", entryname);
+       snprintf(curr_entry->value,
+                               sizeof(curr_entry->value), "%s", entryvalue);
+       section->num_entries++;
+
+       return 0;
+}
+
 static int
 rte_cfgfile_check_params(const struct rte_cfgfile_parameters *params)
 {
@@ -148,7 +193,7 @@ rte_cfgfile_load_with_params(const char *filename, int flags,
        int allocated_entries = 0;
        int curr_section = -1;
        int curr_entry = -1;
-       char buffer[256] = {0};
+       char buffer[CFG_NAME_LEN + CFG_VALUE_LEN + 4] = {0};
        int lineno = 0;
        struct rte_cfgfile *cfg = NULL;
 
@@ -169,17 +214,17 @@ rte_cfgfile_load_with_params(const char *filename, int flags,
        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]) *
+               cfg->sections = malloc(
+                       sizeof(cfg->sections[0]) +
+                       sizeof(cfg->sections[0].entries) *
                        allocated_entries);
-               if (cfg->sections[curr_section] == NULL) {
+               if (cfg->sections == NULL) {
                        printf("Error - no memory for global section\n");
                        goto error1;
                }
 
-               snprintf(cfg->sections[curr_section]->name,
-                                sizeof(cfg->sections[0]->name), "GLOBAL");
+               snprintf(cfg->sections[curr_section].name,
+                                sizeof(cfg->sections[0].name), "GLOBAL");
        }
 
        while (fgets(buffer, sizeof(buffer), f) != NULL) {
@@ -191,7 +236,7 @@ rte_cfgfile_load_with_params(const char *filename, int flags,
                                        "Check if line too long\n", lineno);
                        goto error1;
                }
-               pos = memchr(buffer, params->comment_character, sizeof(buffer));
+               pos = memchr(buffer, params->comment_character, len);
                if (pos != NULL) {
                        *pos = '\0';
                        len = pos -  buffer;
@@ -214,7 +259,7 @@ rte_cfgfile_load_with_params(const char *filename, int flags,
 
                        /* close off old section and add start new one */
                        if (curr_section >= 0)
-                               cfg->sections[curr_section]->num_entries =
+                               cfg->sections[curr_section].num_entries =
                                        curr_entry + 1;
                        curr_section++;
 
@@ -236,17 +281,17 @@ rte_cfgfile_load_with_params(const char *filename, int flags,
                        /* allocate space for new section */
                        allocated_entries = CFG_ALLOC_ENTRY_BATCH;
                        curr_entry = -1;
-                       cfg->sections[curr_section] = malloc(
-                               sizeof(*cfg->sections[0]) +
-                               sizeof(cfg->sections[0]->entries[0]) *
+                       cfg->sections = malloc(
+                               sizeof(cfg->sections[0]) +
+                               sizeof(cfg->sections[0].entries) *
                                allocated_entries);
-                       if (cfg->sections[curr_section] == NULL) {
+                       if (cfg->sections == NULL) {
                                printf("Error - no more memory\n");
                                goto error1;
                        }
 
-                       snprintf(cfg->sections[curr_section]->name,
-                                       sizeof(cfg->sections[0]->name),
+                       snprintf(cfg->sections[curr_section].name,
+                                       sizeof(cfg->sections[0].name),
                                        "%s", &buffer[1]);
                } else {
                        /* value line */
@@ -256,14 +301,28 @@ rte_cfgfile_load_with_params(const char *filename, int flags,
                                goto error1;
                        }
 
-                       struct rte_cfgfile_section *sect =
-                               cfg->sections[curr_section];
-                       char *split[2];
-                       if (rte_strsplit(buffer, sizeof(buffer), split, 2, '=')
-                               != 2) {
-                               printf("Error at line %d - cannot split "
-                                       "string\n", lineno);
+                       struct rte_cfgfile_section *sect = cfg->sections;
+
+                       char *split[2] = {NULL};
+                       split[0] = buffer;
+                       split[1] = memchr(buffer, '=', len);
+
+                       /* when delimeter not found */
+                       if (split[1] == NULL) {
+                               printf("Error at line %d - cannot "
+                                       "split string\n", lineno);
                                goto error1;
+                       } else {
+                               /* when delimeter found */
+                               *split[1] = '\0';
+                               split[1]++;
+
+                               if (!(flags & CFG_FLAG_EMPTY_VALUES) &&
+                                               (*split[1] == '\0')) {
+                                       printf("Error at line %d - cannot "
+                                               "split string\n", lineno);
+                                       goto error1;
+                               }
                        }
 
                        curr_entry++;
@@ -278,22 +337,21 @@ rte_cfgfile_load_with_params(const char *filename, int flags,
                                        printf("Error - no more memory\n");
                                        goto error1;
                                }
-                               sect = cfg->sections[curr_section] = n_sect;
+                               sect = cfg->sections = n_sect;
                        }
 
-                       sect->entries[curr_entry] = malloc(
-                               sizeof(*sect->entries[0]));
-                       if (sect->entries[curr_entry] == NULL) {
+                       sect->entries = malloc(
+                               sizeof(sect->entries[0]));
+                       if (sect->entries == NULL) {
                                printf("Error - no more memory\n");
                                goto error1;
                        }
 
-                       struct rte_cfgfile_entry *entry = sect->entries[
-                               curr_entry];
+                       struct rte_cfgfile_entry *entry = sect->entries;
                        snprintf(entry->name, sizeof(entry->name), "%s",
                                split[0]);
                        snprintf(entry->value, sizeof(entry->value), "%s",
-                               split[1]);
+                                split[1] ? split[1] : "");
                        _strip(entry->name, strnlen(entry->name,
                                sizeof(entry->name)));
                        _strip(entry->value, strnlen(entry->value,
@@ -305,42 +363,194 @@ rte_cfgfile_load_with_params(const char *filename, int flags,
        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;
+               cfg->sections[curr_section].num_entries = curr_entry + 1;
        return cfg;
 
 error1:
        cfg->num_sections = curr_section + 1;
        if (curr_section >= 0)
-               cfg->sections[curr_section]->num_entries = curr_entry + 1;
+               cfg->sections[curr_section].num_entries = curr_entry + 1;
        rte_cfgfile_close(cfg);
 error2:
        fclose(f);
        return NULL;
 }
 
+struct rte_cfgfile *
+rte_cfgfile_create(int flags)
+{
+       int i;
+       struct rte_cfgfile *cfg = NULL;
 
-int rte_cfgfile_close(struct rte_cfgfile *cfg)
+       cfg = malloc(sizeof(*cfg));
+
+       if (cfg == NULL)
+               return NULL;
+
+       cfg->flags = flags;
+       cfg->num_sections = 0;
+
+       /* allocate first batch of sections and entries */
+       cfg->sections = malloc(sizeof(struct rte_cfgfile_section) *
+                       CFG_ALLOC_SECTION_BATCH);
+
+       if (cfg->sections == NULL)
+               return NULL;
+
+       cfg->allocated_sections = CFG_ALLOC_SECTION_BATCH;
+
+       for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
+               cfg->sections[i].entries = malloc(sizeof(
+                       struct rte_cfgfile_entry) * CFG_ALLOC_ENTRY_BATCH);
+
+               if (cfg->sections[i].entries == NULL)
+                       return NULL;
+
+               cfg->sections[i].num_entries = 0;
+               cfg->sections[i].allocated_entries = CFG_ALLOC_ENTRY_BATCH;
+       }
+
+       if (flags & CFG_FLAG_GLOBAL_SECTION)
+               rte_cfgfile_add_section(cfg, "GLOBAL");
+       return cfg;
+}
+
+int
+rte_cfgfile_add_section(struct rte_cfgfile *cfg, const char *sectionname)
+{
+       int i;
+
+       if (cfg == NULL)
+               return -EINVAL;
+
+       if (sectionname == NULL)
+               return -EINVAL;
+
+       /* resize overall struct if we don't have room for more sections */
+       if (cfg->num_sections == cfg->allocated_sections) {
+
+               struct rte_cfgfile_section *n_sections =
+                               realloc(cfg->sections,
+                               sizeof(struct rte_cfgfile_section) *
+                               ((cfg->allocated_sections) +
+                               CFG_ALLOC_SECTION_BATCH));
+
+               if (n_sections == NULL)
+                       return -ENOMEM;
+
+               for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
+                       n_sections[i + cfg->allocated_sections].num_entries = 0;
+                       n_sections[i +
+                                cfg->allocated_sections].allocated_entries = 0;
+                       n_sections[i + cfg->allocated_sections].entries = NULL;
+               }
+               cfg->sections = n_sections;
+               cfg->allocated_sections += CFG_ALLOC_SECTION_BATCH;
+       }
+
+       snprintf(cfg->sections[cfg->num_sections].name,
+                       sizeof(cfg->sections[0].name), "%s", sectionname);
+       cfg->sections[cfg->num_sections].num_entries = 0;
+       cfg->num_sections++;
+
+       return 0;
+}
+
+int rte_cfgfile_add_entry(struct rte_cfgfile *cfg,
+               const char *sectionname, const char *entryname,
+               const char *entryvalue)
+{
+       int ret;
+
+       if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL)
+                       || (entryvalue == NULL))
+               return -EINVAL;
+
+       if (rte_cfgfile_has_entry(cfg, sectionname, entryname) != 0)
+               return -EEXIST;
+
+       /* search for section pointer by sectionname */
+       struct rte_cfgfile_section *curr_section = _get_section(cfg,
+                                                               sectionname);
+       if (curr_section == NULL)
+               return -EINVAL;
+
+       ret = _add_entry(curr_section, entryname, entryvalue);
+
+       return ret;
+}
+
+int rte_cfgfile_set_entry(struct rte_cfgfile *cfg, const char *sectionname,
+               const char *entryname, const char *entryvalue)
+{
+       int i;
+
+       if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL))
+               return -EINVAL;
+
+       /* search for section pointer by sectionname */
+       struct rte_cfgfile_section *curr_section = _get_section(cfg,
+                                                               sectionname);
+       if (curr_section == NULL)
+               return -EINVAL;
+
+       if (entryvalue == NULL)
+               entryvalue = "";
+
+       for (i = 0; i < curr_section->num_entries; i++)
+               if (!strcmp(curr_section->entries[i].name, entryname)) {
+                       snprintf(curr_section->entries[i].value,
+                                       sizeof(curr_section->entries[i].value),
+                                                       "%s", entryvalue);
+                       return 0;
+               }
+       printf("Error - entry name doesn't exist\n");
+       return -EINVAL;
+}
+
+int rte_cfgfile_save(struct rte_cfgfile *cfg, const char *filename)
 {
        int i, j;
 
+       if ((cfg == NULL) || (filename == NULL))
+               return -EINVAL;
+
+       FILE *f = fopen(filename, "w");
+
+       if (f == NULL)
+               return -EINVAL;
+
+       for (i = 0; i < cfg->num_sections; i++) {
+               fprintf(f, "[%s]\n", cfg->sections[i].name);
+
+               for (j = 0; j < cfg->sections[i].num_entries; j++) {
+                       fprintf(f, "%s=%s\n",
+                                       cfg->sections[i].entries[j].name,
+                                       cfg->sections[i].entries[j].value);
+               }
+       }
+       return fclose(f);
+}
+
+int rte_cfgfile_close(struct rte_cfgfile *cfg)
+{
+       int i;
+
        if (cfg == NULL)
                return -1;
 
-       for (i = 0; i < cfg->num_sections; i++) {
-               if (cfg->sections[i] != NULL) {
-                       if (cfg->sections[i]->num_entries) {
-                               for (j = 0; j < cfg->sections[i]->num_entries;
-                                       j++) {
-                                       if (cfg->sections[i]->entries[j] !=
-                                               NULL)
-                                               free(cfg->sections[i]->
-                                                       entries[j]);
-                               }
+       if (cfg->sections != NULL) {
+               for (i = 0; i < cfg->allocated_sections; i++) {
+                       if (cfg->sections[i].entries != NULL) {
+                               free(cfg->sections[i].entries);
+                               cfg->sections[i].entries = NULL;
                        }
-                       free(cfg->sections[i]);
                }
+               free(cfg->sections);
+               cfg->sections = NULL;
        }
        free(cfg);
+       cfg = NULL;
 
        return 0;
 }
@@ -352,7 +562,7 @@ size_t length)
        int i;
        int num_sections = 0;
        for (i = 0; i < cfg->num_sections; i++) {
-               if (strncmp(cfg->sections[i]->name, sectionname, length) == 0)
+               if (strncmp(cfg->sections[i].name, sectionname, length) == 0)
                        num_sections++;
        }
        return num_sections;
@@ -366,23 +576,11 @@ rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
 
        for (i = 0; i < cfg->num_sections && i < max_sections; i++)
                snprintf(sections[i], CFG_NAME_LEN, "%s",
-               cfg->sections[i]->name);
+               cfg->sections[i].name);
 
        return i;
 }
 
-static const struct rte_cfgfile_section *
-_get_section(struct rte_cfgfile *cfg, const char *sectionname)
-{
-       int i;
-       for (i = 0; i < cfg->num_sections; i++) {
-               if (strncmp(cfg->sections[i]->name, sectionname,
-                               sizeof(cfg->sections[0]->name)) == 0)
-                       return cfg->sections[i];
-       }
-       return NULL;
-}
-
 int
 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
 {
@@ -399,7 +597,18 @@ rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
        return s->num_entries;
 }
 
+int
+rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg,
+       char *sectionname, int index)
+{
+       if (index < 0 || index >= cfg->num_sections)
+               return -1;
+
+       const struct rte_cfgfile_section *sect = &(cfg->sections[index]);
 
+       snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
+       return sect->num_entries;
+}
 int
 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
                struct rte_cfgfile_entry *entries, int max_entries)
@@ -409,7 +618,7 @@ rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
        if (sect == NULL)
                return -1;
        for (i = 0; i < max_entries && i < sect->num_entries; i++)
-               entries[i] = *sect->entries[i];
+               entries[i] = sect->entries[i];
        return i;
 }
 
@@ -423,11 +632,10 @@ rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
 
        if (index < 0 || index >= cfg->num_sections)
                return -1;
-
-       sect = cfg->sections[index];
+       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];
+               entries[i] = sect->entries[i];
        return i;
 }
 
@@ -440,9 +648,9 @@ rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
        if (sect == NULL)
                return NULL;
        for (i = 0; i < sect->num_entries; i++)
-               if (strncmp(sect->entries[i]->name, entryname, CFG_NAME_LEN)
-                       == 0)
-                       return sect->entries[i]->value;
+               if (strncmp(sect->entries[i].name, entryname, CFG_NAME_LEN)
+                                                                       == 0)
+                       return sect->entries[i].value;
        return NULL;
 }