utf8 fixes

This commit is contained in:
Jack Andersen 2015-08-31 11:25:55 -10:00
parent 23a536a2b1
commit 799e2cb6ee
5 changed files with 17 additions and 62 deletions

@ -1 +1 @@
Subproject commit 828db515ba316903c367b9dc7acf9d0bec0969e7 Subproject commit 317e75c22e31746947bd1b3700cd72d53c97c837

View File

@ -4,6 +4,10 @@
#if _WIN32 && UNICODE #if _WIN32 && UNICODE
#include <wctype.h> #include <wctype.h>
#include <direct.h> #include <direct.h>
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#endif
#else #else
#include <ctype.h> #include <ctype.h>
#endif #endif
@ -31,10 +35,6 @@ static inline int Mkdir(const char* path, mode_t mode) {return mkdir(path, mode)
static inline int Stat(const char* path, Sstat* statout) {return stat(path, statout);} static inline int Stat(const char* path, Sstat* statout) {return stat(path, statout);}
#endif #endif
/* String Converters */
std::string WideToUTF8(const std::wstring& src);
std::wstring UTF8ToWide(const std::string& src);
/* String-converting views */ /* String-converting views */
#if NOD_UCS2 #if NOD_UCS2
typedef wchar_t SystemChar; typedef wchar_t SystemChar;
@ -48,7 +48,11 @@ class SystemUTF8View
std::string m_utf8; std::string m_utf8;
public: public:
SystemUTF8View(const SystemString& str) SystemUTF8View(const SystemString& str)
: m_utf8(WideToUTF8(str)) {} {
int len = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), str.size(), nullptr, 0, nullptr, nullptr);
m_utf8.assign('\0', len);
WideCharToMultiByte(CP_UTF8, 0, str.c_str(), str.size(), &m_utf8[0], len, nullptr, nullptr);
}
inline const std::string& utf8_str() {return m_utf8;} inline const std::string& utf8_str() {return m_utf8;}
}; };
class SystemStringView class SystemStringView
@ -56,7 +60,11 @@ class SystemStringView
std::wstring m_sys; std::wstring m_sys;
public: public:
SystemStringView(const std::string& str) SystemStringView(const std::string& str)
: m_sys(UTF8ToWide(str)) {} {
int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.size(), nullptr, 0);
m_sys.assign(L'\0', len);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.size(), &m_sys[0], len);
}
inline const std::wstring& sys_str() {return m_sys;} inline const std::wstring& sys_str() {return m_sys;}
}; };
#ifndef _S #ifndef _S

View File

@ -11,7 +11,6 @@ add_library(NOD
FileIOFILE.cpp FileIOFILE.cpp
FileIOMEM.cpp FileIOMEM.cpp
NOD.cpp NOD.cpp
WideStringConvert.cpp
${NOD_HEADERS}) ${NOD_HEADERS})
if(NOT WIN32) if(NOT WIN32)
set_source_files_properties(aes.cpp PROPERTIES COMPILE_FLAGS -maes) set_source_files_properties(aes.cpp PROPERTIES COMPILE_FLAGS -maes)

View File

@ -1,4 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <inttypes.h>
#include "NOD/Util.hpp" #include "NOD/Util.hpp"
#include "NOD/IDiscIO.hpp" #include "NOD/IDiscIO.hpp"
@ -82,7 +83,7 @@ class DiscIOWBFS : public IDiscIO
off*=512ULL; off*=512ULL;
if (fseeko(fp, off, SEEK_SET)) if (fseeko(fp, off, SEEK_SET))
{ {
LogModule.report(LogVisor::FatalError, "error seeking in disc partition: %lld %d", off, count); LogModule.report(LogVisor::FatalError, "error seeking in disc partition: %" PRId64 " %d", off, count);
return 1; return 1;
} }
if (fread(buf, count*512ULL, 1, fp) != 1){ if (fread(buf, count*512ULL, 1, fp) != 1){

View File

@ -1,53 +0,0 @@
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN 1
#endif
#include <windows.h>
#include "NOD/NOD.hpp"
namespace NOD
{
std::string WideToUTF8(const std::wstring& src)
{
#if _WIN32
int len = WideCharToMultiByte(CP_UTF8, 0, src.c_str(), src.size(), nullptr, 0, nullptr, nullptr);
std::string retval(len, '\0');
WideCharToMultiByte(CP_UTF8, 0, src.c_str(), src.size(), &retval[0], len, nullptr, nullptr);
return retval;
#else
std::string retval;
retval.reserve(src.length());
std::mbstate_t state = {};
for (wchar_t ch : src)
{
char mb[MB_LEN_MAX];
int c = std::wcrtomb(mb, ch, &state);
retval.append(mb, c);
}
return retval;
#endif
}
std::wstring UTF8ToWide(const std::string& src)
{
#if _WIN32
int len = MultiByteToWideChar(CP_UTF8, 0, src.c_str(), src.size(), nullptr, 0);
std::wstring retval(len, L'\0');
MultiByteToWideChar(CP_UTF8, 0, src.c_str(), src.size(), &retval[0], len);
return retval;
#else
std::wstring retval;
retval.reserve(src.length());
const char* buf = src.c_str();
std::mbstate_t state = {};
while (*buf)
{
wchar_t wc;
buf += std::mbrtowc(&wc, buf, MB_LEN_MAX, &state);
retval += wc;
}
return retval;
#endif
}
}