about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2013-07-14 01:00:11 +0100
committerJames Booth <boothj5@gmail.com>2013-07-14 01:00:11 +0100
commit7f82dc42f593f6410e4d0058add4b91112047e63 (patch)
treee6295745e26fae8c3884d7a5372d868d6129907e /src
parente7478d8cb8c55db91628a059ff9f1065bfb9cf0e (diff)
downloadprofani-tty-7f82dc42f593f6410e4d0058add4b91112047e63.tar.gz
Remaining parser function unicode compatible
Diffstat (limited to 'src')
-rw-r--r--src/tools/parser.c59
1 files changed, 29 insertions, 30 deletions
diff --git a/src/tools/parser.c b/src/tools/parser.c
index 595032c3..f4cfc3d2 100644
--- a/src/tools/parser.c
+++ b/src/tools/parser.c
@@ -297,34 +297,27 @@ parse_args_with_freetext(const char * const inp, int min, int max)
 int
 count_tokens(char *string)
 {
+    int length = g_utf8_strlen(string, -1);
+    gboolean in_quotes = FALSE;
     int num_tokens = 0;
+    int i = 0;
 
-    // if no quotes, use glib
-    if (g_strrstr(string, "\"") == NULL) {
-        gchar **tokens = g_strsplit(string, " ", 0);
-        num_tokens = g_strv_length(tokens);
-        g_strfreev(tokens);
-
-    // else count tokens including quoted
-    } else {
-        int length = strlen(string);
-        int i = 0;
-        gboolean in_quotes = FALSE;
+    // include first token
+    num_tokens++;
 
-        // include first token
-        num_tokens++;
+    for (i = 0; i < length; i++) {
+        gchar *curr_ch = g_utf8_offset_to_pointer(string, i);
+        gunichar curr_uni = g_utf8_get_char(curr_ch);
 
-        for (i = 0; i < length; i++) {
-            if (string[i] == ' ') {
-                if (!in_quotes) {
-                    num_tokens++;
-                }
-            } else if (string[i] == '"') {
-                if (in_quotes) {
-                    in_quotes = FALSE;
-                } else {
-                    in_quotes = TRUE;
-                }
+        if (curr_uni == ' ') {
+            if (!in_quotes) {
+                num_tokens++;
+            }
+        } else if (curr_uni == '"') {
+            if (in_quotes) {
+                in_quotes = FALSE;
+            } else {
+                in_quotes = TRUE;
             }
         }
     }
@@ -335,25 +328,31 @@ count_tokens(char *string)
 char *
 get_start(char *string, int tokens)
 {
+    GString *result = g_string_new("");
+    int length = g_utf8_strlen(string, -1);
+    gboolean in_quotes = FALSE;
     char *result_str = NULL;
     int num_tokens = 0;
-    int length = strlen(string);
     int i = 0;
-    gboolean in_quotes = FALSE;
-    GString *result = g_string_new("");
 
     // include first token
     num_tokens++;
 
     for (i = 0; i < length; i++) {
+        gchar *curr_ch = g_utf8_offset_to_pointer(string, i);
+        gunichar curr_uni = g_utf8_get_char(curr_ch);
+
         if (num_tokens < tokens) {
-            g_string_append_c(result, string[i]);
+            gchar *uni_char = malloc(7);
+            int len = g_unichar_to_utf8(curr_uni, uni_char);
+            uni_char[len] = '\0';
+            g_string_append(result, uni_char);
         }
-        if (string[i] == ' ') {
+        if (curr_uni == ' ') {
             if (!in_quotes) {
                 num_tokens++;
             }
-        } else if (string[i] == '"') {
+        } else if (curr_uni == '"') {
             if (in_quotes) {
                 in_quotes = FALSE;
             } else {