forked from external/yambar
yml: yml_load(): optionally allocates an error string (on error)
This commit is contained in:
parent
3dc7d0e39d
commit
5abd825137
3 changed files with 24 additions and 8 deletions
3
main.c
3
main.c
|
@ -62,7 +62,8 @@ main(int argc, const char *const *argv)
|
||||||
if (conf_file == NULL)
|
if (conf_file == NULL)
|
||||||
return 1;
|
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);
|
fclose(conf_file);
|
||||||
|
|
||||||
if (conf == NULL)
|
if (conf == NULL)
|
||||||
|
|
27
yml.c
27
yml.c
|
@ -215,7 +215,7 @@ post_process(struct yml_node *node)
|
||||||
}
|
}
|
||||||
|
|
||||||
struct yml_node *
|
struct yml_node *
|
||||||
yml_load(FILE *yml)
|
yml_load(FILE *yml, char **error)
|
||||||
{
|
{
|
||||||
yaml_parser_t yaml;
|
yaml_parser_t yaml;
|
||||||
yaml_parser_initialize(&yaml);
|
yaml_parser_initialize(&yaml);
|
||||||
|
@ -238,11 +238,26 @@ yml_load(FILE *yml)
|
||||||
while (!done) {
|
while (!done) {
|
||||||
yaml_event_t event;
|
yaml_event_t event;
|
||||||
if (!yaml_parser_parse(&yaml, &event)) {
|
if (!yaml_parser_parse(&yaml, &event)) {
|
||||||
//printf("yaml parser error\n");
|
if (error != NULL) {
|
||||||
/* TODO: free node tree */
|
int cnt = snprintf(
|
||||||
root = NULL;
|
NULL, 0, "%zu:%zu: %s %s",
|
||||||
assert(false);
|
yaml.problem_mark.line,
|
||||||
break;
|
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) {
|
switch (event.type) {
|
||||||
|
|
2
yml.h
2
yml.h
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
struct yml_node;
|
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);
|
void yml_destroy(struct yml_node *root);
|
||||||
|
|
||||||
bool yml_is_scalar(const struct yml_node *node);
|
bool yml_is_scalar(const struct yml_node *node);
|
||||||
|
|
Loading…
Add table
Reference in a new issue