]> git.rm.cloudns.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Chat: use full-fledged JSON for communication between client/menu
authorTimePath <andrew.hardaker1995@gmail.com>
Fri, 6 Apr 2018 10:43:30 +0000 (20:43 +1000)
committerTimePath <andrew.hardaker1995@gmail.com>
Fri, 6 Apr 2018 10:43:30 +0000 (20:43 +1000)
qcsrc/lib/cvar.qh
qcsrc/lib/json.qc
qcsrc/lib/json.qh

index 19b48ee3e6a2f2d0358fc6d1d7b81a55afee0b2a..cdd1b23a8e0648b826d3f4eaf287a70ef670d181 100644 (file)
@@ -1,5 +1,6 @@
 #pragma once
 
+#include "json.qh"
 #include "nil.qh"
 #include "progname.qh"
 #include "static.qh"
@@ -33,24 +34,13 @@ string MakeConsoleSafe(string input)
 ERASEABLE
 string console_encode(string input)
 {
-       input = strreplace("$", "$$", input);
-       input = strreplace("\\", "\\\\", input);
-       input = strreplace("\r", "\\r", input);
-       input = strreplace("\n", "\\n", input);
-       input = strreplace("\"", "\\\"", input);
-       input = sprintf("\"%s\"", input);
-       return input;
+       return json_stringify_string(strreplace("$", "$$", input));
 }
 
 ERASEABLE
 string console_decode(string input)
 {
-       input = substring(input, 1, -2);
-       input = strreplace("\\r", "\r", input);
-       input = strreplace("\\n", "\n", input);
-       input = strreplace("\\\"", "\"", input);
-       input = strreplace("\\\\", "\\", input);
-       return input;
+       return json_parse_string(input);
 }
 
 ERASEABLE
index b477fe15e59124924662e4c463030ff7016ca94e..c0199264c2cb8f92ad72ac8cd0aa4c3f92649c3b 100644 (file)
@@ -299,6 +299,67 @@ void json_dump(int buf)
 #undef JSON_FAIL
 #undef JSON_END
 
+ERASEABLE
+string json_stringify_string(string s)
+{
+    string buf = "\"";
+    FOREACH_CHAR(s, true, {
+        switch (it) {
+            default:
+                buf = strcat(buf, chr2str(it));
+                break;
+            case '\b':
+            case '\f':
+            case '\n':
+            case '\r':
+            case '\t':
+            case '\"':
+            case '\\':
+                buf = strcat(buf, chr2str('\\', it));
+                break;
+        }
+    });
+    return strcat(buf, "\"");
+}
+
+ERASEABLE
+string json_parse_string(string s)
+{
+    s = substring(s, 1, -2);
+    string buf = "";
+    bool parseEscape = false;
+    FOREACH_CHAR(s, true, {
+    switch (it) {
+        default:
+            if (!parseEscape) {
+                buf = strcat(buf, chr2str(it));
+            } else {
+                switch (it) {
+                    // do something sane for invalid escape sequences
+                    default: buf = strcat(buf, "\\", chr2str(it)); break;
+                    case 'b': buf = strcat(buf, "\b"); break;
+                    case 'f': buf = strcat(buf, "\f"); break;
+                    case 'n': buf = strcat(buf, "\n"); break;
+                    case 'r': buf = strcat(buf, "\r"); break;
+                    case 't': buf = strcat(buf, "\t"); break;
+                    case '"': buf = strcat(buf, "\""); break;
+                }
+                parseEscape = false;
+            }
+            break;
+        case '\\':
+            if (!parseEscape) {
+                parseEscape = true;
+            } else {
+                buf = strcat(buf, "\\");
+                parseEscape = false;
+            }
+            break;
+        }
+    });
+    return buf;
+}
+
 TEST(json, Parse)
 {
     string s = "{\n\
index 6f70f09beec2219624baeca92e2cd7deaa104fb4..c1f33b1c0f370e23be42037b655443622c5c0ffd 100644 (file)
@@ -1 +1,4 @@
 #pragma once
+
+string json_stringify_string(string s);
+string json_parse_string(string s);