replace std's wstring_convert to SDL's iconv library

This commit is contained in:
RadWolfie 2019-09-25 23:50:19 -05:00
parent b4457c3605
commit f38dc91075
6 changed files with 68 additions and 14 deletions

View File

@ -90,6 +90,7 @@ file (GLOB CXBXR_HEADER_COMMON
"${CXBXR_ROOT_DIR}/src/common/util/CPUID.h"
"${CXBXR_ROOT_DIR}/src/common/util/crc32c.h"
"${CXBXR_ROOT_DIR}/src/common/util/CxbxUtil.h"
"${CXBXR_ROOT_DIR}/src/common/util/strConverter.hpp"
"${CXBXR_ROOT_DIR}/src/common/win32/AlignPosfix1.h"
"${CXBXR_ROOT_DIR}/src/common/win32/AlignPrefix1.h"
"${CXBXR_ROOT_DIR}/src/common/win32/EmuShared.h"

View File

@ -0,0 +1,61 @@
// ******************************************************************
// *
// * This file is part of the Cxbx project.
// *
// * Cxbx and Cxbe are free software; you can redistribute them
// * and/or modify them under the terms of the GNU General Public
// * License as published by the Free Software Foundation; either
// * version 2 of the license, or (at your option) any later version.
// *
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// * GNU General Public License for more details.
// *
// * You should have recieved a copy of the GNU General Public License
// * along with this program; see the file COPYING.
// * If not, write to the Free Software Foundation, Inc.,
// * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
// *
// * (c) 2019 RadWolfie
// *
// * All rights reserved
// *
// ******************************************************************
#pragma once
#include <string>
#include <SDL_stdinc.h>
// Depending on what type of UTF-8 string is input, otherwise will use native by default.
static std::wstring utf8_to_utf16(const char* utf8_string)
{
wchar_t* tempUTF16 = reinterpret_cast<wchar_t*>(SDL_iconv_string(/*to:*/"UTF-16", /*from:*/"UTF-8", utf8_string, strlen(utf8_string) + 1));
std::wstring strUTF16 = tempUTF16;
// Don't forget to free allocated string from SDL library.
SDL_free(tempUTF16);
return strUTF16;
};
// Depending on what type of UTF-16 string is input, otherwise will use native by default.
static std::string utf16_to_ascii(const wchar_t* utf16_string)
{
char* tempASCII = SDL_iconv_string(/*to:*/"ASCII", /*from:*/"UTF-16", reinterpret_cast<const char*>(utf16_string), wcslen(utf16_string)*2 + 1);
const std::string strASCII = tempASCII;
// Don't forget to free allocated string from SDL library.
SDL_free(tempASCII);
return strASCII;
}
// Enforce to expect little endian UTF-16 string is input.
static std::string utf16le_to_ascii(const wchar_t* utf16le_string)
{
char* tempASCII = SDL_iconv_string(/*to:*/"ASCII", /*from:*/"UTF-16LE", reinterpret_cast<const char*>(utf16le_string), wcslen(utf16le_string)*2 + 1);
const std::string strASCII = tempASCII;
// Don't forget to free allocated string from SDL library.
SDL_free(tempASCII);
return strASCII;
}

View File

@ -29,9 +29,9 @@
#include "CxbxVersion.h" // For _CXBX_VERSION
#include <locale> // For ctime
#include <codecvt> // For std::codecvt_utf8<>
#include <sstream> // For std::stringstream
#include <iomanip> // For std::setfill, std::uppercase, std::hex
#include "common/util/strConverter.hpp" // for utf16le_to_ascii
extern std::string FormatTitleId(uint32_t title_id); // Exposed in Emu.cpp
@ -94,14 +94,6 @@ std::string XbePrinter::GenHexRow(
return text.str();
}
// https://stackoverflow.com/questions/4786292/converting-unicode-strings-and-vice-versa
std::string XbePrinter::utf8_to_ascii(const wchar_t* utf8_string)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> utf8_to_ascii;
const std::wstring utf8_filename(utf8_string);
return utf8_to_ascii.to_bytes(utf8_filename);
}
std::string XbePrinter::AllowedMediaToString()
{
const uint32_t dwAllowedMedia = Xbe_certificate->dwAllowedMedia;
@ -253,7 +245,7 @@ std::string XbePrinter::GenGeneralHeaderInfo2()
const uint32_t debug_entry_point = Xbe_header->dwEntryAddr ^ XOR_EP_DEBUG;
const uint32_t retail_thunk_addr = Xbe_header->dwKernelImageThunkAddr ^ XOR_KT_RETAIL;
const uint32_t debug_thunk_addr = Xbe_header->dwKernelImageThunkAddr ^ XOR_KT_DEBUG;
const std::string AsciiFilename = utf8_to_ascii(Xbe_to_print->GetUnicodeFilenameAddr());
const std::string AsciiFilename = utf16le_to_ascii(Xbe_to_print->GetUnicodeFilenameAddr());
std::stringstream text;
SSTREAM_SET_HEX(text);
text << "Entry Point : 0x" << std::setw(8) << Xbe_header->dwEntryAddr << " (Retail: 0x" << std::setw(8) << retail_entry_point << ", Debug: 0x" << std::setw(8) << debug_entry_point << ")\n";

View File

@ -44,7 +44,6 @@ class XbePrinter
Xbe::Certificate *Xbe_certificate;
std::string GenHexRow(uint8_t*, const uint8_t, const uint8_t);
std::string utf8_to_ascii(const wchar_t*);
std::string AllowedMediaToString();
std::string GameRatingToString();

View File

@ -59,6 +59,7 @@ namespace xboxkrnl
#include "WalkIndexBuffer.h"
#include "core\kernel\common\strings.hpp" // For uem_str
#include "common\input\SdlJoystick.h"
#include "common/util/strConverter.hpp" // for utf8_to_utf16
#include <assert.h>
#include <process.h>
@ -549,8 +550,7 @@ void DrawUEM(HWND hWnd)
SetTextColor(hMemDC, RGB(0, 204, 0));
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> UTF8toUTF16;
std::wstring utf16str = UTF8toUTF16.from_bytes(uem_str);
std::wstring utf16str = utf8_to_utf16(uem_str.c_str());
// Unfortunately, DrawTextW doesn't support vertical alignemnt, so we have to do the calculation
// ourselves. See here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/abd89aae-16a0-41c6-8db6-b119ea90b42a/win32-drawtext-how-center-in-vertical-with-new-lines-and-tabs?forum=vclanguage

View File

@ -47,6 +47,7 @@ namespace xboxkrnl
#include "common\EmuEEPROM.h" // For EEPROM
#include "devices\Xbox.h" // For g_SMBus, SMBUS_ADDRESS_SYSTEM_MICRO_CONTROLLER
#include "devices\SMCDevice.h" // For SMC_COMMAND_SCRATCH
#include "common/util/strConverter.hpp" // for utf16_to_ascii
#include <algorithm> // for std::replace
#include <locale>
@ -536,7 +537,7 @@ XBSYSAPI EXPORTNUM(49) xboxkrnl::VOID DECLSPEC_NORETURN NTAPI xboxkrnl::HalRetur
CxbxConvertFilePath(TitlePath, wXbePath, &rootDirectoryHandle, "NtCreateFile");
// Convert Wide String as returned by above to a string, for XbePath
XbePath = std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(wXbePath);
XbePath = utf16_to_ascii(wXbePath.c_str());
// If the rootDirectoryHandle is not null, we have a relative path
// We need to prepend the path of the root directory to get a full DOS path