aboutsummaryrefslogtreecommitdiff
path: root/lib/config.c
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2020-04-01 15:10:50 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2020-04-01 15:11:03 -0400
commit629a8d7dd058f5e8a854022f03055cd4ced9280e (patch)
treeb42162cd0cf08183bc80150e31c8e4360c8a9bfe /lib/config.c
parentbcc9a422cdd0f6b801b5bd4044125219cb3b74a6 (diff)
downloadspmc-629a8d7dd058f5e8a854022f03055cd4ced9280e.tar.gz
Add test_config_read test
* config_read removes whitespace correctly near inline comments
Diffstat (limited to 'lib/config.c')
-rw-r--r--lib/config.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/lib/config.c b/lib/config.c
index 292c633..0fb17e8 100644
--- a/lib/config.c
+++ b/lib/config.c
@@ -74,8 +74,9 @@ ConfigItem **config_read(const char *filename) {
// Get a pointer to the key pair separator
char *sep_pos = strchr(lptr, sep_ch);
if (!sep_pos) {
- printf("invalid entry on line %zu: missing '%s': '%s'\n", record, sep_str, lptr);
- continue;
+ fprintf(stderr, "invalid entry on line %zu: missing '%s': '%s'\n", record, sep_str, lptr);
+ config_free(config);
+ return NULL;
}
// These values are approximations. The real length(s) are recorded using strlen below.
@@ -126,8 +127,9 @@ ConfigItem **config_read(const char *filename) {
// Populate the value, and ignore any inline comments
while (*lptr) {
if (*lptr == '#' || *lptr == ';') {
- // strip trailing whitespace where the comment is and stop processing
- value = strip(value);
+ // `value` was populated up to the comment.
+ // now strip whitespace from the string `value` actually points to
+ value_orig = strip(value_orig);
break;
}
*value++ = *lptr++;
@@ -168,6 +170,9 @@ ConfigItem **config_read(const char *filename) {
* @param item `ConfigItem` array
*/
void config_free(ConfigItem **item) {
+ if (item == NULL) {
+ return;
+ }
for (size_t i = 0; item[i] != NULL; i++) {
free(item[i]->key);
free(item[i]->value);