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

This commit is contained in:
Ehsan 2018-06-23 21:43:39 -04:00 коммит произвёл GitHub
Родитель 82c289db99
Коммит f7cd091e93
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 75 добавлений и 0 удалений

Просмотреть файл

@ -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
{

Просмотреть файл

@ -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)