From: Maciej Gajdzica <maciejx.t.gajdzica@intel.com>
To: dev@dpdk.org
Subject: [dpdk-dev] [PATCH v2 3/6] cfgfile: split rte_cfgfile_load to smaller functions
Date: Wed, 17 Jun 2015 16:48:45 +0200 [thread overview]
Message-ID: <1434552528-3576-4-git-send-email-maciejx.t.gajdzica@intel.com> (raw)
In-Reply-To: <1434552528-3576-1-git-send-email-maciejx.t.gajdzica@intel.com>
From: Pawel Wodkowski <pawelx.wodkowski@intel.com>
Signed-off-by: Pawel Wodkowski <pawelx.wodkowski@intel.com>
---
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 <ctype.h>
#include <rte_string_fns.h>
+#include <rte_common.h>
+
#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
next prev parent reply other threads:[~2015-06-17 15:08 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-06-17 14:48 [dpdk-dev] [PATCH v2 0/6] cfgfile: config file parsing extension Maciej Gajdzica
2015-06-17 14:48 ` [dpdk-dev] [PATCH v2 1/6] librte_cfgfile: fix code formating in header file Maciej Gajdzica
2015-06-17 14:48 ` [dpdk-dev] [PATCH v2 2/6] librte_compat: fix macro definition Maciej Gajdzica
2015-06-17 14:48 ` Maciej Gajdzica [this message]
2015-06-17 14:48 ` [dpdk-dev] [PATCH v2 4/6] cfgfile: added line continue character '\' to make multiline values possible Maciej Gajdzica
2015-06-17 14:48 ` [dpdk-dev] [PATCH v2 5/6] cfgfile: fixed calling free for each section in rte_cfgfile_close Maciej Gajdzica
2015-06-18 12:29 ` Thomas Monjalon
2015-06-18 13:08 ` Gajdzica, MaciejX T
2015-06-17 14:48 ` [dpdk-dev] [PATCH v2 6/6] cfgfile: added new implementation of section parsing Maciej Gajdzica
2015-06-18 12:37 ` Thomas Monjalon
2015-06-18 13:09 ` Gajdzica, MaciejX T
2015-06-17 16:51 ` [dpdk-dev] [PATCH v2 0/6] cfgfile: config file parsing extension Dumitrescu, Cristian
2015-06-18 12:44 ` Thomas Monjalon
2015-06-22 14:58 ` Gajdzica, MaciejX T
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1434552528-3576-4-git-send-email-maciejx.t.gajdzica@intel.com \
--to=maciejx.t.gajdzica@intel.com \
--cc=dev@dpdk.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).