lib/util: add pm_process_with_flags to allow parsing ini files with empty values
authorGünther Deschner <gd@samba.org>
Wed, 14 Sep 2016 16:13:00 +0000 (18:13 +0200)
committerAndreas Schneider <asn@cryptomilk.org>
Fri, 6 Jan 2017 11:28:19 +0000 (12:28 +0100)
Guenther

Signed-off-by: Guenther Deschner <gd@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
lib/util/params.c
lib/util/samba_util.h
lib/util/tini.c
lib/util/tini.h
lib/util/tiniparser.c

index 5ec4fd23dbb844de8513f6c6d909b078d8ffe65e..c5c252613d396af556485d38b1eab47bc8ded757 100644 (file)
@@ -96,7 +96,30 @@ bool pm_process(const char *filename,
                return false;
        }
 
-       ret = tini_parse(f, sfunc, pfunc, private_data);
+       ret = tini_parse(f, false, sfunc, pfunc, private_data);
+
+       fclose(f);
+
+       return ret;
+}
+
+
+bool pm_process_with_flags(const char *filename,
+                          bool allow_empty_values,
+                          bool (*sfunc)(const char *section, void *private_data),
+                          bool (*pfunc)(const char *name, const char *value,
+                                        void *private_data),
+                          void *private_data)
+{
+       FILE *f;
+       bool ret;
+
+       f = fopen(filename, "r");
+       if (f == NULL) {
+               return false;
+       }
+
+       ret = tini_parse(f, allow_empty_values, sfunc, pfunc, private_data);
 
        fclose(f);
 
index 897e0f5923dc5c4f8d139bc0563a79bc710cea72..c19e246bcd451240e71cb23fe814d2fdd3a82661 100644 (file)
@@ -609,6 +609,12 @@ bool pm_process( const char *fileName,
                  bool (*sfunc)(const char *, void *),
                  bool (*pfunc)(const char *, const char *, void *),
                                 void *userdata);
+bool pm_process_with_flags(const char *filename,
+                          bool allow_empty_values,
+                          bool (*sfunc)(const char *section, void *private_data),
+                          bool (*pfunc)(const char *name, const char *value,
+                                        void *private_data),
+                          void *private_data);
 
 void print_asc(int level, const uint8_t *buf,int len);
 void print_asc_cb(const uint8_t *buf, int len,
index 3bfc2d6511fa8fa22f95a80f5b2c6b5b8c7a6673..36d7a4522ce34e7e5acb056d25ab5ae83291d97f 100644 (file)
@@ -227,19 +227,27 @@ static char *trim_one_space(char *buf)
 }
 
 static bool parse_param(char *buf,
+                       bool allow_empty_value,
                        bool (*pfunc)(const char *name, const char *value,
                                      void *private_data),
                        void *private_data)
 {
        char *equals;
-       char *name, *value;
+       char *name;
+       const char *value;
        size_t len;
+       bool no_value = false;
 
        equals = strchr(buf, '=');
-       if (equals == NULL) {
-               return true;
+       if (equals != NULL) {
+               *equals = '\0';
+       } else {
+               if (allow_empty_value) {
+                       no_value = true;
+               } else {
+                       return true;
+               }
        }
-       *equals = '\0';
 
        name = trim_one_space(buf);
        len = strlen(buf);
@@ -247,12 +255,17 @@ static bool parse_param(char *buf,
                return false;
        }
 
-       value = trim_one_space(equals+1);
+       if (no_value) {
+               value = "";
+       } else {
+               value = trim_one_space(equals+1);
+       }
 
        return pfunc(name, value, private_data);
 }
 
 bool tini_parse(FILE *f,
+               bool allow_empty_value,
                bool (*sfunc)(const char *section, void *private_data),
                bool (*pfunc)(const char *name, const char *value,
                              void *private_data),
@@ -293,7 +306,7 @@ bool tini_parse(FILE *f,
                        ok = parse_section(buf, sfunc, private_data);
                        break;
                default:
-                       ok = parse_param(buf, pfunc, private_data);
+                       ok = parse_param(buf, allow_empty_value, pfunc, private_data);
                        break;
                }
 
index 02cc1acbd1ec2fe8c8f1a9f8772724e069264178..36fc08082a12e480379f6adc4366d994fb4be815 100644 (file)
@@ -38,6 +38,7 @@
 #include <stdio.h>
 
 bool tini_parse(FILE *f,
+               bool allow_empty_value,
                bool (*sfunc)(const char *section, void *private_data),
                bool (*pfunc)(const char *name, const char *value,
                              void *private_data),
index 7c106162f01a5a2a123f008875a2f5b761f9c2db..c3ab4e7f80664e3a9284d4cbeeb06c02e2f00748 100644 (file)
@@ -339,6 +339,7 @@ struct tiniparser_dictionary *tiniparser_load(const char *filename)
        d->section_list = NULL;
 
        ret = tini_parse(fp,
+                       false,
                        section_parser,
                        value_parser,
                        d);