]> git.cworth.org Git - apitrace/commitdiff
Fix string formatting.
authorJosé Fonseca <jose.r.fonseca@gmail.com>
Tue, 5 Apr 2011 19:54:00 +0000 (20:54 +0100)
committerJosé Fonseca <jose.r.fonseca@gmail.com>
Tue, 5 Apr 2011 23:54:51 +0000 (00:54 +0100)
In particular, preserve syntax highlighting when piping to 'less -R'

trace_model.cpp

index 7f2ef440f701bcbe00040a47abe4d2ead9f8eead..8821417321616c1b50f46dc9cac1e22641c6d543 100644 (file)
@@ -196,7 +196,35 @@ public:
     }
 
     void visit(String *node) {
-        os << literal << '"' << node->value << '"' << normal;
+        os << literal << "\"";
+        for (std::string::const_iterator it = node->value.begin(); it != node->value.end(); ++it) {
+            unsigned char c = (unsigned char) *it;
+            if (c == '\"')
+                os << "\\\"";
+            else if (c == '\\')
+                os << "\\\\";
+            else if (c >= 0x20 && c <= 0x7e)
+                os << c;
+            else if (c == '\t') {
+                os << "\t";
+            } else if (c == '\r') {
+                // Ignore carriage-return
+            } else if (c == '\n') {
+                // Reset formatting so that it looks correct with 'less -R'
+                os << normal << '\n' << literal;
+            } else {
+                unsigned octal0 = c & 0x7;
+                unsigned octal1 = (c >> 3) & 0x7;
+                unsigned octal2 = (c >> 3) & 0x7;
+                os << "\\";
+                if (octal2)
+                    os << octal2;
+                if (octal1)
+                    os << octal1;
+                os << octal0;
+            }
+        }
+        os << "\"" << normal;
     }
 
     void visit(Enum *node) {