From 778a6857d87205350f0c91f83843a6d6b713dd53 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Mon, 1 Oct 2018 09:54:16 +0200 Subject: [PATCH] Move clean_value function to utils.c This is a typical string manipulation function an has no necessity to stay in option_parser. --- src/option_parser.c | 16 +--------------- src/utils.c | 17 +++++++++++++++++ src/utils.h | 8 ++++++++ test/utils.c | 27 +++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 15 deletions(-) diff --git a/src/option_parser.c b/src/option_parser.c index 16fe623..849a2ba 100644 --- a/src/option_parser.c +++ b/src/option_parser.c @@ -30,7 +30,6 @@ static struct section *new_section(const char *name); static struct section *get_section(const char *name); static void add_entry(const char *section_name, const char *key, const char *value); static const char *get_value(const char *section, const char *key); -static char *clean_value(const char *value); static int cmdline_argc; static char **cmdline_argv; @@ -90,7 +89,7 @@ void add_entry(const char *section_name, const char *key, const char *value) int len = s->entry_count; s->entries = g_realloc(s->entries, sizeof(struct entry) * len); s->entries[s->entry_count - 1].key = g_strdup(key); - s->entries[s->entry_count - 1].value = clean_value(value); + s->entries[s->entry_count - 1].value = string_strip_quotes(value); } const char *get_value(const char *section, const char *key) @@ -201,19 +200,6 @@ int ini_get_bool(const char *section, const char *key, int def) } } -char *clean_value(const char *value) -{ - size_t len = strlen(value); - char *s; - - if (value[0] == '"' && value[len-1] == '"') - s = g_strndup(value + 1, len-2); - else - s = g_strdup(value); - - return s; -} - int load_ini_file(FILE *fp) { if (!fp) diff --git a/src/utils.c b/src/utils.c index 8f06141..e5e7d54 100644 --- a/src/utils.c +++ b/src/utils.c @@ -97,6 +97,23 @@ char *string_append(char *a, const char *b, const char *sep) } +/* see utils.h */ +char *string_strip_quotes(const char *value) +{ + if (!value) + return NULL; + + size_t len = strlen(value); + char *s; + + if (value[0] == '"' && value[len-1] == '"') + s = g_strndup(value + 1, len-2); + else + s = g_strdup(value); + + return s; +} + void string_strip_delimited(char *str, char a, char b) { int iread=-1, iwrite=0, copen=0; diff --git a/src/utils.h b/src/utils.h index 48ee618..ca9022e 100644 --- a/src/utils.h +++ b/src/utils.h @@ -21,6 +21,14 @@ char *string_append(char *a, const char *b, const char *sep); /* strip content between two delimiter characters (inplace) */ void string_strip_delimited(char *str, char a, char b); +/** + * Strip quotes from a string. Won't touch inner quotes. + * + * @param value The string to strip the quotes from + * @returns A copy of the string value with the outer quotes removed (if any) + */ +char *string_strip_quotes(const char *value); + /* replace tilde and path-specific values with its equivalents */ char *string_to_path(char *string); diff --git a/test/utils.c b/test/utils.c index d0b5f4b..b99af9e 100644 --- a/test/utils.c +++ b/test/utils.c @@ -104,6 +104,32 @@ TEST test_string_append(void) PASS(); } +TEST test_string_strip_quotes(void) +{ + char *exp = string_strip_quotes(NULL); + ASSERT_FALSE(exp); + + ASSERT_STR_EQ("NewString", (exp = string_strip_quotes("NewString"))); + g_free(exp); + + ASSERT_STR_EQ("becomes unquoted", (exp = string_strip_quotes("\"becomes unquoted\""))); + g_free(exp); + + ASSERT_STR_EQ("\"stays quoted", (exp = string_strip_quotes("\"stays quoted"))); + g_free(exp); + + ASSERT_STR_EQ("stays quoted\"", (exp = string_strip_quotes("stays quoted\""))); + g_free(exp); + + ASSERT_STR_EQ("stays \"quoted\"", (exp = string_strip_quotes("stays \"quoted\""))); + g_free(exp); + + ASSERT_STR_EQ(" \"stays quoted\"", (exp = string_strip_quotes(" \"stays quoted\""))); + g_free(exp); + + PASS(); +} + TEST test_string_strip_delimited(void) { char *text = malloc(128 * sizeof(char)); @@ -178,6 +204,7 @@ SUITE(suite_utils) RUN_TEST(test_string_replace_all); RUN_TEST(test_string_replace); RUN_TEST(test_string_append); + RUN_TEST(test_string_strip_quotes); RUN_TEST(test_string_strip_delimited); RUN_TEST(test_string_to_path); RUN_TEST(test_string_to_time);