X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=lib%2Flibrte_cfgfile%2Frte_cfgfile.c;h=258809373ac2b74a73a0fe4e62142438fecb5756;hb=c5acbb5ea20e452e4d123a7562d1c46a72b8938a;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..258809373a 100644 --- a/lib/librte_cfgfile/rte_cfgfile.c +++ b/lib/librte_cfgfile/rte_cfgfile.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include "rte_cfgfile.h" @@ -58,6 +59,25 @@ struct rte_cfgfile { * for new entries do we add in */ #define CFG_ALLOC_ENTRY_BATCH 16 +/** + * Default cfgfile load parameters. + */ +static const struct rte_cfgfile_parameters default_cfgfile_params = { + .comment_character = CFG_DEFAULT_COMMENT_CHARACTER, +}; + +/** + * Defines the list of acceptable comment characters supported by this + * library. + */ +static const char valid_comment_chars[] = { + '!', + '#', + '%', + ';', + '@' +}; + static unsigned _strip(char *str, unsigned len) { @@ -85,17 +105,56 @@ _strip(char *str, unsigned len) return newlen; } +static int +rte_cfgfile_check_params(const struct rte_cfgfile_parameters *params) +{ + unsigned int valid_comment; + unsigned int i; + + if (!params) { + printf("Error - missing cfgfile parameters\n"); + return -EINVAL; + } + + valid_comment = 0; + for (i = 0; i < RTE_DIM(valid_comment_chars); i++) { + if (params->comment_character == valid_comment_chars[i]) { + valid_comment = 1; + break; + } + } + + if (valid_comment == 0) { + printf("Error - invalid comment characters %c\n", + params->comment_character); + return -ENOTSUP; + } + + return 0; +} + struct rte_cfgfile * rte_cfgfile_load(const char *filename, int flags) +{ + return rte_cfgfile_load_with_params(filename, flags, + &default_cfgfile_params); +} + +struct rte_cfgfile * +rte_cfgfile_load_with_params(const char *filename, int flags, + const struct rte_cfgfile_parameters *params) { int allocated_sections = CFG_ALLOC_SECTION_BATCH; int allocated_entries = 0; int curr_section = -1; int curr_entry = -1; - char buffer[256]; + char buffer[CFG_NAME_LEN + CFG_VALUE_LEN + 4] = {0}; int lineno = 0; struct rte_cfgfile *cfg = NULL; + if (rte_cfgfile_check_params(params)) + return NULL; + FILE *f = fopen(filename, "r"); if (f == NULL) return NULL; @@ -107,6 +166,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)); @@ -116,7 +191,7 @@ rte_cfgfile_load(const char *filename, int flags) "Check if line too long\n", lineno); goto error1; } - pos = memchr(buffer, ';', sizeof(buffer)); + pos = memchr(buffer, params->comment_character, len); if (pos != NULL) { *pos = '\0'; len = pos - buffer; @@ -151,6 +226,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; } @@ -169,7 +245,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 { @@ -182,12 +258,21 @@ rte_cfgfile_load(const char *filename, int flags) 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); - goto error1; + int n; + char *split[2] = {NULL}; + n = rte_strsplit(buffer, sizeof(buffer), split, 2, '='); + if (flags & CFG_FLAG_EMPTY_VALUES) { + if ((n < 1) || (n > 2)) { + printf("Error at line %d - cannot split string, n=%d\n", + lineno, n); + goto error1; + } + } else { + if (n != 2) { + printf("Error at line %d - cannot split string, n=%d\n", + lineno, n); + goto error1; + } } curr_entry++; @@ -198,6 +283,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; } @@ -213,10 +299,10 @@ 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", - split[1]); + snprintf(entry->value, sizeof(entry->value), "%s", + split[1] ? split[1] : ""); _strip(entry->name, strnlen(entry->name, sizeof(entry->name))); _strip(entry->value, strnlen(entry->value, @@ -225,11 +311,16 @@ 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; + if (curr_section >= 0) + cfg->sections[curr_section]->num_entries = curr_entry + 1; rte_cfgfile_close(cfg); error2: fclose(f); @@ -283,7 +374,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 +395,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 @@ -317,7 +408,20 @@ 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) +{ + 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); + return sect->num_entries; +} int rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname, struct rte_cfgfile_entry *entries, int max_entries) @@ -331,6 +435,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 +472,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; }