From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by dpdk.org (Postfix) with ESMTP id A8B22C4CC for ; Wed, 17 Jun 2015 17:08:49 +0200 (CEST) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga103.jf.intel.com with ESMTP; 17 Jun 2015 08:07:39 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.13,633,1427785200"; d="scan'208";a="748396119" Received: from unknown (HELO stargo) ([10.217.248.233]) by orsmga002.jf.intel.com with SMTP; 17 Jun 2015 08:07:36 -0700 Received: by stargo (sSMTP sendmail emulation); Wed, 17 Jun 2015 17:04:06 +0200 From: Maciej Gajdzica To: dev@dpdk.org Date: Wed, 17 Jun 2015 16:48:45 +0200 Message-Id: <1434552528-3576-4-git-send-email-maciejx.t.gajdzica@intel.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1434552528-3576-1-git-send-email-maciejx.t.gajdzica@intel.com> References: <1434552528-3576-1-git-send-email-maciejx.t.gajdzica@intel.com> Subject: [dpdk-dev] [PATCH v2 3/6] cfgfile: split rte_cfgfile_load to smaller functions X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 17 Jun 2015 15:08:50 -0000 From: Pawel Wodkowski Signed-off-by: Pawel Wodkowski --- lib/librte_cfgfile/rte_cfgfile.c | 93 ++++++++++++++++++++++++++++++++------ 1 file changed, 80 insertions(+), 13 deletions(-) diff --git a/lib/librte_cfgfile/rte_cfgfile.c b/lib/librte_cfgfile/rte_cfgfile.c index b81c273..2e78583 100644 --- a/lib/librte_cfgfile/rte_cfgfile.c +++ b/lib/librte_cfgfile/rte_cfgfile.c @@ -37,6 +37,8 @@ #include #include +#include + #include "rte_cfgfile.h" struct rte_cfgfile_section { @@ -85,8 +87,67 @@ _strip(char *str, unsigned len) return newlen; } -struct rte_cfgfile * -rte_cfgfile_load(const char *filename, int flags) +static size_t +strip_comment(char *buffer, size_t len) +{ + char *pos = memchr(buffer, ';', len); + + if (pos == NULL) + return len; + + if (len == 1) { + *pos = '\0'; + return 0; + } + + if (buffer[len - 1] == '\n') { + if (buffer[len - 2] == '\\') { + pos[0] = '\\'; + pos[1] = '\n'; + pos[2] = '\0'; + len = pos - buffer + 2; + } else { + pos[0] = '\n'; + pos[1] = '\0'; + len = pos - buffer + 1; + } + } + + return len; +} + +/** + * Create new apty config file object. + * + * @param flags + * Config file flags, Reserved for future use. Must be set to 0. + * @return + * Handle to configuration file + */ +static struct rte_cfgfile * +rte_cfgfile_create(__rte_unused int flags, int allocated_sections) +{ + struct rte_cfgfile *cfg = malloc(sizeof(*cfg) + sizeof(cfg->sections[0]) * + allocated_sections); + + if (cfg != NULL) + memset(cfg->sections, 0, sizeof(cfg->sections[0]) * allocated_sections); + + return cfg; +} + +/** +* Open config *file*. +* +* @param file +* Config stream to read. +* @param flags +* Config file flags, Reserved for future use. Must be set to 0. +* @return +* Handle to configuration file +*/ +static struct rte_cfgfile * +rte_cfgfile_read(FILE *f, int flags) { int allocated_sections = CFG_ALLOC_SECTION_BATCH; int allocated_entries = 0; @@ -96,19 +157,14 @@ rte_cfgfile_load(const char *filename, int flags) int lineno = 0; struct rte_cfgfile *cfg = NULL; - FILE *f = fopen(filename, "r"); if (f == NULL) return NULL; - cfg = malloc(sizeof(*cfg) + sizeof(cfg->sections[0]) * - allocated_sections); + cfg = rte_cfgfile_create(flags, allocated_sections); if (cfg == NULL) goto error2; - memset(cfg->sections, 0, sizeof(cfg->sections[0]) * allocated_sections); - while (fgets(buffer, sizeof(buffer), f) != NULL) { - char *pos = NULL; size_t len = strnlen(buffer, sizeof(buffer)); lineno++; if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) { @@ -116,11 +172,7 @@ rte_cfgfile_load(const char *filename, int flags) "Check if line too long\n", lineno); goto error1; } - pos = memchr(buffer, ';', sizeof(buffer)); - if (pos != NULL) { - *pos = '\0'; - len = pos - buffer; - } + len = strip_comment(buffer, len); len = _strip(buffer, len); if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL) @@ -238,6 +290,21 @@ error2: return NULL; } +struct rte_cfgfile * +rte_cfgfile_load(const char *filename, int flags) +{ + struct rte_cfgfile *cfg = NULL; + FILE *file = fopen(filename, "r"); + + if (file == NULL) + return NULL; + + cfg = rte_cfgfile_read(file, flags); + fclose(file); + + return cfg; +} + int rte_cfgfile_close(struct rte_cfgfile *cfg) { -- 1.7.9.5