]> gitweb.factorcode.org Git - factor.git/commitdiff
vm: Add AS_UTF and use it to print wchar_t in Windows terminal.
authorDoug Coleman <doug.coleman@gmail.com>
Sat, 7 Mar 2020 05:57:57 +0000 (23:57 -0600)
committerDoug Coleman <doug.coleman@gmail.com>
Sat, 7 Mar 2020 05:57:57 +0000 (23:57 -0600)
There may be more places to add AS_UTF8 calls--anywhere that prints a
wchar_t string in the Windows vm.

Fixes #992.

vm/image.cpp
vm/os-unix.hpp
vm/os-windows.hpp

index f29ca779a331bb3d483d619d0d48fea52bbc9a9b..c61a0a83cd9ad94b8678a87d54aa2d8651e90c5a 100644 (file)
@@ -232,9 +232,9 @@ void factor_vm::load_image(vm_parameters* p) {
 
   FILE* file = OPEN_READ(p->image_path);
   if (file == NULL) {
-    std::cout << "Cannot open image file: " << p->image_path << std::endl;
+    std::cout << "Cannot open image file: " << AS_UTF8(p->image_path) << std::endl;
     char *msg = threadsafe_strerror(errno);
-    std::cout << "strerror:2: " << msg << std::endl;
+    std::cout << "strerror: " << msg << std::endl;
     free(msg);
     exit(1);
   }
index 33bde83e5b9f955338aafd4daf7cdabf6233e011..b89a9bfcd6c14f2bfdfde83917f18a748870254a 100644 (file)
@@ -53,4 +53,5 @@ void check_ENOMEM(const char* msg);
 
 static inline void breakpoint() { __builtin_trap(); }
 
+#define AS_UTF8(ptr) ptr
 }
index cca3c7e77642a77204498d13d47d5533897ba479..a21a67361b692a3f7ec9c9e10490cc230303912b 100644 (file)
@@ -92,4 +92,36 @@ inline static void breakpoint() { DebugBreak(); }
 
 extern HANDLE boot_thread;
 
+inline static std::string to_utf8(const wchar_t* buffer, int len) {
+  int nChars = ::WideCharToMultiByte(
+    CP_UTF8,
+    0,
+    buffer,
+    len,
+    NULL,
+    0,
+    NULL,
+    NULL);
+  if (nChars == 0) return "";
+
+  std::string newbuffer;
+  newbuffer.resize(nChars) ;
+  ::WideCharToMultiByte(
+    CP_UTF8,
+    0,
+    buffer,
+    len,
+    const_cast<char*>(newbuffer.c_str()),
+    nChars,
+    NULL,
+    NULL);
+  return newbuffer;
+}
+
+inline static std::string to_utf8(const std::wstring& str) {
+  return to_utf8(str.c_str(), (int)str.size());
+}
+
+#define AS_UTF8(ptr) to_utf8(ptr)
+
 }