aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2016-12-20 18:05:51 +0100
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2016-12-20 18:05:51 +0100
commit209ea0657b7970b4b8069ce44b43eecb97681892 (patch)
tree681a531d2d49fa4f12eecc94649069ef0b4d5e1b
parent08fe4ebf7c6b5430134a22177079eadb08d71cdc (diff)
downloadblogc-209ea0657b7970b4b8069ce44b43eecb97681892.tar.gz
blogc-209ea0657b7970b4b8069ce44b43eecb97681892.tar.bz2
blogc-209ea0657b7970b4b8069ce44b43eecb97681892.zip
config-parser: improved error reporting, added bc_config_get_with_default
-rw-r--r--src/common/config-parser.c23
-rw-r--r--src/common/config-parser.h2
-rw-r--r--tests/common/check_config_parser.c17
3 files changed, 32 insertions, 10 deletions
diff --git a/src/common/config-parser.c b/src/common/config-parser.c
index 24fb299..ff05f2e 100644
--- a/src/common/config-parser.c
+++ b/src/common/config-parser.c
@@ -70,8 +70,8 @@ bc_config_parse(const char *src, size_t src_len, bc_error_t **err)
continue;
}
if (err != NULL)
- *err = bc_error_new_printf(BC_ERROR_CONFIG_PARSER,
- "File must start with section");
+ *err = bc_error_parser(BC_ERROR_CONFIG_PARSER, src, src_len,
+ current, "File must start with section.");
break;
case CONFIG_SECTION_START:
@@ -92,8 +92,8 @@ bc_config_parse(const char *src, size_t src_len, bc_error_t **err)
if (c != '\r' && c != '\n')
break;
if (err != NULL)
- *err = bc_error_new_printf(BC_ERROR_CONFIG_PARSER,
- "Section names can't have new lines");
+ *err = bc_error_parser(BC_ERROR_CONFIG_PARSER, src, src_len,
+ current, "Section names can't have new lines.");
break;
case CONFIG_SECTION_KEY:
@@ -109,8 +109,8 @@ bc_config_parse(const char *src, size_t src_len, bc_error_t **err)
size_t end = is_last && c != '\n' && c != '\r' ? src_len :
current;
key = bc_strndup(src + start, end - start);
- *err = bc_error_new_printf(BC_ERROR_CONFIG_PARSER,
- "Key without value: %s", key);
+ *err = bc_error_parser(BC_ERROR_CONFIG_PARSER, src, src_len,
+ current, "Key without value: %s.", key);
free(key);
key = NULL;
}
@@ -225,6 +225,17 @@ bc_config_get(bc_config_t *config, const char *section, const char *key)
}
+const char*
+bc_config_get_with_default(bc_config_t *config, const char *section, const char *key,
+ const char *default_)
+{
+ const char *rv = bc_config_get(config, section, key);
+ if (rv == NULL)
+ return default_;
+ return rv;
+}
+
+
void
bc_config_free(bc_config_t *config)
{
diff --git a/src/common/config-parser.h b/src/common/config-parser.h
index f9f287c..e249c37 100644
--- a/src/common/config-parser.h
+++ b/src/common/config-parser.h
@@ -22,6 +22,8 @@ char** bc_config_list_sections(bc_config_t *config);
char** bc_config_list_keys(bc_config_t *config, const char *section);
const char* bc_config_get(bc_config_t *config, const char *section,
const char *key);
+const char* bc_config_get_with_default(bc_config_t *config, const char *section,
+ const char *key, const char *default_);
void bc_config_free(bc_config_t *config);
#endif /* _CONFIG_PARSER_H */
diff --git a/tests/common/check_config_parser.c b/tests/common/check_config_parser.c
index 2a6ad67..66003e0 100644
--- a/tests/common/check_config_parser.c
+++ b/tests/common/check_config_parser.c
@@ -29,6 +29,7 @@ test_config_empty(void **state)
assert_non_null(c);
assert_non_null(c->root);
assert_int_equal(bc_trie_size(c->root), 0);
+ assert_string_equal(bc_config_get_with_default(c, "bola", "foo", "bar"), "bar");
bc_config_free(c);
}
@@ -369,7 +370,9 @@ test_config_error_start(void **state)
assert_non_null(err);
assert_null(c);
assert_int_equal(err->type, BC_ERROR_CONFIG_PARSER);
- assert_string_equal(err->msg, "File must start with section");
+ assert_string_equal(err->msg,
+ "File must start with section.\n"
+ "Error occurred near line 1, position 1: asd");
bc_error_free(err);
}
@@ -384,7 +387,9 @@ test_config_error_section_with_newline(void **state)
assert_non_null(err);
assert_null(c);
assert_int_equal(err->type, BC_ERROR_CONFIG_PARSER);
- assert_string_equal(err->msg, "Section names can't have new lines");
+ assert_string_equal(err->msg,
+ "Section names can't have new lines.\n"
+ "Error occurred near line 1, position 5: [foo");
bc_error_free(err);
}
@@ -401,7 +406,9 @@ test_config_error_key_without_value(void **state)
assert_non_null(err);
assert_null(c);
assert_int_equal(err->type, BC_ERROR_CONFIG_PARSER);
- assert_string_equal(err->msg, "Key without value: foo");
+ assert_string_equal(err->msg,
+ "Key without value: foo.\n"
+ "Error occurred near line 3, position 3: foo");
bc_error_free(err);
a =
"[foobar]\n"
@@ -412,7 +419,9 @@ test_config_error_key_without_value(void **state)
assert_non_null(err);
assert_null(c);
assert_int_equal(err->type, BC_ERROR_CONFIG_PARSER);
- assert_string_equal(err->msg, "Key without value: foo");
+ assert_string_equal(err->msg,
+ "Key without value: foo.\n"
+ "Error occurred near line 3, position 4: foo");
bc_error_free(err);
}