From d3bd8fb7ff3ac78ac62cd7108e26cd5a259a8047 Mon Sep 17 00:00:00 2001 From: Tsuda Kageyu Date: Sun, 30 Oct 2016 22:25:34 +0900 Subject: [PATCH] Assume that CreateFileW() is always available. This drops support for Windows 9x. --- taglib/toolkit/tfilestream.cpp | 9 +--- taglib/toolkit/tiostream.cpp | 84 +++++++++------------------------- 2 files changed, 24 insertions(+), 69 deletions(-) diff --git a/taglib/toolkit/tfilestream.cpp b/taglib/toolkit/tfilestream.cpp index 229b65c4..507e8784 100644 --- a/taglib/toolkit/tfilestream.cpp +++ b/taglib/toolkit/tfilestream.cpp @@ -52,14 +52,9 @@ namespace const DWORD access = readOnly ? GENERIC_READ : (GENERIC_READ | GENERIC_WRITE); #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0602) - return CreateFile2(path.toString().toCWString(), access, FILE_SHARE_READ, OPEN_EXISTING, NULL); + return CreateFile2(path.wstr().c_str(), access, FILE_SHARE_READ, OPEN_EXISTING, NULL); #else - if(!path.wstr().empty()) - return CreateFileW(path.wstr().c_str(), access, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); - else if(!path.str().empty()) - return CreateFileA(path.str().c_str(), access, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); - else - return InvalidFileHandle; + return CreateFileW(path.wstr().c_str(), access, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); #endif } diff --git a/taglib/toolkit/tiostream.cpp b/taglib/toolkit/tiostream.cpp index 72fe32a6..de0bd505 100644 --- a/taglib/toolkit/tiostream.cpp +++ b/taglib/toolkit/tiostream.cpp @@ -23,73 +23,49 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ +#ifdef _WIN32 +# include +# include +#endif + #include "tiostream.h" using namespace TagLib; #ifdef _WIN32 -# include "tstring.h" -# include "tdebug.h" -# include - namespace { - // Check if the running system has CreateFileW() function. - // Windows9x systems don't have CreateFileW() or can't accept Unicode file names. - - bool supportsUnicode() - { -#ifdef UNICODE - return true; -#else - const FARPROC p = GetProcAddress(GetModuleHandleA("kernel32"), "CreateFileW"); - return (p != NULL); -#endif - } - - // Indicates whether the system supports Unicode file names. - - const bool SystemSupportsUnicode = supportsUnicode(); - - // Converts a UTF-16 string into a local encoding. - // This function should only be used in Windows9x systems which don't support - // Unicode file names. - - std::string unicodeToAnsi(const wchar_t *wstr) + std::wstring ansiToUnicode(const char *str) { - if(SystemSupportsUnicode) { - debug("unicodeToAnsi() - Should not be used on WinNT systems."); - } - - const int len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL); + const int len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0); if(len == 0) - return std::string(); + return std::wstring(); - std::string str(len, '\0'); - WideCharToMultiByte(CP_ACP, 0, wstr, -1, &str[0], len, NULL, NULL); + std::wstring wstr(len - 1, L'\0'); + MultiByteToWideChar(CP_ACP, 0, str, -1, &wstr[0], len); - return str; + return wstr; } } -// If WinNT, stores a Unicode string into m_wname directly. -// If Win9x, converts and stores it into m_name to avoid calling Unicode version functions. +// m_name is no longer used, but kept for backward compatibility. -FileName::FileName(const wchar_t *name) - : m_name (SystemSupportsUnicode ? "" : unicodeToAnsi(name)) - , m_wname(SystemSupportsUnicode ? name : L"") +FileName::FileName(const wchar_t *name) : + m_name(), + m_wname(name) { } -FileName::FileName(const char *name) - : m_name(name) +FileName::FileName(const char *name) : + m_name(), + m_wname(ansiToUnicode(name)) { } -FileName::FileName(const FileName &name) - : m_name (name.m_name) - , m_wname(name.m_wname) +FileName::FileName(const FileName &name) : + m_name(), + m_wname(name.m_wname) { } @@ -115,25 +91,9 @@ const std::string &FileName::str() const String FileName::toString() const { - if(!m_wname.empty()) { - return String(m_wname); - } - else if(!m_name.empty()) { - const int len = MultiByteToWideChar(CP_ACP, 0, m_name.c_str(), -1, NULL, 0); - if(len == 0) - return String(); - - std::vector buf(len); - MultiByteToWideChar(CP_ACP, 0, m_name.c_str(), -1, &buf[0], len); - - return String(&buf[0]); - } - else { - return String(); - } + return String(m_wname.c_str()); } - #endif // _WIN32 //////////////////////////////////////////////////////////////////////////////// -- 2.40.0