From 374c6d0bca88360a2a1baae023b89de19f685736 Mon Sep 17 00:00:00 2001 From: Lukas Slebodnik Date: Wed, 22 Jun 2016 09:13:04 +0200 Subject: [PATCH 08/10] INI: Prepare for schema validation Pointer to function ini_schema_validator_func will be an optional and can be used to schema validation. It shoul also prepare data for validator function if last output argument is not NULL. These prepared data will be passed to validator function as a 5th argument of ini_validator_func. These two functions are responsible for memory management of passed additional data. It isn't an API/ABI change because lib_iniconfig has not been released yet. --- ini/ini_configobj.c | 14 +++++++++----- ini/ini_configobj.h | 9 ++++++++- ini/ini_validators_ut_check.c | 16 +++++++++------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/ini/ini_configobj.c b/ini/ini_configobj.c index 5c3be11913e316db377d20812ab7f8a95bdcf7a1..fe8f6710cc7b3c8312819378f99707aaaf95d41b 100644 --- a/ini/ini_configobj.c +++ b/ini/ini_configobj.c @@ -1082,7 +1082,8 @@ done: static int ini_dummy_noerror(const char *rule_name, struct ini_cfgobj *rules_obj, struct ini_cfgobj *config_obj, - struct ini_errobj *errobj) + struct ini_errobj *errobj, + void **data) { return 0; } @@ -1091,7 +1092,8 @@ static int ini_dummy_noerror(const char *rule_name, static int ini_dummy_error(const char *rule_name, struct ini_cfgobj *rules_obj, struct ini_cfgobj *config_obj, - struct ini_errobj *errobj) + struct ini_errobj *errobj, + void **data) { return ini_errobj_add_msg(errobj, "Error"); } @@ -1133,7 +1135,8 @@ static int is_allowed_section(const char *tested_section, static int ini_allowed_sections(const char *rule_name, struct ini_cfgobj *rules_obj, struct ini_cfgobj *config_obj, - struct ini_errobj *errobj) + struct ini_errobj *errobj, + void **data) { struct value_obj *vo = NULL; int ret; @@ -1357,7 +1360,8 @@ static int check_if_allowed(char *section, char *attr, char **allowed, static int ini_allowed_options(const char *rule_name, struct ini_cfgobj *rules_obj, struct ini_cfgobj *config_obj, - struct ini_errobj *errobj) + struct ini_errobj *errobj, + void **data) { struct value_obj *vo = NULL; int ret; @@ -1620,7 +1624,7 @@ int ini_rules_check(struct ini_cfgobj *rules_obj, goto done; } - ret = vfunc(sections[i], rules_obj, config_obj, localerr); + ret = vfunc(sections[i], rules_obj, config_obj, localerr, NULL); if (ret != 0) { /* Just report the error and continue normally, * maybe there are some errors in localerr */ diff --git a/ini/ini_configobj.h b/ini/ini_configobj.h index 648ef870bbc19c3b7545f87432365d5f23b94f9d..1b61db07940d2a87d1ed7980ca8b460070848c6a 100644 --- a/ini/ini_configobj.h +++ b/ini/ini_configobj.h @@ -2165,7 +2165,13 @@ size_t ini_errobj_count(struct ini_errobj *errobj); typedef int (ini_validator_func)(const char *rule_name, struct ini_cfgobj *rules_obj, struct ini_cfgobj *config_obj, - struct ini_errobj *errobj); + struct ini_errobj *errobj, + void **data); + +typedef int (ini_schema_validator_func)(const char *rule_name, + struct ini_cfgobj *rules_obj, + struct ini_errobj *errobj, + void **data); /** @brief Structure used to define application specific * (external to libini) validator @@ -2173,6 +2179,7 @@ typedef int (ini_validator_func)(const char *rule_name, struct ini_validator { const char *name; ini_validator_func *func; + ini_schema_validator_func *schema_validator; }; /** diff --git a/ini/ini_validators_ut_check.c b/ini/ini_validators_ut_check.c index 9aadc3c99231c19efcec15440db55b000550bb34..fa7105a4afbcc4af2363afa70c71b909413f4f2f 100644 --- a/ini/ini_validators_ut_check.c +++ b/ini/ini_validators_ut_check.c @@ -280,7 +280,8 @@ END_TEST static int custom_noerror(const char *rule_name, struct ini_cfgobj *rules_obj, struct ini_cfgobj *config_obj, - struct ini_errobj *errobj) + struct ini_errobj *errobj, + void **data) { return 0; } @@ -288,7 +289,8 @@ static int custom_noerror(const char *rule_name, static int custom_error(const char *rule_name, struct ini_cfgobj *rules_obj, struct ini_cfgobj *config_obj, - struct ini_errobj *errobj) + struct ini_errobj *errobj, + void **data) { return ini_errobj_add_msg(errobj, "Error"); } @@ -300,12 +302,12 @@ START_TEST(test_custom_noerror) struct ini_errobj *errobj; int ret; struct ini_validator *noerror[] = { - &(struct ini_validator){ "custom_noerror", custom_noerror }, + &(struct ini_validator){ "custom_noerror", custom_noerror, NULL }, NULL }; struct ini_validator *missing_name[] = { - &(struct ini_validator){ NULL, custom_noerror }, - &(struct ini_validator){ "custom_noerror", custom_noerror }, + &(struct ini_validator){ NULL, custom_noerror, NULL }, + &(struct ini_validator){ "custom_noerror", custom_noerror, NULL }, NULL }; @@ -351,11 +353,11 @@ START_TEST(test_custom_error) struct ini_errobj *errobj; int ret; struct ini_validator *error[] = { - &(struct ini_validator){ "custom_error", custom_error }, + &(struct ini_validator){ "custom_error", custom_error, NULL }, NULL }; struct ini_validator *missing_function[] = { - &(struct ini_validator){ "custom_noerror", NULL }, + &(struct ini_validator){ "custom_noerror", NULL, NULL }, NULL }; const char *errmsg; -- 2.7.4