From d3bd8fb7ff3ac78ac62cd7108e26cd5a259a8047 Mon Sep 17 00:00:00 2001 From: Tsuda Kageyu Date: Sun, 30 Oct 2016 22:25:34 +0900 Subject: [PATCH 1/4] 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() + 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 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 //////////////////////////////////////////////////////////////////////////////// From 873c917081809e1324396fa29cf4bf70ee0eaf87 Mon Sep 17 00:00:00 2001 From: Tsuda Kageyu Date: Sun, 30 Oct 2016 22:36:18 +0900 Subject: [PATCH 2/4] Assume that SetFilePointerEx() and GetFileSizeEx() are always available. This drops support for Windows 9x and NT 4.0 or older. --- taglib/toolkit/tfilestream.cpp | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/taglib/toolkit/tfilestream.cpp b/taglib/toolkit/tfilestream.cpp index 507e8784..478f09a0 100644 --- a/taglib/toolkit/tfilestream.cpp +++ b/taglib/toolkit/tfilestream.cpp @@ -362,12 +362,12 @@ void FileStream::seek(long offset, Position p) return; } - SetLastError(NO_ERROR); - SetFilePointer(d->file, offset, NULL, whence); + LARGE_INTEGER liOffset; + liOffset.QuadPart = offset; - const int lastError = GetLastError(); - if(lastError != NO_ERROR && lastError != ERROR_NEGATIVE_SEEK) + if(!SetFilePointerEx(d->file, liOffset, NULL, whence)) { debug("FileStream::seek() -- Failed to set the file pointer."); + } #else @@ -409,10 +409,11 @@ long FileStream::tell() const { #ifdef _WIN32 - SetLastError(NO_ERROR); - const DWORD position = SetFilePointer(d->file, 0, NULL, FILE_CURRENT); - if(GetLastError() == NO_ERROR) { - return static_cast(position); + const LARGE_INTEGER zero = {}; + LARGE_INTEGER position; + + if(SetFilePointerEx(d->file, zero, &position, FILE_CURRENT) && position.QuadPart <= LONG_MAX) { + return static_cast(position.QuadPart); } else { debug("FileStream::tell() -- Failed to get the file pointer."); @@ -435,15 +436,9 @@ long FileStream::length() #ifdef _WIN32 - SetLastError(NO_ERROR); -#if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0602) LARGE_INTEGER fileSize; - GetFileSizeEx(d->file, &fileSize); -#else - ULARGE_INTEGER fileSize; - fileSize.LowPart = GetFileSize(d->file, &fileSize.HighPart); -#endif - if(GetLastError() == NO_ERROR && fileSize.QuadPart <= LONG_MAX) { + + if(GetFileSizeEx(d->file, &fileSize) && fileSize.QuadPart <= LONG_MAX) { return static_cast(fileSize.QuadPart); } else { @@ -477,9 +472,7 @@ void FileStream::truncate(long length) seek(length); - SetLastError(NO_ERROR); - SetEndOfFile(d->file); - if(GetLastError() != NO_ERROR) { + if(!SetEndOfFile(d->file)) { debug("FileStream::truncate() -- Failed to truncate the file."); } From ee7fa78011b1bc2e5dc6327b39173dc2ca0b4db4 Mon Sep 17 00:00:00 2001 From: Tsuda Kageyu Date: Sun, 30 Oct 2016 22:51:15 +0900 Subject: [PATCH 3/4] Update NEWS. --- NEWS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS b/NEWS index 88e859a0..3e5b192b 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,8 @@ ============================ * Added support for WinRT. + * Dropped support for Windows 9x and NT 4.0 or older. + * Several smaller bug fixes and performance improvements. TagLib 1.11.1 (Oct 24, 2016) ============================ From f5ce49818286324305c9a6842d81ecc0ff1913bb Mon Sep 17 00:00:00 2001 From: Tsuda Kageyu Date: Sun, 30 Oct 2016 23:51:35 +0900 Subject: [PATCH 4/4] Suppress MSVC warnings about narrowing conversions. --- taglib/riff/rifffile.cpp | 4 ++-- taglib/toolkit/tbytevector.cpp | 22 +++++++++++----------- taglib/toolkit/tdebug.cpp | 9 ++++----- taglib/toolkit/tfilestream.cpp | 6 +++--- taglib/toolkit/tlist.tcc | 2 +- taglib/toolkit/tmap.tcc | 2 +- taglib/toolkit/tstring.cpp | 16 +++++++++------- 7 files changed, 31 insertions(+), 30 deletions(-) diff --git a/taglib/riff/rifffile.cpp b/taglib/riff/rifffile.cpp index f874f7ad..51c9af41 100644 --- a/taglib/riff/rifffile.cpp +++ b/taglib/riff/rifffile.cpp @@ -95,7 +95,7 @@ unsigned int RIFF::File::riffSize() const unsigned int RIFF::File::chunkCount() const { - return d->chunks.size(); + return static_cast(d->chunks.size()); } unsigned int RIFF::File::chunkDataSize(unsigned int i) const @@ -269,7 +269,7 @@ void RIFF::File::removeChunk(unsigned int i) void RIFF::File::removeChunk(const ByteVector &name) { - for(int i = d->chunks.size() - 1; i >= 0; --i) { + for(int i = static_cast(d->chunks.size()) - 1; i >= 0; --i) { if(d->chunks[i].name == name) removeChunk(i); } diff --git a/taglib/toolkit/tbytevector.cpp b/taglib/toolkit/tbytevector.cpp index 6494a448..a2258bc5 100644 --- a/taglib/toolkit/tbytevector.cpp +++ b/taglib/toolkit/tbytevector.cpp @@ -63,7 +63,7 @@ int findChar( for(TIterator it = dataBegin + offset; it < dataEnd; it += byteAlign) { if(*it == c) - return (it - dataBegin); + return static_cast(it - dataBegin); } return -1; @@ -105,7 +105,7 @@ int findVector( ++itPattern; if(itPattern == patternEnd) - return (it - dataBegin); + return static_cast(it - dataBegin); } } @@ -125,7 +125,7 @@ T toNumber(const ByteVector &v, size_t offset, size_t length, bool mostSignifica T sum = 0; for(size_t i = 0; i < length; i++) { const size_t shift = (mostSignificantByteFirst ? length - 1 - i : i) * 8; - sum |= static_cast(static_cast(v[offset + i])) << shift; + sum |= static_cast(static_cast(v[static_cast(offset + i)])) << shift; } return sum; @@ -300,7 +300,7 @@ ByteVector ByteVector::null; ByteVector ByteVector::fromCString(const char *s, unsigned int length) { if(length == 0xffffffff) - return ByteVector(s, ::strlen(s)); + return ByteVector(s, static_cast(::strlen(s))); else return ByteVector(s, length); } @@ -375,7 +375,7 @@ ByteVector::ByteVector(const char *data, unsigned int length) : } ByteVector::ByteVector(const char *data) : - d(new ByteVectorPrivate(data, ::strlen(data))) + d(new ByteVectorPrivate(data, static_cast(::strlen(data)))) { } @@ -493,14 +493,14 @@ ByteVector &ByteVector::replace(const ByteVector &pattern, const ByteVector &wit if(pattern.size() == 1 && with.size() == 1) return replace(pattern[0], with[0]); - const size_t withSize = with.size(); - const size_t patternSize = pattern.size(); - const ptrdiff_t diff = withSize - patternSize; + const unsigned int withSize = with.size(); + const unsigned int patternSize = pattern.size(); + const int diff = withSize - patternSize; - size_t offset = 0; + unsigned int offset = 0; while (true) { offset = find(pattern, offset); - if(offset == static_cast(-1)) // Use npos in taglib2. + if(offset == static_cast(-1)) break; detach(); @@ -963,7 +963,7 @@ ByteVector ByteVector::fromBase64(const ByteVector & input) // Only return output if we processed all bytes if(len == 0) { - output.resize(dst - (unsigned char*) output.data()); + output.resize(static_cast(dst - (unsigned char*) output.data())); return output; } return ByteVector(); diff --git a/taglib/toolkit/tdebug.cpp b/taglib/toolkit/tdebug.cpp index d630791c..b2efc4cb 100644 --- a/taglib/toolkit/tdebug.cpp +++ b/taglib/toolkit/tdebug.cpp @@ -50,11 +50,10 @@ namespace TagLib void debugData(const ByteVector &v) { - for(size_t i = 0; i < v.size(); ++i) - { - std::string bits = std::bitset<8>(v[i]).to_string(); - String msg = Utils::formatString( - "*** [%d] - char '%c' - int %d, 0x%02x, 0b%s\n", + for(unsigned int i = 0; i < v.size(); ++i) { + const std::string bits = std::bitset<8>(v[i]).to_string(); + const String msg = Utils::formatString( + "*** [%u] - char '%c' - int %d, 0x%02x, 0b%s\n", i, v[i], v[i], v[i], bits.c_str()); debugListener->printMessage(msg); diff --git a/taglib/toolkit/tfilestream.cpp b/taglib/toolkit/tfilestream.cpp index 478f09a0..4f522a62 100644 --- a/taglib/toolkit/tfilestream.cpp +++ b/taglib/toolkit/tfilestream.cpp @@ -261,7 +261,7 @@ void FileStream::insert(const ByteVector &data, unsigned long start, unsigned lo // to overwrite. Appropriately increment the readPosition. seek(readPosition); - const size_t bytesRead = readFile(d->file, aboutToOverwrite); + const unsigned int bytesRead = static_cast(readFile(d->file, aboutToOverwrite)); aboutToOverwrite.resize(bytesRead); readPosition += bufferLength; @@ -304,10 +304,10 @@ void FileStream::removeBlock(unsigned long start, unsigned long length) ByteVector buffer(static_cast(bufferLength)); - for(size_t bytesRead = -1; bytesRead != 0;) + for(unsigned int bytesRead = -1; bytesRead != 0;) { seek(readPosition); - bytesRead = readFile(d->file, buffer); + bytesRead = static_cast(readFile(d->file, buffer)); readPosition += bytesRead; // Check to see if we just read the last block. We need to call clear() diff --git a/taglib/toolkit/tlist.tcc b/taglib/toolkit/tlist.tcc index bf8b0007..a55eb620 100644 --- a/taglib/toolkit/tlist.tcc +++ b/taglib/toolkit/tlist.tcc @@ -196,7 +196,7 @@ List &List::clear() template unsigned int List::size() const { - return d->list.size(); + return static_cast(d->list.size()); } template diff --git a/taglib/toolkit/tmap.tcc b/taglib/toolkit/tmap.tcc index 68f0d311..c1227f3a 100644 --- a/taglib/toolkit/tmap.tcc +++ b/taglib/toolkit/tmap.tcc @@ -152,7 +152,7 @@ Map &Map::erase(const Key &key) template unsigned int Map::size() const { - return d->map.size(); + return static_cast(d->map.size()); } template diff --git a/taglib/toolkit/tstring.cpp b/taglib/toolkit/tstring.cpp index 83287905..a1891a45 100644 --- a/taglib/toolkit/tstring.cpp +++ b/taglib/toolkit/tstring.cpp @@ -54,7 +54,8 @@ namespace #ifdef _WIN32 - len = ::WideCharToMultiByte(CP_UTF8, 0, src, srcLength, dst, dstLength, NULL, NULL); + len = ::WideCharToMultiByte( + CP_UTF8, 0, src, static_cast(srcLength), dst, static_cast(dstLength), NULL, NULL); #else @@ -86,7 +87,8 @@ namespace #ifdef _WIN32 - len = ::MultiByteToWideChar(CP_UTF8, 0, src, srcLength, dst, dstLength); + len = ::MultiByteToWideChar( + CP_UTF8, 0, src, static_cast(srcLength), dst, static_cast(dstLength)); #else @@ -410,12 +412,12 @@ String::ConstIterator String::end() const int String::find(const String &s, int offset) const { - return d->data.find(s.d->data, offset); + return static_cast(d->data.find(s.d->data, offset)); } int String::rfind(const String &s, int offset) const { - return d->data.rfind(s.d->data, offset); + return static_cast(d->data.rfind(s.d->data, offset)); } StringList String::split(const String &separator) const @@ -479,7 +481,7 @@ String String::upper() const unsigned int String::size() const { - return d->data.size(); + return static_cast(d->data.size()); } unsigned int String::length() const @@ -518,7 +520,7 @@ ByteVector String::data(Type t) const const size_t len = UTF16toUTF8( d->data.c_str(), d->data.size(), v.data(), v.size()); - v.resize(len); + v.resize(static_cast(len)); return v; } @@ -604,7 +606,7 @@ String String::stripWhiteSpace() const return String(); const size_t pos2 = d->data.find_last_not_of(WhiteSpaceChars); - return substr(pos1, pos2 - pos1 + 1); + return substr(static_cast(pos1), static_cast(pos2 - pos1 + 1)); } bool String::isLatin1() const