X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=lib%2Flibrte_cfgfile%2Frte_cfgfile.c;h=7d8c941ead8083365f3c941c4b61d3a88a6cd905;hb=2b947bd294ffce2d980d092683de82269abe3676;hp=ad9bb2e64dc88f59efab60ac40f7a8defbcc8e6a;hpb=d4cb8197589d645b74d5ee8fe27649030717b7ab;p=dpdk.git diff --git a/lib/librte_cfgfile/rte_cfgfile.c b/lib/librte_cfgfile/rte_cfgfile.c index ad9bb2e64d..7d8c941ead 100644 --- a/lib/librte_cfgfile/rte_cfgfile.c +++ b/lib/librte_cfgfile/rte_cfgfile.c @@ -1,34 +1,5 @@ -/*- - * BSD LICENSE - * - * Copyright(c) 2010-2014 Intel Corporation. All rights reserved. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Intel Corporation nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2010-2014 Intel Corporation */ #include @@ -189,10 +160,6 @@ 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[CFG_NAME_LEN + CFG_VALUE_LEN + 4] = {0}; int lineno = 0; struct rte_cfgfile *cfg = NULL; @@ -204,28 +171,7 @@ rte_cfgfile_load_with_params(const char *filename, int flags, if (f == NULL) return NULL; - cfg = malloc(sizeof(*cfg) + sizeof(cfg->sections[0]) * - allocated_sections); - if (cfg == NULL) - goto error2; - - 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 = malloc( - sizeof(cfg->sections[0]) + - sizeof(cfg->sections[0].entries) * - allocated_entries); - 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"); - } + cfg = rte_cfgfile_create(flags); while (fgets(buffer, sizeof(buffer), f) != NULL) { char *pos = NULL; @@ -236,13 +182,15 @@ rte_cfgfile_load_with_params(const char *filename, int flags, "Check if line too long\n", lineno); goto error1; } + /* skip parsing if comment character found */ pos = memchr(buffer, params->comment_character, len); - if (pos != NULL) { + if (pos != NULL && (*(pos-1) != '\\')) { *pos = '\0'; len = pos - buffer; } len = _strip(buffer, len); + /* skip lines without useful content */ if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL) continue; @@ -250,128 +198,59 @@ rte_cfgfile_load_with_params(const char *filename, int flags, /* section heading line */ char *end = memchr(buffer, ']', len); if (end == NULL) { - printf("Error line %d - no terminating '['" + printf("Error line %d - no terminating ']'" "character found\n", lineno); goto error1; } *end = '\0'; _strip(&buffer[1], end - &buffer[1]); - /* close off old section and add start new one */ - if (curr_section >= 0) - cfg->sections[curr_section].num_entries = - curr_entry + 1; - curr_section++; - - /* resize overall struct if we don't have room for more - sections */ - if (curr_section == allocated_sections) { - allocated_sections += CFG_ALLOC_SECTION_BATCH; - struct rte_cfgfile *n_cfg = realloc(cfg, - sizeof(*cfg) + sizeof(cfg->sections[0]) - * allocated_sections); - if (n_cfg == NULL) { - curr_section--; - printf("Error - no more memory\n"); - goto error1; - } - cfg = n_cfg; - } - - /* allocate space for new section */ - allocated_entries = CFG_ALLOC_ENTRY_BATCH; - curr_entry = -1; - cfg->sections = malloc( - sizeof(cfg->sections[0]) + - sizeof(cfg->sections[0].entries) * - allocated_entries); - if (cfg->sections == NULL) { - printf("Error - no more memory\n"); - goto error1; - } - - snprintf(cfg->sections[curr_section].name, - sizeof(cfg->sections[0].name), - "%s", &buffer[1]); + rte_cfgfile_add_section(cfg, &buffer[1]); } else { - /* value line */ - if (curr_section < 0) { - printf("Error line %d - value outside of" - "section\n", lineno); - goto error1; - } - - struct rte_cfgfile_section *sect = cfg->sections; - + /* key and value line */ 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); + printf("Error line %d - no '='" + "character found\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++; - if (curr_entry == allocated_entries) { - allocated_entries += CFG_ALLOC_ENTRY_BATCH; - struct rte_cfgfile_section *n_sect = realloc( - sect, sizeof(*sect) + - sizeof(sect->entries[0]) * - allocated_entries); - if (n_sect == NULL) { - curr_entry--; - printf("Error - no more memory\n"); - goto error1; - } - sect = cfg->sections = n_sect; + *split[1] = '\0'; + split[1]++; + + _strip(split[0], strlen(split[0])); + _strip(split[1], strlen(split[1])); + char *end = memchr(split[1], '\\', strlen(split[1])); + + while (end != NULL) { + if (*(end+1) == params->comment_character) { + *end = '\0'; + strcat(split[1], end+1); + } else + end++; + end = memchr(end, '\\', strlen(end)); } - sect->entries = malloc( - sizeof(sect->entries[0])); - if (sect->entries == NULL) { - printf("Error - no more memory\n"); + if (!(flags & CFG_FLAG_EMPTY_VALUES) && + (*split[1] == '\0')) { + printf("Error at line %d - cannot use empty " + "values\n", lineno); goto error1; } - 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] : ""); - _strip(entry->name, strnlen(entry->name, - sizeof(entry->name))); - _strip(entry->value, strnlen(entry->value, - sizeof(entry->value))); + if (cfg->num_sections == 0) + goto error1; + + _add_entry(&cfg->sections[cfg->num_sections - 1], + split[0], split[1]); } } fclose(f); - cfg->flags = 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; 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); return NULL; } @@ -395,7 +274,7 @@ rte_cfgfile_create(int flags) CFG_ALLOC_SECTION_BATCH); if (cfg->sections == NULL) - return NULL; + goto error1; cfg->allocated_sections = CFG_ALLOC_SECTION_BATCH; @@ -404,7 +283,7 @@ rte_cfgfile_create(int flags) struct rte_cfgfile_entry) * CFG_ALLOC_ENTRY_BATCH); if (cfg->sections[i].entries == NULL) - return NULL; + goto error1; cfg->sections[i].num_entries = 0; cfg->sections[i].allocated_entries = CFG_ALLOC_ENTRY_BATCH; @@ -412,7 +291,21 @@ rte_cfgfile_create(int flags) if (flags & CFG_FLAG_GLOBAL_SECTION) rte_cfgfile_add_section(cfg, "GLOBAL"); + return cfg; +error1: + 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); + cfg->sections = NULL; + } + free(cfg); + return NULL; } int