Browse Source

[linux-port] Add MultiByte<->WideChar methods. (#1357)

Ehsan 7 years ago
parent
commit
f7cd091e93
2 changed files with 75 additions and 0 deletions
  1. 19 0
      include/dxc/Support/Unicode.h
  2. 56 0
      lib/DxcSupport/Unicode.cpp

+ 19 - 0
include/dxc/Support/Unicode.h

@@ -12,7 +12,26 @@
 #pragma once
 
 #include <string>
+
+#ifdef _WIN32
 #include <specstrings.h>
+#else
+// MultiByteToWideChar which is a Windows-specific method.
+// This is a very simplistic implementation for non-Windows platforms. This
+// implementation completely ignores CodePage and dwFlags.
+int MultiByteToWideChar(uint32_t CodePage, uint32_t dwFlags,
+                        const char *lpMultiByteStr, int cbMultiByte,
+                        wchar_t *lpWideCharStr, int cchWideChar);
+
+// WideCharToMultiByte is a Windows-specific method.
+// This is a very simplistic implementation for non-Windows platforms. This
+// implementation completely ignores CodePage and dwFlags.
+int WideCharToMultiByte(uint32_t CodePage, uint32_t dwFlags,
+                        const wchar_t *lpWideCharStr, int cchWideChar,
+                        char *lpMultiByteStr, int cbMultiByte,
+                        const char *lpDefaultChar = nullptr,
+                        bool *lpUsedDefaultChar = nullptr);
+#endif // _WIN32
 
 namespace Unicode
 {

+ 56 - 0
lib/DxcSupport/Unicode.cpp

@@ -17,6 +17,62 @@
 #include "dxc/Support/Unicode.h"
 #include "dxc/Support/WinIncludes.h"
 
+#ifndef _WIN32
+// MultiByteToWideChar which is a Windows-specific method.
+// This is a very simplistic implementation for non-Windows platforms. This
+// implementation completely ignores CodePage and dwFlags.
+int MultiByteToWideChar(uint32_t /*CodePage*/, uint32_t /*dwFlags*/,
+                        const char *lpMultiByteStr, int cbMultiByte,
+                        wchar_t *lpWideCharStr, int cchWideChar) {
+  // if cbMultiByte is -1, it indicates that lpMultiByteStr is null-terminated
+  // and the entire string should be processed.
+  if (cbMultiByte == -1) {
+    for (cbMultiByte = 0; lpMultiByteStr[cbMultiByte] != '\0'; ++cbMultiByte)
+      ;
+    // Add 1 for the null-terminating character.
+    ++cbMultiByte;
+  }
+  // if zero is given as the destination size, this function should
+  // return the required size (including the null-terminating character).
+  if (cchWideChar == 0) {
+    wchar_t *tempStr = (wchar_t *)malloc(cbMultiByte * sizeof(wchar_t));
+    size_t requiredSize = mbstowcs(tempStr, lpMultiByteStr, cbMultiByte);
+    free(tempStr);
+    return requiredSize;
+  }
+
+  return mbstowcs(lpWideCharStr, lpMultiByteStr, cbMultiByte);
+}
+
+// WideCharToMultiByte is a Windows-specific method.
+// This is a very simplistic implementation for non-Windows platforms. This
+// implementation completely ignores CodePage and dwFlags.
+int WideCharToMultiByte(uint32_t /*CodePage*/, uint32_t /*dwFlags*/,
+                        const wchar_t *lpWideCharStr, int cchWideChar,
+                        char *lpMultiByteStr, int cbMultiByte,
+                        const char * /*lpDefaultChar*/,
+                        bool * /*lpUsedDefaultChar*/) {
+  // if cchWideChar is -1, it indicates that lpWideCharStr is null-terminated
+  // and the entire string should be processed.
+  if (cchWideChar == -1) {
+    for (cchWideChar = 0; lpWideCharStr[cchWideChar] != '\0'; ++cchWideChar)
+      ;
+    // Add 1 for the null-terminating character.
+    ++cchWideChar;
+  }
+  // if zero is given as the destination size, this function should
+  // return the required size (including the null-terminating character).
+  if (cbMultiByte == 0) {
+    char *tempStr = (char *)malloc(cchWideChar * sizeof(char));
+    size_t requiredSize = wcstombs(tempStr, lpWideCharStr, cchWideChar);
+    free(tempStr);
+    return requiredSize;
+  }
+
+  return wcstombs(lpMultiByteStr, lpWideCharStr, cchWideChar);
+}
+#endif // _WIN32
+
 namespace Unicode {
 
 _Success_(return != false)