Skip to content

Commit de04fef

Browse files
Lukas Slebodnikjhrozek
authored andcommitted
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. Reviewed-by: Jakub Hrozek <[email protected]>
1 parent 5078bf8 commit de04fef

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

ini/ini_configobj.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,8 @@ int ini_rules_read_from_file(const char *filename,
10821082
static int ini_dummy_noerror(const char *rule_name,
10831083
struct ini_cfgobj *rules_obj,
10841084
struct ini_cfgobj *config_obj,
1085-
struct ini_errobj *errobj)
1085+
struct ini_errobj *errobj,
1086+
void **data)
10861087
{
10871088
return 0;
10881089
}
@@ -1091,7 +1092,8 @@ static int ini_dummy_noerror(const char *rule_name,
10911092
static int ini_dummy_error(const char *rule_name,
10921093
struct ini_cfgobj *rules_obj,
10931094
struct ini_cfgobj *config_obj,
1094-
struct ini_errobj *errobj)
1095+
struct ini_errobj *errobj,
1096+
void **data)
10951097
{
10961098
return ini_errobj_add_msg(errobj, "Error");
10971099
}
@@ -1133,7 +1135,8 @@ static int is_allowed_section(const char *tested_section,
11331135
static int ini_allowed_sections(const char *rule_name,
11341136
struct ini_cfgobj *rules_obj,
11351137
struct ini_cfgobj *config_obj,
1136-
struct ini_errobj *errobj)
1138+
struct ini_errobj *errobj,
1139+
void **data)
11371140
{
11381141
struct value_obj *vo = NULL;
11391142
int ret;
@@ -1357,7 +1360,8 @@ static int check_if_allowed(char *section, char *attr, char **allowed,
13571360
static int ini_allowed_options(const char *rule_name,
13581361
struct ini_cfgobj *rules_obj,
13591362
struct ini_cfgobj *config_obj,
1360-
struct ini_errobj *errobj)
1363+
struct ini_errobj *errobj,
1364+
void **data)
13611365
{
13621366
struct value_obj *vo = NULL;
13631367
int ret;
@@ -1620,7 +1624,7 @@ int ini_rules_check(struct ini_cfgobj *rules_obj,
16201624
goto done;
16211625
}
16221626

1623-
ret = vfunc(sections[i], rules_obj, config_obj, localerr);
1627+
ret = vfunc(sections[i], rules_obj, config_obj, localerr, NULL);
16241628
if (ret != 0) {
16251629
/* Just report the error and continue normally,
16261630
* maybe there are some errors in localerr */

ini/ini_configobj.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2165,14 +2165,22 @@ size_t ini_errobj_count(struct ini_errobj *errobj);
21652165
typedef int (ini_validator_func)(const char *rule_name,
21662166
struct ini_cfgobj *rules_obj,
21672167
struct ini_cfgobj *config_obj,
2168-
struct ini_errobj *errobj);
2168+
struct ini_errobj *errobj,
2169+
void **data);
2170+
2171+
typedef int (ini_schema_validator_func)(const char *rule_name,
2172+
struct ini_cfgobj *rules_obj,
2173+
struct ini_errobj *errobj,
2174+
void **data);
21692175

21702176
/** @brief Structure used to define application specific
21712177
* (external to libini) validator
21722178
*/
21732179
struct ini_validator {
21742180
const char *name;
21752181
ini_validator_func *func;
2182+
/* currently unused, for future expansion */
2183+
ini_schema_validator_func *schema_validator;
21762184
};
21772185

21782186
/**

ini/ini_validators_ut_check.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -280,15 +280,17 @@ END_TEST
280280
static int custom_noerror(const char *rule_name,
281281
struct ini_cfgobj *rules_obj,
282282
struct ini_cfgobj *config_obj,
283-
struct ini_errobj *errobj)
283+
struct ini_errobj *errobj,
284+
void **data)
284285
{
285286
return 0;
286287
}
287288

288289
static int custom_error(const char *rule_name,
289290
struct ini_cfgobj *rules_obj,
290291
struct ini_cfgobj *config_obj,
291-
struct ini_errobj *errobj)
292+
struct ini_errobj *errobj,
293+
void **data)
292294
{
293295
return ini_errobj_add_msg(errobj, "Error");
294296
}
@@ -300,12 +302,12 @@ START_TEST(test_custom_noerror)
300302
struct ini_errobj *errobj;
301303
int ret;
302304
struct ini_validator *noerror[] = {
303-
&(struct ini_validator){ "custom_noerror", custom_noerror },
305+
&(struct ini_validator){ "custom_noerror", custom_noerror, NULL },
304306
NULL
305307
};
306308
struct ini_validator *missing_name[] = {
307-
&(struct ini_validator){ NULL, custom_noerror },
308-
&(struct ini_validator){ "custom_noerror", custom_noerror },
309+
&(struct ini_validator){ NULL, custom_noerror, NULL },
310+
&(struct ini_validator){ "custom_noerror", custom_noerror, NULL },
309311
NULL
310312
};
311313

@@ -351,11 +353,11 @@ START_TEST(test_custom_error)
351353
struct ini_errobj *errobj;
352354
int ret;
353355
struct ini_validator *error[] = {
354-
&(struct ini_validator){ "custom_error", custom_error },
356+
&(struct ini_validator){ "custom_error", custom_error, NULL },
355357
NULL
356358
};
357359
struct ini_validator *missing_function[] = {
358-
&(struct ini_validator){ "custom_noerror", NULL },
360+
&(struct ini_validator){ "custom_noerror", NULL, NULL },
359361
NULL
360362
};
361363
const char *errmsg;

0 commit comments

Comments
 (0)