Handle unicode chars in command parser

This commit is contained in:
James Booth
2013-07-12 00:46:33 +01:00
parent 5233000498
commit bb550fed55

View File

@@ -58,7 +58,7 @@ parse_args(const char * const inp, int min, int max)
char *copy = strdup(inp); char *copy = strdup(inp);
g_strstrip(copy); g_strstrip(copy);
int inp_size = strlen(copy); int inp_size = g_utf8_strlen(copy, -1);
gboolean in_token = FALSE; gboolean in_token = FALSE;
gboolean in_quotes = FALSE; gboolean in_quotes = FALSE;
char *token_start = &copy[0]; char *token_start = &copy[0];
@@ -67,43 +67,48 @@ parse_args(const char * const inp, int min, int max)
// add tokens to GSList // add tokens to GSList
int i; int i;
for (i = 0; i <= inp_size; i++) { for (i = 0; i < inp_size; i++) {
gchar *curr_ch = g_utf8_offset_to_pointer(copy, i);
gunichar curr_uni = g_utf8_get_char(curr_ch);
if (!in_token) { if (!in_token) {
if (copy[i] == ' ') { if (curr_uni == ' ') {
continue; continue;
} else { } else {
in_token = TRUE; in_token = TRUE;
if (copy[i] == '"') { if (curr_uni == '"') {
in_quotes = TRUE; in_quotes = TRUE;
i++; i++;
} }
token_start = &copy[i]; token_start = curr_ch;
token_size++; token_size += g_unichar_to_utf8(curr_uni, NULL);
} }
} else { } else {
if (in_quotes) { if (in_quotes) {
if ((copy[i] == '\0') || (copy[i] == '"')) { if (curr_uni == '"') {
tokens = g_slist_append(tokens, g_strndup(token_start, tokens = g_slist_append(tokens, g_strndup(token_start,
token_size)); token_size));
token_size = 0; token_size = 0;
in_token = FALSE; in_token = FALSE;
in_quotes = FALSE; in_quotes = FALSE;
} else { } else {
token_size++; token_size += g_unichar_to_utf8(curr_uni, NULL);
} }
} else { } else {
if (copy[i] == ' ' || copy[i] == '\0') { if (curr_uni == ' ') {
tokens = g_slist_append(tokens, g_strndup(token_start, tokens = g_slist_append(tokens, g_strndup(token_start,
token_size)); token_size));
token_size = 0; token_size = 0;
in_token = FALSE; in_token = FALSE;
} else { } else {
token_size += g_unichar_to_utf8(curr_uni, NULL);
token_size++; token_size++;
} }
} }
} }
} }
tokens = g_slist_append(tokens, g_strndup(token_start, token_size));
int num = g_slist_length(tokens) - 1; int num = g_slist_length(tokens) - 1;
// if num args not valid return NULL // if num args not valid return NULL