Use g_strsplit instead of homebrewing a solution

Lesson: don't reinvent the wheel.
Additionally, changed a `g_malloc` call to `g_malloc_n` for safety.
This commit is contained in:
Michael Krasnitski 2020-04-05 19:35:20 -04:00
parent a4a1a1ac9e
commit 8bcc3070fb
2 changed files with 4 additions and 12 deletions

View File

@ -150,7 +150,7 @@ bool string_parse_mouse_action_list(char **s, enum mouse_action **ret)
while (s[len])
len++;
*ret = g_malloc((len + 1) * sizeof(enum mouse_action));
*ret = g_malloc_n((len + 1), sizeof(enum mouse_action));
for (int i = 0; i < len; i++) {
if (!string_parse_mouse_action(s[i], *ret + i)) {
LOG_W("Unknown mouse action value: '%s'", s[i]);

View File

@ -151,18 +151,10 @@ char **string_to_array(const char *string)
{
char **arr = NULL;
if (string) {
char* dup = g_strdup(string);
char* tmp = dup;
int num_tokens = 0;
char *token = strsep(&tmp, ",");
while (token) {
arr = g_realloc_n(arr, num_tokens + 2, sizeof(char*));
arr[num_tokens] = g_strdup(g_strstrip(token));
num_tokens++;
token = strsep(&tmp, ",");
arr = g_strsplit(string, ",", 0);
for (int i = 0; arr[i]; i++){
g_strstrip(arr[i]);
}
arr[num_tokens] = NULL;
g_free(dup);
}
return arr;
}