1
0
mirror of https://github.com/samba-team/samba.git synced 2025-02-02 09:47:23 +03:00

lib/util: add pm_process_with_flags to allow parsing ini files with empty values

Guenther

Signed-off-by: Guenther Deschner <gd@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
This commit is contained in:
Günther Deschner 2016-09-14 18:13:00 +02:00 committed by Andreas Schneider
parent 235aa67544
commit 7eeb2edc50
5 changed files with 51 additions and 7 deletions

View 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);

View 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,

View 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;
}

View 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),

View 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);