From: Pawel Wodkowski <pawelx.wodkow...@intel.com>

Signed-off-by: Pawel Wodkowski <pawelx.wodkowski at 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

Reply via email to