Move clean_value function to utils.c

This is a typical string manipulation function an has no necessity to
stay in option_parser.
This commit is contained in:
Benedikt Heine 2018-10-01 09:54:16 +02:00
parent 6cc7ca361a
commit 778a6857d8
4 changed files with 53 additions and 15 deletions

View File

@ -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)

View File

@ -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;

View File

@ -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);

View File

@ -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);