Browse Source

[Fix #5] display unicode chars

isundil 9 years ago
parent
commit
b1d2220eb7
4 changed files with 29 additions and 4 deletions
  1. 1 1
      include/streamConsumer.hh
  2. 4 0
      src/main.cpp
  3. 23 3
      src/streamConsumer.cpp
  4. 1 0
      test/testUnicode.json

+ 1 - 1
include/streamConsumer.hh

@@ -29,7 +29,7 @@ class StreamConsumer
         JSonObject *readObject(JSonContainer *parent);
         JSonArray *readArray(JSonContainer *parent);
         bool ignoreChar(char c) const noexcept;
-        void appendUnicode(const char [4], std::string &);
+        static void appendUnicode(const char [4], std::string &);
 
         std::istream &stream;
         JSonElement *root;

+ 4 - 0
src/main.cpp

@@ -1,5 +1,6 @@
 #include <iostream>
 #include <typeinfo>
+#include <locale.h>
 #include "streamConsumer.hh"
 #include "curseOutput.hh"
 #include "params.hh"
@@ -7,11 +8,14 @@
 
 void run(Params *params)
 {
+
     StreamConsumer stream(StreamConsumer(params->getInput()));
     stream.withConfig(params);
     CurseOutput *out;
     JSonElement *root;
 
+    if (!params->isIgnoringUnicode())
+        setlocale(LC_ALL, "");
     try
     {
         root = stream.read()->getRoot();

+ 23 - 3
src/streamConsumer.cpp

@@ -251,11 +251,31 @@ JSonElement *StreamConsumer::consumeToken(JSonContainer *parent, std::string &bu
     return nullptr;
 }
 
+static unsigned char hexbyte(const char c)
+{
+    if (c >= '0' && c <= '9')
+        return c - '0';
+    if (c >= 'A' && c <= 'F')
+        return c - 'A' + 10;
+    if (c >= 'a' && c <= 'f')
+        return c - 'a' + 10;
+    return 0;
+}
+
+static unsigned char hexbyte(const char str[2])
+{
+    unsigned char result = 0;
+    result = (hexbyte(*str) <<4) + hexbyte(str[1]);
+    return result;
+}
+
 void StreamConsumer::appendUnicode(const char unicode[4], std::string &buf)
 {
-    std::string rawHex = { '0', 'x', unicode[0], unicode[1], unicode[2], unicode[3], '\0' };
-    wchar_t unichar = std::stoul(rawHex, nullptr, 16);
-    buf += unichar;
+    unsigned short uni = (hexbyte(unicode) <<8) + hexbyte(unicode+2);
+    char test[5];
+    bzero(test, sizeof(*test) *5);
+    snprintf(test, 4, "%lc", uni);
+    buf += test;
 }
 
 bool StreamConsumer::ignoreChar(char c) const noexcept

+ 1 - 0
test/testUnicode.json

@@ -1,4 +1,5 @@
 [
     "ascii-only",
     "\u058e",
+    "\u20ac"
 ]