Assume that CreateFileW() is always available.

This drops support for Windows 9x.
This commit is contained in:
Tsuda Kageyu 2016-10-30 22:25:34 +09:00
parent 005441faaa
commit d3bd8fb7ff
2 changed files with 24 additions and 69 deletions

View File

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

View File

@ -23,73 +23,49 @@
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifdef _WIN32
# include <windows.h>
# include <tstring.h>
#endif
#include "tiostream.h"
using namespace TagLib;
#ifdef _WIN32
# include "tstring.h"
# include "tdebug.h"
# include <windows.h>
namespace
{
// Check if the running system has CreateFileW() function.
// Windows9x systems don't have CreateFileW() or can't accept Unicode file names.
bool supportsUnicode()
std::wstring ansiToUnicode(const char *str)
{
#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)
{
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<wchar_t> 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
////////////////////////////////////////////////////////////////////////////////