yml: yml_load(): optionally allocates an error string (on error)

This commit is contained in:
Daniel Eklöf 2019-01-01 23:25:36 +01:00
parent 3dc7d0e39d
commit 5abd825137
3 changed files with 24 additions and 8 deletions

3
main.c
View file

@ -62,7 +62,8 @@ main(int argc, const char *const *argv)
if (conf_file == NULL)
return 1;
struct yml_node *conf = yml_load(conf_file);
char *yml_error;
struct yml_node *conf = yml_load(conf_file, &yml_error);
fclose(conf_file);
if (conf == NULL)

27
yml.c
View file

@ -215,7 +215,7 @@ post_process(struct yml_node *node)
}
struct yml_node *
yml_load(FILE *yml)
yml_load(FILE *yml, char **error)
{
yaml_parser_t yaml;
yaml_parser_initialize(&yaml);
@ -238,11 +238,26 @@ yml_load(FILE *yml)
while (!done) {
yaml_event_t event;
if (!yaml_parser_parse(&yaml, &event)) {
//printf("yaml parser error\n");
/* TODO: free node tree */
root = NULL;
assert(false);
break;
if (error != NULL) {
int cnt = snprintf(
NULL, 0, "%zu:%zu: %s %s",
yaml.problem_mark.line,
yaml.problem_mark.column,
yaml.problem,
yaml.context != NULL ? yaml.context : "");
*error = malloc(cnt + 1);
snprintf(*error, cnt + 1, "%zu:%zu: %s %s",
yaml.problem_mark.line,
yaml.problem_mark.column,
yaml.problem,
yaml.context != NULL ? yaml.context : "");
(*error)[cnt] = '\0';
}
yml_destroy(root);
yaml_parser_delete(&yaml);
return NULL;
}
switch (event.type) {

2
yml.h
View file

@ -4,7 +4,7 @@
struct yml_node;
struct yml_node *yml_load(FILE *yml);
struct yml_node *yml_load(FILE *yml, char **error);
void yml_destroy(struct yml_node *root);
bool yml_is_scalar(const struct yml_node *node);