From 972aa1feef27027f59d41dc925d0a3c83a37f11d Mon Sep 17 00:00:00 2001 From: Tsuda Kageyu Date: Wed, 6 Aug 2014 11:53:49 +0900 Subject: [PATCH 1/3] Replaced codecvt with Win32 API. --- ConfigureChecks.cmake | 10 ----- config.h.cmake | 3 -- taglib/CMakeLists.txt | 21 +++++---- taglib/toolkit/tstring.cpp | 87 +++++++++++++------------------------- taglib/toolkit/unicode.h | 5 --- 5 files changed, 42 insertions(+), 84 deletions(-) diff --git a/ConfigureChecks.cmake b/ConfigureChecks.cmake index 23830f93..d0069f57 100644 --- a/ConfigureChecks.cmake +++ b/ConfigureChecks.cmake @@ -220,16 +220,6 @@ if(NOT HAVE_SNPRINTF) " HAVE_SPRINTF_S) endif() -# Determine whether your compiler supports codecvt. - -check_cxx_source_compiles(" - #include - int main() { - std::codecvt_utf8_utf16 x; - return 0; - } -" HAVE_STD_CODECVT) - # Check for libz using the cmake supplied FindZLIB.cmake if(NOT ZLIB_SOURCE) diff --git a/config.h.cmake b/config.h.cmake index 07abffa6..8fd8391d 100644 --- a/config.h.cmake +++ b/config.h.cmake @@ -17,9 +17,6 @@ #cmakedefine HAVE_MAC_BYTESWAP 1 #cmakedefine HAVE_OPENBSD_BYTESWAP 1 -/* Defined if your compiler supports codecvt */ -#cmakedefine HAVE_STD_CODECVT 1 - /* Defined if your compiler supports some atomic operations */ #cmakedefine HAVE_STD_ATOMIC 1 #cmakedefine HAVE_BOOST_ATOMIC 1 diff --git a/taglib/CMakeLists.txt b/taglib/CMakeLists.txt index eebe61a5..2eadf391 100644 --- a/taglib/CMakeLists.txt +++ b/taglib/CMakeLists.txt @@ -303,9 +303,14 @@ set(toolkit_SRCS toolkit/tpropertymap.cpp toolkit/trefcounter.cpp toolkit/tdebuglistener.cpp - toolkit/unicode.cpp ) +if(NOT WIN32) + set(unicode_SRCS + toolkit/unicode.cpp + ) +endif() + if(HAVE_ZLIB_SOURCE) set(zlib_SRCS ${ZLIB_SOURCE}/adler32.c @@ -323,13 +328,14 @@ set(tag_LIB_SRCS ${vorbis_SRCS} ${oggflacs_SRCS} ${mpc_SRCS} ${ape_SRCS} ${toolkit_SRCS} ${flacs_SRCS} ${wavpack_SRCS} ${speex_SRCS} ${trueaudio_SRCS} ${riff_SRCS} ${aiff_SRCS} ${wav_SRCS} ${asf_SRCS} ${mp4_SRCS} ${mod_SRCS} ${s3m_SRCS} ${it_SRCS} ${xm_SRCS} ${opus_SRCS} + ${unicode_SRCS} ${zlib_SRCS} tag.cpp tagunion.cpp fileref.cpp audioproperties.cpp ) -add_library(tag ${tag_LIB_SRCS} ${zlib_SRCS} ${tag_HDRS}) +add_library(tag ${tag_LIB_SRCS} ${tag_HDRS}) if(ZLIB_FOUND) target_link_libraries(tag ${ZLIB_LIBRARIES}) @@ -348,10 +354,9 @@ if(BUILD_FRAMEWORK) endif() install(TARGETS tag - FRAMEWORK DESTINATION ${FRAMEWORK_INSTALL_DIR} - LIBRARY DESTINATION ${LIB_INSTALL_DIR} - RUNTIME DESTINATION ${BIN_INSTALL_DIR} - ARCHIVE DESTINATION ${LIB_INSTALL_DIR} - PUBLIC_HEADER DESTINATION ${INCLUDE_INSTALL_DIR}/taglib + FRAMEWORK DESTINATION ${FRAMEWORK_INSTALL_DIR} + LIBRARY DESTINATION ${LIB_INSTALL_DIR} + RUNTIME DESTINATION ${BIN_INSTALL_DIR} + ARCHIVE DESTINATION ${LIB_INSTALL_DIR} + PUBLIC_HEADER DESTINATION ${INCLUDE_INSTALL_DIR}/taglib ) - diff --git a/taglib/toolkit/tstring.cpp b/taglib/toolkit/tstring.cpp index 8287244b..aae26edf 100644 --- a/taglib/toolkit/tstring.cpp +++ b/taglib/toolkit/tstring.cpp @@ -39,101 +39,72 @@ #include #include -#ifdef HAVE_STD_CODECVT -# include +#ifdef _WIN32 +# include #else # include "unicode.h" #endif namespace { + using namespace TagLib; - void UTF16toUTF8(const wchar_t *src, size_t srcLength, char *dst, size_t dstLength) + inline void UTF16toUTF8( + const wchar_t *src, size_t srcLength, char *dst, size_t dstLength) { -#ifdef HAVE_STD_CODECVT +#ifdef _WIN32 - typedef std::codecvt_utf8_utf16 utf8_utf16_t; - - using namespace TagLib; - - const wchar_t *srcBegin = src; - const wchar_t *srcEnd = srcBegin + srcLength; - - char *dstBegin = dst; - char *dstEnd = dstBegin + dstLength; - - std::mbstate_t st; - const wchar_t *source; - char *target; - memset(&st, 0, sizeof(st)); - std::codecvt_base::result result = utf8_utf16_t().out( - st, srcBegin, srcEnd, source, dstBegin, dstEnd, target); - - if(result != utf8_utf16_t::ok) { + const int len = ::WideCharToMultiByte( + CP_UTF8, 0, src, srcLength, dst, dstLength, NULL, NULL); + if(len == 0) { debug("String::UTF16toUTF8() - Unicode conversion error."); } #else using namespace Unicode; - using namespace TagLib; - const Unicode::UTF16 *srcBegin = src; - const Unicode::UTF16 *srcEnd = srcBegin + srcLength; + const UTF16 *srcBegin = src; + const UTF16 *srcEnd = srcBegin + srcLength; - Unicode::UTF8 *dstBegin = reinterpret_cast(dst); - Unicode::UTF8 *dstEnd = dstBegin + dstLength; + UTF8 *dstBegin = reinterpret_cast(dst); + UTF8 *dstEnd = dstBegin + dstLength; - Unicode::ConversionResult result = Unicode::ConvertUTF16toUTF8( - &srcBegin, srcEnd, &dstBegin, dstEnd, Unicode::lenientConversion); + ConversionResult result = ConvertUTF16toUTF8( + &srcBegin, srcEnd, &dstBegin, dstEnd, lenientConversion); - if(result != Unicode::conversionOK) { + if(result != conversionOK) { debug("String::UTF16toUTF8() - Unicode conversion error."); } #endif } - void UTF8toUTF16(const char *src, size_t srcLength, wchar_t *dst, size_t dstLength) + inline void UTF8toUTF16( + const char *src, size_t srcLength, wchar_t *dst, size_t dstLength) { -#ifdef HAVE_STD_CODECVT +#ifdef _WIN32 - typedef std::codecvt_utf8_utf16 utf8_utf16_t; - - using namespace TagLib; - - const char *srcBegin = src; - const char *srcEnd = srcBegin + srcLength; - - wchar_t *dstBegin = dst; - wchar_t *dstEnd = dstBegin + dstLength; - - std::mbstate_t st; - const char *source; - wchar_t *target; - memset(&st, 0, sizeof(st)); - std::codecvt_base::result result = utf8_utf16_t().in( - st, srcBegin, srcEnd, source, dstBegin, dstEnd, target); - - if(result != utf8_utf16_t::ok) { + const int len = ::MultiByteToWideChar( + CP_UTF8, 0, src, srcLength, dst, dstLength); + if (len == 0) { debug("String::UTF8toUTF16() - Unicode conversion error."); } #else using namespace Unicode; - using namespace TagLib; - const Unicode::UTF8 *srcBegin = reinterpret_cast(src); - const Unicode::UTF8 *srcEnd = srcBegin + srcLength; + const UTF8 *srcBegin = reinterpret_cast(src); + const UTF8 *srcEnd = srcBegin + srcLength; - Unicode::UTF16 *dstBegin = dst; - Unicode::UTF16 *dstEnd = dstBegin + dstLength; + UTF16 *dstBegin = dst; + UTF16 *dstEnd = dstBegin + dstLength; - Unicode::ConversionResult result = Unicode::ConvertUTF8toUTF16( - &srcBegin, srcEnd, &dstBegin, dstEnd, Unicode::lenientConversion); + ConversionResult result = ConvertUTF8toUTF16( + &srcBegin, srcEnd, &dstBegin, dstEnd, lenientConversion); - if(result != Unicode::conversionOK) { + if(result != conversionOK) { debug("String::UTF8toUTF16() - Unicode conversion error."); } diff --git a/taglib/toolkit/unicode.h b/taglib/toolkit/unicode.h index ebf1915d..d3a869f0 100644 --- a/taglib/toolkit/unicode.h +++ b/taglib/toolkit/unicode.h @@ -106,11 +106,6 @@ bit mask & shift operations. ------------------------------------------------------------------------ */ -// Workaround for when MSVC doesn't have wchar_t as a built-in type. -#if defined(_MSC_VER) && !defined(_WCHAR_T_DEFINED) -# include -#endif - /* Some fundamental constants */ #define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD #define UNI_MAX_BMP (UTF32)0x0000FFFF From 929829b2b547607b999a8a98158b9dfc0eb423ab Mon Sep 17 00:00:00 2001 From: Tsuda Kageyu Date: Sun, 10 Aug 2014 01:13:25 +0900 Subject: [PATCH 2/3] Removed useless strlen() and wcslen(). --- taglib/toolkit/tstring.cpp | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/taglib/toolkit/tstring.cpp b/taglib/toolkit/tstring.cpp index aae26edf..9bed6180 100644 --- a/taglib/toolkit/tstring.cpp +++ b/taglib/toolkit/tstring.cpp @@ -49,7 +49,7 @@ namespace { using namespace TagLib; - inline void UTF16toUTF8( + inline size_t UTF16toUTF8( const wchar_t *src, size_t srcLength, char *dst, size_t dstLength) { #ifdef _WIN32 @@ -59,6 +59,7 @@ namespace if(len == 0) { debug("String::UTF16toUTF8() - Unicode conversion error."); } + return len; #else @@ -73,23 +74,29 @@ namespace ConversionResult result = ConvertUTF16toUTF8( &srcBegin, srcEnd, &dstBegin, dstEnd, lenientConversion); - if(result != conversionOK) { + if(result == conversionOK) { + return (dstBegin - reinterpret_cast(dst)); + } + else + { debug("String::UTF16toUTF8() - Unicode conversion error."); + return 0; } #endif } - inline void UTF8toUTF16( + inline size_t UTF8toUTF16( const char *src, size_t srcLength, wchar_t *dst, size_t dstLength) { #ifdef _WIN32 const int len = ::MultiByteToWideChar( CP_UTF8, 0, src, srcLength, dst, dstLength); - if (len == 0) { + if(len == 0) { debug("String::UTF8toUTF16() - Unicode conversion error."); } + return len; #else @@ -104,8 +111,12 @@ namespace ConversionResult result = ConvertUTF8toUTF16( &srcBegin, srcEnd, &dstBegin, dstEnd, lenientConversion); - if(result != conversionOK) { + if(result == conversionOK) { + return (dstBegin - dst); + } + else { debug("String::UTF8toUTF16() - Unicode conversion error."); + return 0; } #endif @@ -410,8 +421,9 @@ ByteVector String::data(Type t) const { ByteVector v(size() * 4 + 1, 0); - UTF16toUTF8(d->data.c_str(), d->data.size(), v.data(), v.size()); - v.resize(::strlen(v.data())); + const size_t len = UTF16toUTF8( + d->data.c_str(), d->data.size(), v.data(), v.size()); + v.resize(len); return v; } @@ -744,8 +756,8 @@ void String::copyFromUTF8(const char *s, size_t length) d->data.resize(length); if(length > 0) { - UTF8toUTF16(s, length, &d->data[0], d->data.size()); - d->data.resize(::wcslen(d->data.c_str())); + const size_t len = UTF8toUTF16(s, length, &d->data[0], d->data.size()); + d->data.resize(len); } } From a055933e10b65ea35817ff2dddf63a9b96ee25fe Mon Sep 17 00:00:00 2001 From: Tsuda Kageyu Date: Tue, 12 Aug 2014 13:33:25 +0900 Subject: [PATCH 3/3] Unified the same debug messages. --- taglib/toolkit/tstring.cpp | 47 ++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/taglib/toolkit/tstring.cpp b/taglib/toolkit/tstring.cpp index 9bed6180..b311cfbb 100644 --- a/taglib/toolkit/tstring.cpp +++ b/taglib/toolkit/tstring.cpp @@ -52,14 +52,11 @@ namespace inline size_t UTF16toUTF8( const wchar_t *src, size_t srcLength, char *dst, size_t dstLength) { + size_t len = 0; + #ifdef _WIN32 - const int len = ::WideCharToMultiByte( - CP_UTF8, 0, src, srcLength, dst, dstLength, NULL, NULL); - if(len == 0) { - debug("String::UTF16toUTF8() - Unicode conversion error."); - } - return len; + len = ::WideCharToMultiByte(CP_UTF8, 0, src, srcLength, dst, dstLength, NULL, NULL); #else @@ -74,29 +71,25 @@ namespace ConversionResult result = ConvertUTF16toUTF8( &srcBegin, srcEnd, &dstBegin, dstEnd, lenientConversion); - if(result == conversionOK) { - return (dstBegin - reinterpret_cast(dst)); - } - else - { - debug("String::UTF16toUTF8() - Unicode conversion error."); - return 0; - } + if(result == conversionOK) + len = dstBegin - reinterpret_cast(dst); #endif + + if(len == 0) + debug("String::UTF16toUTF8() - Unicode conversion error."); + + return len; } inline size_t UTF8toUTF16( const char *src, size_t srcLength, wchar_t *dst, size_t dstLength) { + size_t len = 0; + #ifdef _WIN32 - const int len = ::MultiByteToWideChar( - CP_UTF8, 0, src, srcLength, dst, dstLength); - if(len == 0) { - debug("String::UTF8toUTF16() - Unicode conversion error."); - } - return len; + len = ::MultiByteToWideChar(CP_UTF8, 0, src, srcLength, dst, dstLength); #else @@ -111,15 +104,15 @@ namespace ConversionResult result = ConvertUTF8toUTF16( &srcBegin, srcEnd, &dstBegin, dstEnd, lenientConversion); - if(result == conversionOK) { - return (dstBegin - dst); - } - else { - debug("String::UTF8toUTF16() - Unicode conversion error."); - return 0; - } + if(result == conversionOK) + len = dstBegin - dst; #endif + + if(len == 0) + debug("String::UTF8toUTF16() - Unicode conversion error."); + + return len; } }